Fix back stack when coming in from a widget or notification.

Also, got into a weird state and was able to find a few places
where null checks would be useful.
Change-Id: Ida4ac37d618ffb3736f1582351a58e5f86ab6e76
diff --git a/src/com/android/mail/ConversationListContext.java b/src/com/android/mail/ConversationListContext.java
index 6c32e26..14aa267 100644
--- a/src/com/android/mail/ConversationListContext.java
+++ b/src/com/android/mail/ConversationListContext.java
@@ -66,9 +66,6 @@
      */
     public final String searchQuery;
 
-    // Tokenized search terms for search queries.
-    private ArrayList<String> mSearchTerms;
-
     static {
         sUrlMatcher.addURI(UIProvider.AUTHORITY, "account/*/folder/*", 0);
     }
@@ -102,8 +99,7 @@
                     if (settings != null) {
                         folderCursor = context.getContentResolver().query(settings.defaultInbox,
                                 UIProvider.FOLDERS_PROJECTION, null, null, null);
-                        if (folderCursor != null) {
-                            folderCursor.moveToFirst();
+                        if (folderCursor != null && folderCursor.moveToFirst()) {
                             folder = new Folder(folderCursor);
                         }
                     }
diff --git a/src/com/android/mail/browse/ConversationListFooterView.java b/src/com/android/mail/browse/ConversationListFooterView.java
index a9c89d8..b5a041b 100644
--- a/src/com/android/mail/browse/ConversationListFooterView.java
+++ b/src/com/android/mail/browse/ConversationListFooterView.java
@@ -87,6 +87,9 @@
      * Update the view to reflect the new folder status.
      */
     public void updateStatus(final Folder folder) {
+        if (folder == null) {
+            return;
+        }
         mRetry.setTag(folder);
         mLoadMore.setTag(folder);
         if (folder.isSyncInProgress()) {
diff --git a/src/com/android/mail/ui/AbstractActivityController.java b/src/com/android/mail/ui/AbstractActivityController.java
index c1680f7..8bff494 100644
--- a/src/com/android/mail/ui/AbstractActivityController.java
+++ b/src/com/android/mail/ui/AbstractActivityController.java
@@ -111,6 +111,7 @@
     private FetchSearchFolderTask mFetchSearchFolderTask;
     /** Whether we have recorded this folder as a recent folder yet? */
     private boolean mFolderTouched = false;
+    private FetchInboxTask mFetchInboxTask;
 
     protected static final String LOG_TAG = new LogUtils().getLogTag();
     /** Constants used to differentiate between the types of loaders. */
@@ -273,6 +274,16 @@
         }
     }
 
+    // TODO(mindyp): set this up to store a copy of the folder locally
+    // as soon as we realize we haven't gotten the inbox folder yet.
+    public void loadInbox() {
+        if (mFetchInboxTask != null) {
+            mFetchInboxTask.cancel(true);
+        }
+        mFetchInboxTask = new FetchInboxTask();
+        mFetchInboxTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
+    }
+
     /** Set the current folder */
     private void setFolder(Folder folder) {
         // Start watching folder for sync status.
@@ -753,6 +764,25 @@
         }
     }
 
+    private class FetchInboxTask extends AsyncTask<Void, Void, ConversationListContext> {
+        @Override
+        public ConversationListContext doInBackground(Void... params) {
+            // Gets the default inbox since there is no context.
+            return ConversationListContext.forFolder(mActivity.getActivityContext(), mAccount,
+                    (Folder) null);
+        }
+
+        @Override
+        public void onPostExecute(ConversationListContext result) {
+            mConvListContext = result;
+            setFolder(mConvListContext.folder);
+            if (mFolderListFragment != null) {
+                mFolderListFragment.selectFolder(mConvListContext.folder);
+            }
+            showConversationList(mConvListContext);
+        }
+    }
+
     private class FetchAccountFolderTask extends AsyncTask<Void, Void, ConversationListContext> {
         @Override
         public ConversationListContext doInBackground(Void... params) {
diff --git a/src/com/android/mail/ui/OnePaneController.java b/src/com/android/mail/ui/OnePaneController.java
index 4048fdd..c7d628d 100644
--- a/src/com/android/mail/ui/OnePaneController.java
+++ b/src/com/android/mail/ui/OnePaneController.java
@@ -227,16 +227,20 @@
 
     private void transitionToInbox() {
         mViewMode.enterConversationListMode();
-        ConversationListContext listContext = ConversationListContext.forFolder(mContext,
-                mAccount, mInbox);
-        // Set the correct context for what the conversation view will be now.
-        onFolderChanged(mInbox);
-        if (isTransactionIdValid(mLastConversationListTransactionId)) {
-           // showConversationList(listContext);
-            mActivity.getFragmentManager().popBackStack(mLastConversationListTransactionId, 0);
-            resetActionBarIcon();
+        if (mInbox == null) {
+            loadInbox();
         } else {
-            showConversationList(listContext);
+            ConversationListContext listContext = ConversationListContext.forFolder(mContext,
+                    mAccount, mInbox);
+            // Set the correct context for what the conversation view will be now.
+            onFolderChanged(mInbox);
+            if (isTransactionIdValid(mLastConversationListTransactionId)) {
+               // showConversationList(listContext);
+                mActivity.getFragmentManager().popBackStack(mLastConversationListTransactionId, 0);
+                resetActionBarIcon();
+            } else {
+                showConversationList(listContext);
+            }
         }
     }