Merge "Fixing bug where drag would sometimes get cancelled inadvertently" into ics-mr1
diff --git a/src/com/android/launcher2/PagedView.java b/src/com/android/launcher2/PagedView.java
index 2f66537..ba303a1 100644
--- a/src/com/android/launcher2/PagedView.java
+++ b/src/com/android/launcher2/PagedView.java
@@ -61,8 +61,6 @@
 
     // the min drag distance for a fling to register, to prevent random page shifts
     private static final int MIN_LENGTH_FOR_FLING = 25;
-    // The min drag distance to trigger a page shift (regardless of velocity)
-    private static final int MIN_LENGTH_FOR_MOVE = 200;
 
     private static final int PAGE_SNAP_ANIMATION_DURATION = 550;
     protected static final float NANOTIME_DIV = 1000000000.0f;
@@ -72,6 +70,8 @@
     private static final int MINIMUM_SNAP_VELOCITY = 2200;
     private static final int MIN_FLING_VELOCITY = 250;
     private static final float RETURN_TO_ORIGINAL_PAGE_THRESHOLD = 0.33f;
+    // The page is moved more than halfway, automatically move to the next page on touch up.
+    private static final float SIGNIFICANT_MOVE_THRESHOLD = 0.4f;
 
     // the velocity at which a fling gesture will cause us to snap to the next page
     protected int mSnapVelocity = 500;
@@ -1205,24 +1205,25 @@
                 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                 int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
                 final int deltaX = (int) (x - mDownMotionX);
-                boolean isSignificantMove = Math.abs(deltaX) > MIN_LENGTH_FOR_MOVE;
+                final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
+                boolean isSignificantMove = Math.abs(deltaX) > pageWidth *
+                        SIGNIFICANT_MOVE_THRESHOLD;
                 final int snapVelocity = mSnapVelocity;
 
                 mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);
 
+                boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
+                        Math.abs(velocityX) > snapVelocity;
+
                 // In the case that the page is moved far to one direction and then is flung
                 // in the opposite direction, we use a threshold to determine whether we should
                 // just return to the starting page, or if we should skip one further.
                 boolean returnToOriginalPage = false;
-                final int pageWidth = getScaledMeasuredWidth(getPageAt(mCurrentPage));
                 if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD &&
-                        Math.signum(velocityX) != Math.signum(deltaX)) {
+                        Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
                     returnToOriginalPage = true;
                 }
 
-                boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING &&
-                        Math.abs(velocityX) > snapVelocity;
-
                 int finalPage;
                 // We give flings precedence over large moves, which is why we short-circuit our
                 // test for a large move if a fling has been registered. That is, a large
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index 034b11c..ea7c0be 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -57,6 +57,7 @@
 import android.view.DragEvent;
 import android.view.MotionEvent;
 import android.view.View;
+import android.view.ViewConfiguration;
 import android.view.ViewGroup;
 import android.view.animation.DecelerateInterpolator;
 import android.widget.ImageView;
@@ -1280,21 +1281,34 @@
         return (mBackground != null && mBackgroundAlpha > 0.0f && mDrawBackground);
     }
 
-    @Override
-    protected void dispatchDraw(Canvas canvas) {
+    public void scrollTo (int x, int y) {
+        super.scrollTo(x, y);
+        syncChildrenLayersEnabledOnVisiblePages();
+    }
+
+    // This method just applies the value mChildrenLayersEnabled to all the pages that
+    // will be rendered on the next frame.
+    // We do this because calling setChildrenLayersEnabled on a view that's not
+    // visible/rendered causes slowdowns on some graphics cards
+    private void syncChildrenLayersEnabledOnVisiblePages() {
         if (mChildrenLayersEnabled) {
             getVisiblePages(mTempVisiblePagesRange);
             final int leftScreen = mTempVisiblePagesRange[0];
             final int rightScreen = mTempVisiblePagesRange[1];
             if (leftScreen != -1 && rightScreen != -1) {
-                // calling setChildrenLayersEnabled on a view that's not visible/rendered
-                // causes slowdowns on some graphics cards, so we set it here to be sure
-                // it's only called when it's safe (ie when the view will be rendered)
                 for (int i = leftScreen; i <= rightScreen; i++) {
-                    ((ViewGroup)getPageAt(i)).setChildrenLayersEnabled(true);
+                    ViewGroup page = (ViewGroup) getPageAt(i);
+                    if (page.getVisibility() == VISIBLE &&
+                            page.getAlpha() > ViewConfiguration.ALPHA_THRESHOLD) {
+                        ((ViewGroup)getPageAt(i)).setChildrenLayersEnabled(true);
+                    }
                 }
             }
         }
+    }
+
+    @Override
+    protected void dispatchDraw(Canvas canvas) {
         super.dispatchDraw(canvas);
 
         if (mInScrollArea && !LauncherApplication.isScreenLarge()) {
@@ -1723,6 +1737,7 @@
                                 b * mNewBackgroundAlphaMultipliers[i]);
                         cl.setFastAlpha(a * mOldAlphas[i] + b * mNewAlphas[i]);
                     }
+                    syncChildrenLayersEnabledOnVisiblePages();
                 }
             });
 
@@ -1760,6 +1775,7 @@
             // Fade the background gradient away
             animateBackgroundGradient(0f, true);
         }
+        syncChildrenLayersEnabledOnVisiblePages();
     }
 
     /**