Fix issue with out of order callbacks

- If any call to finish the recents animation comes in before
  onRecentsAnimationStarted, we'll clear the callbacks but the listener
  registered in startRecentsAnimation will persist.  Since the call
  to actually finish the animation with the system is posted on the
  main thread, if the animation started callback returns in that time,
  the controller will be set (via the listener) but the callbacks will
  be unset.  Instead, we can clear all the listeners from the previous
  callbacks when cleaning up the animation.

Bug: 145758818
Change-Id: I29dab94f5fb25f762a823d59ef318a361282b595
diff --git a/quickstep/src/com/android/quickstep/RecentsAnimationCallbacks.java b/quickstep/src/com/android/quickstep/RecentsAnimationCallbacks.java
index acf61b4..0f98b32 100644
--- a/quickstep/src/com/android/quickstep/RecentsAnimationCallbacks.java
+++ b/quickstep/src/com/android/quickstep/RecentsAnimationCallbacks.java
@@ -62,6 +62,12 @@
         mListeners.remove(listener);
     }
 
+    @UiThread
+    public void removeAllListeners() {
+        Preconditions.assertUIThread();
+        mListeners.clear();
+    }
+
     public void notifyAnimationCanceled() {
         mCancelled = true;
         onAnimationCanceled(null);
diff --git a/quickstep/src/com/android/quickstep/TaskAnimationManager.java b/quickstep/src/com/android/quickstep/TaskAnimationManager.java
index e3e8ace..c362e52 100644
--- a/quickstep/src/com/android/quickstep/TaskAnimationManager.java
+++ b/quickstep/src/com/android/quickstep/TaskAnimationManager.java
@@ -156,9 +156,9 @@
             mTargets.release();
         }
 
-        // Remove gesture state from callbacks
-        if (mCallbacks != null && mLastGestureState != null) {
-            mCallbacks.removeListener(mLastGestureState);
+        // Clean up all listeners to ensure we don't get subsequent callbacks
+        if (mCallbacks != null) {
+            mCallbacks.removeAllListeners();
         }
 
         mController = null;