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
This commit is contained in:
Ben Komalo 2011-10-10 15:53:27 -07:00
parent 1dd5475ecf
commit 47bf70cf7b
3 changed files with 40 additions and 2 deletions

View File

@ -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();
}

View File

@ -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);
}
/**

View File

@ -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.