1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.exchange; 18 19 import com.android.email.Email; 20 import com.android.email.R; 21 import com.android.email.service.MailService; 22 23 import android.accounts.Account; 24 import android.accounts.AccountManager; 25 import android.app.Notification; 26 import android.app.NotificationManager; 27 import android.app.PendingIntent; 28 import android.content.ContentResolver; 29 import android.content.Context; 30 import android.content.Intent; 31 import android.net.Uri; 32 import android.provider.Calendar; 33 import android.provider.ContactsContract; 34 import android.util.Log; 35 36 /** 37 * Utility class to enable Exchange calendar sync for all existing Exchange accounts. 38 * 39 * <p>Exchange calendar was first supported on Froyo. It wasn't supported on Eclair, which 40 * was the first version that supported Exchange email. 41 * 42 * <p>This class is used only once when the devices is upgraded to Froyo (or later) from Eclair, 43 * to enable calendar sync for all the existing Exchange accounts. 44 */ 45 public class CalendarSyncEnabler { 46 private final Context mContext; 47 48 public CalendarSyncEnabler(Context context) { 49 this.mContext = context; 50 } 51 52 /** 53 * Enable calendar sync for all the existing exchange accounts, and post a notification if any. 54 */ 55 public final void enableEasCalendarSync() { 56 String emailAddresses = enableEasCalendarSyncInternal(); 57 if (emailAddresses.length() > 0) { 58 // Exchange account(s) found. 59 showNotification(emailAddresses.toString()); 60 } 61 } 62 63 /** 64 * Enable calendar sync for all the existing exchange accounts 65 * 66 * @return email addresses of the Exchange accounts joined with spaces as delimiters, 67 * or the empty string if there's no Exchange accounts. 68 */ 69 /* package for testing */ final String enableEasCalendarSyncInternal() { 70 StringBuilder emailAddresses = new StringBuilder(); 71 72 Account[] exchangeAccounts = AccountManager.get(mContext) 73 .getAccountsByType(Email.EXCHANGE_ACCOUNT_MANAGER_TYPE); 74 for (Account account : exchangeAccounts) { 75 final String emailAddress = account.name; 76 Log.i(Email.LOG_TAG, "Enabling Exchange calendar sync for " + emailAddress); 77 78 ContentResolver.setIsSyncable(account, Calendar.AUTHORITY, 1); 79 ContentResolver.setSyncAutomatically(account, Calendar.AUTHORITY, true); 80 81 // Accumulate addresses for notification. 82 if (emailAddresses.length() > 0) { 83 emailAddresses.append(' '); 84 } 85 emailAddresses.append(emailAddress); 86 } 87 return emailAddresses.toString(); 88 } 89 90 /** 91 * Show the "Exchange calendar added" notification. 92 * 93 * @param emailAddresses space delimited list of email addresses of Exchange accounts. It'll 94 * be shown on the notification. 95 */ 96 /* package for testing */ void showNotification(String emailAddresses) { 97 // Launch Calendar app when clicked. 98 PendingIntent launchCalendarPendingIntent = PendingIntent.getActivity(mContext, 0, 99 createLaunchCalendarIntent(), 0); 100 101 String tickerText = mContext.getString(R.string.notification_exchange_calendar_added); 102 Notification n = new Notification(R.drawable.stat_notify_calendar, 103 tickerText, System.currentTimeMillis()); 104 n.setLatestEventInfo(mContext, tickerText, emailAddresses, launchCalendarPendingIntent); 105 n.flags = Notification.FLAG_AUTO_CANCEL; 106 107 NotificationManager nm = 108 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 109 nm.notify(MailService.NOTIFICATION_ID_EXCHANGE_CALENDAR_ADDED, n); 110 } 111 112 /** @return {@link Intent} to launch the Calendar app. */ 113 private Intent createLaunchCalendarIntent() { 114 return new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.calendar/time")); 115 } 116 } 117