From deefa3db71c5a64113b48fda0f7cb74821d181e7 Mon Sep 17 00:00:00 2001 From: Uwais Ashraf Date: Mon, 24 Mar 2025 16:51:38 +0000 Subject: [PATCH] Also cancel TaskView collection Jobs when Overview is not on screen. This prevents old scopes still being active when new TaskView state emissions are being sent to main. They were previously cancelled in time but still result in empty continuations occuring on main. Bug: 395294954 Flag: com.android.launcher3.enable_refactor_task_thumbnail Test: Manual - checking perfetto traces Change-Id: I539a6fde8c9315ceeac74c9bf622d1e2dca1403a --- .../android/quickstep/views/RecentsView.java | 1 + .../com/android/quickstep/views/TaskView.kt | 24 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/quickstep/src/com/android/quickstep/views/RecentsView.java b/quickstep/src/com/android/quickstep/views/RecentsView.java index 3e4b953b5b..53b1301f8f 100644 --- a/quickstep/src/com/android/quickstep/views/RecentsView.java +++ b/quickstep/src/com/android/quickstep/views/RecentsView.java @@ -2771,6 +2771,7 @@ public abstract class RecentsView< } if (enableRefactorTaskThumbnail()) { mRecentsViewModel.onReset(); + getTaskViews().forEach(TaskView::cancelJobs); } } diff --git a/quickstep/src/com/android/quickstep/views/TaskView.kt b/quickstep/src/com/android/quickstep/views/TaskView.kt index fa3fd91615..14fecda909 100644 --- a/quickstep/src/com/android/quickstep/views/TaskView.kt +++ b/quickstep/src/com/android/quickstep/views/TaskView.kt @@ -888,17 +888,21 @@ constructor( return thumbnailPosition } - override fun onDetachedFromWindow() = - traceSection("TaskView.onDetachedFromWindow") { - super.onDetachedFromWindow() - if (enableRefactorTaskThumbnail()) { - // The jobs are being cancelled in the background thread. So we make a copy of the - // list to prevent cleaning a new job that might be added to this list during - // onAttach or another moment in the lifecycle. - val coroutineJobsToCancel = coroutineJobs.toList() - coroutineJobs.clear() + override fun onDetachedFromWindow() { + super.onDetachedFromWindow() + cancelJobs() + } + + fun cancelJobs() = + traceSection("TaskView.cancelJobs") { + // The jobs are being cancelled in the background thread. So we make a copy of the + // list to prevent cleaning a new job that might be added to this list during + // onAttach or another moment in the lifecycle. + val coroutineJobsToCancel = coroutineJobs.toList() + coroutineJobs.clear() + if (coroutineJobsToCancel.isNotEmpty()) { coroutineScope.launch(dispatcherProvider.background) { - traceSection("TaskView.onDetachedFromWindow.cancellingJobs") { + traceSection("TaskView.cancelJobs.cancellingJobs") { coroutineJobsToCancel.forEach { it.cancel("TaskView detaching from window") }