Commit leave behind items when tapping account switcher

There is no onclick listener for it
or onshow for the spinner popup window
this is my best guess as to how to tell when it got tapped
when the popup is shown, the dropdown views will be accessed/ built
Dismiss when cab mode is exited.

part of b/6864248 dismiss leave behind on next action taken

Change-Id: Ib706f61a47356412cef73822a4a40453214d22f2
diff --git a/src/com/android/mail/AccountSpinnerAdapter.java b/src/com/android/mail/AccountSpinnerAdapter.java
index f191415..cf0fe8a 100644
--- a/src/com/android/mail/AccountSpinnerAdapter.java
+++ b/src/com/android/mail/AccountSpinnerAdapter.java
@@ -31,6 +31,7 @@
 import com.android.mail.providers.Folder;
 import com.android.mail.providers.FolderWatcher;
 import com.android.mail.ui.ControllableActivity;
+import com.android.mail.ui.ConversationListCallbacks;
 import com.android.mail.ui.RecentFolderList;
 import com.android.mail.utils.LogTag;
 import com.android.mail.utils.LogUtils;
@@ -43,6 +44,7 @@
  * This class keeps the account and folder information and returns appropriate views.
  */
 public class AccountSpinnerAdapter extends BaseAdapter {
+    private ConversationListCallbacks mActivityController;
     private final LayoutInflater mInflater;
     /**
      * The position of the current account being viewed.
@@ -206,6 +208,7 @@
 
     /**
      * Create a spinner adapter with the context and the list of recent folders.
+     * @param activity
      * @param context
      * @param recentFolders
      * @param showAllFolders
@@ -216,11 +219,12 @@
         mInflater = LayoutInflater.from(context);
         mRecentFolders = recentFolders;
         mShowAllFoldersItem = showAllFolders;
-        // Owned by the AccountSpinnerAdapter since nobody else needed it. Move to controller if
-        // required. The folder watcher tells us directly when new data is available. We are only
-        // interested in unread counts at this point.
+        // Owned by the AccountSpinnerAdapter since nobody else needed it. Move
+        // to controller if required. The folder watcher tells us directly when
+        // new data is available. We are only interested in unread counts at this point.
         mFolderWatcher = new FolderWatcher(activity, this);
         mCurrentAccount = mAccountObserver.initialize(activity.getAccountController());
+        mActivityController = activity.getListHandler();
     }
 
     /**
@@ -360,6 +364,10 @@
 
     @Override
     public View getDropDownView(int position, View view, ViewGroup parent) {
+        if (position == 0) {
+            // Commit any leave behind items.
+            mActivityController.commitDestructiveActions(false);
+        }
         // Shown in the first text view with big font.
         String bigText = "";
         // Shown in the second text view with smaller font.
diff --git a/src/com/android/mail/browse/ConversationItemView.java b/src/com/android/mail/browse/ConversationItemView.java
index b3b2acf..a0a03b8 100644
--- a/src/com/android/mail/browse/ConversationItemView.java
+++ b/src/com/android/mail/browse/ConversationItemView.java
@@ -1191,12 +1191,15 @@
             mChecked = !mChecked;
             Conversation conv = mHeader.conversation;
             // Set the list position of this item in the conversation
-            ListView listView = getListView();
+            SwipeableListView listView = getListView();
             conv.position = mChecked && listView != null ? listView.getPositionForView(this)
                     : Conversation.NO_POSITION;
             if (mSelectedConversationSet != null) {
                 mSelectedConversationSet.toggle(this, conv);
             }
+            if (mSelectedConversationSet.isEmpty()) {
+                listView.commitDestructiveActions(true);
+            }
             // We update the background after the checked state has changed
             // now that we have a selected background asset. Setting the background
             // usually waits for a layout pass, but we don't need a full layout,
diff --git a/src/com/android/mail/browse/SelectedConversationsActionMenu.java b/src/com/android/mail/browse/SelectedConversationsActionMenu.java
index 6840f1f..102c3e8 100644
--- a/src/com/android/mail/browse/SelectedConversationsActionMenu.java
+++ b/src/com/android/mail/browse/SelectedConversationsActionMenu.java
@@ -110,7 +110,7 @@
         mListView = list;
 
         mContext = mActivity.getActivityContext();
-        mUpdater = ((ControllableActivity) mActivity).getConversationUpdater();
+        mUpdater = activity.getConversationUpdater();
     }
 
     @Override
@@ -452,6 +452,10 @@
         // active, that implies the user hit "Done" in the top right, and resources need cleaning.
         if (mActivated) {
             destroy();
+            // Only commit destructive actions if the user actually pressed
+            // done; otherwise, this was handled when we toggled conversation
+            // selection state.
+            ((ControllableActivity)mActivity).getListHandler().commitDestructiveActions(true);
         }
         mMenu = null;
     }
diff --git a/src/com/android/mail/ui/AbstractActivityController.java b/src/com/android/mail/ui/AbstractActivityController.java
index 198de26..f93a550 100644
--- a/src/com/android/mail/ui/AbstractActivityController.java
+++ b/src/com/android/mail/ui/AbstractActivityController.java
@@ -1183,7 +1183,8 @@
         return mDestroyed;
     }
 
-    private void commitDestructiveActions(boolean animate) {
+    @Override
+    public void commitDestructiveActions(boolean animate) {
         ConversationListFragment fragment = getConversationListFragment();
         if (fragment != null) {
             fragment.commitDestructiveActions(animate);
diff --git a/src/com/android/mail/ui/ConversationListCallbacks.java b/src/com/android/mail/ui/ConversationListCallbacks.java
index 4ff3777..8c83a8c 100644
--- a/src/com/android/mail/ui/ConversationListCallbacks.java
+++ b/src/com/android/mail/ui/ConversationListCallbacks.java
@@ -44,4 +44,10 @@
 
     void registerConversationListObserver(DataSetObserver observer);
     void unregisterConversationListObserver(DataSetObserver observer);
+
+    /**
+     * Commit any destructive action leave behind items so that it is no longer
+     * possible to undo them.
+     */
+    void commitDestructiveActions(boolean animate);
 }
diff --git a/src/com/android/mail/ui/MailActionBarView.java b/src/com/android/mail/ui/MailActionBarView.java
index b5f8060..2cbf708 100644
--- a/src/com/android/mail/ui/MailActionBarView.java
+++ b/src/com/android/mail/ui/MailActionBarView.java
@@ -233,7 +233,8 @@
         mController.registerFolderObserver(mFolderObserver);
         // We don't want to include the "Show all folders" menu item on tablet devices
         final boolean showAllFolders = !Utils.useTabletUI(getContext());
-        mSpinner = new AccountSpinnerAdapter(activity, getContext(), recentFolders, showAllFolders);
+        mSpinner = new AccountSpinnerAdapter(activity, getContext(), recentFolders,
+                showAllFolders);
         mAccount = mAccountObserver.initialize(activity.getAccountController());
     }
 
diff --git a/src/com/android/mail/ui/SwipeableListView.java b/src/com/android/mail/ui/SwipeableListView.java
index b4601be..490da24 100644
--- a/src/com/android/mail/ui/SwipeableListView.java
+++ b/src/com/android/mail/ui/SwipeableListView.java
@@ -219,6 +219,11 @@
         if (mConvSelectionSet != null && !mConvSelectionSet.isEmpty()
                 && mConvSelectionSet.contains(conv)) {
             mConvSelectionSet.toggle(null, conv);
+            // Don't commit destructive actions if the item we just removed from
+            // the selection set is the item we just destroyed!
+            if (!conv.isMostlyDead() && mConvSelectionSet.isEmpty()) {
+                commitDestructiveActions(true);
+            }
         }
     }