Allow touches to fall through AllApps nearing end of transition (Bug: 5991846, 6016062, 5991846)

- Also updating mTransitionProgress so control when drops succeed

Change-Id: I2f672c64f0a87249dcf7d254f963b8b2710c849f
diff --git a/src/com/android/launcher2/AppsCustomizeTabHost.java b/src/com/android/launcher2/AppsCustomizeTabHost.java
index a5964df..4639c57 100644
--- a/src/com/android/launcher2/AppsCustomizeTabHost.java
+++ b/src/com/android/launcher2/AppsCustomizeTabHost.java
@@ -53,6 +53,7 @@
     private LinearLayout mContent;
 
     private boolean mInTransition;
+    private boolean mTransitioningToWorkspace;
     private boolean mResetAfterTransition;
 
     public AppsCustomizeTabHost(Context context, AttributeSet attrs) {
@@ -154,8 +155,23 @@
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
     }
 
+     public boolean onInterceptTouchEvent(MotionEvent ev) {
+         // If we are mid transition then intercept touch events here so we can ignore them
+         if (mInTransition) {
+             return true;
+         }
+         return super.onInterceptTouchEvent(ev);
+     };
+
     @Override
     public boolean onTouchEvent(MotionEvent event) {
+        // Allow touch events to fall through if we are transitioning to the workspace
+        if (mInTransition) {
+            if (mTransitioningToWorkspace) {
+                return super.onTouchEvent(event);
+            }
+        }
+
         // Intercept all touch events up to the bottom of the AppsCustomizePane so they do not fall
         // through to the workspace and trigger showWorkspace()
         if (event.getY() < mAppsCustomizePane.getBottom()) {
@@ -349,6 +365,7 @@
     @Override
     public void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace) {
         mInTransition = true;
+        mTransitioningToWorkspace = toWorkspace;
 
         if (toWorkspace) {
             // Going from All Apps -> Workspace
@@ -377,6 +394,11 @@
     }
 
     @Override
+    public void onLauncherTransitionStep(Launcher l, float t) {
+        // Do nothing
+    }
+
+    @Override
     public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
         mInTransition = false;
         if (animated) {
diff --git a/src/com/android/launcher2/Launcher.java b/src/com/android/launcher2/Launcher.java
index 92a3d08..1a6691d 100644
--- a/src/com/android/launcher2/Launcher.java
+++ b/src/com/android/launcher2/Launcher.java
@@ -23,6 +23,7 @@
 import android.animation.ObjectAnimator;
 import android.animation.PropertyValuesHolder;
 import android.animation.ValueAnimator;
+import android.animation.ValueAnimator.AnimatorUpdateListener;
 import android.app.Activity;
 import android.app.ActivityManager;
 import android.app.AlertDialog;
@@ -2224,12 +2225,24 @@
         if (v instanceof LauncherTransitionable) {
             ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
         }
+
+        // Update the workspace transition step as well
+        dispatchOnLauncherTransitionStep(v, 0f);
+    }
+
+    private void dispatchOnLauncherTransitionStep(View v, float t) {
+        if (v instanceof LauncherTransitionable) {
+            ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
+        }
     }
 
     private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
         if (v instanceof LauncherTransitionable) {
             ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
         }
+
+        // Update the workspace transition step as well
+        dispatchOnLauncherTransitionStep(v, 1f);
     }
 
     /**
@@ -2314,6 +2327,14 @@
                 .ofFloat(toView, "alpha", 0f, 1f)
                 .setDuration(fadeDuration);
             alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
+            alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
+                @Override
+                public void onAnimationUpdate(ValueAnimator animation) {
+                    float t = (Float) animation.getAnimatedValue();
+                    dispatchOnLauncherTransitionStep(fromView, t);
+                    dispatchOnLauncherTransitionStep(toView, t);
+                }
+            });
 
             // toView should appear right at the end of the workspace shrink
             // animation
@@ -2469,6 +2490,14 @@
                 .ofFloat(fromView, "alpha", 1f, 0f)
                 .setDuration(fadeOutDuration);
             alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
+            alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
+                @Override
+                public void onAnimationUpdate(ValueAnimator animation) {
+                    float t = 1f - (Float) animation.getAnimatedValue();
+                    dispatchOnLauncherTransitionStep(fromView, t);
+                    dispatchOnLauncherTransitionStep(toView, t);
+                }
+            });
 
             mStateAnimation = new AnimatorSet();
 
@@ -3447,5 +3476,6 @@
 interface LauncherTransitionable {
     View getContent();
     void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
+    void onLauncherTransitionStep(Launcher l, float t);
     void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
 }
diff --git a/src/com/android/launcher2/Workspace.java b/src/com/android/launcher2/Workspace.java
index f61917f..0d7498a 100644
--- a/src/com/android/launcher2/Workspace.java
+++ b/src/com/android/launcher2/Workspace.java
@@ -55,6 +55,7 @@
 import android.view.DragEvent;
 import android.view.MotionEvent;
 import android.view.View;
+import android.view.ViewConfiguration;
 import android.view.ViewGroup;
 import android.view.View.MeasureSpec;
 import android.view.animation.DecelerateInterpolator;
@@ -234,7 +235,7 @@
     private float[] mNewBackgroundAlphaMultipliers;
     private float[] mNewAlphas;
     private float[] mNewRotationYs;
-    private float mTransitionProgress = 1f;
+    private float mTransitionProgress;
 
     /**
      * Used to inflate the Workspace from XML.
@@ -577,20 +578,26 @@
      */
     @Override
     public boolean onTouch(View v, MotionEvent event) {
-        return (isSmall() || mIsSwitchingState);
+        return (isSmall() || !isFinishedSwitchingState());
     }
 
     public boolean isSwitchingState() {
         return mIsSwitchingState;
     }
 
+    /** This differs from isSwitchingState in that we take into account how far the transition
+     *  has completed. */
+    private boolean isFinishedSwitchingState() {
+        return !mIsSwitchingState || (mTransitionProgress > 0.5f);
+    }
+
     protected void onWindowVisibilityChanged (int visibility) {
         mLauncher.onWindowVisibilityChanged(visibility);
     }
 
     @Override
     public boolean dispatchUnhandledMove(View focused, int direction) {
-        if (isSmall() || mIsSwitchingState) {
+        if (isSmall() || !isFinishedSwitchingState()) {
             // when the home screens are shrunken, shouldn't allow side-scrolling
             return false;
         }
@@ -618,35 +625,36 @@
 
     @Override
     protected void determineScrollingStart(MotionEvent ev) {
-        if (!isSmall() && !mIsSwitchingState) {
-            float deltaX = Math.abs(ev.getX() - mXDown);
-            float deltaY = Math.abs(ev.getY() - mYDown);
+        if (isSmall()) return;
+        if (!isFinishedSwitchingState()) return;
 
-            if (Float.compare(deltaX, 0f) == 0) return;
+        float deltaX = Math.abs(ev.getX() - mXDown);
+        float deltaY = Math.abs(ev.getY() - mYDown);
 
-            float slope = deltaY / deltaX;
-            float theta = (float) Math.atan(slope);
+        if (Float.compare(deltaX, 0f) == 0) return;
 
-            if (deltaX > mTouchSlop || deltaY > mTouchSlop) {
-                cancelCurrentPageLongPress();
-            }
+        float slope = deltaY / deltaX;
+        float theta = (float) Math.atan(slope);
 
-            if (theta > MAX_SWIPE_ANGLE) {
-                // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace
-                return;
-            } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) {
-                // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to
-                // increase the touch slop to make it harder to begin scrolling the workspace. This 
-                // results in vertically scrolling widgets to more easily. The higher the angle, the
-                // more we increase touch slop.
-                theta -= START_DAMPING_TOUCH_SLOP_ANGLE;
-                float extraRatio = (float)
-                        Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE)));
-                super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio);
-            } else {
-                // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special
-                super.determineScrollingStart(ev);
-            }
+        if (deltaX > mTouchSlop || deltaY > mTouchSlop) {
+            cancelCurrentPageLongPress();
+        }
+
+        if (theta > MAX_SWIPE_ANGLE) {
+            // Above MAX_SWIPE_ANGLE, we don't want to ever start scrolling the workspace
+            return;
+        } else if (theta > START_DAMPING_TOUCH_SLOP_ANGLE) {
+            // Above START_DAMPING_TOUCH_SLOP_ANGLE and below MAX_SWIPE_ANGLE, we want to
+            // increase the touch slop to make it harder to begin scrolling the workspace. This
+            // results in vertically scrolling widgets to more easily. The higher the angle, the
+            // more we increase touch slop.
+            theta -= START_DAMPING_TOUCH_SLOP_ANGLE;
+            float extraRatio = (float)
+                    Math.sqrt((theta / (MAX_SWIPE_ANGLE - START_DAMPING_TOUCH_SLOP_ANGLE)));
+            super.determineScrollingStart(ev, 1 + TOUCH_SLOP_DAMPING_FACTOR * extraRatio);
+        } else {
+            // Below START_DAMPING_TOUCH_SLOP_ANGLE, we don't do anything special
+            super.determineScrollingStart(ev);
         }
     }
 
@@ -676,6 +684,14 @@
             showOutlines();
         }
 
+        // If we are not fading in adjacent screens, we still need to restore the alpha in case the
+        // user scrolls while we are transitioning (should not affect dispatchDraw optimizations)
+        if (!mFadeInAdjacentScreens) {
+            for (int i = 0; i < getChildCount(); ++i) {
+                getPageAt(i).setAlpha(1f);
+            }
+        }
+
         // Show the scroll indicator as you pan the page
         showScrollingIndicator(false);
     }
@@ -1704,6 +1720,11 @@
     }
 
     @Override
+    public void onLauncherTransitionStep(Launcher l, float t) {
+        mTransitionProgress = t;
+    }
+
+    @Override
     public void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace) {
         mIsSwitchingState = false;
         mWallpaperOffset.setOverrideHorizontalCatchupConstant(false);