Fix 6399813: tapping on a photo that isn't in the middle of the screen doesn't select it.

Also make tapping while flinging stop the scroll instead of opening the item.

Change-Id: Iefef4738d7d74b29e4594ea3ae2cb1c91e0e17ab
diff --git a/src/com/android/gallery3d/ui/PhotoView.java b/src/com/android/gallery3d/ui/PhotoView.java
index adbf791..7b97e78 100644
--- a/src/com/android/gallery3d/ui/PhotoView.java
+++ b/src/com/android/gallery3d/ui/PhotoView.java
@@ -748,11 +748,15 @@
         private boolean mModeChanged;
         // If this scaling gesture should be ignored.
         private boolean mIgnoreScalingGesture;
+        // whether the down action happened while the view is scrolling.
+        private boolean mDownInScrolling;
 
         @Override
         public boolean onSingleTapUp(float x, float y) {
-            if (mFilmMode) {
+            if (mFilmMode && !mDownInScrolling) {
+                switchToHitPicture((int) (x + 0.5f), (int) (y + 0.5f));
                 setFilmMode(false);
+                mIgnoreUpEvent = true;
                 return true;
             }
 
@@ -881,6 +885,13 @@
         @Override
         public void onDown() {
             mHolding |= HOLD_TOUCH_DOWN;
+
+            if (mFilmMode && mPositionController.isScrolling()) {
+                mDownInScrolling = true;
+                mPositionController.stopScrolling();
+            } else {
+                mDownInScrolling = false;
+            }
         }
 
         @Override
@@ -1020,6 +1031,26 @@
         return 0;
     }
 
+    // Switch to the previous or next picture if the hit position is inside
+    // one of their boxes. This runs in main thread.
+    private void switchToHitPicture(int x, int y) {
+        if (mPrevBound < 0) {
+            Rect r = mPositionController.getPosition(-1);
+            if (r.right >= x) {
+                slideToPrevPicture();
+                return;
+            }
+        }
+
+        if (mNextBound > 0) {
+            Rect r = mPositionController.getPosition(1);
+            if (r.left <= x) {
+                slideToNextPicture();
+                return;
+            }
+        }
+    }
+
     ////////////////////////////////////////////////////////////////////////////
     //  Page mode focus switching
     //
diff --git a/src/com/android/gallery3d/ui/PositionController.java b/src/com/android/gallery3d/ui/PositionController.java
index 03fe65b..c09ffea 100644
--- a/src/com/android/gallery3d/ui/PositionController.java
+++ b/src/com/android/gallery3d/ui/PositionController.java
@@ -1003,6 +1003,16 @@
         return edges;
     }
 
+    public boolean isScrolling() {
+        return mPlatform.mAnimationStartTime != NO_ANIMATION
+                && mPlatform.mCurrentX != mPlatform.mToX;
+    }
+
+    public void stopScrolling() {
+        if (mPlatform.mAnimationStartTime == NO_ANIMATION) return;
+        mPlatform.mFromX = mPlatform.mToX = mPlatform.mCurrentX;
+    }
+
     ////////////////////////////////////////////////////////////////////////////
     //  Private utilities
     ////////////////////////////////////////////////////////////////////////////