WidgetProvider.java revision dfd2c87740cb3f4fe3d3b882116a6fe00cdcb2bd
1/* 2 * Copyright (C) 2012 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 17package com.android.email.provider; 18 19import android.content.ContentResolver; 20import android.content.Context; 21import android.content.SharedPreferences; 22import android.database.Cursor; 23import android.net.Uri; 24 25import com.android.emailcommon.provider.Account; 26import com.android.emailcommon.provider.Mailbox; 27import com.android.mail.providers.Folder; 28import com.android.mail.providers.UIProvider; 29import com.android.mail.utils.LogTag; 30import com.android.mail.utils.LogUtils; 31import com.android.mail.widget.BaseWidgetProvider; 32import com.android.mail.widget.WidgetService; 33 34public class WidgetProvider extends BaseWidgetProvider { 35 private static final String LEGACY_PREFS_NAME = "com.android.email.widget.WidgetManager"; 36 private static final String LEGACY_ACCOUNT_ID_PREFIX = "accountId_"; 37 private static final String LEGACY_MAILBOX_ID_PREFIX = "mailboxId_"; 38 39 private static final String LOG_TAG = LogTag.getLogTag(); 40 41 /** 42 * Remove preferences when deleting widget 43 */ 44 @Override 45 public void onDeleted(Context context, int[] appWidgetIds) { 46 super.onDeleted(context, appWidgetIds); 47 48 // Remove any legacy Email widget information 49 final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0); 50 final SharedPreferences.Editor editor = prefs.edit(); 51 for (int widgetId : appWidgetIds) { 52 // Remove the account in the preference 53 editor.remove(LEGACY_ACCOUNT_ID_PREFIX + widgetId); 54 editor.remove(LEGACY_MAILBOX_ID_PREFIX + widgetId); 55 } 56 editor.apply(); 57 } 58 59 @Override 60 protected com.android.mail.providers.Account getAccountObject( 61 Context context, String accountUri) { 62 final ContentResolver resolver = context.getContentResolver(); 63 final Cursor accountCursor = resolver.query(Uri.parse(accountUri), 64 UIProvider.ACCOUNTS_PROJECTION_NO_CAPABILITIES, null, null, null); 65 66 return getPopulatedAccountObject(accountCursor); 67 } 68 69 70 @Override 71 protected boolean isAccountValid(Context context, com.android.mail.providers.Account account) { 72 if (account != null) { 73 final ContentResolver resolver = context.getContentResolver(); 74 final Cursor accountCursor = resolver.query(account.uri, 75 UIProvider.ACCOUNTS_PROJECTION_NO_CAPABILITIES, null, null, null); 76 if (accountCursor != null) { 77 try { 78 return accountCursor.getCount() > 0; 79 } finally { 80 accountCursor.close(); 81 } 82 } 83 } 84 return false; 85 } 86 87 @Override 88 protected void migrateLegacyWidgetInformation(Context context, int widgetId) { 89 final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0); 90 final SharedPreferences.Editor editor = prefs.edit(); 91 92 long accountId = loadAccountIdPref(context, widgetId); 93 long mailboxId = loadMailboxIdPref(context, widgetId); 94 // Legacy support; if preferences haven't been saved for this widget, load something 95 if (accountId == Account.NO_ACCOUNT || mailboxId == Mailbox.NO_MAILBOX) { 96 LogUtils.d(LOG_TAG, "Couldn't load account or mailbox. accountId: %d" + 97 " mailboxId: %d widgetId %d", accountId, mailboxId); 98 return; 99 } 100 101 // Get Account and folder objects for the account id and mailbox id 102 final com.android.mail.providers.Account uiAccount = getAccount(context, accountId); 103 final Folder uiFolder = getFolder(context, mailboxId); 104 105 if (uiAccount != null && uiFolder != null) { 106 WidgetService.saveWidgetInformation(context, widgetId, uiAccount, 107 uiFolder.uri.toString()); 108 109 updateWidgetInternal(context, widgetId, uiAccount, uiFolder.uri, 110 uiFolder.conversationListUri, uiFolder.name); 111 112 // Now remove the old legacy preference value 113 editor.remove(LEGACY_ACCOUNT_ID_PREFIX + widgetId); 114 editor.remove(LEGACY_MAILBOX_ID_PREFIX + widgetId); 115 } 116 editor.apply(); 117 } 118 119 private static com.android.mail.providers.Account getAccount(Context context, long accountId) { 120 final ContentResolver resolver = context.getContentResolver(); 121 final Cursor ac = resolver.query(EmailProvider.uiUri("uiaccount", accountId), 122 UIProvider.ACCOUNTS_PROJECTION_NO_CAPABILITIES, null, null, null); 123 124 com.android.mail.providers.Account uiAccount = getPopulatedAccountObject(ac); 125 126 return uiAccount; 127 } 128 129 private static com.android.mail.providers.Account getPopulatedAccountObject( 130 final Cursor accountCursor) { 131 if (accountCursor == null) { 132 LogUtils.e(LOG_TAG, "Null account cursor"); 133 return null; 134 } 135 136 com.android.mail.providers.Account uiAccount = null; 137 try { 138 if (accountCursor.moveToFirst()) { 139 uiAccount = new com.android.mail.providers.Account(accountCursor); 140 } 141 } finally { 142 accountCursor.close(); 143 } 144 return uiAccount; 145 } 146 147 private static Folder getFolder(Context context, long mailboxId) { 148 final ContentResolver resolver = context.getContentResolver(); 149 final Cursor fc = resolver.query(EmailProvider.uiUri("uifolder", mailboxId), 150 UIProvider.FOLDERS_PROJECTION, null, null, null); 151 152 if (fc == null) { 153 LogUtils.e(LOG_TAG, "Null folder cursor for mailboxId %d", mailboxId); 154 return null; 155 } 156 157 Folder uiFolder = null; 158 try { 159 if (fc.moveToFirst()) { 160 uiFolder = new Folder(fc); 161 } 162 } finally { 163 fc.close(); 164 } 165 return uiFolder; 166 } 167 168 /** 169 * Returns the saved account ID for the given widget. Otherwise, 170 * {@link com.android.emailcommon.provider.Account#NO_ACCOUNT} if 171 * the account ID was not previously saved. 172 */ 173 static long loadAccountIdPref(Context context, int appWidgetId) { 174 final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0); 175 long accountId = prefs.getLong(LEGACY_ACCOUNT_ID_PREFIX + appWidgetId, Account.NO_ACCOUNT); 176 return accountId; 177 } 178 179 /** 180 * Returns the saved mailbox ID for the given widget. Otherwise, 181 * {@link com.android.emailcommon.provider.Mailbox#NO_MAILBOX} if 182 * the mailbox ID was not previously saved. 183 */ 184 static long loadMailboxIdPref(Context context, int appWidgetId) { 185 final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0); 186 long mailboxId = prefs.getLong(LEGACY_MAILBOX_ID_PREFIX + appWidgetId, Mailbox.NO_MAILBOX); 187 return mailboxId; 188 } 189} 190