Merge "Prevent account flip flopping from compose"

This commit is contained in:
Jorge Lugo 2011-07-13 16:35:50 -07:00 committed by Android (Google) Code Review
commit abec4e631c
3 changed files with 17 additions and 12 deletions

View File

@ -549,7 +549,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce
* Return the id of the default account. If one hasn't been explicitly specified, return
* the first one in the database (the logic is provided within EmailProvider)
* @param context the caller's context
* @return the id of the default account, or -1 if there are no accounts
* @return the id of the default account, or Account.NO_ACCOUNT if there are no accounts
*/
static public long getDefaultAccountId(Context context) {
Cursor c = context.getContentResolver().query(
@ -561,7 +561,7 @@ public final class Account extends EmailContent implements AccountColumns, Parce
} finally {
c.close();
}
return -1;
return Account.NO_ACCOUNT;
}
/**

View File

@ -322,10 +322,10 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus
private void setAccount(Intent intent) {
long accountId = intent.getLongExtra(EXTRA_ACCOUNT_ID, -1);
if (accountId == -1) {
if (accountId == Account.NO_ACCOUNT) {
accountId = Account.getDefaultAccountId(this);
}
if (accountId == -1) {
if (accountId == Account.NO_ACCOUNT) {
// There are no accounts set up. This should not have happened. Prompt the
// user to set up an account as an acceptable bailout.
Welcome.actionStart(this);
@ -1653,10 +1653,11 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus
private void onActionBarHomePressed() {
finish();
if (isOpenedFromWithinApp()) {
// If opend from within the app, we just close it.
// If opened from within the app, we just close it.
} else {
// Otherwise, need to open the main screen. Let Welcome do that.
Welcome.actionStart(this);
// Otherwise, need to open the main screen for the appropriate account.
// Note that mAccount should always be set by the time the action bar is set up.
startActivity(Welcome.createOpenAccountInboxIntent(this, mAccount.mId));
}
}

View File

@ -1763,7 +1763,9 @@ public class EmailProvider extends ContentProvider {
// Start with a snapshot of the cache
Map<String, Cursor> accountCache = mCacheAccount.getSnapshot();
long accountId = Account.NO_ACCOUNT;
// Find the account with "isDefault" set
// Find the account with "isDefault" set, or the lowest account ID otherwise.
// Note that the snapshot from the cached isn't guaranteed to be sorted in any
// way.
Collection<Cursor> accounts = accountCache.values();
for (Cursor accountCursor: accounts) {
// For now, at least, we can have zero count cursors (e.g. if someone looks
@ -1771,11 +1773,13 @@ public class EmailProvider extends ContentProvider {
if (accountCursor.moveToFirst()) {
boolean isDefault =
accountCursor.getInt(Account.CONTENT_IS_DEFAULT_COLUMN) == 1;
long iterId = accountCursor.getLong(Account.CONTENT_ID_COLUMN);
// We'll remember this one if it's the default or the first one we see
if (isDefault || accountId == Account.NO_ACCOUNT) {
accountId = accountCursor.getLong(Account.CONTENT_ID_COLUMN);
// If it's the default, we're done
if (isDefault) break;
if (isDefault) {
accountId = iterId;
break;
} else if ((accountId == Account.NO_ACCOUNT) || (iterId < accountId)) {
accountId = iterId;
}
}
}