Save widget config to shared preferences
The way the config activity communicates with the widget will be through shared preferences. We now read / write shared preferences for widget configuration. One step closer to the configuration activity... Change-Id: I7c54259d84ad8d304a61652af5b3edff4c7d67db
This commit is contained in:
parent
44f5cd67c9
commit
fa1b3a8f37
|
@ -633,13 +633,13 @@ public abstract class EmailContent {
|
|||
|
||||
/** Selection to retrieve unread messages in "inbox" for one account */
|
||||
public static final String PER_ACCOUNT_UNREAD_SELECTION =
|
||||
ACCOUNT_KEY_SELECTION + " AND " + ALL_UNREAD_SELECTION;
|
||||
ACCOUNT_KEY_SELECTION + " AND " + ALL_UNREAD_SELECTION;
|
||||
|
||||
/** Selection to retrieve all messages in "inbox" for one account */
|
||||
public static final String PER_ACCOUNT_INBOX_SELECTION =
|
||||
ACCOUNT_KEY_SELECTION + " AND " + ALL_INBOX_SELECTION;
|
||||
|
||||
private static final String ACCOUNT_FAVORITE_SELECTION =
|
||||
public static final String PER_ACCOUNT_FAVORITE_SELECTION =
|
||||
ACCOUNT_KEY_SELECTION + " AND " + ALL_FAVORITE_SELECTION;
|
||||
|
||||
// _id field is in AbstractContent
|
||||
|
@ -923,7 +923,7 @@ public abstract class EmailContent {
|
|||
* @return number of favorite (starred) messages for an account
|
||||
*/
|
||||
public static int getFavoriteMessageCount(Context context, long accountId) {
|
||||
return count(context, Message.CONTENT_URI, ACCOUNT_FAVORITE_SELECTION,
|
||||
return count(context, Message.CONTENT_URI, PER_ACCOUNT_FAVORITE_SELECTION,
|
||||
new String[]{Long.toString(accountId)});
|
||||
}
|
||||
|
||||
|
|
|
@ -121,8 +121,9 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
|
|||
private final EmailWidgetLoader mLoader;
|
||||
private final ResourceHelper mResourceHelper;
|
||||
|
||||
/** The account ID of this widget. May be {@link Account#ACCOUNT_ID_COMBINED_VIEW}. */
|
||||
private long mAccountId = Account.NO_ACCOUNT;
|
||||
private long mMailboxId = Mailbox.NO_MAILBOX;
|
||||
/** The display name of this account */
|
||||
private String mAccountName;
|
||||
|
||||
/**
|
||||
|
@ -131,9 +132,7 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
|
|||
* Note this cursor can be closed any time by the loader. Always use {@link #isCursorValid()}
|
||||
* before touching its contents.
|
||||
*/
|
||||
private EmailWidgetLoader.CursorWithCounts mCursor;
|
||||
|
||||
private final EmailAsyncTask.Tracker mTaskTracker = new EmailAsyncTask.Tracker();
|
||||
private EmailWidgetLoader.WidgetCursor mCursor;
|
||||
|
||||
public EmailWidget(Context context, int _widgetId) {
|
||||
super();
|
||||
|
@ -176,7 +175,10 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
|
|||
accountId = data.getLong(ID_NAME_COLUMN_ID);
|
||||
accountName = data.getString(ID_NAME_COLUMN_NAME);
|
||||
}
|
||||
loadView(accountId, accountName);
|
||||
WidgetManager.saveWidgetPrefs(
|
||||
mContext, mWidgetId, accountId, Mailbox.QUERY_ALL_INBOXES);
|
||||
loadView();
|
||||
loader.reset();
|
||||
}
|
||||
});
|
||||
accountLoader.startLoading();
|
||||
|
@ -191,13 +193,9 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
|
|||
*/
|
||||
@Override
|
||||
public void onLoadComplete(Loader<Cursor> loader, Cursor cursor) {
|
||||
// Save away the cursor
|
||||
mCursor = (EmailWidgetLoader.CursorWithCounts) cursor;
|
||||
|
||||
RemoteViews views = new RemoteViews(mContext.getPackageName(), R.layout.widget);
|
||||
mCursor = (EmailWidgetLoader.WidgetCursor) cursor; // Save away the cursor
|
||||
mAccountName = mCursor.getAccountName();
|
||||
updateHeader();
|
||||
setupTitleAndCount(views);
|
||||
mWidgetManager.partiallyUpdateAppWidget(mWidgetId, views);
|
||||
mWidgetManager.notifyAppWidgetViewDataChanged(mWidgetId, R.id.message_list);
|
||||
}
|
||||
|
||||
|
@ -205,10 +203,10 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
|
|||
* Start loading the data. At this point nothing on the widget changes -- the current view
|
||||
* will remain valid until the loader loads the latest data.
|
||||
*/
|
||||
private void loadView(long accountId, String accountName) {
|
||||
mAccountId = accountId;
|
||||
mAccountName = accountName;
|
||||
mLoader.load(mAccountId, mMailboxId);
|
||||
private void loadView() {
|
||||
mAccountId = WidgetManager.loadAccountIdPref(mContext, mWidgetId);
|
||||
final long mailboxId = WidgetManager.loadMailboxIdPref(mContext, mWidgetId);
|
||||
mLoader.load(mAccountId, mailboxId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -299,6 +297,7 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
|
|||
}
|
||||
views.setTextViewText(R.id.widget_count, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the "header" of the widget (i.e. everything that doesn't include the scrolling
|
||||
* message list)
|
||||
|
@ -376,8 +375,7 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
|
|||
* @param read whether or not the message is read
|
||||
* @return a CharSequence suitable for use in RemoteViews.setTextViewText()
|
||||
*/
|
||||
private CharSequence getStyledSubjectSnippet (String subject, String snippet,
|
||||
boolean read) {
|
||||
private CharSequence getStyledSubjectSnippet(String subject, String snippet, boolean read) {
|
||||
SpannableStringBuilder ssb = new SpannableStringBuilder();
|
||||
boolean hasSubject = false;
|
||||
if (!TextUtils.isEmpty(subject)) {
|
||||
|
@ -517,7 +515,6 @@ public class EmailWidget implements RemoteViewsService.RemoteViewsFactory,
|
|||
if (mLoader != null) {
|
||||
mLoader.reset();
|
||||
}
|
||||
mTaskTracker.cancellAllInterrupt();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -18,6 +18,7 @@ package com.android.email.widget;
|
|||
|
||||
import com.android.email.data.ThrottlingCursorLoader;
|
||||
import com.android.emailcommon.provider.EmailContent;
|
||||
import com.android.emailcommon.provider.EmailContent.Account;
|
||||
import com.android.emailcommon.provider.EmailContent.Message;
|
||||
import com.android.emailcommon.provider.EmailContent.MessageColumns;
|
||||
import com.android.emailcommon.provider.Mailbox;
|
||||
|
@ -61,24 +62,32 @@ class EmailWidgetLoader extends ThrottlingCursorLoader {
|
|||
private long mMailboxId;
|
||||
|
||||
/**
|
||||
* The actual data returned by this loader.
|
||||
* Cursor data specifically for use by the Email widget. Contains a cursor of messages in
|
||||
* addition to a message count and account name. The later elements were opportunistically
|
||||
* placed in this cursor. We could have defined multiple loaders for these items.
|
||||
*/
|
||||
static class CursorWithCounts extends CursorWrapper {
|
||||
static class WidgetCursor extends CursorWrapper {
|
||||
private final int mMessageCount;
|
||||
private final String mAccountName;
|
||||
|
||||
public CursorWithCounts(Cursor cursor, int messageCount) {
|
||||
public WidgetCursor(Cursor cursor, int messageCount, String accountName) {
|
||||
super(cursor);
|
||||
mMessageCount = messageCount;
|
||||
mAccountName = accountName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The count that should be shown on the widget header.
|
||||
* Note depending on the view, it may be the unread count, which is different from
|
||||
* the record count (i.e. {@link #getCount()}}.
|
||||
* Gets the count to be shown on the widget header. If the currently viewed mailbox ID is
|
||||
* not {@link Mailbox#QUERY_ALL_FAVORITES}, it is the unread count, which is different from
|
||||
* number of records returned by {@link #getCount()}.
|
||||
*/
|
||||
public int getMessageCount() {
|
||||
return mMessageCount;
|
||||
}
|
||||
/** Gets the display name of the account */
|
||||
public String getAccountName() {
|
||||
return mAccountName;
|
||||
}
|
||||
}
|
||||
|
||||
private final Context mContext;
|
||||
|
@ -105,8 +114,15 @@ class EmailWidgetLoader extends ThrottlingCursorLoader {
|
|||
// Just use the number of all messages shown.
|
||||
messageCount = messagesCursor.getCount();
|
||||
}
|
||||
Account account = Account.restoreAccountWithId(mContext, mAccountId);
|
||||
final String accountName;
|
||||
if (account != null) {
|
||||
accountName = account.mDisplayName;
|
||||
} else {
|
||||
accountName = null;
|
||||
}
|
||||
|
||||
return new CursorWithCounts(messagesCursor, messageCount);
|
||||
return new WidgetCursor(messagesCursor, messageCount, accountName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -114,15 +130,44 @@ class EmailWidgetLoader extends ThrottlingCursorLoader {
|
|||
*
|
||||
* Must be called from the UI thread
|
||||
*
|
||||
* @param accountId
|
||||
* @param mailboxId
|
||||
* @param accountId The ID of the account. May be {@link Account#ACCOUNT_ID_COMBINED_VIEW}.
|
||||
* @param mailboxId The mailbox to load; may be one of the special mailbox IDs.
|
||||
*/
|
||||
void load(long accountId, long mailboxId) {
|
||||
reset();
|
||||
mAccountId = accountId;
|
||||
mMailboxId = mailboxId;
|
||||
setSelection(Message.PER_ACCOUNT_UNREAD_SELECTION);
|
||||
setSelectionArgs(new String[]{ Long.toString(mAccountId) });
|
||||
setSelectionAndArgs();
|
||||
startLoading();
|
||||
}
|
||||
|
||||
/** Sets the loader's selection and arguments depending upon the account and mailbox */
|
||||
private void setSelectionAndArgs() {
|
||||
if (mAccountId == Account.ACCOUNT_ID_COMBINED_VIEW) {
|
||||
if (mMailboxId == Mailbox.QUERY_ALL_INBOXES) {
|
||||
setSelection(Message.ALL_INBOX_SELECTION);
|
||||
} else if (mMailboxId == Mailbox.QUERY_ALL_FAVORITES) {
|
||||
setSelection(Message.ALL_FAVORITE_SELECTION);
|
||||
} else { // default to all unread
|
||||
setSelection(Message.ALL_UNREAD_SELECTION);
|
||||
}
|
||||
setSelectionArgs(null);
|
||||
} else {
|
||||
if (mMailboxId > 0L) {
|
||||
// Simple mailbox selection
|
||||
setSelection(MessageColumns.ACCOUNT_KEY + "=? AND " + MessageColumns.ID + "=?");
|
||||
setSelectionArgs(
|
||||
new String[] { Long.toString(mAccountId), Long.toString(mMailboxId) });
|
||||
} else {
|
||||
if (mMailboxId == Mailbox.QUERY_ALL_INBOXES) {
|
||||
setSelection(Message.PER_ACCOUNT_INBOX_SELECTION);
|
||||
} else if (mMailboxId == Mailbox.QUERY_ALL_FAVORITES) {
|
||||
setSelection(Message.PER_ACCOUNT_FAVORITE_SELECTION);
|
||||
} else { // default to all unread for the account's inbox
|
||||
setSelection(Message.PER_ACCOUNT_UNREAD_SELECTION);
|
||||
}
|
||||
setSelectionArgs(new String[] { Long.toString(mAccountId) });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,8 +17,11 @@
|
|||
package com.android.email.widget;
|
||||
|
||||
import com.android.email.Email;
|
||||
import com.android.emailcommon.provider.Mailbox;
|
||||
import com.android.emailcommon.provider.EmailContent.Account;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.FileDescriptor;
|
||||
|
@ -30,6 +33,10 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||
* Class that maintains references to all widgets.
|
||||
*/
|
||||
public class WidgetManager {
|
||||
private static final String PREFS_NAME = "com.android.email.widget.WidgetManager";
|
||||
private static final String ACCOUNT_ID_PREFIX = "accountId_";
|
||||
private static final String MAILBOX_ID_PREFIX = "mailboxId_";
|
||||
|
||||
private final static WidgetManager sInstance = new WidgetManager();
|
||||
|
||||
// Widget ID -> Widget
|
||||
|
@ -57,7 +64,7 @@ public class WidgetManager {
|
|||
// Stop loading and remove the widget from the map
|
||||
widget.onDeleted();
|
||||
}
|
||||
remove(widgetId);
|
||||
remove(context, widgetId);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -82,8 +89,9 @@ public class WidgetManager {
|
|||
mWidgets.put(widgetId, widget);
|
||||
}
|
||||
|
||||
private void remove(int widgetId) {
|
||||
private void remove(Context context, int widgetId) {
|
||||
mWidgets.remove(widgetId);
|
||||
WidgetManager.removeWidgetPrefs(context, widgetId);
|
||||
}
|
||||
|
||||
public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
|
||||
|
@ -93,4 +101,39 @@ public class WidgetManager {
|
|||
writer.println(" " + widget.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/** Saves shared preferences for the given widget */
|
||||
static void saveWidgetPrefs(Context context, int appWidgetId, long accountId, long mailboxId) {
|
||||
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
|
||||
prefs.edit()
|
||||
.putLong(ACCOUNT_ID_PREFIX + appWidgetId, accountId)
|
||||
.putLong(MAILBOX_ID_PREFIX + appWidgetId, mailboxId)
|
||||
.commit(); // preferences must be committed before we return
|
||||
}
|
||||
|
||||
/** Removes shared preferences for the given widget */
|
||||
static void removeWidgetPrefs(Context context, int appWidgetId) {
|
||||
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
for (String key : prefs.getAll().keySet()) {
|
||||
if (key.endsWith("_" + appWidgetId)) {
|
||||
editor.remove(key);
|
||||
}
|
||||
}
|
||||
editor.apply(); // just want to clean up; don't care when preferences are actually removed
|
||||
}
|
||||
|
||||
/** Gets the saved account ID for the given widget */
|
||||
static long loadAccountIdPref(Context context, int appWidgetId) {
|
||||
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
|
||||
long accountId = prefs.getLong(ACCOUNT_ID_PREFIX + appWidgetId, Account.NO_ACCOUNT);
|
||||
return accountId;
|
||||
}
|
||||
|
||||
/** Gets the saved mailbox ID for the given widget */
|
||||
static long loadMailboxIdPref(Context context, int appWidgetId) {
|
||||
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
|
||||
long accountId = prefs.getLong(MAILBOX_ID_PREFIX + appWidgetId, Mailbox.NO_MAILBOX);
|
||||
return accountId;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue