Improve choice mode in Conversation List

1. Set CHOICE_MODE_NONE by default on phone. We never show activated
   state on phone, so this avoids a lot of existing list view choice mode
   changes.

2. Set CHOICE_MODE_SINGLE only on tablet, and set it to NONE when in
   list view.

3. In SEARCH_CONVERSATION viewmode, set the background drawable, just
   like in CONVERSATION mode.  This is the reason why the selection
   sometimes continues in the search results mode, because it is not
   redrawn. Changing the drawable on view mode changes forces a
   refresh, which removes the selection highlights.

Bug: 8029180 Selection highlight often remains after viewing a
     conversation from search results

Change-Id: I545fe1d4e330d0b3e102ff4f898a169b13733299
diff --git a/src/com/android/mail/ui/ConversationListFragment.java b/src/com/android/mail/ui/ConversationListFragment.java
index 070bd53..a54aab5 100644
--- a/src/com/android/mail/ui/ConversationListFragment.java
+++ b/src/com/android/mail/ui/ConversationListFragment.java
@@ -235,8 +235,8 @@
     }
 
     @Override
-    public void onActivityCreated(Bundle savedInstanceState) {
-        super.onActivityCreated(savedInstanceState);
+    public void onActivityCreated(Bundle savedState) {
+        super.onActivityCreated(savedState);
         // Strictly speaking, we get back an android.app.Activity from
         // getActivity. However, the
         // only activity creating a ConversationListContext is a MailActivity
@@ -296,6 +296,21 @@
             conversationCursor.sync();
         }
 
+        // On a phone we never highlight a conversation, so the default is to select none.
+        // On a tablet, we highlight a SINGLE conversation in landscape conversation view.
+        int choice = getDefaultChoiceMode(mTabletDevice);
+        if (savedState != null) {
+            // Restore the choice mode if it was set earlier, or NONE if creating a fresh view.
+            // Choice mode here represents the current conversation only. CAB mode does not rely on
+            // the platform: checked state is a local variable {@link ConversationItemView#mChecked}
+            choice = savedState.getInt(CHOICE_MODE_KEY, choice);
+            if (savedState.containsKey(LIST_STATE_KEY)) {
+                // TODO: find a better way to unset the selected item when restoring
+                mListView.clearChoices();
+            }
+        }
+        setChoiceMode(choice);
+
         // Show list and start loading list.
         showList();
         ToastBarOperation pendingOp = mActivity.getPendingToastOperation();
@@ -306,6 +321,16 @@
         }
     }
 
+    /**
+     * Returns the default choice mode for the list based on whether the list is displayed on tablet
+     * or not.
+     * @param isTablet
+     * @return
+     */
+    private final static int getDefaultChoiceMode(boolean isTablet) {
+        return isTablet ? ListView.CHOICE_MODE_SINGLE : ListView.CHOICE_MODE_NONE;
+    }
+
     public AnimatedAdapter getAnimatedAdapter() {
         return mListAdapter;
     }
@@ -343,21 +368,9 @@
         mListView.enableSwipe(mAccount.supportsCapability(AccountCapabilities.UNDO));
         mListView.setSwipedListener(this);
 
-        final int choiceMode;
-        if (savedState != null) {
-            // Restore the choice mode if it was set earlier, or SINGLE if creating a fresh view.
-            // Choice mode here represents the current conversation only. CAB mode does not rely on
-            // the platform: it is a local variable within conversation items.
-            choiceMode = savedState.getInt(CHOICE_MODE_KEY, ListView.CHOICE_MODE_SINGLE);
-            if (savedState.containsKey(LIST_STATE_KEY)) {
-                mListView.onRestoreInstanceState(savedState.getParcelable(LIST_STATE_KEY));
-                // TODO: find a better way to unset the selected item when restoring
-                mListView.clearChoices();
-            }
-        } else {
-            choiceMode = ListView.CHOICE_MODE_SINGLE;
+        if (savedState != null && savedState.containsKey(LIST_STATE_KEY)) {
+            mListView.onRestoreInstanceState(savedState.getParcelable(LIST_STATE_KEY));
         }
-        setChoiceMode(choiceMode);
         return rootView;
     }
 
@@ -373,6 +386,10 @@
      * Tell the list to select nothing.
      */
     public final void setChoiceNone() {
+        // On a phone, the default choice mode is already none, so nothing to do.
+        if (!mTabletDevice) {
+            return;
+        }
         final int currentSelected = mListView.getCheckedItemPosition();
         mListView.clearChoices();
         // We use the activated state to show the blue highlight on tablet. Clearing the choices
@@ -391,7 +408,11 @@
      * Tell the list to get out of selecting none.
      */
     public final void revertChoiceMode() {
-        setChoiceMode(ListView.CHOICE_MODE_SINGLE);
+        // On a phone, the default choice mode is always none, so nothing to do.
+        if (!mTabletDevice) {
+            return;
+        }
+        setChoiceMode(getDefaultChoiceMode(mTabletDevice));
     }
 
     @Override
@@ -518,17 +539,13 @@
     public void onViewModeChanged(int newMode) {
         // Change the divider based on view mode.
         if (mTabletDevice) {
-            if (newMode == ViewMode.CONVERSATION) {
+            if (ViewMode.isConversationMode(newMode)) {
                 mListView.setBackgroundResource(R.drawable.panel_conversation_leftstroke);
-            } else if (newMode == ViewMode.CONVERSATION_LIST
-                    || newMode == ViewMode.SEARCH_RESULTS_LIST) {
-                // There are no selected conversations when in conversation
-                // list mode.
-                mListView.clearChoices();
+            } else if (ViewMode.isListMode(newMode)) {
+                // There are no selected conversations when in conversation list mode.
                 mListView.setBackgroundDrawable(null);
+                mListView.clearChoices();
             }
-        } else {
-            mListView.setBackgroundDrawable(null);
         }
         if (mFooterView != null) {
             mFooterView.onViewModeChanged(newMode);