OSDN Git Service

Add velocity threshold so that fast pinches (flings) are detected.
authorTony Wickham <twickham@google.com>
Wed, 6 Apr 2016 01:31:43 +0000 (18:31 -0700)
committerTony Wickham <twickham@google.com>
Wed, 6 Apr 2016 21:13:29 +0000 (14:13 -0700)
Bug: 27676309
Change-Id: Id3ba05c67abac8847fcff22532ea9da9ef10a5ae

src/com/android/launcher3/PinchToOverviewListener.java

index 74e6b39..f32c845 100644 (file)
@@ -33,6 +33,11 @@ import android.view.ScaleGestureDetector;
 public class PinchToOverviewListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
     private static final float OVERVIEW_PROGRESS = 0f;
     private static final float WORKSPACE_PROGRESS = 1f;
+    /**
+     * The velocity threshold at which a pinch will be completed instead of canceled,
+     * even if the first threshold has not been passed. Measured in progress / millisecond
+     */
+    private static final float FLING_VELOCITY = 0.003f;
 
     private ScaleGestureDetector mPinchDetector;
     private Launcher mLauncher;
@@ -110,17 +115,20 @@ public class PinchToOverviewListener extends ScaleGestureDetector.SimpleOnScaleG
     public void onScaleEnd(ScaleGestureDetector detector) {
         super.onScaleEnd(detector);
 
+        float progressVelocity = mProgressDelta / mTimeDelta;
         float passedThreshold = mThresholdManager.getPassedThreshold();
+        boolean isFling = mWorkspace.isInOverviewMode() && progressVelocity >= FLING_VELOCITY
+                || !mWorkspace.isInOverviewMode() && progressVelocity <= -FLING_VELOCITY;
+        boolean shouldCancelPinch = !isFling && passedThreshold < PinchThresholdManager.THRESHOLD_ONE;
         // If we are going towards overview, mPreviousProgress is how much further we need to
         // go, since it is going from 1 to 0. If we are going to workspace, we want
         // 1 - mPreviousProgress.
         float remainingProgress = mPreviousProgress;
-        if (mWorkspace.isInOverviewMode() || passedThreshold < PinchThresholdManager.THRESHOLD_ONE) {
+        if (mWorkspace.isInOverviewMode() || shouldCancelPinch) {
             remainingProgress = 1f - mPreviousProgress;
         }
-        int duration = computeDuration(remainingProgress, mProgressDelta, mTimeDelta);
-
-        if (passedThreshold < PinchThresholdManager.THRESHOLD_ONE) {
+        int duration = computeDuration(remainingProgress, progressVelocity);
+        if (shouldCancelPinch) {
             cancelPinch(mPreviousProgress, duration);
         } else if (passedThreshold < PinchThresholdManager.THRESHOLD_THREE) {
             float toProgress = mWorkspace.isInOverviewMode() ?
@@ -138,8 +146,8 @@ public class PinchToOverviewListener extends ScaleGestureDetector.SimpleOnScaleG
      * Compute the amount of time required to complete the transition based on the current pinch
      * speed. If this time is too long, instead return the normal duration, ignoring the speed.
      */
-    private int computeDuration(float remainingProgress, float progressDelta, long timeDelta) {
-        float progressSpeed = Math.abs(progressDelta) / timeDelta;
+    private int computeDuration(float remainingProgress, float progressVelocity) {
+        float progressSpeed = Math.abs(progressVelocity);
         int remainingMillis = (int) (remainingProgress / progressSpeed);
         return Math.min(remainingMillis, mAnimationManager.getNormalOverviewTransitionDuration());
     }