diff --git a/quickstep/src/com/android/launcher3/taskbar/TaskbarRecentAppsController.kt b/quickstep/src/com/android/launcher3/taskbar/TaskbarRecentAppsController.kt index 4866e399ac..659f7c8948 100644 --- a/quickstep/src/com/android/launcher3/taskbar/TaskbarRecentAppsController.kt +++ b/quickstep/src/com/android/launcher3/taskbar/TaskbarRecentAppsController.kt @@ -156,9 +156,16 @@ class TaskbarRecentAppsController( } val tasks = desktopTask?.tasks ?: emptyList() // Kind of hacky, we wrap each single task in the Desktop as a GroupTask. - val desktopTaskAsList = tasks.map { GroupTask(it) } + var desktopTaskAsList = tasks.map { GroupTask(it) } // TODO(b/315344726 Multi-instance support): dedupe Tasks of the same package too. - return dedupeHotseatTasks(desktopTaskAsList, shownHotseatItems) + desktopTaskAsList = dedupeHotseatTasks(desktopTaskAsList, shownHotseatItems) + val desktopPackages = desktopTaskAsList.map { it.packageNames } + // Remove any missing Tasks. + val newShownTasks = shownTasks.filter { it.packageNames in desktopPackages }.toMutableList() + val newShownPackages = newShownTasks.map { it.packageNames } + // Add any new Tasks, maintaining the order from previous shownTasks. + newShownTasks.addAll(desktopTaskAsList.filter { it.packageNames !in newShownPackages }) + return newShownTasks.toList() } private fun computeShownRecentTasks(): List { diff --git a/quickstep/tests/src/com/android/launcher3/taskbar/TaskbarRecentAppsControllerTest.kt b/quickstep/tests/src/com/android/launcher3/taskbar/TaskbarRecentAppsControllerTest.kt index d0985610c4..5813708df2 100644 --- a/quickstep/tests/src/com/android/launcher3/taskbar/TaskbarRecentAppsControllerTest.kt +++ b/quickstep/tests/src/com/android/launcher3/taskbar/TaskbarRecentAppsControllerTest.kt @@ -230,6 +230,78 @@ class TaskbarRecentAppsControllerTest : TaskbarBaseTestCase() { assertThat(recentAppsController.minimizedAppPackages).isEmpty() } + @Test + fun onRecentTasksChanged_inDesktopMode_shownTasks_maintainsOrder() { + setInDesktopMode(true) + val originalOrder = listOf(RUNNING_APP_PACKAGE_1, RUNNING_APP_PACKAGE_2) + prepareHotseatAndRunningAndRecentApps( + hotseatPackages = emptyList(), + runningTaskPackages = originalOrder, + recentTaskPackages = emptyList() + ) + prepareHotseatAndRunningAndRecentApps( + hotseatPackages = emptyList(), + runningTaskPackages = listOf(RUNNING_APP_PACKAGE_2, RUNNING_APP_PACKAGE_1), + recentTaskPackages = emptyList() + ) + val shownPackages = recentAppsController.shownTasks.flatMap { it.packageNames } + assertThat(shownPackages).isEqualTo(originalOrder) + } + + @Test + fun onRecentTasksChanged_inDesktopMode_addTask_shownTasks_maintainsOrder() { + setInDesktopMode(true) + prepareHotseatAndRunningAndRecentApps( + hotseatPackages = emptyList(), + runningTaskPackages = listOf(RUNNING_APP_PACKAGE_1, RUNNING_APP_PACKAGE_2), + recentTaskPackages = emptyList() + ) + prepareHotseatAndRunningAndRecentApps( + hotseatPackages = emptyList(), + runningTaskPackages = + listOf(RUNNING_APP_PACKAGE_2, RUNNING_APP_PACKAGE_1, RUNNING_APP_PACKAGE_3), + recentTaskPackages = emptyList() + ) + val shownPackages = recentAppsController.shownTasks.flatMap { it.packageNames } + val expectedOrder = + listOf(RUNNING_APP_PACKAGE_1, RUNNING_APP_PACKAGE_2, RUNNING_APP_PACKAGE_3) + assertThat(shownPackages).isEqualTo(expectedOrder) + } + + @Test + fun onRecentTasksChanged_inDesktopMode_removeTask_shownTasks_maintainsOrder() { + setInDesktopMode(true) + prepareHotseatAndRunningAndRecentApps( + hotseatPackages = emptyList(), + runningTaskPackages = + listOf(RUNNING_APP_PACKAGE_1, RUNNING_APP_PACKAGE_2, RUNNING_APP_PACKAGE_3), + recentTaskPackages = emptyList() + ) + prepareHotseatAndRunningAndRecentApps( + hotseatPackages = emptyList(), + runningTaskPackages = listOf(RUNNING_APP_PACKAGE_2, RUNNING_APP_PACKAGE_1), + recentTaskPackages = emptyList() + ) + val shownPackages = recentAppsController.shownTasks.flatMap { it.packageNames } + assertThat(shownPackages).isEqualTo(listOf(RUNNING_APP_PACKAGE_1, RUNNING_APP_PACKAGE_2)) + } + + @Test + fun onRecentTasksChanged_enterDesktopMode_shownTasks_onlyIncludesRunningTasks() { + setInDesktopMode(false) + val runningTaskPackages = listOf(RUNNING_APP_PACKAGE_1, RUNNING_APP_PACKAGE_2) + val recentTaskPackages = listOf(RECENT_PACKAGE_1, RECENT_PACKAGE_2) + prepareHotseatAndRunningAndRecentApps( + hotseatPackages = emptyList(), + runningTaskPackages = runningTaskPackages, + recentTaskPackages = recentTaskPackages + ) + setInDesktopMode(true) + recentTasksChangedListener.onRecentTasksChanged() + val shownPackages = recentAppsController.shownTasks.flatMap { it.packageNames } + assertThat(shownPackages).containsExactlyElementsIn(runningTaskPackages) + } + private fun prepareHotseatAndRunningAndRecentApps( hotseatPackages: List, runningTaskPackages: List,