Merge "Only enter conversation mode if we have search results." into jb-ub-mail
diff --git a/src/com/android/mail/ui/AbstractActivityController.java b/src/com/android/mail/ui/AbstractActivityController.java
index 6efb8d1..b3ee6f5 100644
--- a/src/com/android/mail/ui/AbstractActivityController.java
+++ b/src/com/android/mail/ui/AbstractActivityController.java
@@ -297,6 +297,8 @@
     private boolean mRecentsDataUpdated;
     /** A wait fragment we added, if any. */
     private WaitFragment mWaitFragment;
+    /** True if we have results from a search query */
+    private boolean mHaveSearchResults = false;
     public static final String SYNC_ERROR_DIALOG_FRAGMENT_TAG = "SyncErrorDialogFragment";
 
     public AbstractActivityController(MailActivity activity, ViewMode viewMode) {
@@ -1576,19 +1578,20 @@
             }
         } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
             if (intent.hasExtra(Utils.EXTRA_ACCOUNT)) {
+                mHaveSearchResults = false;
                 // Save this search query for future suggestions.
                 final String query = intent.getStringExtra(SearchManager.QUERY);
                 final String authority = mContext.getString(R.string.suggestions_authority);
                 final SearchRecentSuggestions suggestions = new SearchRecentSuggestions(
                         mContext, authority, SuggestionsProvider.MODE);
                 suggestions.saveRecentQuery(query, null);
-                if (Utils.showTwoPaneSearchResults(mActivity.getActivityContext())) {
+                setAccount((Account) intent.getParcelableExtra(Utils.EXTRA_ACCOUNT));
+                fetchSearchFolder(intent);
+                if (shouldEnterSearchConvMode()) {
                     mViewMode.enterSearchResultsConversationMode();
                 } else {
                     mViewMode.enterSearchResultsListMode();
                 }
-                setAccount((Account) intent.getParcelableExtra(Utils.EXTRA_ACCOUNT));
-                fetchSearchFolder(intent);
             } else {
                 LogUtils.e(LOG_TAG, "Missing account extra from search intent.  Finishing");
                 mActivity.finish();
@@ -1600,6 +1603,13 @@
     }
 
     /**
+     * Returns true if we should enter conversation mode with search.
+     */
+    protected final boolean shouldEnterSearchConvMode() {
+        return mHaveSearchResults && Utils.showTwoPaneSearchResults(mActivity.getActivityContext());
+    }
+
+    /**
      * Copy any selected conversations stored in the saved bundle into our selection set,
      * triggering {@link ConversationSetObserver} callbacks as our selection set changes.
      *
@@ -2096,6 +2106,7 @@
                                     .getStringExtra(UIProvider.SearchQueryParameters.QUERY));
                     showConversationList(mConvListContext);
                     mActivity.invalidateOptionsMenu();
+                    mHaveSearchResults = search.totalCount > 0;
                     mActivity.getLoaderManager().destroyLoader(LOADER_SEARCH);
                 } else {
                     LogUtils.e(LOG_TAG, "Null or empty cursor returned by LOADER_SEARCH loader");
@@ -2360,6 +2371,7 @@
             mConversationListCursor.sync();
         }
         mTracker.onCursorUpdated();
+        perhapsShowFirstSearchResult();
     }
 
     @Override
@@ -2620,15 +2632,7 @@
                 // check and inform the cursor of the change in visibility here.
                 informCursorVisiblity(true);
             }
-            // Shown for search results in two-pane mode only.
-            if (shouldShowFirstConversation()) {
-                if (mConversationListCursor.getCount() > 0) {
-                    mConversationListCursor.moveToPosition(0);
-                    final Conversation conv = new Conversation(mConversationListCursor);
-                    conv.position = 0;
-                    onConversationSelected(conv, true /* checkSafeToModifyFragments */);
-                }
-            }
+            perhapsShowFirstSearchResult();
         }
 
         @Override
@@ -2649,6 +2653,22 @@
     }
 
     /**
+     * Updates controller state based on search results and shows first conversation if required.
+     */
+    private final void perhapsShowFirstSearchResult() {
+        // Shown for search results in two-pane mode only.
+        mHaveSearchResults = Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction())
+                && mConversationListCursor.getCount() > 0;
+        if (!shouldShowFirstConversation()) {
+            return;
+        }
+        mConversationListCursor.moveToPosition(0);
+        final Conversation conv = new Conversation(mConversationListCursor);
+        conv.position = 0;
+        onConversationSelected(conv, true /* checkSafeToModifyFragments */);
+    }
+
+    /**
      * Destroy the pending {@link DestructiveAction} till now and assign the given action as the
      * next destructive action..
      * @param nextAction the next destructive action to be performed. This can be null.
diff --git a/src/com/android/mail/ui/SearchMailActionBarView.java b/src/com/android/mail/ui/SearchMailActionBarView.java
index c218403..6fff316 100644
--- a/src/com/android/mail/ui/SearchMailActionBarView.java
+++ b/src/com/android/mail/ui/SearchMailActionBarView.java
@@ -95,7 +95,7 @@
         // Work around b/6664203 by manually forcing this view to be VISIBLE
         // upon ActionView collapse. DISPLAY_SHOW_CUSTOM will still control its final
         // visibility.
-        int mode = getMode();
+        final int mode = getMode();
         if (mode == ViewMode.SEARCH_RESULTS_LIST
                 || (Utils.showTwoPaneSearchResults(getContext())
                         && mode == ViewMode.SEARCH_RESULTS_CONVERSATION)) {
diff --git a/src/com/android/mail/ui/TwoPaneController.java b/src/com/android/mail/ui/TwoPaneController.java
index 4def9dc..61a699d 100644
--- a/src/com/android/mail/ui/TwoPaneController.java
+++ b/src/com/android/mail/ui/TwoPaneController.java
@@ -61,7 +61,7 @@
     private void initializeConversationListFragment(boolean show) {
         if (show) {
             if (Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction())) {
-                if (Utils.showTwoPaneSearchResults(mActivity.getActivityContext())) {
+                if (shouldEnterSearchConvMode()) {
                     mViewMode.enterSearchResultsConversationMode();
                 } else {
                     mViewMode.enterSearchResultsListMode();
@@ -419,7 +419,7 @@
 
     @Override
     public void exitSearchMode() {
-        int mode = mViewMode.getMode();
+        final int mode = mViewMode.getMode();
         if (mode == ViewMode.SEARCH_RESULTS_LIST
                 || (mode == ViewMode.SEARCH_RESULTS_CONVERSATION
                         && Utils.showTwoPaneSearchResults(mActivity.getApplicationContext()))) {
@@ -430,7 +430,7 @@
     @Override
     public boolean shouldShowFirstConversation() {
         return Intent.ACTION_SEARCH.equals(mActivity.getIntent().getAction())
-                && Utils.showTwoPaneSearchResults(mActivity.getApplicationContext());
+                && shouldEnterSearchConvMode();
     }
 
     private int getUndoBarWidth(int mode, ToastBarOperation op) {