Merge "Making QuickScrub work, when the fallback activity is on top" into ub-launcher3-master
diff --git a/quickstep/src/com/android/quickstep/ActivityControlHelper.java b/quickstep/src/com/android/quickstep/ActivityControlHelper.java
index 708bec0..5841285 100644
--- a/quickstep/src/com/android/quickstep/ActivityControlHelper.java
+++ b/quickstep/src/com/android/quickstep/ActivityControlHelper.java
@@ -35,7 +35,6 @@
 import com.android.launcher3.DeviceProfile;
 import com.android.launcher3.Launcher;
 import com.android.launcher3.LauncherAppState;
-import com.android.launcher3.LauncherAppTransitionManagerImpl;
 import com.android.launcher3.LauncherInitListener;
 import com.android.launcher3.LauncherState;
 import com.android.launcher3.allapps.AllAppsTransitionController;
@@ -62,7 +61,13 @@
 
     void onQuickstepGestureStarted(T activity, boolean activityVisible);
 
-    void onQuickInteractionStart(T activity, boolean activityVisible);
+    /**
+     * Updates the UI to indicate quick interaction.
+     * @return true if there any any UI change as a result of this
+     */
+    boolean onQuickInteractionStart(T activity, boolean activityVisible);
+
+    void executeOnWindowAvailable(T activity, Runnable action);
 
     void executeOnNextDraw(T activity, TaskView targetView, Runnable action);
 
@@ -83,6 +88,9 @@
     void startRecentsFromSwipe(Intent intent, AssistDataReceiver assistDataReceiver,
             final RecentsAnimationListener remoteAnimationListener);
 
+    @Nullable
+    T getCreatedActivity();
+
     @UiThread
     @Nullable
     RecentsView getVisibleRecentsView();
@@ -103,8 +111,18 @@
         }
 
         @Override
-        public void onQuickInteractionStart(Launcher activity, boolean activityVisible) {
+        public boolean onQuickInteractionStart(Launcher activity, boolean activityVisible) {
+            LauncherState fromState = activity.getStateManager().getState();
             activity.getStateManager().goToState(FAST_OVERVIEW, activityVisible);
+            return !fromState.overviewUi;
+        }
+
+        @Override
+        public void executeOnWindowAvailable(Launcher activity, Runnable action) {
+            if (activity.getWorkspace().runOnOverlayHidden(action)) {
+                // Notify the activity that qiuckscrub has started
+                onQuickstepGestureStarted(activity, true);
+            }
         }
 
         @Override
@@ -210,8 +228,8 @@
         }
 
         @Nullable
-        @UiThread
-        private Launcher getLauncher() {
+        @Override
+        public Launcher getCreatedActivity() {
             LauncherAppState app = LauncherAppState.getInstanceNoCreate();
             if (app == null) {
                 return null;
@@ -222,7 +240,7 @@
         @Nullable
         @UiThread
         private Launcher getVisibleLaucher() {
-            Launcher launcher = getLauncher();
+            Launcher launcher = getCreatedActivity();
             return (launcher != null) && launcher.isStarted() && launcher.hasWindowFocus() ?
                     launcher : null;
         }
@@ -254,8 +272,14 @@
         }
 
         @Override
-        public void onQuickInteractionStart(RecentsActivity activity, boolean activityVisible) {
-            // TODO:
+        public boolean onQuickInteractionStart(RecentsActivity activity, boolean activityVisible) {
+            // Activity does not need any UI change for quickscrub.
+            return false;
+        }
+
+        @Override
+        public void executeOnWindowAvailable(RecentsActivity activity, Runnable action) {
+            action.run();
         }
 
         @Override
@@ -339,8 +363,14 @@
 
         @Nullable
         @Override
+        public RecentsActivity getCreatedActivity() {
+            return RecentsActivityTracker.getCurrentActivity();
+        }
+
+        @Nullable
+        @Override
         public RecentsView getVisibleRecentsView() {
-            RecentsActivity activity = RecentsActivityTracker.getCurrentActivity();
+            RecentsActivity activity = getCreatedActivity();
             if (activity != null && activity.hasWindowFocus()) {
                 return activity.getOverviewPanel();
             }
diff --git a/quickstep/src/com/android/quickstep/TouchInteractionService.java b/quickstep/src/com/android/quickstep/TouchInteractionService.java
index b610f4d..84d8983 100644
--- a/quickstep/src/com/android/quickstep/TouchInteractionService.java
+++ b/quickstep/src/com/android/quickstep/TouchInteractionService.java
@@ -21,8 +21,7 @@
 import static android.view.MotionEvent.ACTION_POINTER_DOWN;
 import static android.view.MotionEvent.ACTION_POINTER_UP;
 import static android.view.MotionEvent.ACTION_UP;
-import static com.android.launcher3.LauncherState.FAST_OVERVIEW;
-import static com.android.launcher3.LauncherState.NORMAL;
+
 import static com.android.systemui.shared.system.NavigationBarCompat.HIT_TARGET_NONE;
 
 import android.annotation.TargetApi;
@@ -43,9 +42,7 @@
 import android.view.View;
 import android.view.ViewConfiguration;
 
-import com.android.launcher3.Launcher;
-import com.android.launcher3.LauncherAppState;
-import com.android.launcher3.LauncherState;
+import com.android.launcher3.BaseDraggingActivity;
 import com.android.launcher3.MainThreadExecutor;
 import com.android.launcher3.R;
 import com.android.launcher3.util.TraceHelper;
@@ -220,13 +217,13 @@
 
     private TouchConsumer getCurrentTouchConsumer(
             @HitTarget int downHitTarget, boolean forceToLauncher, VelocityTracker tracker) {
-        RunningTaskInfo runningTaskInfo = mAM.getRunningTask();
+        RunningTaskInfo runningTaskInfo = mAM.getRunningTask(0);
 
         if (runningTaskInfo == null && !forceToLauncher) {
             return mNoOpTouchConsumer;
         } else if (forceToLauncher ||
                 runningTaskInfo.topActivity.equals(mOverviewCommandHelper.launcher)) {
-            return getLauncherConsumer();
+            return getOverviewConsumer();
         } else {
             if (tracker == null) {
                 tracker = VelocityTracker.obtain();
@@ -238,18 +235,20 @@
         }
     }
 
-    private TouchConsumer getLauncherConsumer() {
-        Launcher launcher = (Launcher) LauncherAppState.getInstance(this).getModel().getCallback();
-        if (launcher == null) {
+    private TouchConsumer getOverviewConsumer() {
+        ActivityControlHelper activityHelper = mOverviewCommandHelper.getActivityControlHelper();
+        BaseDraggingActivity activity = activityHelper.getCreatedActivity();
+        if (activity == null) {
             return mNoOpTouchConsumer;
         }
-        View target = launcher.getDragLayer();
-        return new LauncherTouchConsumer(launcher, target);
+        return new OverviewTouchConsumer(activityHelper, activity);
     }
 
-    private static class LauncherTouchConsumer implements TouchConsumer {
+    private static class OverviewTouchConsumer<T extends BaseDraggingActivity>
+            implements TouchConsumer {
 
-        private final Launcher mLauncher;
+        private final ActivityControlHelper<T> mActivityHelper;
+        private final T mActivity;
         private final View mTarget;
         private final int[] mLocationOnScreen = new int[2];
         private final PointF mDownPos = new PointF();
@@ -260,12 +259,17 @@
         private boolean mInvalidated = false;
         private boolean mHadWindowFocusOnDown;
 
-        LauncherTouchConsumer(Launcher launcher, View target) {
-            mLauncher = launcher;
-            mTarget = target;
+        private float mLastProgress = 0;
+        private boolean mStartPending = false;
+        private boolean mEndPending = false;
+
+        OverviewTouchConsumer(ActivityControlHelper<T> activityHelper, T activity) {
+            mActivityHelper = activityHelper;
+            mActivity = activity;
+            mTarget = activity.getDragLayer();
             mTouchSlop = ViewConfiguration.get(mTarget.getContext()).getScaledTouchSlop();
 
-            mQuickScrubController = mLauncher.<RecentsView>getOverviewPanel()
+            mQuickScrubController = mActivity.<RecentsView>getOverviewPanel()
                     .getQuickScrubController();
         }
 
@@ -327,16 +331,22 @@
                 return;
             }
             if (interactionType == INTERACTION_QUICK_SCRUB) {
+                mStartPending = true;
+
                 Runnable action = () -> {
-                    LauncherState fromState = mLauncher.getStateManager().getState();
-                    mLauncher.getStateManager().goToState(FAST_OVERVIEW, true);
-                    mQuickScrubController.onQuickScrubStart(fromState == NORMAL);
+                    mQuickScrubController.onQuickScrubStart(
+                            mActivityHelper.onQuickInteractionStart(mActivity, true));
+                    mQuickScrubController.onQuickScrubProgress(mLastProgress);
+                    mStartPending = false;
+
+                    if (mEndPending) {
+                        mQuickScrubController.onQuickScrubEnd();
+                        mEndPending = false;
+                    }
+
                 };
 
-                if (mLauncher.getWorkspace().runOnOverlayHidden(action)) {
-                    // Hide the minus one overlay so launcher can get window focus.
-                    mLauncher.onQuickstepGestureStarted(true);
-                }
+                mActivityHelper.executeOnWindowAvailable(mActivity, action);
             }
         }
 
@@ -345,12 +355,17 @@
             if (mInvalidated) {
                 return;
             }
-            mQuickScrubController.onQuickScrubEnd();
+            if (mStartPending) {
+                mEndPending = true;
+            } else {
+                mQuickScrubController.onQuickScrubEnd();
+            }
         }
 
         @Override
         public void onQuickScrubProgress(float progress) {
-            if (mInvalidated) {
+            mLastProgress = progress;
+            if (mInvalidated || mEndPending) {
                 return;
             }
             mQuickScrubController.onQuickScrubProgress(progress);