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.emailcommon.Logging; 20 21 import android.accounts.Account; 22 import android.accounts.AccountManager; 23 import android.app.Notification; 24 import android.app.NotificationManager; 25 import android.app.PendingIntent; 26 import android.content.ContentResolver; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.net.Uri; 30 import android.provider.CalendarContract; 31 import android.util.Log; 32 33 /** 34 * Utility class to enable Exchange calendar sync for all existing Exchange accounts. 35 * 36 * <p>Exchange calendar was first supported on Froyo. It wasn't supported on Eclair, which 37 * was the first version that supported Exchange email. 38 * 39 * <p>This class is used only once when the devices is upgraded to Froyo (or later) from Eclair, 40 * to enable calendar sync for all the existing Exchange accounts. 41 */ 42 public class CalendarSyncEnabler { 43 private final Context mContext; 44 CalendarSyncEnabler(Context context)45 public CalendarSyncEnabler(Context context) { 46 this.mContext = context; 47 } 48 49 /** 50 * Enable calendar sync for all the existing exchange accounts, and post a notification if any. 51 */ enableEasCalendarSync()52 public final void enableEasCalendarSync() { 53 String emailAddresses = enableEasCalendarSyncInternalForTest(); 54 if (emailAddresses.length() > 0) { 55 // Exchange account(s) found. 56 showNotificationForTest(emailAddresses.toString()); 57 } 58 } 59 60 /** 61 * Enable calendar sync for all the existing exchange accounts 62 * 63 * @return email addresses of the Exchange accounts joined with spaces as delimiters, 64 * or the empty string if there's no Exchange accounts. 65 */ enableEasCalendarSyncInternalForTest()66 /* package for testing */ final String enableEasCalendarSyncInternalForTest() { 67 StringBuilder emailAddresses = new StringBuilder(); 68 69 Account[] exchangeAccounts = AccountManager.get(mContext) 70 .getAccountsByType(Eas.EXCHANGE_ACCOUNT_MANAGER_TYPE); 71 for (Account account : exchangeAccounts) { 72 final String emailAddress = account.name; 73 Log.i(Logging.LOG_TAG, "Enabling Exchange calendar sync for " + emailAddress); 74 75 ContentResolver.setIsSyncable(account, CalendarContract.AUTHORITY, 1); 76 ContentResolver.setSyncAutomatically(account, CalendarContract.AUTHORITY, true); 77 78 // Accumulate addresses for notification. 79 if (emailAddresses.length() > 0) { 80 emailAddresses.append(' '); 81 } 82 emailAddresses.append(emailAddress); 83 } 84 return emailAddresses.toString(); 85 } 86 87 // *** Taken from NotificationController 88 public static final int NOTIFICATION_ID_EXCHANGE_CALENDAR_ADDED = 2; 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 */ showNotificationForTest(String emailAddresses)96 /* package for testing */ void showNotificationForTest(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(NOTIFICATION_ID_EXCHANGE_CALENDAR_ADDED, n); 110 } 111 112 /** @return {@link Intent} to launch the Calendar app. */ createLaunchCalendarIntent()113 private Intent createLaunchCalendarIntent() { 114 return new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.calendar/time")); 115 } 116 } 117