Start the recents window animation on touch down.

Change-Id: Ib95f0c41e2dc0e9274268e70da63b63e3092fc94
diff --git a/quickstep/src/com/android/launcher3/LauncherAppTransitionManagerImpl.java b/quickstep/src/com/android/launcher3/LauncherAppTransitionManagerImpl.java
index 7433dfd..6289bc1 100644
--- a/quickstep/src/com/android/launcher3/LauncherAppTransitionManagerImpl.java
+++ b/quickstep/src/com/android/launcher3/LauncherAppTransitionManagerImpl.java
@@ -421,8 +421,9 @@
                                          Runnable finishedCallback) {
                 Handler handler = mLauncher.getWindow().getDecorView().getHandler();
                 postAtFrontOfQueueAsynchronously(handler, () -> {
-                    // We use a separate transition for Overview mode.
-                    if (mLauncher.isInState(LauncherState.OVERVIEW)) {
+                    if (Utilities.getPrefs(mLauncher).getBoolean("pref_use_screenshot_animation",
+                            true) && mLauncher.isInState(LauncherState.OVERVIEW)) {
+                        // We use a separate transition for Overview mode.
                         setCurrentAnimator(null);
                         finishedCallback.run();
                         return;
diff --git a/quickstep/src/com/android/quickstep/TouchInteractionService.java b/quickstep/src/com/android/quickstep/TouchInteractionService.java
index a760b75..1d8877c 100644
--- a/quickstep/src/com/android/quickstep/TouchInteractionService.java
+++ b/quickstep/src/com/android/quickstep/TouchInteractionService.java
@@ -241,6 +241,19 @@
                 mDownPos.set(ev.getX(), ev.getY());
                 mLastPos.set(mDownPos);
                 mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
+                mTouchThresholdCrossed = false;
+
+                // Clean up the old interaction handler
+                if (mInteractionHandler != null) {
+                    final BaseSwipeInteractionHandler handler = mInteractionHandler;
+                    mMainThreadExecutor.execute(handler::reset);
+                    mInteractionHandler = null;
+                }
+
+                // Start the window animation on down to give more time for launcher to draw
+                if (!isUsingScreenShot()) {
+                    startTouchTrackingForWindowAnimation();
+                }
 
                 if (mVelocityTracker == null) {
                     mVelocityTracker = VelocityTracker.obtain();
@@ -248,12 +261,6 @@
                     mVelocityTracker.clear();
                 }
                 mVelocityTracker.addMovement(ev);
-                if (mInteractionHandler != null) {
-                    final BaseSwipeInteractionHandler handler = mInteractionHandler;
-                    mMainThreadExecutor.execute(handler::reset);
-                    mInteractionHandler = null;
-                }
-                mTouchThresholdCrossed = false;
 
                 Display display = getSystemService(WindowManager.class).getDefaultDisplay();
                 mDisplayRotation = display.getRotation();
@@ -293,7 +300,11 @@
                     if (mTouchThresholdCrossed) {
                         mStartDisplacement = Math.signum(displacement) * mTouchSlop;
 
-                        startTouchTracking();
+                        if (isUsingScreenShot()) {
+                            startTouchTrackingForScreenshotAnimation();
+                        }
+
+                        // Notify the handler that the gesture has actually started
                         mInteractionHandler.onGestureStarted();
 
                         // Notify the system that we have started tracking the event
@@ -334,77 +345,76 @@
     /**
      * Called when the gesture has started.
      */
-    private void startTouchTracking() {
-        if (isUsingScreenShot()) {
-            // Create the shared handler
-            final NavBarSwipeInteractionHandler handler =
-                    new NavBarSwipeInteractionHandler(mRunningTask, this, INTERACTION_NORMAL);
+    private void startTouchTrackingForScreenshotAnimation() {
+        // Create the shared handler
+        final NavBarSwipeInteractionHandler handler =
+                new NavBarSwipeInteractionHandler(mRunningTask, this, INTERACTION_NORMAL);
 
-            TraceHelper.partitionSection("TouchInt", "Thershold crossed ");
+        TraceHelper.partitionSection("TouchInt", "Thershold crossed ");
 
-            // Start the recents activity on a background thread
-            BackgroundExecutor.get().submit(() -> {
-                // Get the snap shot before
-                handler.setTaskSnapshot(getCurrentTaskSnapshot());
+        // Start the recents activity on a background thread
+        BackgroundExecutor.get().submit(() -> {
+            // Get the snap shot before
+            handler.setTaskSnapshot(getCurrentTaskSnapshot());
 
-                // Start the launcher activity with our custom handler
-                Intent homeIntent = handler.addToIntent(new Intent(mHomeIntent));
-                startActivity(homeIntent, ActivityOptions.makeCustomAnimation(this, 0, 0).toBundle());
-                TraceHelper.partitionSection("TouchInt", "Home started");
-            });
+            // Start the launcher activity with our custom handler
+            Intent homeIntent = handler.addToIntent(new Intent(mHomeIntent));
+            startActivity(homeIntent, ActivityOptions.makeCustomAnimation(this, 0, 0).toBundle());
+            TraceHelper.partitionSection("TouchInt", "Home started");
+        });
 
-            // Preload the plan
-            mRecentsModel.loadTasks(mRunningTask.id, null);
-            mInteractionHandler = handler;
-            mInteractionHandler.setGestureEndCallback(() ->  mInteractionHandler = null);
-        } else {
+        // Preload the plan
+        mRecentsModel.loadTasks(mRunningTask.id, null);
+        mInteractionHandler = handler;
+        mInteractionHandler.setGestureEndCallback(() ->  mInteractionHandler = null);
+    }
 
-            // Create the shared handler
-            final WindowTransformSwipeHandler handler =
-                    new WindowTransformSwipeHandler(mRunningTask, this);
-            BackgroundExecutor.get().submit(() -> {
-                ActivityManagerWrapper.getInstance().startRecentsActivity(mHomeIntent,
-                        new AssistDataReceiver() {
-                            @Override
-                            public void onHandleAssistData(Bundle bundle) {
-                                // Pass to AIAI
+    private void startTouchTrackingForWindowAnimation() {
+        // Create the shared handler
+        final WindowTransformSwipeHandler handler =
+                new WindowTransformSwipeHandler(mRunningTask, this);
+        BackgroundExecutor.get().submit(() -> {
+            ActivityManagerWrapper.getInstance().startRecentsActivity(mHomeIntent,
+                    new AssistDataReceiver() {
+                        @Override
+                        public void onHandleAssistData(Bundle bundle) {
+                            // Pass to AIAI
+                        }
+                    },
+                    new RecentsAnimationListener() {
+                        public void onAnimationStart(
+                                RecentsAnimationControllerCompat controller,
+                                RemoteAnimationTargetCompat[] apps) {
+                            if (mInteractionHandler == handler) {
+                                handler.setRecentsAnimation(controller, apps);
+
+                            } else {
+                                controller.finish(false /* toHome */);
                             }
-                        },
-                        new RecentsAnimationListener() {
-                            public void onAnimationStart(
-                                    RecentsAnimationControllerCompat controller,
-                                    RemoteAnimationTargetCompat[] apps) {
-                                if (mInteractionHandler == handler) {
-                                    handler.setRecentsAnimation(controller, apps);
+                        }
 
-                                } else {
-                                    controller.finish(false /* toHome */);
-                                }
+                        public void onAnimationCanceled() {
+                            if (mInteractionHandler == handler) {
+                                handler.setRecentsAnimation(null, null);
                             }
+                        }
+                    }, null, null);
+        });
 
-                            public void onAnimationCanceled() {
-                                if (mInteractionHandler == handler) {
-                                    handler.setRecentsAnimation(null, null);
-                                }
-                            }
-                        }, null, null);
-            });
-
-            // Preload the plan
-            mRecentsModel.loadTasks(mRunningTask.id, null);
-            mInteractionHandler = handler;
-            handler.setGestureEndCallback(() -> {
-                if (handler == mInteractionHandler) {
-                    mInteractionHandler = null;
-                }
-            });
-            handler.setLauncherOnDrawCallback(() -> {
-                if (handler == mInteractionHandler) {
-                    mEventQueue.setInterimChoreographer(null);
-                }
-            });
-            mMainThreadExecutor.execute(handler::initWhenReady);
-        }
+        // Preload the plan
+        mRecentsModel.loadTasks(mRunningTask.id, null);
+        mInteractionHandler = handler;
+        handler.setGestureEndCallback(() -> {
+            if (handler == mInteractionHandler) {
+                mInteractionHandler = null;
+            }
+        });
+        handler.setLauncherOnDrawCallback(() -> {
+            if (handler == mInteractionHandler) {
+                mEventQueue.setInterimChoreographer(null);
+            }
+        });
+        mMainThreadExecutor.execute(handler::initWhenReady);
     }
 
     private void updateTouchTracking(@InteractionType int interactionType) {
@@ -425,6 +435,11 @@
                     : isNavBarOnLeft() ? -mVelocityTracker.getXVelocity(mActivePointerId)
                     : mVelocityTracker.getYVelocity(mActivePointerId);
             mInteractionHandler.onGestureEnded(velocity);
+        } else if (!isUsingScreenShot()) {
+            // Since we start touch tracking on DOWN, we may reach this state without actually
+            // starting the gesture. In that case, just cleanup immediately.
+            final BaseSwipeInteractionHandler handler = mInteractionHandler;
+            mMainThreadExecutor.execute(handler::reset);
         }
         mVelocityTracker.recycle();
         mVelocityTracker = null;
diff --git a/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java b/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java
index 652f142..1351973 100644
--- a/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java
+++ b/quickstep/src/com/android/quickstep/WindowTransformSwipeHandler.java
@@ -31,12 +31,10 @@
 import android.app.ActivityManager.RunningTaskInfo;
 import android.content.Context;
 import android.content.res.Resources;
-import android.graphics.Color;
 import android.graphics.Matrix;
 import android.graphics.Rect;
 import android.os.Build;
 import android.os.Looper;
-import android.os.UserHandle;
 import android.support.annotation.UiThread;
 import android.support.annotation.WorkerThread;
 import android.view.View;
@@ -57,8 +55,6 @@
 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.Task;
-import com.android.systemui.shared.recents.model.Task.TaskKey;
 import com.android.systemui.shared.recents.model.ThumbnailData;
 import com.android.systemui.shared.system.RecentsAnimationControllerCompat;
 import com.android.systemui.shared.system.RemoteAnimationTargetCompat;
@@ -454,10 +450,12 @@
             mGestureEndCallback.run();
         }
 
-        // TODO: These should be done as part of ActivityOptions#OnAnimationStarted
-        mLauncher.getStateManager().reapplyState();
-        mLauncher.setOnResumeCallback(() -> mLauncherLayoutListener.close(false));
-        mLauncherTransitionController.setPlayFraction(1);
+        if (mLauncher != null) {
+            // TODO: These should be done as part of ActivityOptions#OnAnimationStarted
+            mLauncher.getStateManager().reapplyState();
+            mLauncher.setOnResumeCallback(() -> mLauncherLayoutListener.close(false));
+            mLauncherTransitionController.setPlayFraction(1);
+        }
         clearReference();
     }