From a68b3eb151a18a3c93194dbba3f54dab91ef43f7 Mon Sep 17 00:00:00 2001 From: Chilun Date: Wed, 10 Mar 2021 17:51:52 +0800 Subject: Do not use obsoleted snapshot as staring window We will use splash screen as starting window if the activity is in finishing state. Now we will not use the activity with finishing state to show the starting window, so we check the top activity component to avoid using obsoleted snapshot as starting window. Bug: 180893837 Test: atest ActivityStarterTests ActivityRecordTests Change-Id: I27fb40d6d4e8808caf803ef78c39050258eab1a2 --- .../startingsurface/StartingWindowController.java | 7 +++++++ .../java/com/android/server/wm/ActivityRecord.java | 4 ++++ .../com/android/server/wm/ActivityRecordTests.java | 21 +++++++++++++++++++++ .../server/wm/TaskSnapshotPersisterTestBase.java | 10 ++++++++-- 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/StartingWindowController.java b/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/StartingWindowController.java index a694e525a761..d7cae36e4d1a 100644 --- a/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/StartingWindowController.java +++ b/libs/WindowManager/Shell/src/com/android/wm/shell/startingsurface/StartingWindowController.java @@ -150,6 +150,13 @@ public class StartingWindowController { } return false; } + if (!snapshot.getTopActivityComponent().equals(windowInfo.taskInfo.topActivity)) { + if (DEBUG_SPLASH_SCREEN || DEBUG_TASK_SNAPSHOT) { + Slog.d(TAG, "isSnapshotCompatible obsoleted snapshot " + + windowInfo.taskInfo.topActivity); + } + return false; + } final int taskRotation = windowInfo.taskInfo.configuration .windowConfiguration.getRotation(); diff --git a/services/core/java/com/android/server/wm/ActivityRecord.java b/services/core/java/com/android/server/wm/ActivityRecord.java index db3d7ad0c398..66628d5ea3ea 100644 --- a/services/core/java/com/android/server/wm/ActivityRecord.java +++ b/services/core/java/com/android/server/wm/ActivityRecord.java @@ -2154,6 +2154,10 @@ final class ActivityRecord extends WindowToken implements WindowManagerService.A if (snapshot == null) { return false; } + if (!snapshot.getTopActivityComponent().equals(mActivityComponent)) { + // Obsoleted snapshot. + return false; + } final int rotation = mDisplayContent.rotationForActivityInDifferentOrientation(this); final int targetRotation = rotation != ROTATION_UNDEFINED // The display may rotate according to the orientation of this activity. diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java index c19f3489898d..96ebd2460342 100644 --- a/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/ActivityRecordTests.java @@ -1687,6 +1687,7 @@ public class ActivityRecordTests extends WindowTestsBase { public void testIsSnapshotCompatible() { final ActivityRecord activity = createActivityWithTask(); final TaskSnapshot snapshot = new TaskSnapshotPersisterTestBase.TaskSnapshotBuilder() + .setTopActivityComponent(activity.mActivityComponent) .setRotation(activity.getWindowConfiguration().getRotation()) .build(); @@ -1697,6 +1698,26 @@ public class ActivityRecordTests extends WindowTestsBase { assertFalse(activity.isSnapshotCompatible(snapshot)); } + /** + * Test that the snapshot should be obsoleted if the top activity changed. + */ + @Test + public void testIsSnapshotCompatibleTopActivityChanged() { + final ActivityRecord activity = createActivityWithTask(); + final ActivityRecord secondActivity = new ActivityBuilder(mAtm) + .setTask(activity.getTask()) + .setOnTop(true) + .build(); + final TaskSnapshot snapshot = new TaskSnapshotPersisterTestBase.TaskSnapshotBuilder() + .setTopActivityComponent(secondActivity.mActivityComponent) + .build(); + + assertTrue(secondActivity.isSnapshotCompatible(snapshot)); + + // Emulate the top activity changed. + assertFalse(activity.isSnapshotCompatible(snapshot)); + } + @Test public void testFixedRotationSnapshotStartingWindow() { final ActivityRecord activity = createActivityWithTask(); diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java index edf70567b232..b5219fda1cc8 100644 --- a/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java +++ b/services/tests/wmtests/src/com/android/server/wm/TaskSnapshotPersisterTestBase.java @@ -28,7 +28,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; -import android.window.TaskSnapshot; import android.content.ComponentName; import android.content.ContextWrapper; import android.content.res.Resources; @@ -42,6 +41,7 @@ import android.graphics.Rect; import android.hardware.HardwareBuffer; import android.os.UserManager; import android.view.Surface; +import android.window.TaskSnapshot; import com.android.server.LocalServices; import com.android.server.pm.UserManagerInternal; @@ -154,10 +154,16 @@ class TaskSnapshotPersisterTestBase extends WindowTestsBase { private int mWindowingMode = WINDOWING_MODE_FULLSCREEN; private int mSystemUiVisibility = 0; private int mRotation = Surface.ROTATION_0; + private ComponentName mTopActivityComponent = new ComponentName("", ""); TaskSnapshotBuilder() { } + TaskSnapshotBuilder setTopActivityComponent(ComponentName topActivityComponent) { + mTopActivityComponent = topActivityComponent; + return this; + } + TaskSnapshotBuilder setScaleFraction(float scale) { mScaleFraction = scale; return this; @@ -199,7 +205,7 @@ class TaskSnapshotPersisterTestBase extends WindowTestsBase { Canvas c = buffer.lockCanvas(); c.drawColor(Color.RED); buffer.unlockCanvasAndPost(c); - return new TaskSnapshot(MOCK_SNAPSHOT_ID, new ComponentName("", ""), + return new TaskSnapshot(MOCK_SNAPSHOT_ID, mTopActivityComponent, HardwareBuffer.createFromGraphicBuffer(buffer), ColorSpace.get(ColorSpace.Named.SRGB), ORIENTATION_PORTRAIT, mRotation, taskSize, TEST_INSETS, -- cgit v1.2.3-59-g8ed1b