Fix 6360834 Select All is shown in place of Deselect all option

The CL changes the SelectionManager's toggle method so it changes itself to
inverse selection mode when all items are already selected, and onSelectionModeChange
will be triggered so the listener can update the selection menu (Select all/Deselect all)
and ActionBar.

Change-Id: I570a299cc8fd7d1d5acab948f9c6531a9adde486
b: 6360834
diff --git a/src/com/android/gallery3d/app/AlbumSetPage.java b/src/com/android/gallery3d/app/AlbumSetPage.java
index ec0fd0c..a3000b7 100644
--- a/src/com/android/gallery3d/app/AlbumSetPage.java
+++ b/src/com/android/gallery3d/app/AlbumSetPage.java
@@ -545,7 +545,7 @@
                 break;
             }
             case SelectionManager.SELECT_ALL_MODE: {
-                mActionModeHandler.setTitle(getSelectedString());
+                mActionModeHandler.updateSupportedOperation();
                 mRootPane.invalidate();
                 break;
             }
diff --git a/src/com/android/gallery3d/ui/ActionModeHandler.java b/src/com/android/gallery3d/ui/ActionModeHandler.java
index d8e60d3..786fd27 100644
--- a/src/com/android/gallery3d/ui/ActionModeHandler.java
+++ b/src/com/android/gallery3d/ui/ActionModeHandler.java
@@ -133,10 +133,6 @@
                 needsConfirm = true;
             }
             mMenuExecutor.onMenuClicked(item, needsConfirm, listener);
-            if (action == R.id.action_select_all) {
-                updateSupportedOperation();
-                updateSelectionMenu();
-            }
         } finally {
             root.unlockRenderThread();
         }
@@ -282,6 +278,8 @@
             mMenuTask.cancel();
         }
 
+        updateSelectionMenu();
+
         // Disable share action until share intent is in good shape
         final MenuItem item = mShareActionProvider != null ?
                 mMenu.findItem(R.id.action_share) : null;
diff --git a/src/com/android/gallery3d/ui/SelectionManager.java b/src/com/android/gallery3d/ui/SelectionManager.java
index 1783b11..d012913 100644
--- a/src/com/android/gallery3d/ui/SelectionManager.java
+++ b/src/com/android/gallery3d/ui/SelectionManager.java
@@ -107,15 +107,19 @@
         return mInverseSelection ^ mClickedSet.contains(itemId);
     }
 
+    private int getTotalCount() {
+        if (mTotal < 0) {
+            mTotal = mIsAlbumSet
+                    ? mSourceMediaSet.getSubMediaSetCount()
+                    : mSourceMediaSet.getMediaItemCount();
+        }
+        return mTotal;
+    }
+
     public int getSelectedCount() {
         int count = mClickedSet.size();
         if (mInverseSelection) {
-            if (mTotal < 0) {
-                mTotal = mIsAlbumSet
-                        ? mSourceMediaSet.getSubMediaSetCount()
-                        : mSourceMediaSet.getMediaItemCount();
-            }
-            count = mTotal - count;
+            count = getTotalCount() - count;
         }
         return count;
     }
@@ -128,8 +132,14 @@
             mClickedSet.add(path);
         }
 
+        // Convert to inverse selection mode if everything is selected.
+        int count = getSelectedCount();
+        if (count == getTotalCount()) {
+            selectAll();
+        }
+
         if (mListener != null) mListener.onSelectionChange(path, isItemSelected(path));
-        if (getSelectedCount() == 0 && mAutoLeave) {
+        if (count == 0 && mAutoLeave) {
             leaveSelectionMode();
         }
     }