From 5fedc1b697617d32b4ef8f2195903a33d1504286 Mon Sep 17 00:00:00 2001 From: Jeff Chang Date: Wed, 12 May 2021 18:09:14 +0800 Subject: [PATCH] Attempt to fix NPE when reparent to the original task. 7f64b1447, Fix back stack for PiP from multi-activity Task. Trying to reparent pinned activity back to its original task. The NPE is introduced when the original task is removed. This CL removes the pinned task when the original task is to be removed and checks for the task is attached before doing the re-parent. Bug: 186374793 Test: atest PinnedStackTests Test: atest RootWindowContainerTests Change-Id: I6ea6100cb94b3982737c89f68de824a14415283a --- .../core/java/com/android/server/wm/Task.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java index 7bbb2f560a717..1e16c27582319 100644 --- a/services/core/java/com/android/server/wm/Task.java +++ b/services/core/java/com/android/server/wm/Task.java @@ -1854,12 +1854,16 @@ class Task extends WindowContainer { return autoRemoveRecents || (!hasChild() && !getHasBeenVisible()); } - /** Completely remove all activities associated with an existing task. */ - void performClearTask(String reason) { + private void clearPinnedTaskIfNeed() { // The original task is to be removed, try remove also the pinned task. if (mChildPipActivity != null && mChildPipActivity.getTask() != null) { mTaskSupervisor.removeRootTask(mChildPipActivity.getTask()); } + } + + /** Completely remove all activities associated with an existing task. */ + void performClearTask(String reason) { + clearPinnedTaskIfNeed(); // Broken down into to cases to avoid object create due to capturing mStack. if (getRootTask() == null) { forAllActivities((r) -> { @@ -3246,7 +3250,7 @@ class Task extends WindowContainer { mRemoving = true; EventLogTags.writeWmTaskRemoved(mTaskId, reason); - + clearPinnedTaskIfNeed(); // If applicable let the TaskOrganizer know the Task is vanishing. setTaskOrganizer(null); @@ -5449,10 +5453,12 @@ class Task extends WindowContainer { // force hidden flag. if (!isForceHidden()) { final Task lastParentBeforePip = topActivity.getLastParentBeforePip(); - topActivity.reparent(lastParentBeforePip, - lastParentBeforePip.getChildCount() /* top */, - "movePinnedActivityToOriginalTask"); - lastParentBeforePip.moveToFront("movePinnedActivityToOriginalTask"); + if (lastParentBeforePip.isAttached()) { + topActivity.reparent(lastParentBeforePip, + lastParentBeforePip.getChildCount() /* top */, + "movePinnedActivityToOriginalTask"); + lastParentBeforePip.moveToFront("movePinnedActivityToOriginalTask"); + } } } }