Fix how taskview is used by controlsUi

When controlsUi is dismissed, it calls taskview#release, which
should only be done when the task & taskview are done being used
which isn't really the case until the task is explicitly removed.

Since the controlsUi is going away, the taskview surface gets
destroyed which triggers a transition to move its task TO_BACK,
however, because release has been called, we have no reference to
the taskview in TaskViewTransitions, so the TO_BACK transition
falls to the default transition handler which results in the
incorrect animation.

To fix this, instead of calling taskview#release when the controls
ui is being dismissed, call taskview#removeTask to actually remove
the task. In the onTaskRemovalStarted callback, we can then clean
up the taskview.

Test: manual - open controls panel and swipe up to home
             => observe that there isn't a second animation of the
                task animating away
Bug: 282102652
Change-Id: Ie40d078e4a05d3323a3364b82f6af171ae97181c
diff --git a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt
index d73c85b..776b336e 100644
--- a/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt
+++ b/packages/SystemUI/src/com/android/systemui/controls/ui/ControlsUiControllerImpl.kt
@@ -279,7 +279,7 @@
 
         controlsListingController.get().removeCallback(listingCallback)
         controlsController.get().unsubscribe()
-        taskViewController?.dismiss()
+        taskViewController?.removeTask()
         taskViewController = null
 
         val fadeAnim = ObjectAnimator.ofFloat(parent, "alpha", 1.0f, 0.0f)
@@ -777,7 +777,7 @@
 
             closeDialogs(true)
             controlsController.get().unsubscribe()
-            taskViewController?.dismiss()
+            taskViewController?.removeTask()
             taskViewController = null
 
             controlsById.clear()
diff --git a/packages/SystemUI/src/com/android/systemui/controls/ui/PanelTaskViewController.kt b/packages/SystemUI/src/com/android/systemui/controls/ui/PanelTaskViewController.kt
index 025d7e4..db009dc 100644
--- a/packages/SystemUI/src/com/android/systemui/controls/ui/PanelTaskViewController.kt
+++ b/packages/SystemUI/src/com/android/systemui/controls/ui/PanelTaskViewController.kt
@@ -18,7 +18,6 @@
 package com.android.systemui.controls.ui
 
 import android.app.ActivityOptions
-import android.app.ActivityTaskManager
 import android.app.ActivityTaskManager.INVALID_TASK_ID
 import android.app.PendingIntent
 import android.content.ComponentName
@@ -28,6 +27,7 @@
 import android.graphics.drawable.ShapeDrawable
 import android.graphics.drawable.shapes.RoundRectShape
 import android.os.Trace
+import com.android.internal.annotations.VisibleForTesting
 import com.android.systemui.R
 import com.android.systemui.util.boundsOnScreen
 import com.android.wm.shell.taskview.TaskView
@@ -54,12 +54,6 @@
             addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
         }
 
-    private fun removeDetailTask() {
-        if (detailTaskId == INVALID_TASK_ID) return
-        ActivityTaskManager.getInstance().removeTask(detailTaskId)
-        detailTaskId = INVALID_TASK_ID
-    }
-
     private val stateCallback =
         object : TaskView.Listener {
             override fun onInitialized() {
@@ -95,7 +89,7 @@
 
             override fun onTaskRemovalStarted(taskId: Int) {
                 detailTaskId = INVALID_TASK_ID
-                dismiss()
+                release()
             }
 
             override fun onTaskCreated(taskId: Int, name: ComponentName?) {
@@ -103,12 +97,7 @@
                 taskView.alpha = 1f
             }
 
-            override fun onReleased() {
-                removeDetailTask()
-            }
-
             override fun onBackPressedOnTaskRoot(taskId: Int) {
-                dismiss()
                 hide()
             }
         }
@@ -117,10 +106,17 @@
         taskView.onLocationChanged()
     }
 
-    fun dismiss() {
+    /** Call when the taskView is no longer being used, shouldn't be called before removeTask. */
+    @VisibleForTesting
+    fun release() {
         taskView.release()
     }
 
+    /** Call to explicitly remove the task from window manager. */
+    fun removeTask() {
+        taskView.removeTask()
+    }
+
     fun launchTaskView() {
         taskView.setListener(uiExecutor, stateCallback)
     }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/controls/ui/PanelTaskViewControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/PanelTaskViewControllerTest.kt
index 7840525..021facc 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/controls/ui/PanelTaskViewControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/controls/ui/PanelTaskViewControllerTest.kt
@@ -146,17 +146,8 @@
     }
 
     @Test
-    fun testTaskViewReleasedOnDismiss() {
-        underTest.dismiss()
-        verify(taskView).release()
-    }
-
-    @Test
-    fun testTaskViewReleasedOnBackOnRoot() {
-        underTest.launchTaskView()
-        verify(taskView).setListener(any(), capture(listenerCaptor))
-
-        listenerCaptor.value.onBackPressedOnTaskRoot(0)
+    fun testTaskViewReleasedOnRelease() {
+        underTest.release()
         verify(taskView).release()
     }