Merge "Fix another issue that DocUI may not refresh on content change." into nyc-andromeda-dev
diff --git a/src/com/android/documentsui/DirectoryReloadLock.java b/src/com/android/documentsui/DirectoryReloadLock.java
index ab74330..b44a963 100644
--- a/src/com/android/documentsui/DirectoryReloadLock.java
+++ b/src/com/android/documentsui/DirectoryReloadLock.java
@@ -16,8 +16,11 @@
 
 package com.android.documentsui;
 
+import static com.android.documentsui.base.Shared.DEBUG;
+
 import android.annotation.MainThread;
 import android.annotation.Nullable;
+import android.util.Log;
 
 import com.android.documentsui.base.Shared;
 import com.android.documentsui.selection.BandController;
@@ -27,6 +30,8 @@
  * while Band Selection is active.
  */
 public final class DirectoryReloadLock {
+    private static final String TAG = "DirectoryReloadLock";
+
     private int mPauseCount = 0;
     private @Nullable Runnable mCallback;
 
@@ -37,6 +42,7 @@
     public void block() {
         Shared.checkMainLoop();
         mPauseCount++;
+        if (DEBUG) Log.v(TAG, "Block count increments to " + mPauseCount + ".");
     }
 
     /**
@@ -48,6 +54,7 @@
         Shared.checkMainLoop();
         assert(mPauseCount > 0);
         mPauseCount--;
+        if (DEBUG) Log.v(TAG, "Block count decrements to " + mPauseCount + ".");
         if (mPauseCount == 0 && mCallback != null) {
             mCallback.run();
             mCallback = null;
diff --git a/src/com/android/documentsui/selection/GestureSelector.java b/src/com/android/documentsui/selection/GestureSelector.java
index 8442fdf..ff77bd0 100644
--- a/src/com/android/documentsui/selection/GestureSelector.java
+++ b/src/com/android/documentsui/selection/GestureSelector.java
@@ -128,10 +128,6 @@
             handled = handleInterceptedDownEvent(e);
         }
 
-        if (e.isActionUp()) {
-            handled = handleUpEvent(e);
-        }
-
         if (e.isActionMove()) {
             handled = handleInterceptedMoveEvent(e);
         }
@@ -148,6 +144,10 @@
             handleUpEvent(e);
         }
 
+        if (e.isActionCancel()) {
+            handleCancelEvent(e);
+        }
+
         if (e.isActionMove()) {
             handleOnTouchMoveEvent(rv, e);
         }
@@ -157,9 +157,9 @@
     // If down event happens on a file/doc, we mark that item's position as last started.
     private boolean handleInterceptedDownEvent(InputEvent e) {
         View itemView = mViewFinder.findView(e.getX(), e.getY());
-            if (itemView != null) {
-                mLastStartedItemPos = e.getItemPosition();
-            }
+        if (itemView != null) {
+            mLastStartedItemPos = e.getItemPosition();
+        }
         return false;
     }
 
@@ -175,16 +175,27 @@
         return false;
     }
 
-    // Called when ACTION_UP event is intercepted.
-    // Essentially, since this means all gesture movement is over, reset everything.
-    private boolean handleUpEvent(InputEvent e) {
+    // Called when ACTION_UP event is to be handled.
+    // Essentially, since this means all gesture movement is over, reset everything and apply
+    // provisional selection.
+    private void handleUpEvent(InputEvent e) {
+        mSelectionMgr.getSelection().applyProvisionalSelection();
+        endSelection();
+    }
+
+    // Called when ACTION_CANCEL event is to be handled.
+    // This means this gesture selection is aborted, so reset everything and abandon provisional
+    // selection.
+    private void handleCancelEvent(InputEvent e) {
+        mSelectionMgr.cancelProvisionalSelection();
+        endSelection();
+    }
+
+    private void endSelection() {
+        assert(mStarted);
         mLastStartedItemPos = -1;
-        if (mStarted) {
-            mStarted = false;
-            mSelectionMgr.getSelection().applyProvisionalSelection();
-            mLock.unblock();
-        }
-        return false;
+        mStarted = false;
+        mLock.unblock();
     }
 
     // Call when an intercepted ACTION_MOVE event is passed down.
diff --git a/src/com/android/documentsui/selection/SelectionManager.java b/src/com/android/documentsui/selection/SelectionManager.java
index 98d1cda..103ef8f 100644
--- a/src/com/android/documentsui/selection/SelectionManager.java
+++ b/src/com/android/documentsui/selection/SelectionManager.java
@@ -322,6 +322,13 @@
         notifySelectionChanged();
     }
 
+    void cancelProvisionalSelection() {
+        for (String id : mSelection.mProvisionalSelection) {
+            notifyItemStateChanged(id, false);
+        }
+        mSelection.cancelProvisionalSelection();
+    }
+
     /**
      * Stops an in-progress range selection. All selection done with
      * {@link #snapRangeSelection(int, int)} with type RANGE_PROVISIONAL will be lost if
@@ -330,7 +337,7 @@
     public void endRangeSelection() {
         mRanger = null;
         // Clean up in case there was any leftover provisional selection
-        mSelection.cancelProvisionalSelection();
+        cancelProvisionalSelection();
     }
 
     /**