Merge "Reusing the existing handler when quickscrub starts" into ub-launcher3-master
diff --git a/quickstep/src/com/android/quickstep/NavBarSwipeInteractionHandler.java b/quickstep/src/com/android/quickstep/NavBarSwipeInteractionHandler.java
index eb5195c..b295df0 100644
--- a/quickstep/src/com/android/quickstep/NavBarSwipeInteractionHandler.java
+++ b/quickstep/src/com/android/quickstep/NavBarSwipeInteractionHandler.java
@@ -15,6 +15,11 @@
  */
 package com.android.quickstep;
 
+import static com.android.quickstep.TouchInteractionService.INTERACTION_NORMAL;
+import static com.android.quickstep.TouchInteractionService.INTERACTION_QUICK_SCRUB;
+import static com.android.quickstep.TouchInteractionService.INTERACTION_QUICK_SWITCH;
+import static com.android.quickstep.TouchInteractionService.isInteractionQuick;
+
 import android.animation.Animator;
 import android.animation.ObjectAnimator;
 import android.animation.RectEvaluator;
@@ -44,8 +49,9 @@
 import com.android.launcher3.anim.AnimationSuccessListener;
 import com.android.launcher3.anim.Interpolators;
 import com.android.launcher3.states.InternalStateHandler;
-import com.android.launcher3.uioverrides.RecentsViewStateController;
+import com.android.launcher3.util.Preconditions;
 import com.android.launcher3.util.TraceHelper;
+import com.android.quickstep.TouchInteractionService.InteractionType;
 import com.android.systemui.shared.recents.model.RecentsTaskLoadPlan;
 import com.android.systemui.shared.recents.model.Task;
 import com.android.systemui.shared.recents.model.Task.TaskKey;
@@ -93,20 +99,22 @@
     private Launcher mLauncher;
     private SnapshotDragView mDragView;
     private RecentsView mRecentsView;
-    private RecentsViewStateController mStateController;
     private QuickScrubController mQuickScrubController;
     private Hotseat mHotseat;
 
+    private boolean mWasLauncherAlreadyVisible;
+
     private boolean mLauncherReady;
     private boolean mTouchEndHandled;
     private float mCurrentDisplacement;
-    private @TouchInteractionService.InteractionType int mInteractionType;
+
+    private @InteractionType int mInteractionType;
     private boolean mStartedQuickScrubFromHome;
 
     private Bitmap mTaskSnapshot;
 
     NavBarSwipeInteractionHandler(RunningTaskInfo runningTaskInfo, Context context,
-            @TouchInteractionService.InteractionType int interactionType) {
+            @InteractionType int interactionType) {
         // TODO: We need a better way for this
         TaskKey taskKey = new TaskKey(runningTaskInfo.id, 0, null, UserHandle.myUserId(), 0);
         mRunningTask = new Task(taskKey, null, null, "", "", Color.BLACK, Color.BLACK,
@@ -183,8 +191,8 @@
         mLauncher = launcher;
         mRecentsView = launcher.getOverviewPanel();
         mRecentsView.showTask(mRunningTask);
-        mStateController = mRecentsView.getStateController();
         mHotseat = mLauncher.getHotseat();
+        mWasLauncherAlreadyVisible = alreadyOnHome;
 
         AbstractFloatingView.closeAllOpenViews(mLauncher, alreadyOnHome);
         mLauncher.getStateManager().goToState(LauncherState.OVERVIEW, alreadyOnHome);
@@ -194,17 +202,8 @@
         mDragView.setPivotX(0);
         mDragView.setPivotY(0);
 
-        boolean interactionIsQuick
-                = mInteractionType == TouchInteractionService.INTERACTION_QUICK_SCRUB
-                || mInteractionType == TouchInteractionService.INTERACTION_QUICK_SWITCH;
-        mStartedQuickScrubFromHome = alreadyOnHome && interactionIsQuick;
-        if (interactionIsQuick) {
-            mQuickScrubController = mRecentsView.getQuickScrubController();
-            mQuickScrubController.onQuickScrubStart(mStartedQuickScrubFromHome);
-            animateToProgress(1f, MAX_SWIPE_DURATION);
-            if (mStartedQuickScrubFromHome) {
-                mDragView.setVisibility(View.INVISIBLE);
-            }
+        if (isInteractionQuick(mInteractionType)) {
+            updateUiForQuickScrub();
         }
 
         // Optimization
@@ -215,6 +214,34 @@
         TraceHelper.partitionSection("TouchInt", "Launcher on new intent");
     }
 
+
+    public void updateInteractionType(@InteractionType int interactionType) {
+        Preconditions.assertUIThread();
+        if (mInteractionType != INTERACTION_NORMAL) {
+            throw new IllegalArgumentException(
+                    "Can't change interaction type from " + mInteractionType);
+        }
+        if (!isInteractionQuick(interactionType)) {
+            throw new IllegalArgumentException(
+                    "Can't change interaction type to " + interactionType);
+        }
+        mInteractionType = interactionType;
+
+        if (mLauncher != null) {
+            updateUiForQuickScrub();
+        }
+    }
+
+    private void updateUiForQuickScrub() {
+        mStartedQuickScrubFromHome = mWasLauncherAlreadyVisible;
+        mQuickScrubController = mRecentsView.getQuickScrubController();
+        mQuickScrubController.onQuickScrubStart(mStartedQuickScrubFromHome);
+        animateToProgress(1f, MAX_SWIPE_DURATION);
+        if (mStartedQuickScrubFromHome) {
+            mDragView.setVisibility(View.INVISIBLE);
+        }
+    }
+
     @UiThread
     public void updateDisplacement(float displacement) {
         mCurrentDisplacement = displacement;
@@ -334,7 +361,7 @@
         if (currentRecentsPage instanceof TaskView) {
             ((TaskView) currentRecentsPage).animateIconToScale(1f);
         }
-        if (mInteractionType == TouchInteractionService.INTERACTION_QUICK_SWITCH) {
+        if (mInteractionType == INTERACTION_QUICK_SWITCH) {
             for (int i = mRecentsView.getFirstTaskIndex(); i < mRecentsView.getPageCount(); i++) {
                 TaskView taskView = (TaskView) mRecentsView.getPageAt(i);
                 // TODO: Match the keys directly
@@ -345,7 +372,7 @@
                     break;
                 }
             }
-        } else if (mInteractionType == TouchInteractionService.INTERACTION_QUICK_SCRUB) {
+        } else if (mInteractionType == INTERACTION_QUICK_SCRUB) {
             if (mQuickScrubController != null) {
                 mQuickScrubController.snapToPageForCurrentQuickScrubSection();
             }
@@ -355,12 +382,16 @@
     public void onQuickScrubEnd() {
         if (mQuickScrubController != null) {
             mQuickScrubController.onQuickScrubEnd();
+        } else {
+            // TODO:
         }
     }
 
     public void onQuickScrubProgress(float progress) {
         if (mQuickScrubController != null) {
             mQuickScrubController.onQuickScrubProgress(progress);
+        } else {
+            // TODO:
         }
     }
 }
diff --git a/quickstep/src/com/android/quickstep/TouchInteractionService.java b/quickstep/src/com/android/quickstep/TouchInteractionService.java
index c35ffee..9b31c14 100644
--- a/quickstep/src/com/android/quickstep/TouchInteractionService.java
+++ b/quickstep/src/com/android/quickstep/TouchInteractionService.java
@@ -52,6 +52,7 @@
 
 import com.android.launcher3.Launcher;
 import com.android.launcher3.LauncherAppState;
+import com.android.launcher3.MainThreadExecutor;
 import com.android.launcher3.util.TraceHelper;
 import com.android.systemui.shared.recents.IOverviewProxy;
 import com.android.systemui.shared.recents.ISystemUiProxy;
@@ -99,6 +100,7 @@
         @Override
         public void onQuickSwitch() {
             startTouchTracking(INTERACTION_QUICK_SWITCH);
+            mInteractionHandler = null;
         }
 
         @Override
@@ -110,6 +112,7 @@
         public void onQuickScrubEnd() {
             if (mInteractionHandler != null) {
                 mInteractionHandler.onQuickScrubEnd();
+                mInteractionHandler = null;
             }
         }
 
@@ -137,6 +140,7 @@
     private Intent mHomeIntent;
     private ComponentName mLauncher;
     private MotionEventQueue mEventQueue;
+    private MainThreadExecutor mMainThreadExecutor;
 
     private final PointF mDownPos = new PointF();
     private final PointF mLastPos = new PointF();
@@ -156,6 +160,7 @@
         super.onCreate();
         mAM = ActivityManagerWrapper.getInstance();
         mRecentsModel = RecentsModel.getInstance(this);
+        mMainThreadExecutor = new MainThreadExecutor();
 
         mHomeIntent = new Intent(Intent.ACTION_MAIN)
                 .addCategory(Intent.CATEGORY_HOME)
@@ -284,7 +289,17 @@
         return mDisplayRotation == Surface.ROTATION_270 && mStableInsets.left > 0;
     }
 
+
     private void startTouchTracking(@InteractionType int interactionType) {
+        if (isInteractionQuick(interactionType)) {
+            // TODO: Send action cancel if its the Launcher consumer
+        }
+        if (mInteractionHandler != null) {
+            final NavBarSwipeInteractionHandler handler = mInteractionHandler;
+            mMainThreadExecutor.execute(() -> handler.updateInteractionType(interactionType));
+            return;
+        }
+
         // Create the shared handler
         final NavBarSwipeInteractionHandler handler =
                 new NavBarSwipeInteractionHandler(mRunningTask, this, interactionType);
@@ -430,4 +445,9 @@
             ev.setEdgeFlags(flags);
         }
     }
+
+    public static boolean isInteractionQuick(@InteractionType int interactionType) {
+        return interactionType == INTERACTION_QUICK_SCRUB ||
+                interactionType == INTERACTION_QUICK_SWITCH;
+    }
 }