From c117a22d6287b46bf3d5c8a3fe06cdb5e4a2b94d Mon Sep 17 00:00:00 2001 From: Louis Chang Date: Fri, 25 Feb 2022 10:54:45 +0800 Subject: [PATCH] Adding logs and re-organized the logic of get root task Comb through the get root task logic and adding warning logs when the targeted launching display/task is invalid. Bug: 184806710 Bug: 218216633 Test: wm presubmit Change-Id: I9163a06e1c7d40709bda139d3d70c6e4145c573e --- .../server/wm/RootWindowContainer.java | 123 +++++++++++------- 1 file changed, 73 insertions(+), 50 deletions(-) diff --git a/services/core/java/com/android/server/wm/RootWindowContainer.java b/services/core/java/com/android/server/wm/RootWindowContainer.java index b9cd657da0e27..f65b17db1e106 100644 --- a/services/core/java/com/android/server/wm/RootWindowContainer.java +++ b/services/core/java/com/android/server/wm/RootWindowContainer.java @@ -2750,57 +2750,62 @@ class RootWindowContainer extends WindowContainer @Nullable ActivityOptions options, @Nullable Task candidateTask, @Nullable Task sourceTask, boolean onTop, @Nullable LaunchParamsController.LaunchParams launchParams, int launchFlags) { - int taskId = INVALID_TASK_ID; - int displayId = INVALID_DISPLAY; - TaskDisplayArea taskDisplayArea = null; - - // We give preference to the launch preference in activity options. + // First preference goes to the launch root task set in the activity options. if (options != null) { - taskId = options.getLaunchTaskId(); - displayId = options.getLaunchDisplayId(); - final WindowContainerToken daToken = options.getLaunchTaskDisplayArea(); - taskDisplayArea = daToken != null - ? (TaskDisplayArea) WindowContainer.fromBinder(daToken.asBinder()) : null; - - final Task rootTask = Task.fromWindowContainerToken(options.getLaunchRootTask()); - if (rootTask != null) { - return rootTask; + final Task candidateRoot = Task.fromWindowContainerToken(options.getLaunchRootTask()); + if (canLaunchOnDisplay(r, candidateRoot)) { + return candidateRoot; } } - // First preference for root task goes to the task Id set in the activity options. Use - // the root task associated with that if possible. - if (taskId != INVALID_TASK_ID) { - // Temporarily set the task id to invalid in case in re-entry. - options.setLaunchTaskId(INVALID_TASK_ID); - final Task task = anyTaskForId(taskId, - MATCH_ATTACHED_TASK_OR_RECENT_TASKS_AND_RESTORE, options, onTop); - options.setLaunchTaskId(taskId); - if (task != null) { - return task.getRootTask(); + // Next preference goes to the task id set in the activity options. + if (options != null) { + final int candidateTaskId = options.getLaunchTaskId(); + if (candidateTaskId != INVALID_TASK_ID) { + // Temporarily set the task id to invalid in case in re-entry. + options.setLaunchTaskId(INVALID_TASK_ID); + final Task task = anyTaskForId(candidateTaskId, + MATCH_ATTACHED_TASK_OR_RECENT_TASKS_AND_RESTORE, options, onTop); + options.setLaunchTaskId(candidateTaskId); + if (canLaunchOnDisplay(r, task)) { + return task.getRootTask(); + } + } + } + + // Next preference goes to the TaskDisplayArea candidate from launchParams + // or activity options. + TaskDisplayArea taskDisplayArea = null; + if (launchParams != null && launchParams.mPreferredTaskDisplayArea != null) { + taskDisplayArea = launchParams.mPreferredTaskDisplayArea; + } else if (options != null) { + final WindowContainerToken daToken = options.getLaunchTaskDisplayArea(); + taskDisplayArea = daToken != null + ? (TaskDisplayArea) WindowContainer.fromBinder(daToken.asBinder()) : null; + if (taskDisplayArea == null) { + final int launchDisplayId = options.getLaunchDisplayId(); + if (launchDisplayId != INVALID_DISPLAY) { + final DisplayContent displayContent = getDisplayContent(launchDisplayId); + if (displayContent != null) { + taskDisplayArea = displayContent.getDefaultTaskDisplayArea(); + } + } } } final int activityType = resolveActivityType(r, options, candidateTask); - Task rootTask = null; - - // Next preference for root task goes to the taskDisplayArea candidate. - if (launchParams != null && launchParams.mPreferredTaskDisplayArea != null - && canLaunchOnDisplay(r, launchParams.mPreferredTaskDisplayArea.getDisplayId())) { - taskDisplayArea = launchParams.mPreferredTaskDisplayArea; - } - if (taskDisplayArea == null && displayId != INVALID_DISPLAY - && canLaunchOnDisplay(r, displayId)) { - taskDisplayArea = getDisplayContent(displayId).getDefaultTaskDisplayArea(); - } if (taskDisplayArea != null) { - return taskDisplayArea.getOrCreateRootTask(r, options, candidateTask, - sourceTask, launchParams, launchFlags, activityType, onTop); + if (canLaunchOnDisplay(r, taskDisplayArea.getDisplayId())) { + return taskDisplayArea.getOrCreateRootTask(r, options, candidateTask, + sourceTask, launchParams, launchFlags, activityType, onTop); + } else { + taskDisplayArea = null; + } } // Give preference to the root task and display of the input task and activity if they // match the mode we want to launch into. - TaskDisplayArea container = null; + Task rootTask = null; if (candidateTask != null) { rootTask = candidateTask.getRootTask(); } @@ -2810,10 +2815,11 @@ class RootWindowContainer extends WindowContainer int windowingMode = launchParams != null ? launchParams.mWindowingMode : WindowConfiguration.WINDOWING_MODE_UNDEFINED; if (rootTask != null) { - container = rootTask.getDisplayArea(); - if (container != null && canLaunchOnDisplay(r, container.mDisplayContent.mDisplayId)) { + taskDisplayArea = rootTask.getDisplayArea(); + if (taskDisplayArea != null + && canLaunchOnDisplay(r, taskDisplayArea.mDisplayContent.mDisplayId)) { if (windowingMode == WindowConfiguration.WINDOWING_MODE_UNDEFINED) { - windowingMode = container.resolveWindowingMode(r, options, candidateTask); + windowingMode = taskDisplayArea.resolveWindowingMode(r, options, candidateTask); } // Always allow organized tasks that created by organizer since the activity type // of an organized task is decided by the activity type of its top child, which @@ -2822,19 +2828,32 @@ class RootWindowContainer extends WindowContainer || rootTask.mCreatedByOrganizer) { return rootTask; } + } else { + taskDisplayArea = null; } + } - if (container == null - || !canLaunchOnDisplay(r, container.mDisplayContent.mDisplayId)) { - container = getDefaultTaskDisplayArea(); - if (windowingMode == WindowConfiguration.WINDOWING_MODE_UNDEFINED) { - windowingMode = container.resolveWindowingMode(r, options, candidateTask); - } + // Falling back to default task container + if (taskDisplayArea == null) { + taskDisplayArea = getDefaultTaskDisplayArea(); + } + return taskDisplayArea.getOrCreateRootTask(r, options, candidateTask, sourceTask, + launchParams, launchFlags, activityType, onTop); + } + + private boolean canLaunchOnDisplay(ActivityRecord r, Task task) { + if (task == null) { + Slog.w(TAG, "canLaunchOnDisplay(), invalid task: " + task); + return false; } - return container.getOrCreateRootTask(r, options, candidateTask, sourceTask, launchParams, - launchFlags, activityType, onTop); + if (!task.isAttached()) { + Slog.w(TAG, "canLaunchOnDisplay(), Task is not attached: " + task); + return false; + } + + return canLaunchOnDisplay(r, task.getTaskDisplayArea().getDisplayId()); } /** @return true if activity record is null or can be launched on provided display. */ @@ -2842,7 +2861,11 @@ class RootWindowContainer extends WindowContainer if (r == null) { return true; } - return r.canBeLaunchedOnDisplay(displayId); + if (!r.canBeLaunchedOnDisplay(displayId)) { + Slog.w(TAG, "Not allow to launch " + r + " on display " + displayId); + return false; + } + return true; } int resolveActivityType(@Nullable ActivityRecord r, @Nullable ActivityOptions options,