From 47bf70cf7bb109ee44757194c34fc8549d8b9086 Mon Sep 17 00:00:00 2001 From: Ben Komalo Date: Mon, 10 Oct 2011 15:53:27 -0700 Subject: [PATCH] Keeps track of "last account used" This only applies for the main EmailActivity. The compose window still uses the user-visible setting for "Default account" as the default account when handling intents like "mailto:" links. In the future, we should probably just remove that setting, now that we remember the last account in the reading-email case. Bug: 4460450 Change-Id: I4b3e7c9e13b7e89320891346383780cd670492ae --- src/com/android/email/Preferences.java | 25 +++++++++++++++++++ .../email/activity/UIControllerBase.java | 2 ++ src/com/android/email/activity/Welcome.java | 15 +++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/com/android/email/Preferences.java b/src/com/android/email/Preferences.java index b9288ba14..8e448bb90 100644 --- a/src/com/android/email/Preferences.java +++ b/src/com/android/email/Preferences.java @@ -22,6 +22,7 @@ import android.text.TextUtils; import android.util.Log; import com.android.emailcommon.Logging; +import com.android.emailcommon.provider.Account; import org.json.JSONArray; import org.json.JSONException; @@ -48,6 +49,7 @@ public class Preferences { private static final String TEXT_ZOOM = "textZoom"; private static final String BACKGROUND_ATTACHMENTS = "backgroundAttachments"; private static final String TRUSTED_SENDERS = "trustedSenders"; + private static final String LAST_ACCOUNT_USED = "lastAccountUsed"; public static final int AUTO_ADVANCE_NEWER = 0; public static final int AUTO_ADVANCE_OLDER = 1; @@ -262,6 +264,29 @@ public class Preferences { return new JSONArray(set).toString(); } + /** + * Returns the last used account ID as set by {@link #setLastUsedAccountId}. + * The system makes no attempt to automatically track what is considered a "use" - clients + * are expected to call {@link #setLastUsedAccountId} manually. + * + * Note that the last used account may have been deleted in the background so there is also + * no guarantee that the account exists. + */ + public long getLastUsedAccountId() { + return mSharedPreferences.getLong(LAST_ACCOUNT_USED, Account.NO_ACCOUNT); + } + + /** + * Sets the specified ID of the last account used. Treated as an opaque ID and does not + * validate the value. Value is saved asynchronously. + */ + public void setLastUsedAccountId(long accountId) { + mSharedPreferences + .edit() + .putLong(LAST_ACCOUNT_USED, accountId) + .apply(); + } + public void clear() { mSharedPreferences.edit().clear().apply(); } diff --git a/src/com/android/email/activity/UIControllerBase.java b/src/com/android/email/activity/UIControllerBase.java index 6f9d7cc0b..4d3556ec9 100644 --- a/src/com/android/email/activity/UIControllerBase.java +++ b/src/com/android/email/activity/UIControllerBase.java @@ -206,6 +206,7 @@ abstract class UIControllerBase implements MailboxListFragment.Callback, if (mNfcHandler != null) { mNfcHandler.onAccountChanged(); // workaround for email not set on initial load } + Preferences.getPreferences(mActivity).setLastUsedAccountId(getUIAccountId()); } /** @@ -556,6 +557,7 @@ abstract class UIControllerBase implements MailboxListFragment.Callback, if (mNfcHandler != null) { mNfcHandler.onAccountChanged(); } + Preferences.getPreferences(mActivity).setLastUsedAccountId(accountId); } /** diff --git a/src/com/android/email/activity/Welcome.java b/src/com/android/email/activity/Welcome.java index bfa8afc67..e77746129 100644 --- a/src/com/android/email/activity/Welcome.java +++ b/src/com/android/email/activity/Welcome.java @@ -30,6 +30,7 @@ import android.view.View; import android.view.ViewGroup.LayoutParams; import com.android.email.Email; +import com.android.email.Preferences; import com.android.email.R; import com.android.email.activity.setup.AccountSettings; import com.android.email.activity.setup.AccountSetupBasics; @@ -314,8 +315,18 @@ public class Welcome extends Activity { } } else { // Neither an accountID or a UUID is specified. - // Use the default, without showing the "account removed?" toast. - accountId = Account.getDefaultAccountId(context); + // Use the last account used, falling back to the default. + long lastUsedId = Preferences.getPreferences(context).getLastUsedAccountId(); + if (lastUsedId != Account.NO_ACCOUNT) { + if (!Account.isValidId(context, lastUsedId)) { + // The last account that was used has since been deleted. + lastUsedId = Account.NO_ACCOUNT; + Preferences.getPreferences(context).setLastUsedAccountId(Account.NO_ACCOUNT); + } + } + accountId = (lastUsedId == Account.NO_ACCOUNT) + ? Account.getDefaultAccountId(context) + : lastUsedId; } if (accountId != Account.NO_ACCOUNT) { // Okay, the given account is valid.