Prevent account flip flopping from compose

There were two issues:
- the default account ID from the provider wasn't consistent: it was
using a snapshot from the cached data but that simply returns a Map, and
the values aren't guaranteed to be sorted (and indeed I saw that the
order was different on consecutive calls!)
- hitting app up from Compose always just kicked you out to the inbox
for the default account, but it probably should have used the account of
the compose screen, since it can be specified in the Intent

Bug: 5012008
Change-Id: Ic9a753b261e047790453bc1a9417bc0c6d2f87f9
This commit is contained in:
Ben Komalo 2011-07-13 14:43:21 -07:00
parent 5b291fe086
commit 4de538be2d
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;
}
}
}