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" +
|
|
|
|
" mailboxId: %d widgetId %d", accountId, mailboxId);
|
|
|
|
return;
|
|
|
|
}
|
2012-07-20 21:57:40 +00:00
|
|
|
|
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);
|
|
|
|
final Folder uiFolder = getFolder(context, mailboxId);
|
2012-07-20 21:57:40 +00:00
|
|
|
|
2012-07-25 20:55:09 +00:00
|
|
|
if (uiAccount != null && uiFolder != null) {
|
|
|
|
WidgetService.saveWidgetInformation(context, widgetId, uiAccount, uiFolder);
|
2012-07-20 21:57:40 +00:00
|
|
|
|
2012-07-25 20:55:09 +00:00
|
|
|
updateWidgetInternal(context, widgetId, uiAccount, uiFolder);
|
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();
|
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
uiAccount = new com.android.mail.providers.Account(accountCursor);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
accountCursor.close();
|
|
|
|
}
|
|
|
|
return uiAccount;
|
|
|
|
}
|
|
|
|
|
2012-12-11 18:37:35 +00:00
|
|
|
private static Folder getFolder(Context context, long mailboxId) {
|
2012-07-20 21:57:40 +00:00
|
|
|
final ContentResolver resolver = context.getContentResolver();
|
|
|
|
final Cursor fc = resolver.query(EmailProvider.uiUri("uifolder", mailboxId),
|
|
|
|
UIProvider.FOLDERS_PROJECTION, null, null, null);
|
|
|
|
|
|
|
|
if (fc == null) {
|
|
|
|
LogUtils.e(LOG_TAG, "Null folder cursor for mailboxId %d", mailboxId);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
Folder uiFolder = null;
|
|
|
|
try {
|
|
|
|
if (fc.moveToFirst()) {
|
|
|
|
uiFolder = new Folder(fc);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
fc.close();
|
|
|
|
}
|
|
|
|
return uiFolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|