From 34976aa3f25bd88a2a813231bf0ac89301816cad Mon Sep 17 00:00:00 2001 From: Chilun Date: Wed, 3 Mar 2021 14:42:01 +0800 Subject: [PATCH] Add restored tasks with persisted activity to hierarchy The Activity with persistAcrossReboots mode will be restored and added into Task while rebooting. However, after the WM hierarchy is unified, adding Activity to Task will trigger a series of events, such as relayout and create surface control, which require Task in the hierarchy. This causes the restore to fail. The solution here is to add the restored task to the hierarchy as early as the point of restoration. Bug: 178782815 Test: atest TaskPersisterTest TaskRecordTests Change-Id: I604d6c31f49cff959609373f85d177eadebb062b --- services/core/java/com/android/server/wm/Task.java | 10 ++++++++-- .../java/com/android/server/wm/TaskPersister.java | 13 +++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java index 80173b5942e65..f6f0581c1fb45 100644 --- a/services/core/java/com/android/server/wm/Task.java +++ b/services/core/java/com/android/server/wm/Task.java @@ -4912,9 +4912,15 @@ class Task extends WindowContainer { task.mLastNonFullscreenBounds = lastNonFullscreenBounds; task.setBounds(lastNonFullscreenBounds); task.mWindowLayoutAffinity = windowLayoutAffinity; + if (activities.size() > 0) { + // We need to add the task into hierarchy before adding child to it. + final DisplayContent dc = + taskSupervisor.mRootWindowContainer.getDisplayContent(DEFAULT_DISPLAY); + dc.getDefaultTaskDisplayArea().addChild(task, POSITION_BOTTOM); - for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { - task.addChild(activities.get(activityNdx)); + for (int activityNdx = activities.size() - 1; activityNdx >= 0; --activityNdx) { + task.addChild(activities.get(activityNdx)); + } } if (DEBUG_RECENTS) Slog.d(TAG_RECENTS, "Restored task=" + task); diff --git a/services/core/java/com/android/server/wm/TaskPersister.java b/services/core/java/com/android/server/wm/TaskPersister.java index 855dd7e416b75..b8d2febd5dd41 100644 --- a/services/core/java/com/android/server/wm/TaskPersister.java +++ b/services/core/java/com/android/server/wm/TaskPersister.java @@ -328,10 +328,19 @@ public class TaskPersister implements PersisterQueue.Listener { // mWriteQueue.add(new TaskWriteQueueItem(task)); final int taskId = task.mTaskId; - if (mService.mRootWindowContainer.anyTaskForId(taskId, + final boolean persistedTask = task.hasActivity(); + if (persistedTask && mRecentTasks.getTask(taskId) != null) { + // The persisted task is added into hierarchy and will also be + // added to recent tasks later. So this task should not exist + // in recent tasks before it is added. + Slog.wtf(TAG, "Existing persisted task with taskId " + taskId + + " found"); + } else if (!persistedTask + && mService.mRootWindowContainer.anyTaskForId(taskId, MATCH_ATTACHED_TASK_OR_RECENT_TASKS) != null) { // Should not happen. - Slog.wtf(TAG, "Existing task with taskId " + taskId + "found"); + Slog.wtf(TAG, "Existing task with taskId " + taskId + + " found"); } else if (userId != task.mUserId) { // Should not happen. Slog.wtf(TAG, "Task with userId " + task.mUserId + " found in "