2012-07-20 21:57:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.provider;
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
|
|
import com.android.emailcommon.provider.Account;
|
|
|
|
import com.android.emailcommon.provider.Mailbox;
|
|
|
|
import com.android.mail.providers.Folder;
|
|
|
|
import com.android.mail.providers.UIProvider;
|
|
|
|
import com.android.mail.utils.LogTag;
|
|
|
|
import com.android.mail.utils.LogUtils;
|
|
|
|
import com.android.mail.widget.BaseWidgetProvider;
|
|
|
|
import com.android.mail.widget.WidgetService;
|
|
|
|
|
|
|
|
public class WidgetProvider extends BaseWidgetProvider {
|
|
|
|
private static final String LEGACY_PREFS_NAME = "com.android.email.widget.WidgetManager";
|
|
|
|
private static final String LEGACY_ACCOUNT_ID_PREFIX = "accountId_";
|
|
|
|
private static final String LEGACY_MAILBOX_ID_PREFIX = "mailboxId_";
|
|
|
|
|
|
|
|
private static final String LOG_TAG = LogTag.getLogTag();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove preferences when deleting widget
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onDeleted(Context context, int[] appWidgetIds) {
|
|
|
|
super.onDeleted(context, appWidgetIds);
|
|
|
|
|
|
|
|
// Remove any legacy Email widget information
|
|
|
|
final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0);
|
|
|
|
final SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
for (int widgetId : appWidgetIds) {
|
|
|
|
// Remove the account in the preference
|
|
|
|
editor.remove(LEGACY_ACCOUNT_ID_PREFIX + widgetId);
|
|
|
|
editor.remove(LEGACY_MAILBOX_ID_PREFIX + widgetId);
|
|
|
|
}
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected com.android.mail.providers.Account getAccountObject(
|
|
|
|
Context context, String accountUri) {
|
|
|
|
final ContentResolver resolver = context.getContentResolver();
|
2012-12-11 18:37:35 +00:00
|
|
|
final Cursor accountCursor = resolver.query(Uri.parse(accountUri),
|
|
|
|
UIProvider.ACCOUNTS_PROJECTION_NO_CAPABILITIES, null, null, null);
|
2012-07-20 21:57:40 +00:00
|
|
|
|
2012-12-11 18:37:35 +00:00
|
|
|
return getPopulatedAccountObject(accountCursor);
|
2012-07-20 21:57:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected boolean isAccountValid(Context context, com.android.mail.providers.Account account) {
|
|
|
|
if (account != null) {
|
|
|
|
final ContentResolver resolver = context.getContentResolver();
|
2012-12-11 18:37:35 +00:00
|
|
|
final Cursor accountCursor = resolver.query(account.uri,
|
|
|
|
UIProvider.ACCOUNTS_PROJECTION_NO_CAPABILITIES, null, null, null);
|
|
|
|
if (accountCursor != null) {
|
2012-07-20 21:57:40 +00:00
|
|
|
try {
|
2012-12-11 18:37:35 +00:00
|
|
|
return accountCursor.getCount() > 0;
|
2012-07-20 21:57:40 +00:00
|
|
|
} finally {
|
2012-12-11 18:37:35 +00:00
|
|
|
accountCursor.close();
|
2012-07-20 21:57:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-25 20:55:09 +00:00
|
|
|
@Override
|
|
|
|
protected void migrateLegacyWidgetInformation(Context context, int widgetId) {
|
2012-07-20 21:57:40 +00:00
|
|
|
final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0);
|
|
|
|
final SharedPreferences.Editor editor = prefs.edit();
|
|
|
|
|
2012-07-25 20:55:09 +00:00
|
|
|
long accountId = loadAccountIdPref(context, widgetId);
|
|
|
|
long mailboxId = loadMailboxIdPref(context, widgetId);
|
|
|
|
// Legacy support; if preferences haven't been saved for this widget, load something
|
|
|
|
if (accountId == Account.NO_ACCOUNT || mailboxId == Mailbox.NO_MAILBOX) {
|
|
|
|
LogUtils.d(LOG_TAG, "Couldn't load account or mailbox. accountId: %d" +
|
2013-09-26 21:15:50 +00:00
|
|
|
" mailboxId: %d widgetId %d", accountId, mailboxId, widgetId);
|
2012-07-25 20:55:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-07-20 21:57:40 +00:00
|
|
|
|
2013-04-19 22:06:11 +00:00
|
|
|
accountId = migrateLegacyWidgetAccountId(accountId);
|
|
|
|
mailboxId = migrateLegacyWidgetMailboxId(mailboxId, accountId);
|
|
|
|
|
2012-07-25 20:55:09 +00:00
|
|
|
// Get Account and folder objects for the account id and mailbox id
|
|
|
|
final com.android.mail.providers.Account uiAccount = getAccount(context, accountId);
|
2013-05-23 18:31:29 +00:00
|
|
|
final Folder uiFolder = EmailProvider.getFolder(context, mailboxId);
|
2012-07-20 21:57:40 +00:00
|
|
|
|
2012-07-25 20:55:09 +00:00
|
|
|
if (uiAccount != null && uiFolder != null) {
|
2013-03-07 19:00:43 +00:00
|
|
|
WidgetService.saveWidgetInformation(context, widgetId, uiAccount,
|
2013-07-11 20:24:59 +00:00
|
|
|
uiFolder.folderUri.fullUri.toString());
|
2012-07-20 21:57:40 +00:00
|
|
|
|
2014-03-27 21:33:58 +00:00
|
|
|
updateWidgetInternal(context, widgetId, uiAccount, uiFolder.type, uiFolder.capabilities,
|
2013-07-11 20:24:59 +00:00
|
|
|
uiFolder.folderUri.fullUri, uiFolder.conversationListUri, uiFolder.name);
|
2012-07-20 21:57:40 +00:00
|
|
|
|
2012-07-25 20:55:09 +00:00
|
|
|
// Now remove the old legacy preference value
|
|
|
|
editor.remove(LEGACY_ACCOUNT_ID_PREFIX + widgetId);
|
|
|
|
editor.remove(LEGACY_MAILBOX_ID_PREFIX + widgetId);
|
2012-07-20 21:57:40 +00:00
|
|
|
}
|
|
|
|
editor.apply();
|
|
|
|
}
|
|
|
|
|
2013-06-25 22:15:02 +00:00
|
|
|
private static long migrateLegacyWidgetAccountId(long accountId) {
|
2013-04-19 22:06:11 +00:00
|
|
|
if (accountId == Account.ACCOUNT_ID_COMBINED_VIEW) {
|
|
|
|
return EmailProvider.COMBINED_ACCOUNT_ID;
|
|
|
|
}
|
|
|
|
return accountId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param accountId The migrated accountId
|
|
|
|
* @return
|
|
|
|
*/
|
2013-06-25 22:15:02 +00:00
|
|
|
private static long migrateLegacyWidgetMailboxId(long mailboxId, long accountId) {
|
2013-04-19 22:06:11 +00:00
|
|
|
if (mailboxId == Mailbox.QUERY_ALL_INBOXES) {
|
|
|
|
return EmailProvider.getVirtualMailboxId(accountId, Mailbox.TYPE_INBOX);
|
|
|
|
} else if (mailboxId == Mailbox.QUERY_ALL_UNREAD) {
|
2013-06-27 00:19:17 +00:00
|
|
|
return EmailProvider.getVirtualMailboxId(accountId, Mailbox.TYPE_UNREAD);
|
2013-04-19 22:06:11 +00:00
|
|
|
}
|
|
|
|
return mailboxId;
|
|
|
|
}
|
|
|
|
|
2012-12-11 18:37:35 +00:00
|
|
|
private static com.android.mail.providers.Account getAccount(Context context, long accountId) {
|
2012-07-20 21:57:40 +00:00
|
|
|
final ContentResolver resolver = context.getContentResolver();
|
|
|
|
final Cursor ac = resolver.query(EmailProvider.uiUri("uiaccount", accountId),
|
2012-12-11 18:37:35 +00:00
|
|
|
UIProvider.ACCOUNTS_PROJECTION_NO_CAPABILITIES, null, null, null);
|
2012-07-20 21:57:40 +00:00
|
|
|
|
|
|
|
com.android.mail.providers.Account uiAccount = getPopulatedAccountObject(ac);
|
|
|
|
|
|
|
|
return uiAccount;
|
|
|
|
}
|
|
|
|
|
2012-12-11 18:37:35 +00:00
|
|
|
private static com.android.mail.providers.Account getPopulatedAccountObject(
|
|
|
|
final Cursor accountCursor) {
|
|
|
|
if (accountCursor == null) {
|
2012-07-20 21:57:40 +00:00
|
|
|
LogUtils.e(LOG_TAG, "Null account cursor");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
com.android.mail.providers.Account uiAccount = null;
|
|
|
|
try {
|
|
|
|
if (accountCursor.moveToFirst()) {
|
2014-07-11 15:17:54 +00:00
|
|
|
uiAccount = com.android.mail.providers.Account.builder().buildFrom(accountCursor);
|
2012-07-20 21:57:40 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
accountCursor.close();
|
|
|
|
}
|
|
|
|
return uiAccount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the saved account ID for the given widget. Otherwise,
|
|
|
|
* {@link com.android.emailcommon.provider.Account#NO_ACCOUNT} if
|
|
|
|
* the account ID was not previously saved.
|
|
|
|
*/
|
|
|
|
static long loadAccountIdPref(Context context, int appWidgetId) {
|
|
|
|
final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0);
|
|
|
|
long accountId = prefs.getLong(LEGACY_ACCOUNT_ID_PREFIX + appWidgetId, Account.NO_ACCOUNT);
|
|
|
|
return accountId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the saved mailbox ID for the given widget. Otherwise,
|
|
|
|
* {@link com.android.emailcommon.provider.Mailbox#NO_MAILBOX} if
|
|
|
|
* the mailbox ID was not previously saved.
|
|
|
|
*/
|
|
|
|
static long loadMailboxIdPref(Context context, int appWidgetId) {
|
|
|
|
final SharedPreferences prefs = context.getSharedPreferences(LEGACY_PREFS_NAME, 0);
|
|
|
|
long mailboxId = prefs.getLong(LEGACY_MAILBOX_ID_PREFIX + appWidgetId, Mailbox.NO_MAILBOX);
|
|
|
|
return mailboxId;
|
|
|
|
}
|
|
|
|
}
|