Merge "Finish wrapped launcher animations if impl is gone" into sc-dev
diff --git a/quickstep/src/com/android/quickstep/TouchInteractionService.java b/quickstep/src/com/android/quickstep/TouchInteractionService.java
index e52405b..c9b68df 100644
--- a/quickstep/src/com/android/quickstep/TouchInteractionService.java
+++ b/quickstep/src/com/android/quickstep/TouchInteractionService.java
@@ -813,7 +813,13 @@
     }
 
     private void reset() {
-        mConsumer = mUncheckedConsumer = mResetGestureInputConsumer;
+        if (mResetGestureInputConsumer != null) {
+            mConsumer = mUncheckedConsumer = mResetGestureInputConsumer;
+        } else {
+            // mResetGestureInputConsumer isn't initialized until onUserUnlocked(), so reset to
+            // NO_OP until then (we never want these to be null).
+            mConsumer = mUncheckedConsumer = InputConsumer.NO_OP;
+        }
         mGestureState = DEFAULT_STATE;
         // By default, use batching of the input events, but check receiver before using in the rare
         // case that the monitor was disposed before the swipe settled
diff --git a/quickstep/src/com/android/quickstep/views/RecentsView.java b/quickstep/src/com/android/quickstep/views/RecentsView.java
index df7f8b5..8c3e5b5 100644
--- a/quickstep/src/com/android/quickstep/views/RecentsView.java
+++ b/quickstep/src/com/android/quickstep/views/RecentsView.java
@@ -2245,7 +2245,7 @@
     public PendingAnimation createTaskDismissAnimation(TaskView taskView, boolean animateTaskView,
             boolean shouldRemoveTask, long duration) {
         if (mPendingAnimation != null) {
-            mPendingAnimation.createPlaybackController().dispatchOnCancel();
+            mPendingAnimation.createPlaybackController().dispatchOnCancel().dispatchOnEnd();
         }
         PendingAnimation anim = new PendingAnimation(duration);
 
diff --git a/src/com/android/launcher3/anim/AnimatorPlaybackController.java b/src/com/android/launcher3/anim/AnimatorPlaybackController.java
index 7d8b82a..85ca280 100644
--- a/src/com/android/launcher3/anim/AnimatorPlaybackController.java
+++ b/src/com/android/launcher3/anim/AnimatorPlaybackController.java
@@ -278,12 +278,19 @@
         }
     }
 
-    public void dispatchOnStart() {
+    public AnimatorPlaybackController dispatchOnStart() {
         callListenerCommandRecursively(mAnim, AnimatorListener::onAnimationStart);
+        return this;
     }
 
-    public void dispatchOnCancel() {
+    public AnimatorPlaybackController dispatchOnCancel() {
         callListenerCommandRecursively(mAnim, AnimatorListener::onAnimationCancel);
+        return this;
+    }
+
+    public AnimatorPlaybackController dispatchOnEnd() {
+        callListenerCommandRecursively(mAnim, AnimatorListener::onAnimationEnd);
+        return this;
     }
 
     public void dispatchSetInterpolator(TimeInterpolator interpolator) {
@@ -328,7 +335,7 @@
         public void onAnimationSuccess(Animator animator) {
             // We wait for the spring (if any) to finish running before completing the end callback.
             if (!mDispatched) {
-                callListenerCommandRecursively(mAnim, AnimatorListener::onAnimationEnd);
+                dispatchOnEnd();
                 if (mEndAction != null) {
                     mEndAction.run();
                 }
diff --git a/src/com/android/launcher3/statemanager/StateManager.java b/src/com/android/launcher3/statemanager/StateManager.java
index 13d6568..b34af97 100644
--- a/src/com/android/launcher3/statemanager/StateManager.java
+++ b/src/com/android/launcher3/statemanager/StateManager.java
@@ -209,7 +209,7 @@
 
         // Cancel the current animation. This will reset mState to mCurrentStableState, so store it.
         STATE_TYPE fromState = mState;
-        mConfig.reset();
+        cancelAnimation();
 
         if (!animated) {
             mAtomicAnimationFactory.cancelAllStateElementAnimation();
@@ -303,7 +303,7 @@
     public AnimatorPlaybackController createAnimationToNewWorkspace(STATE_TYPE state,
             StateAnimationConfig config) {
         config.userControlled = true;
-        mConfig.reset();
+        cancelAnimation();
         config.copyTo(mConfig);
         mConfig.playbackController = createAnimationToNewWorkspaceInternal(state)
                 .createPlaybackController();
@@ -393,6 +393,11 @@
      */
     public void cancelAnimation() {
         mConfig.reset();
+        // It could happen that a new animation is set as a result of an endListener on the
+        // existing animation.
+        while (mConfig.currentAnimation != null || mConfig.playbackController != null) {
+            mConfig.reset();
+        }
     }
 
     public void setCurrentUserControlledAnimation(AnimatorPlaybackController controller) {
@@ -508,14 +513,19 @@
          * Cancels the current animation and resets config variables.
          */
         public void reset() {
+            AnimatorSet anim = currentAnimation;
+            AnimatorPlaybackController pc = playbackController;
+
             DEFAULT.copyTo(this);
             targetState = null;
+            currentAnimation = null;
+            playbackController = null;
+            changeId++;
 
-            if (playbackController != null) {
-                playbackController.getAnimationPlayer().cancel();
-                playbackController.dispatchOnCancel();
-            } else if (currentAnimation != null) {
-                AnimatorSet anim = currentAnimation;
+            if (pc != null) {
+                pc.getAnimationPlayer().cancel();
+                pc.dispatchOnCancel().dispatchOnEnd();
+            } else if (anim != null) {
                 anim.setDuration(0);
                 if (!anim.isStarted()) {
                     // If the animation is not started the listeners do not get notified,
@@ -525,10 +535,6 @@
                 }
                 anim.cancel();
             }
-
-            currentAnimation = null;
-            playbackController = null;
-            changeId++;
         }
 
         @Override