Call onLauncherTransitionPrepare() and End() from pinch.

This makes the pinch transition more consistent with other transitions.
One immediate benefit of this is that it updates adjacent overview
panels during pinch, regardless of whether they are completely visible.

Previously the adjacent panels' alphas weren't always reset to 0.
Specifically, if you made a small pinch from workspace, which
canceled and went back to workspace, adjacent pages retained a
slightly visible panel.

Bug: 27676309
Change-Id: I7e79fddec31cd649e0811e4524b9a9a501c627f9
diff --git a/src/com/android/launcher3/LauncherStateTransitionAnimation.java b/src/com/android/launcher3/LauncherStateTransitionAnimation.java
index 5692046..17a5424 100644
--- a/src/com/android/launcher3/LauncherStateTransitionAnimation.java
+++ b/src/com/android/launcher3/LauncherStateTransitionAnimation.java
@@ -524,11 +524,13 @@
         // Cancel the current animation
         cancelAnimation();
 
+        boolean multiplePagesVisible = toWorkspaceState.hasMultipleVisiblePages;
+
         playCommonTransitionAnimations(toWorkspaceState, fromWorkspace, null,
                 animated, animated, animation, revealDuration, layerViews);
 
         if (animated) {
-            dispatchOnLauncherTransitionPrepare(fromWorkspace, animated, true);
+            dispatchOnLauncherTransitionPrepare(fromWorkspace, animated, multiplePagesVisible);
 
             final AnimatorSet stateAnimation = animation;
             final Runnable startAnimRunnable = new Runnable() {
@@ -577,7 +579,7 @@
             fromWorkspace.post(startAnimRunnable);
             mCurrentAnimation = animation;
         } else /* if (!animated) */ {
-            dispatchOnLauncherTransitionPrepare(fromWorkspace, animated, true);
+            dispatchOnLauncherTransitionPrepare(fromWorkspace, animated, multiplePagesVisible);
             dispatchOnLauncherTransitionStart(fromWorkspace, animated, true);
             dispatchOnLauncherTransitionEnd(fromWorkspace, animated, true);
 
@@ -615,6 +617,8 @@
         // Cancel the current animation
         cancelAnimation();
 
+        boolean multiplePagesVisible = toWorkspaceState.hasMultipleVisiblePages;
+
         playCommonTransitionAnimations(toWorkspaceState, fromView, toView,
                 animated, initialized, animation, revealDuration, layerViews);
 
@@ -725,8 +729,8 @@
                 }
             }
 
-            dispatchOnLauncherTransitionPrepare(fromView, animated, true);
-            dispatchOnLauncherTransitionPrepare(toView, animated, true);
+            dispatchOnLauncherTransitionPrepare(fromView, animated, multiplePagesVisible);
+            dispatchOnLauncherTransitionPrepare(toView, animated, multiplePagesVisible);
 
             animation.addListener(new AnimatorListenerAdapter() {
                 @Override
@@ -789,10 +793,10 @@
             return animation;
         } else /* if (!(animated && initialized)) */ {
             fromView.setVisibility(View.GONE);
-            dispatchOnLauncherTransitionPrepare(fromView, animated, true);
+            dispatchOnLauncherTransitionPrepare(fromView, animated, multiplePagesVisible);
             dispatchOnLauncherTransitionStart(fromView, animated, true);
             dispatchOnLauncherTransitionEnd(fromView, animated, true);
-            dispatchOnLauncherTransitionPrepare(toView, animated, true);
+            dispatchOnLauncherTransitionPrepare(toView, animated, multiplePagesVisible);
             dispatchOnLauncherTransitionStart(toView, animated, true);
             dispatchOnLauncherTransitionEnd(toView, animated, true);
             pCb.onTransitionComplete();
@@ -809,10 +813,11 @@
     /**
      * Dispatches the prepare-transition event to suitable views.
      */
-    void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
+    void dispatchOnLauncherTransitionPrepare(View v, boolean animated,
+            boolean multiplePagesVisible) {
         if (v instanceof LauncherTransitionable) {
             ((LauncherTransitionable) v).onLauncherTransitionPrepare(mLauncher, animated,
-                    toWorkspace);
+                    multiplePagesVisible);
         }
     }
 
diff --git a/src/com/android/launcher3/LauncherTransitionable.java b/src/com/android/launcher3/LauncherTransitionable.java
index 49af692..b97aaec 100644
--- a/src/com/android/launcher3/LauncherTransitionable.java
+++ b/src/com/android/launcher3/LauncherTransitionable.java
@@ -20,7 +20,7 @@
  * An interface to get callbacks during a launcher transition.
  */
 public interface LauncherTransitionable {
-    void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
+    void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean multiplePagesVisible);
     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/launcher3/PinchAnimationManager.java b/src/com/android/launcher3/PinchAnimationManager.java
index f42d37e..c8c8fa4 100644
--- a/src/com/android/launcher3/PinchAnimationManager.java
+++ b/src/com/android/launcher3/PinchAnimationManager.java
@@ -51,10 +51,10 @@
     private static final int THRESHOLD_ANIM_DURATION = 150;
     private static final LinearInterpolator INTERPOLATOR = new LinearInterpolator();
 
-    private static int INDEX_PAGE_INDICATOR = 0;
-    private static int INDEX_HOTSEAT = 1;
-    private static int INDEX_OVERVIEW_PANEL_BUTTONS = 2;
-    private static int INDEX_SCRIM = 3;
+    private static final int INDEX_PAGE_INDICATOR = 0;
+    private static final int INDEX_HOTSEAT = 1;
+    private static final int INDEX_OVERVIEW_PANEL_BUTTONS = 2;
+    private static final int INDEX_SCRIM = 3;
 
     private final Animator[] mAnimators = new Animator[4];
 
@@ -93,20 +93,21 @@
         }
         ValueAnimator animator = ValueAnimator.ofFloat(currentProgress, toProgress);
         animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
-               @Override
-               public void onAnimationUpdate(ValueAnimator animation) {
-                   float pinchProgress = (Float) animation.getAnimatedValue();
-                   setAnimationProgress(pinchProgress);
-                   thresholdManager.updateAndAnimatePassedThreshold(pinchProgress,
-                           PinchAnimationManager.this);
-               }
-           }
+                @Override
+                public void onAnimationUpdate(ValueAnimator animation) {
+                    float pinchProgress = (Float) animation.getAnimatedValue();
+                    setAnimationProgress(pinchProgress);
+                    thresholdManager.updateAndAnimatePassedThreshold(pinchProgress,
+                            PinchAnimationManager.this);
+                }
+            }
         );
         animator.addListener(new AnimatorListenerAdapter() {
             @Override
             public void onAnimationEnd(Animator animation) {
                 mIsAnimating = false;
                 thresholdManager.reset();
+                mWorkspace.onLauncherTransitionEnd(mLauncher, false, true);
             }
         });
         animator.setDuration(duration).start();
@@ -129,10 +130,6 @@
         mWorkspace.setScaleY(interpolatedScale);
         mWorkspace.setTranslationY(interpolatedTranslationY);
         setOverviewPanelsAlpha(1f - interpolatedProgress, 0);
-
-        // Make sure adjacent pages, except custom content page, are visible while scaling.
-        mWorkspace.setCustomContentVisibility(View.INVISIBLE);
-        mWorkspace.invalidate();
     }
 
     /**
@@ -168,7 +165,7 @@
             // Passing threshold 3 ends the pinch and snaps to the new state.
             if (startState == OVERVIEW && goingTowards == NORMAL) {
                 mLauncher.showWorkspace(true);
-                mWorkspace.snapToPage(mWorkspace.getPageNearestToCenterOfScreen());
+                mWorkspace.snapToPage(mWorkspace.getCurrentPage());
             } else if (startState == NORMAL && goingTowards == OVERVIEW) {
                 mLauncher.showOverviewMode(true);
             }
diff --git a/src/com/android/launcher3/PinchToOverviewListener.java b/src/com/android/launcher3/PinchToOverviewListener.java
index f32c845..0c8568e 100644
--- a/src/com/android/launcher3/PinchToOverviewListener.java
+++ b/src/com/android/launcher3/PinchToOverviewListener.java
@@ -108,6 +108,7 @@
         mInterpolator = mWorkspace.isInOverviewMode() ? new LogDecelerateInterpolator(100, 0)
                 : new LogAccelerateInterpolator(100, 0);
         mPinchStarted = true;
+        mWorkspace.onLauncherTransitionPrepare(mLauncher, false, true);
         return true;
     }
 
@@ -137,6 +138,7 @@
                     mThresholdManager);
         } else {
             mThresholdManager.reset();
+            mWorkspace.onLauncherTransitionEnd(mLauncher, false, true);
         }
         mPinchStarted = false;
         mPinchCanceled = false;
@@ -206,6 +208,4 @@
         mPreviousTimeMillis = System.currentTimeMillis();
         return false;
     }
-
-
 }
\ No newline at end of file
diff --git a/src/com/android/launcher3/Workspace.java b/src/com/android/launcher3/Workspace.java
index d55e124..88e5251 100644
--- a/src/com/android/launcher3/Workspace.java
+++ b/src/com/android/launcher3/Workspace.java
@@ -179,18 +179,21 @@
     // in all apps or customize mode)
 
     enum State {
-        NORMAL          (SearchDropTargetBar.State.SEARCH_BAR, false),
-        NORMAL_HIDDEN   (SearchDropTargetBar.State.INVISIBLE_TRANSLATED, false),
-        SPRING_LOADED   (SearchDropTargetBar.State.DROP_TARGET, false),
-        OVERVIEW        (SearchDropTargetBar.State.INVISIBLE, true),
-        OVERVIEW_HIDDEN (SearchDropTargetBar.State.INVISIBLE, true);
+        NORMAL          (SearchDropTargetBar.State.SEARCH_BAR, false, false),
+        NORMAL_HIDDEN   (SearchDropTargetBar.State.INVISIBLE_TRANSLATED, false, false),
+        SPRING_LOADED   (SearchDropTargetBar.State.DROP_TARGET, false, true),
+        OVERVIEW        (SearchDropTargetBar.State.INVISIBLE, true, true),
+        OVERVIEW_HIDDEN (SearchDropTargetBar.State.INVISIBLE, true, false);
 
         public final SearchDropTargetBar.State searchDropTargetBarState;
         public final boolean shouldUpdateWidget;
+        public final boolean hasMultipleVisiblePages;
 
-        State(SearchDropTargetBar.State searchBarState, boolean shouldUpdateWidget) {
+        State(SearchDropTargetBar.State searchBarState, boolean shouldUpdateWidget,
+                boolean hasMultipleVisiblePages) {
             searchDropTargetBarState = searchBarState;
             this.shouldUpdateWidget = shouldUpdateWidget;
+            this.hasMultipleVisiblePages = hasMultipleVisiblePages;
         }
     }
 
@@ -1838,7 +1841,7 @@
         int end = getChildCount() - 1;
 
         range[0] = Math.max(0, Math.min(start, getChildCount() - 1));
-        range[1] = Math.max(0,  end);
+        range[1] = Math.max(0, end);
     }
 
     public void onStartReordering() {
@@ -1921,11 +1924,6 @@
         // Update the current state
         mState = toState;
         updateAccessibilityFlags();
-        if (mState == State.OVERVIEW || mState == State.SPRING_LOADED) {
-            // Redraw pages, as we might want to draw pages which were not visible.
-            mForceDrawAdjacentPages = true;
-            invalidate(); // This will call dispatchDraw(), which calls getVisiblePages().
-        }
 
         if (shouldNotifyWidgetChange) {
             mLauncher.notifyWidgetProvidersChanged();
@@ -1978,12 +1976,15 @@
     }
 
     @Override
-    public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
+    public void onLauncherTransitionPrepare(Launcher l, boolean animated,
+            boolean multiplePagesVisible) {
         mIsSwitchingState = true;
         mTransitionProgress = 0;
 
-        // Invalidate here to ensure that the pages are rendered during the state change transition.
-        invalidate();
+        if (multiplePagesVisible) {
+            mForceDrawAdjacentPages = true;
+        }
+        invalidate(); // This will call dispatchDraw(), which calls getVisiblePages().
 
         updateChildrenLayersEnabled(false);
         hideCustomContentIfNecessary();
diff --git a/src/com/android/launcher3/allapps/AllAppsContainerView.java b/src/com/android/launcher3/allapps/AllAppsContainerView.java
index fa34d75..c9bd02c 100644
--- a/src/com/android/launcher3/allapps/AllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/AllAppsContainerView.java
@@ -514,7 +514,8 @@
     }
 
     @Override
-    public void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace) {
+    public void onLauncherTransitionPrepare(Launcher l, boolean animated,
+            boolean multiplePagesVisible) {
         // Do nothing
     }