From 922079e2a49c1d11853f14f57742db52b1749946 Mon Sep 17 00:00:00 2001 From: Riddle Hsu Date: Tue, 27 Oct 2020 05:35:27 +0000 Subject: [PATCH] Reland "Distinguish warm/hot launch by whether the activity has attached process" If the process of an activity was died in background, its activity and task record will still be in history stack. If the process is started again in background such as receiving broadcast. Then the launch of the same activity will have the conditions: process exists and the result code is START_TASK_TO_FRONT. But the activity still need to create so it should be warm launch. This reverts commit d92b3605bc1420e4c13a23cd82988e5bbe10bbd2. Reason for revert: The original patch is no problem Bug: 171549711 Test: ActivityMetricsLaunchObserverTests#testOnActivityLaunchFinished Change-Id: I2b8e45ddfd76ce4fb0fdb0c244a8f4170fbabd21 --- .../server/wm/ActivityMetricsLogger.java | 20 ++++++++----------- .../ActivityMetricsLaunchObserverTests.java | 12 +++++++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/services/core/java/com/android/server/wm/ActivityMetricsLogger.java b/services/core/java/com/android/server/wm/ActivityMetricsLogger.java index 9d08b1be8d36b..f50ce1ab8e8d2 100644 --- a/services/core/java/com/android/server/wm/ActivityMetricsLogger.java +++ b/services/core/java/com/android/server/wm/ActivityMetricsLogger.java @@ -131,7 +131,6 @@ class ActivityMetricsLogger { */ private static final int IGNORE_CALLER = -1; private static final int INVALID_DELAY = -1; - private static final int INVALID_TRANSITION_TYPE = -1; // Preallocated strings we are sending to tron, so we don't have to allocate a new one every // time we log. @@ -232,22 +231,19 @@ class ActivityMetricsLogger { static TransitionInfo create(@NonNull ActivityRecord r, @NonNull LaunchingState launchingState, @Nullable ActivityOptions options, boolean processRunning, boolean processSwitch, int startResult) { - int transitionType = INVALID_TRANSITION_TYPE; + if (startResult != START_SUCCESS && startResult != START_TASK_TO_FRONT) { + return null; + } + final int transitionType; if (processRunning) { - if (startResult == START_SUCCESS) { - transitionType = TYPE_TRANSITION_WARM_LAUNCH; - } else if (startResult == START_TASK_TO_FRONT) { - transitionType = TYPE_TRANSITION_HOT_LAUNCH; - } - } else if (startResult == START_SUCCESS || startResult == START_TASK_TO_FRONT) { + transitionType = r.attachedToProcess() + ? TYPE_TRANSITION_HOT_LAUNCH + : TYPE_TRANSITION_WARM_LAUNCH; + } else { // Task may still exist when cold launching an activity and the start result will be // set to START_TASK_TO_FRONT. Treat this as a COLD launch. transitionType = TYPE_TRANSITION_COLD_LAUNCH; } - if (transitionType == INVALID_TRANSITION_TYPE) { - // That means the startResult is neither START_SUCCESS nor START_TASK_TO_FRONT. - return null; - } return new TransitionInfo(r, launchingState, options, transitionType, processRunning, processSwitch); } diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityMetricsLaunchObserverTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityMetricsLaunchObserverTests.java index 9be31647cf737..f1d3e1840588f 100644 --- a/services/tests/wmtests/src/com/android/server/wm/ActivityMetricsLaunchObserverTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/ActivityMetricsLaunchObserverTests.java @@ -35,6 +35,7 @@ import static org.mockito.Mockito.timeout; import android.app.ActivityOptions; import android.app.ActivityOptions.SourceInfo; +import android.app.WaitResult; import android.content.Intent; import android.os.SystemClock; import android.platform.test.annotations.Presubmit; @@ -167,10 +168,15 @@ public class ActivityMetricsLaunchObserverTests extends WindowTestsBase { @Test public void testOnActivityLaunchFinished() { + // Assume that the process is started (ActivityBuilder has mocked the returned value of + // ATMS#getProcessController) but the activity has not attached process. + mTopActivity.app = null; onActivityLaunched(mTopActivity); notifyTransitionStarting(mTopActivity); - notifyWindowsDrawn(mTopActivity); + final ActivityMetricsLogger.TransitionInfoSnapshot info = notifyWindowsDrawn(mTopActivity); + assertWithMessage("Warm launch").that(info.getLaunchState()) + .isEqualTo(WaitResult.LAUNCH_STATE_WARM); verifyOnActivityLaunchFinished(mTopActivity); verifyNoMoreInteractions(mLaunchObserver); @@ -204,7 +210,7 @@ public class ActivityMetricsLaunchObserverTests extends WindowTestsBase { notifyActivityLaunching(noDrawnActivity.intent); notifyActivityLaunched(START_SUCCESS, noDrawnActivity); - noDrawnActivity.destroyIfPossible("test"); + noDrawnActivity.mVisibleRequested = false; mActivityMetricsLogger.notifyVisibilityChanged(noDrawnActivity); verifyAsync(mLaunchObserver).onActivityLaunchCancelled(eqProto(noDrawnActivity)); @@ -225,6 +231,8 @@ public class ActivityMetricsLaunchObserverTests extends WindowTestsBase { assertWithMessage("Record start source").that(info.sourceType) .isEqualTo(SourceInfo.TYPE_LAUNCHER); assertWithMessage("Record event time").that(info.sourceEventDelayMs).isAtLeast(10); + assertWithMessage("Hot launch").that(info.getLaunchState()) + .isEqualTo(WaitResult.LAUNCH_STATE_HOT); verifyAsync(mLaunchObserver).onReportFullyDrawn(eqProto(mTopActivity), anyLong()); verifyOnActivityLaunchFinished(mTopActivity);