Merge "Prevent execute app transition too early when performClearTask" into sc-v2-dev am: f49cb0da83 am: 2034ed1177

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/15784351

Change-Id: I1f227fb1e76729fb0b8e28e1ec1b8e350323b8b0
diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java
index 20ed0c6..7e1a4e7 100644
--- a/services/core/java/com/android/server/wm/Task.java
+++ b/services/core/java/com/android/server/wm/Task.java
@@ -1606,9 +1606,15 @@
         } else {
             forAllActivities((r) -> {
                 if (r.finishing) return;
-                // TODO: figure-out how to avoid object creation due to capture of reason variable.
-                r.finishIfPossible(Activity.RESULT_CANCELED,
-                        null /* resultData */, null /* resultGrants */, reason, false /* oomAdj */);
+                // 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.
+                if (r.isState(RESUMED) || (r.isVisible()
+                        && !mDisplayContent.mAppTransition.containsTransitRequest(TRANSIT_CLOSE))) {
+                    r.finishIfPossible(reason, false /* oomAdj */);
+                } else {
+                    r.destroyIfPossible(reason);
+                }
             });
         }
     }
diff --git a/services/tests/wmtests/src/com/android/server/wm/AppTransitionControllerTest.java b/services/tests/wmtests/src/com/android/server/wm/AppTransitionControllerTest.java
index 53bae41..0d0cec7 100644
--- a/services/tests/wmtests/src/com/android/server/wm/AppTransitionControllerTest.java
+++ b/services/tests/wmtests/src/com/android/server/wm/AppTransitionControllerTest.java
@@ -103,6 +103,20 @@
     }
 
     @Test
+    public void testClearTaskSkipAppExecuteTransition() {
+        final ActivityRecord behind = createActivityRecord(mDisplayContent,
+                WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD);
+        final Task task = behind.getTask();
+        final ActivityRecord top = createActivityRecord(task);
+        top.setState(ActivityRecord.State.RESUMED, "test");
+        behind.setState(ActivityRecord.State.STARTED, "test");
+        behind.mVisibleRequested = true;
+
+        task.performClearTask("test");
+        assertFalse(mDisplayContent.mAppTransition.isReady());
+    }
+
+    @Test
     public void testTranslucentOpen() {
         final ActivityRecord behind = createActivityRecord(mDisplayContent,
                 WINDOWING_MODE_FULLSCREEN, ACTIVITY_TYPE_STANDARD);