diff --git a/src/com/android/email/activity/MessageList.java b/src/com/android/email/activity/MessageList.java index 9195556bf..8df353d02 100644 --- a/src/com/android/email/activity/MessageList.java +++ b/src/com/android/email/activity/MessageList.java @@ -250,9 +250,6 @@ public class MessageList extends ListActivity implements OnItemClickListener, On * Show the appropriate account/mailbox specified by an {@link Intent}. */ private void selectAccountAndMailbox(Intent intent) { - - // TODO if we don't know the specified mailbox/account, move to the welcome screen. - mMailboxId = intent.getLongExtra(EXTRA_MAILBOX_ID, -1); if (mMailboxId != -1) { // Specific mailbox ID was provided - go directly to it @@ -1046,9 +1043,10 @@ public class MessageList extends ListActivity implements OnItemClickListener, On */ private class FindMailboxTask extends AsyncTask { - private long mAccountId; - private int mMailboxType; - private boolean mOkToRecurse; + private final long mAccountId; + private final int mMailboxType; + private final boolean mOkToRecurse; + private boolean showWelcomeActivity; /** * Special constructor to cache some local info @@ -1063,26 +1061,38 @@ public class MessageList extends ListActivity implements OnItemClickListener, On protected Long doInBackground(Void... params) { // See if we can find the requested mailbox in the DB. long mailboxId = Mailbox.findMailboxOfType(MessageList.this, mAccountId, mMailboxType); - if (mailboxId == Mailbox.NO_MAILBOX && mOkToRecurse) { - // Not found - launch network lookup - mControllerCallback.mWaitForMailboxType = mMailboxType; - mController.updateMailboxList(mAccountId, mControllerCallback); + if (mailboxId == Mailbox.NO_MAILBOX) { + // Mailbox not found. Does the account really exists? + final boolean accountExists = Account.isValidId(MessageList.this, mAccountId); + if (accountExists && mOkToRecurse) { + // launch network lookup + mControllerCallback.mWaitForMailboxType = mMailboxType; + mController.updateMailboxList(mAccountId, mControllerCallback); + } else { + // We don't want to do the network lookup, or the account doesn't exist in the + // first place. + showWelcomeActivity = true; + } } return mailboxId; } @Override protected void onPostExecute(Long mailboxId) { - if (mailboxId == null) { + if (showWelcomeActivity) { + // Let the Welcome activity show the default screen. + Welcome.actionStart(MessageList.this); + finish(); return; } - if (mailboxId != Mailbox.NO_MAILBOX) { - mMailboxId = mailboxId; - mSetTitleTask = new SetTitleTask(mMailboxId); - mSetTitleTask.execute(); - mLoadMessagesTask = new LoadMessagesTask(mMailboxId, mAccountId); - mLoadMessagesTask.execute(); + if (mailboxId == null || mailboxId == Mailbox.NO_MAILBOX) { + return; } + mMailboxId = mailboxId; + mSetTitleTask = new SetTitleTask(mMailboxId); + mSetTitleTask.execute(); + mLoadMessagesTask = new LoadMessagesTask(mMailboxId, mAccountId); + mLoadMessagesTask.execute(); } } diff --git a/src/com/android/email/provider/EmailContent.java b/src/com/android/email/provider/EmailContent.java index f2be7d153..fb7fb5190 100644 --- a/src/com/android/email/provider/EmailContent.java +++ b/src/com/android/email/provider/EmailContent.java @@ -1338,6 +1338,23 @@ public abstract class EmailContent { return id; } + /** + * @return true if an {@code accountId} is assigned to any existing account. + */ + public static boolean isValidId(Context context, long accountId) { + Cursor cursor = context.getContentResolver().query( + ContentUris.withAppendedId(CONTENT_URI, accountId), ID_PROJECTION, + null, null, null); + try { + if (cursor.moveToFirst()) { + return true; + } + } finally { + cursor.close(); + } + return false; // Not found. + } + /** * Override update to enforce a single default account, and do it atomically */ diff --git a/tests/src/com/android/email/provider/ProviderTests.java b/tests/src/com/android/email/provider/ProviderTests.java index 1a7d3e97c..ab22659a7 100644 --- a/tests/src/com/android/email/provider/ProviderTests.java +++ b/tests/src/com/android/email/provider/ProviderTests.java @@ -167,6 +167,16 @@ public class ProviderTests extends ProviderTestCase2 { return Account.CONTENT_URI.buildUpon().appendEncodedPath("" + account.mId).build(); } + public void testAccountIsValidId() { + final Account account1 = ProviderTestUtils.setupAccount("account-1", true, mMockContext); + final Account account2 = ProviderTestUtils.setupAccount("account-2", true, mMockContext); + + assertTrue(Account.isValidId(mMockContext, account1.mId)); + assertTrue(Account.isValidId(mMockContext, account2.mId)); + + assertFalse(Account.isValidId(mMockContext, 1234567)); // Some random ID + } + private final static String[] MAILBOX_UNREAD_COUNT_PROJECTION = new String [] { MailboxColumns.UNREAD_COUNT };