Remove a layer of callback in inbox finder.

I'm changing it so that inbox finding happens at an earlier stage so
that the UIController.open() methods can be simpler. Specifically: I
want them to just always accept a mailbox, and not have to deal with an
intermediate state where it's still looking for a mailbox.

Change-Id: I1c5be783859a3bee7e46007e778de13eb1685cbe
diff --git a/src/com/android/email/activity/EmailActivity.java b/src/com/android/email/activity/EmailActivity.java
index 380d9aa..12d2516 100644
--- a/src/com/android/email/activity/EmailActivity.java
+++ b/src/com/android/email/activity/EmailActivity.java
@@ -231,9 +231,11 @@
 
             mUIController.open(accountId, searchMailboxId, Message.NO_MESSAGE);
         } else {
-            final long messageId = intent.getLongExtra(EXTRA_MESSAGE_ID, Message.NO_MESSAGE);
-            if (accountId != Account.NO_ACCOUNT) {
+            if (mailboxId != Mailbox.NO_MAILBOX) {
+                final long messageId = intent.getLongExtra(EXTRA_MESSAGE_ID, Message.NO_MESSAGE);
                 mUIController.open(accountId, mailboxId, messageId);
+            } else {
+                mUIController.switchAccount(accountId);
             }
         }
     }
diff --git a/src/com/android/email/activity/UIControllerBase.java b/src/com/android/email/activity/UIControllerBase.java
index e0fd1b2..5e7b795 100644
--- a/src/com/android/email/activity/UIControllerBase.java
+++ b/src/com/android/email/activity/UIControllerBase.java
@@ -29,6 +29,7 @@
 import com.android.email.Email;
 import com.android.email.R;
 import com.android.email.RefreshManager;
+import com.android.email.activity.setup.AccountSecurity;
 import com.android.email.activity.setup.AccountSettings;
 import com.android.emailcommon.Logging;
 import com.android.emailcommon.provider.Account;
@@ -454,14 +455,13 @@
             // Do nothing if the account is already selected.  Not even going back to the inbox.
             return;
         }
-        openAccount(accountId);
-    }
 
-    /**
-     * Shortcut for {@link #open} with {@link Mailbox#NO_MAILBOX} and {@link Message#NO_MESSAGE}.
-     */
-    protected final void openAccount(long accountId) {
-        open(accountId, Mailbox.NO_MAILBOX, Message.NO_MESSAGE);
+        if (accountId == Account.ACCOUNT_ID_COMBINED_VIEW) {
+            open(accountId, Mailbox.QUERY_ALL_INBOXES, Message.NO_MESSAGE);
+        } else {
+            // For a normal account, just open the inbox. Unfortunately, we have to look it up first
+            startInboxLookup(accountId);
+        }
     }
 
     /**
@@ -477,8 +477,8 @@
      *
      * @param accountId ID of the account to load.  Can be {@link Account#ACCOUNT_ID_COMBINED_VIEW}.
      *     Must never be {@link Account#NO_ACCOUNT}.
-     * @param mailboxId ID of the mailbox to load. If {@link Mailbox#NO_MAILBOX},
-     *     load the account's inbox.
+     * @param mailboxId ID of the mailbox to load. Must always be specified and cannot be
+     *     {@link Mailbox#NO_MAILBOX}
      * @param messageId ID of the message to load. If {@link Message#NO_MESSAGE},
      *     do not open a message.
      */
@@ -507,11 +507,6 @@
         mActionBarController.enterSearchMode(null);
     }
 
-    /**
-     * Callback called when the inbox lookup (started by {@link #startInboxLookup}) is finished.
-     */
-    protected abstract MailboxFinder.Callback getInboxLookupCallback();
-
     private final MailboxFinder.Callback mMailboxFinderCallback = new MailboxFinder.Callback() {
         private void cleanUp() {
             mInboxFinder = null;
@@ -519,25 +514,28 @@
 
         @Override
         public void onAccountNotFound() {
-            getInboxLookupCallback().onAccountNotFound();
+            // Account removed?
+            Welcome.actionStart(mActivity);
             cleanUp();
         }
 
         @Override
         public void onAccountSecurityHold(long accountId) {
-            getInboxLookupCallback().onAccountSecurityHold(accountId);
+            mActivity.startActivity(
+                    AccountSecurity.actionUpdateSecurityIntent(mActivity, accountId, true));
             cleanUp();
         }
 
         @Override
         public void onMailboxFound(long accountId, long mailboxId) {
-            getInboxLookupCallback().onMailboxFound(accountId, mailboxId);
+            openMailbox(accountId, mailboxId);
             cleanUp();
         }
 
         @Override
         public void onMailboxNotFound(long accountId) {
-            getInboxLookupCallback().onMailboxNotFound(accountId);
+            // Inbox not found.
+            Welcome.actionStart(mActivity);
             cleanUp();
         }
     };
@@ -545,7 +543,7 @@
     /**
      * Start inbox lookup.
      */
-    protected void startInboxLookup(long accountId) {
+    private void startInboxLookup(long accountId) {
         if (mInboxFinder != null) {
             return; // already running
         }
@@ -562,7 +560,7 @@
     /**
      * Stop inbox lookup.
      */
-    protected void stopInboxLookup() {
+    private void stopInboxLookup() {
         if (mInboxFinder == null) {
             return; // not running
         }
diff --git a/src/com/android/email/activity/UIControllerOnePane.java b/src/com/android/email/activity/UIControllerOnePane.java
index eff44d4..46cec85 100644
--- a/src/com/android/email/activity/UIControllerOnePane.java
+++ b/src/com/android/email/activity/UIControllerOnePane.java
@@ -24,8 +24,6 @@
 
 import com.android.email.Email;
 import com.android.email.R;
-import com.android.email.activity.MailboxFinder.Callback;
-import com.android.email.activity.setup.AccountSecurity;
 import com.android.emailcommon.Logging;
 import com.android.emailcommon.provider.Account;
 import com.android.emailcommon.provider.EmailContent.Message;
@@ -370,37 +368,6 @@
         return Message.NO_MESSAGE;
     }
 
-    private final MailboxFinder.Callback mInboxLookupCallback = new MailboxFinder.Callback() {
-        @Override
-        public void onMailboxFound(long accountId, long mailboxId) {
-            // Inbox found.
-            openMailbox(accountId, mailboxId);
-        }
-
-        @Override
-        public void onAccountNotFound() {
-            // Account removed?
-            Welcome.actionStart(mActivity);
-        }
-
-        @Override
-        public void onMailboxNotFound(long accountId) {
-            // Inbox not found??
-            Welcome.actionStart(mActivity);
-        }
-
-        @Override
-        public void onAccountSecurityHold(long accountId) {
-            mActivity.startActivity(AccountSecurity.actionUpdateSecurityIntent(mActivity, accountId,
-                    true));
-        }
-    };
-
-    @Override
-    protected Callback getInboxLookupCallback() {
-        return mInboxLookupCallback;
-    }
-
     @Override
     public boolean onBackPressed(boolean isSystemBackKey) {
         if (Email.DEBUG) {
@@ -461,15 +428,12 @@
         final boolean accountChanging = (getUIAccountId() != accountId);
         if (messageId != Message.NO_MESSAGE) {
             showMessageView(accountId, mailboxId, messageId, accountChanging);
-        } else if (mailboxId != Mailbox.NO_MAILBOX) {
-            showMessageList(accountId, mailboxId, accountChanging);
         } else {
-            // Mailbox not specified.  Open Inbox or Combined Inbox.
-            if (accountId == Account.ACCOUNT_ID_COMBINED_VIEW) {
-                showMessageList(accountId, Mailbox.QUERY_ALL_INBOXES, accountChanging);
-            } else {
-                startInboxLookup(accountId);
+            if (mailboxId == Mailbox.NO_MAILBOX) {
+                Log.e(Logging.LOG_TAG, this + " unspecified mailbox.");
+                return;
             }
+            showMessageList(accountId, mailboxId, accountChanging);
         }
     }
 
diff --git a/src/com/android/email/activity/UIControllerTwoPane.java b/src/com/android/email/activity/UIControllerTwoPane.java
index 88c1c51..f2c0348 100644
--- a/src/com/android/email/activity/UIControllerTwoPane.java
+++ b/src/com/android/email/activity/UIControllerTwoPane.java
@@ -28,8 +28,6 @@
 import com.android.email.Preferences;
 import com.android.email.R;
 import com.android.email.RefreshManager;
-import com.android.email.activity.MailboxFinder.Callback;
-import com.android.email.activity.setup.AccountSecurity;
 import com.android.emailcommon.Logging;
 import com.android.emailcommon.provider.Account;
 import com.android.emailcommon.provider.EmailContent.Message;
@@ -46,7 +44,6 @@
  * so that we can easily switch between synchronous and asynchronous transactions.
  */
 class UIControllerTwoPane extends UIControllerBase implements
-        MailboxFinder.Callback,
         ThreePaneLayout.Callback,
         MailboxListFragment.Callback,
         MessageListFragment.Callback,
@@ -89,43 +86,6 @@
         return R.layout.email_activity_two_pane;
     }
 
-    // MailboxFinder$Callback
-    @Override
-    public void onAccountNotFound() {
-        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
-            Log.d(Logging.LOG_TAG, this + " onAccountNotFound()");
-        }
-        // Shouldn't happen
-    }
-
-    // MailboxFinder$Callback
-    @Override
-    public void onAccountSecurityHold(long accountId) {
-        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
-            Log.d(Logging.LOG_TAG, this + " onAccountSecurityHold()");
-        }
-        mActivity.startActivity(AccountSecurity.actionUpdateSecurityIntent(mActivity, accountId,
-                true));
-    }
-
-    // MailboxFinder$Callback
-    @Override
-    public void onMailboxFound(long accountId, long mailboxId) {
-        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
-            Log.d(Logging.LOG_TAG, this + " onMailboxFound()");
-        }
-        updateMessageList(accountId, mailboxId, true);
-    }
-
-    // MailboxFinder$Callback
-    @Override
-    public void onMailboxNotFound(long accountId) {
-        if (Logging.DEBUG_LIFECYCLE && Email.DEBUG) {
-            Log.d(Logging.LOG_TAG, this + " onMailboxNotFound()");
-        }
-        Log.e(Logging.LOG_TAG, "unable to find mailbox for account " + accountId);
-    }
-
     // ThreePaneLayoutCallback
     @Override
     public void onVisiblePanesChanged(int previousVisiblePanes) {
@@ -492,11 +452,6 @@
     }
 
     @Override
-    protected Callback getInboxLookupCallback() {
-        return this;
-    }
-
-    @Override
     protected void installMessageListFragment(MessageListFragment fragment) {
         super.installMessageListFragment(fragment);
 
@@ -543,18 +498,6 @@
         final FragmentTransaction ft = mFragmentManager.beginTransaction();
         if (accountId == Account.NO_ACCOUNT) {
             throw new IllegalArgumentException();
-        } else if (mailboxId == Mailbox.NO_MAILBOX) {
-            updateMailboxList(ft, accountId, Mailbox.NO_MAILBOX, true);
-
-            // Show the appropriate message list
-            if (accountId == Account.ACCOUNT_ID_COMBINED_VIEW) {
-                // When opening the Combined view, the right pane will be "combined inbox".
-                updateMessageList(ft, accountId, Mailbox.QUERY_ALL_INBOXES, true);
-            } else {
-                // Try to find the inbox for the account
-                startInboxLookup(accountId);
-            }
-            mThreePane.showLeftPane();
         } else if (messageId == Message.NO_MESSAGE) {
             updateMailboxList(ft, accountId, mailboxId, true);
             updateMessageList(ft, accountId, mailboxId, true);
@@ -567,6 +510,10 @@
                 mThreePane.showLeftPane();
             }
         } else {
+            if (mailboxId == Mailbox.NO_MAILBOX) {
+                Log.e(Logging.LOG_TAG, this + " unspecified mailbox ");
+                return;
+            }
             updateMailboxList(ft, accountId, mailboxId, true);
             updateMessageList(ft, accountId, mailboxId, true);
             updateMessageView(ft, messageId);
@@ -647,8 +594,6 @@
             throw new IllegalArgumentException();
         }
 
-        stopInboxLookup();
-
         if (mailboxId != getMessageListMailboxId()) {
             removeMessageListFragment(ft);
             ft.add(mThreePane.getMiddlePaneId(), MessageListFragment.newInstance(