Merge changes Ib95f0c41,I5a1488aa into ub-launcher3-master

* changes:
  Start the recents window animation on touch down.
  Only keep running task id.
diff --git a/quickstep/src/com/android/launcher3/LauncherAppTransitionManagerImpl.java b/quickstep/src/com/android/launcher3/LauncherAppTransitionManagerImpl.java
index f10b574..31c195d 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/NavBarSwipeInteractionHandler.java b/quickstep/src/com/android/quickstep/NavBarSwipeInteractionHandler.java
index 944804b..e2abd59 100644
--- a/quickstep/src/com/android/quickstep/NavBarSwipeInteractionHandler.java
+++ b/quickstep/src/com/android/quickstep/NavBarSwipeInteractionHandler.java
@@ -92,7 +92,7 @@
     // animated to 1, so allow for a smooth transition.
     private final AnimatedFloat mActivityMultiplier = new AnimatedFloat(this::updateFinalShift);
 
-    private final Task mRunningTask;
+    private final int mRunningTaskId;
     private final Context mContext;
 
     private final MultiStateCallback mStateCallback;
@@ -116,13 +116,9 @@
 
     NavBarSwipeInteractionHandler(RunningTaskInfo runningTaskInfo, Context context,
             @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,
-                true, false, false, false, null, 0, null, false);
-
         mContext = context;
         mInteractionType = interactionType;
+        mRunningTaskId = runningTaskInfo.id;
         WindowManagerWrapper.getInstance().getStableInsets(mStableInsets);
 
         DeviceProfile dp = LauncherAppState.getIDP(mContext).getDeviceProfile(mContext);
@@ -192,7 +188,7 @@
         launcher.setOnResumeCallback(this);
         mLauncher = launcher;
         mRecentsView = launcher.getOverviewPanel();
-        mRecentsView.showTask(mRunningTask);
+        mRecentsView.showTask(mRunningTaskId);
         mHotseat = mLauncher.getHotseat();
         mWasLauncherAlreadyVisible = alreadyOnHome;
 
@@ -340,18 +336,15 @@
 
     @UiThread
     private void resumeLastTask() {
-        // TODO: We need a better way for this
-        TaskKey key = mRunningTask.key;
         RecentsTaskLoadPlan loadPlan = RecentsModel.getInstance(mContext).getLastLoadPlan();
         if (loadPlan != null) {
-            Task task = loadPlan.getTaskStack().findTaskWithId(key.id);
+            Task task = loadPlan.getTaskStack().findTaskWithId(mRunningTaskId);
             if (task != null) {
-                key = task.key;
+                ActivityOptions opts = ActivityOptions.makeCustomAnimation(mContext, 0, 0);
+                ActivityManagerWrapper.getInstance().startActivityFromRecentsAsync(task.key, opts,
+                        null, null);
             }
         }
-
-        ActivityOptions opts = ActivityOptions.makeCustomAnimation(mContext, 0, 0);
-        ActivityManagerWrapper.getInstance().startActivityFromRecentsAsync(key, opts, null, null);
     }
 
     public void reset() {
@@ -380,8 +373,7 @@
         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
-                if (taskView.getTask().key.id != mRunningTask.key.id) {
+                if (taskView.getTask().key.id != mRunningTaskId) {
                     mRecentsView.snapToPage(i, QUICK_SWITCH_SNAP_DURATION);
                     taskView.postDelayed(() -> {taskView.launchTask(true);},
                             QUICK_SWITCH_SNAP_DURATION);
diff --git a/quickstep/src/com/android/quickstep/RecentsView.java b/quickstep/src/com/android/quickstep/RecentsView.java
index 6937c1f..8e03f37 100644
--- a/quickstep/src/com/android/quickstep/RecentsView.java
+++ b/quickstep/src/com/android/quickstep/RecentsView.java
@@ -98,7 +98,8 @@
     private final RecentsModel mModel;
     private int mLoadPlanId = -1;
 
-    private Task mFirstTask;
+    // Only valid until the launcher state changes to NORMAL
+    private int mRunningTaskId = -1;
 
     private Bitmap mScrim;
     private Paint mFadePaint;
@@ -241,13 +242,6 @@
         final ArrayList<Task> tasks = new ArrayList<>(stack.getTasks());
         setLayoutTransition(null);
 
-        if (mFirstTask != null) {
-            // TODO: Handle this case here once we have a valid implementation for mFirstTask
-            if (tasks.isEmpty() || !keysEquals(tasks.get(tasks.size() - 1), mFirstTask)) {
-                // tasks.add(mFirstTask);
-            }
-        }
-
         final int requiredChildCount = tasks.size() + mFirstTaskIndex;
         for (int i = getChildCount(); i < requiredChildCount; i++) {
             final TaskView taskView = (TaskView) inflater.inflate(R.layout.task, this, false);
@@ -394,7 +388,7 @@
     }
 
     public void reset() {
-        mFirstTask = null;
+        mRunningTaskId = -1;
         setCurrentPage(0);
     }
 
@@ -407,11 +401,7 @@
      */
     public void reloadIfNeeded() {
         if (!mModel.isLoadPlanValid(mLoadPlanId)) {
-            int taskId = -1;
-            if (mFirstTask != null) {
-                taskId = mFirstTask.key.id;
-            }
-            mLoadPlanId = mModel.loadTasks(taskId, this::applyLoadPlan);
+            mLoadPlanId = mModel.loadTasks(mRunningTaskId, this::applyLoadPlan);
         }
     }
 
@@ -423,41 +413,30 @@
      * is called.
      * Also scrolls the view to this task
      */
-    public void showTask(Task task) {
+    public void showTask(int runningTaskId) {
         boolean needsReload = false;
-        boolean inflateFirstChild = true;
-        if (getTaskCount() > 0) {
-            TaskView tv = (TaskView) getChildAt(mFirstTaskIndex);
-            inflateFirstChild = !keysEquals(tv.getTask(), task);
-        }
-        if (inflateFirstChild) {
+        if (getTaskCount() == 0) {
             needsReload = true;
-            setLayoutTransition(null);
             // Add an empty view for now
+            setLayoutTransition(null);
             final TaskView taskView = (TaskView) LayoutInflater.from(getContext())
                     .inflate(R.layout.task, this, false);
             addView(taskView, mFirstTaskIndex);
-            taskView.bind(task);
             setLayoutTransition(mLayoutTransition);
         }
         if (!needsReload) {
             needsReload = !mModel.isLoadPlanValid(mLoadPlanId);
         }
         if (needsReload) {
-            mLoadPlanId = mModel.loadTasks(task.key.id, this::applyLoadPlan);
+            mLoadPlanId = mModel.loadTasks(runningTaskId, this::applyLoadPlan);
         }
-        mFirstTask = task;
+        mRunningTaskId = runningTaskId;
         setCurrentPage(mFirstTaskIndex);
         if (mCurrentPage >= mFirstTaskIndex) {
             ((TaskView) getPageAt(mCurrentPage)).setIconScale(0);
         }
     }
 
-    private static boolean keysEquals(Task t1, Task t2) {
-        // TODO: Match the keys directly
-        return t1.key.id == t2.key.id;
-    }
-
     public QuickScrubController getQuickScrubController() {
         return mQuickScrubController;
     }
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 e5110e6..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;
@@ -106,8 +102,8 @@
 
     private final MainThreadExecutor mMainExecutor = new MainThreadExecutor();
 
-    private final Task mRunningTask;
     private final Context mContext;
+    private final int mRunningTaskId;
 
     private MultiStateCallback mStateCallback;
     private boolean mControllerStateAnimation;
@@ -131,11 +127,8 @@
     private Matrix mTmpMatrix = new Matrix();
 
     WindowTransformSwipeHandler(RunningTaskInfo runningTaskInfo, Context context) {
-        // 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,
-                true, false, false, false, null, 0, null, false);
         mContext = context;
+        mRunningTaskId = runningTaskInfo.id;
 
         WindowManagerWrapper.getInstance().getStableInsets(mStableInsets);
 
@@ -257,7 +250,7 @@
         }
 
         mRecentsView = mLauncher.getOverviewPanel();
-        mRecentsView.showTask(mRunningTask);
+        mRecentsView.showTask(mRunningTaskId);
         mWasLauncherAlreadyVisible = alreadyOnHome;
         mLauncherLayoutListener = new LauncherLayoutListener(mLauncher, this);
         mLauncher.getDragLayer().addView(mLauncherLayoutListener);
@@ -457,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();
     }
 
@@ -480,7 +475,7 @@
             for (int i = mRecentsView.getFirstTaskIndex(); i < mRecentsView.getPageCount(); i++) {
                 TaskView taskView = (TaskView) mRecentsView.getPageAt(i);
                 // TODO: Match the keys directly
-                if (taskView.getTask().key.id != mRunningTask.key.id) {
+                if (taskView.getTask().key.id != mRunningTaskId) {
                     mRecentsView.snapToPage(i, QUICK_SWITCH_SNAP_DURATION);
                     taskView.postDelayed(() -> {taskView.launchTask(true);},
                             QUICK_SWITCH_SNAP_DURATION);