Trims Recent Task when Task windowing mode changed after dismiss split-screen

While starting an activity in split-screen, a Task was created in
multi-window mode and added to the RecentTasks. Once the activity is
finished, the same Task was reparented to TDA and became fullscreen
windowing mode before the activity is removed.

The next time starting the same activity in split-screen, a multi-window
mode Task was added to the RecentTasks again without trimming the legacy
recent Task because the windowing modes are incompatible (multi-window
vs. fullscreen). Repeating the steps could resulted in redundant tasks
in Recents.

Bug: 289317239
Test: atest RecentTasksTest
Change-Id: I7af0acaa6d528d7a45018b1b7f57dbe6d57f1585
This commit is contained in:
Louis Chang
2023-07-12 02:43:13 +00:00
parent 5a74298a1c
commit abf91abaef
3 changed files with 53 additions and 0 deletions

View File

@@ -682,6 +682,26 @@ class RecentTasks {
}
}
/**
* Removes the oldest recent task that is compatible with the given one. This is possible if
* the task windowing mode changed after being added to the Recents.
*/
void removeCompatibleRecentTask(Task task) {
final int taskIndex = mTasks.indexOf(task);
if (taskIndex < 0) {
return;
}
final int candidateIndex = findRemoveIndexForTask(task, false /* includingSelf */);
if (candidateIndex == -1) {
// Nothing to trim
return;
}
final Task taskToRemove = taskIndex > candidateIndex ? task : mTasks.get(candidateIndex);
remove(taskToRemove);
}
void removeTasksByPackageName(String packageName, int userId) {
for (int i = mTasks.size() - 1; i >= 0; --i) {
final Task task = mTasks.get(i);
@@ -1540,6 +1560,10 @@ class RecentTasks {
* list (if any).
*/
private int findRemoveIndexForAddTask(Task task) {
return findRemoveIndexForTask(task, true /* includingSelf */);
}
private int findRemoveIndexForTask(Task task, boolean includingSelf) {
final int recentsCount = mTasks.size();
final Intent intent = task.intent;
final boolean document = intent != null && intent.isDocument();
@@ -1595,6 +1619,8 @@ class RecentTasks {
// existing task
continue;
}
} else if (!includingSelf) {
continue;
}
return i;
}

View File

@@ -1608,6 +1608,7 @@ class WindowOrganizerController extends IWindowOrganizerController.Stub
final int count = tasksToReparent.size();
for (int i = 0; i < count; ++i) {
final Task task = tasksToReparent.get(i);
final int prevWindowingMode = task.getWindowingMode();
if (syncId >= 0) {
addToSyncSet(syncId, task);
}
@@ -1621,6 +1622,12 @@ class WindowOrganizerController extends IWindowOrganizerController.Stub
hop.getToTop() ? POSITION_TOP : POSITION_BOTTOM,
false /*moveParents*/, "processChildrenTaskReparentHierarchyOp");
}
// Trim the compatible Recent task (if any) after the Task is reparented and now has
// a different windowing mode, in order to prevent redundant Recent tasks after
// reparenting.
if (prevWindowingMode != task.getWindowingMode()) {
mService.mTaskSupervisor.mRecentTasks.removeCompatibleRecentTask(task);
}
}
if (transition != null) transition.collect(newParent);

View File

@@ -1315,6 +1315,26 @@ public class RecentTasksTest extends WindowTestsBase {
assertTrue(info.supportsMultiWindow);
}
@Test
public void testRemoveCompatibleRecentTask() {
final Task task1 = createTaskBuilder(".Task").setWindowingMode(
WINDOWING_MODE_FULLSCREEN).build();
mRecentTasks.add(task1);
final Task task2 = createTaskBuilder(".Task").setWindowingMode(
WINDOWING_MODE_MULTI_WINDOW).build();
mRecentTasks.add(task2);
assertEquals(2, mRecentTasks.getRecentTasks(MAX_VALUE, 0 /* flags */,
true /* getTasksAllowed */, TEST_USER_0_ID, 0).getList().size());
// Set windowing mode and ensure the same fullscreen task that created earlier is removed.
task2.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
mRecentTasks.removeCompatibleRecentTask(task2);
assertEquals(1, mRecentTasks.getRecentTasks(MAX_VALUE, 0 /* flags */,
true /* getTasksAllowed */, TEST_USER_0_ID, 0).getList().size());
assertEquals(task2.mTaskId, mRecentTasks.getRecentTasks(MAX_VALUE, 0 /* flags */,
true /* getTasksAllowed */, TEST_USER_0_ID, 0).getList().get(0).taskId);
}
private TaskSnapshot createSnapshot(Point taskSize, Point bufferSize) {
HardwareBuffer buffer = null;
if (bufferSize != null) {