From 2f821c90014b5d67dfdb19fe364f5bd7c22db37a Mon Sep 17 00:00:00 2001 From: Louis Chang Date: Wed, 7 Dec 2022 09:47:57 +0000 Subject: [PATCH] Prevent IndexOutOfBoundsException while removing activities IndexOutOfBoundsException was thrown when traversing the Task from bottom to top because the activity could be removed from the Task and the activities are less than its original size. Bug: 249658397 Bug: 260643770 Test: 1) Start multiple activities in a Task 2) Swipe up to home 3) Kill the app process from shell 4) Remove the task from Recents Change-Id: I64955415c04da30d7dc8a4681474ceb06db1e88b Merged-In: I64955415c04da30d7dc8a4681474ceb06db1e88b --- services/core/java/com/android/server/wm/Task.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java index 51eec03855a5e..c0c675e334fab 100644 --- a/services/core/java/com/android/server/wm/Task.java +++ b/services/core/java/com/android/server/wm/Task.java @@ -1612,10 +1612,18 @@ class Task extends TaskFragment { // removed. Otherwise, shell transitions wouldn't run because there would be no event // that sets the transition ready. final boolean traverseTopToBottom = !mTransitionController.isShellTransitionsEnabled(); - forAllActivities((r) -> { + final ArrayList finishingActivities = new ArrayList<>(); + forAllActivities(r -> { if (r.finishing || (excludingTaskOverlay && r.isTaskOverlay())) { return; } + finishingActivities.add(r); + }, traverseTopToBottom); + + + for (int i = 0; i < finishingActivities.size(); i++) { + final ActivityRecord r = finishingActivities.get(i); + // Prevent the transition from being executed too early if the top activity is // resumed but the mVisibleRequested of any other activity is true, the transition // should wait until next activity resumed. @@ -1625,7 +1633,7 @@ class Task extends TaskFragment { } else { r.destroyIfPossible(reason); } - }, traverseTopToBottom); + } } }