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 import com.android.mail.utils.LogUtils; 21 22 import android.accounts.Account; 23 import android.accounts.AccountManager; 24 import android.app.Notification; 25 import android.app.NotificationManager; 26 import android.app.PendingIntent; 27 import android.content.ContentResolver; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.net.Uri; 31 import android.provider.CalendarContract; 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 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 */ 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 */ 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 LogUtils.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 */ 96 @SuppressWarnings("deprecation") 97 /* package for testing */ void showNotificationForTest(String emailAddresses) { 98 // Launch Calendar app when clicked. 99 PendingIntent launchCalendarPendingIntent = PendingIntent.getActivity(mContext, 0, 100 createLaunchCalendarIntent(), 0); 101 102 String tickerText = mContext.getString(R.string.notification_exchange_calendar_added); 103 Notification n = new Notification(R.drawable.stat_notify_calendar, 104 tickerText, System.currentTimeMillis()); 105 n.setLatestEventInfo(mContext, tickerText, emailAddresses, launchCalendarPendingIntent); 106 n.flags = Notification.FLAG_AUTO_CANCEL; 107 108 NotificationManager nm = 109 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 110 nm.notify(NOTIFICATION_ID_EXCHANGE_CALENDAR_ADDED, n); 111 } 112 113 /** @return {@link Intent} to launch the Calendar app. */ 114 private static Intent createLaunchCalendarIntent() { 115 return new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.calendar/time")); 116 } 117 } 118