From 639a03698ffec588cdf10770a979588ebb7892a6 Mon Sep 17 00:00:00 2001 From: Riddle Hsu Date: Tue, 19 Jul 2022 22:08:46 +0800 Subject: [PATCH] Don't track launch time for activity which won't resume If "doResume" is false, it may only put an ActivityRecord with INITIALIZING state on a background task. Then if the task is moved to front after a long time, the launch time will be unexpected long. Also remove the parameter "doResume" of several methods which is always true. Bug: 204488635 Test: Start activity A. Press home key. A starts B after 5s. The launch will be blocked by background-activity-launch. Because there is an existing task for B, B will be put on the task silently without creating client side. Then when launching A again (or use recents to choose the task), The launch time should not include the duration since A starts B. The duration will count since the last time moving the task to front. Change-Id: I63b11fc123c265b30159843285e6f5555b2fd440 --- .../android/server/wm/ActivityStarter.java | 23 +++++++++++-------- .../server/wm/ActivityStarterTests.java | 2 +- .../DisplayWindowPolicyControllerTests.java | 1 - 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/services/core/java/com/android/server/wm/ActivityStarter.java b/services/core/java/com/android/server/wm/ActivityStarter.java index 59dbf22235063..f5c5a134402fb 100644 --- a/services/core/java/com/android/server/wm/ActivityStarter.java +++ b/services/core/java/com/android/server/wm/ActivityStarter.java @@ -724,13 +724,15 @@ class ActivityStarter { // used here because it may be cleared in setTargetRootTaskIfNeeded. final ActivityOptions originalOptions = mRequest.activityOptions != null ? mRequest.activityOptions.getOriginalOptions() : null; + // Only track the launch time of activity that will be resumed. + final ActivityRecord launchingRecord = mDoResume ? mLastStartActivityRecord : null; // If the new record is the one that started, a new activity has created. - final boolean newActivityCreated = mStartActivity == mLastStartActivityRecord; + final boolean newActivityCreated = mStartActivity == launchingRecord; // Notify ActivityMetricsLogger that the activity has launched. // ActivityMetricsLogger will then wait for the windows to be drawn and populate // WaitResult. mSupervisor.getActivityMetricsLogger().notifyActivityLaunched(launchingState, res, - newActivityCreated, mLastStartActivityRecord, originalOptions); + newActivityCreated, launchingRecord, originalOptions); if (mRequest.waitResult != null) { mRequest.waitResult.result = res; res = waitResultIfNeeded(mRequest.waitResult, mLastStartActivityRecord, @@ -1214,7 +1216,7 @@ class ActivityStarter { } mLastStartActivityResult = startActivityUnchecked(r, sourceRecord, voiceSession, - request.voiceInteractor, startFlags, true /* doResume */, checkedOptions, + request.voiceInteractor, startFlags, checkedOptions, inTask, inTaskFragment, restrictedBgActivity, intentGrants); if (request.outActivity != null) { @@ -1640,7 +1642,7 @@ class ActivityStarter { */ private int startActivityUnchecked(final ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, - int startFlags, boolean doResume, ActivityOptions options, Task inTask, + int startFlags, ActivityOptions options, Task inTask, TaskFragment inTaskFragment, boolean restrictedBgActivity, NeededUriGrants intentGrants) { int result = START_CANCELED; @@ -1664,7 +1666,7 @@ class ActivityStarter { try { Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "startActivityInner"); result = startActivityInner(r, sourceRecord, voiceSession, voiceInteractor, - startFlags, doResume, options, inTask, inTaskFragment, restrictedBgActivity, + startFlags, options, inTask, inTaskFragment, restrictedBgActivity, intentGrants); } finally { Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER); @@ -1808,10 +1810,10 @@ class ActivityStarter { @VisibleForTesting int startActivityInner(final ActivityRecord r, ActivityRecord sourceRecord, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, - int startFlags, boolean doResume, ActivityOptions options, Task inTask, + int startFlags, ActivityOptions options, Task inTask, TaskFragment inTaskFragment, boolean restrictedBgActivity, NeededUriGrants intentGrants) { - setInitialState(r, options, inTask, inTaskFragment, doResume, startFlags, sourceRecord, + setInitialState(r, options, inTask, inTaskFragment, startFlags, sourceRecord, voiceSession, voiceInteractor, restrictedBgActivity); computeLaunchingTaskFlags(); @@ -2429,7 +2431,7 @@ class ActivityStarter { } private void setInitialState(ActivityRecord r, ActivityOptions options, Task inTask, - TaskFragment inTaskFragment, boolean doResume, int startFlags, + TaskFragment inTaskFragment, int startFlags, ActivityRecord sourceRecord, IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor, boolean restrictedBgActivity) { reset(false /* clearRequest */); @@ -2494,10 +2496,11 @@ class ActivityStarter { // If the caller has asked not to resume at this point, we make note // of this in the record so that we can skip it when trying to find // the top running activity. - mDoResume = doResume; - if (!doResume || !r.showToCurrentUser() || mLaunchTaskBehind) { + if (!r.showToCurrentUser() || mLaunchTaskBehind) { r.delayedResume = true; mDoResume = false; + } else { + mDoResume = true; } if (mOptions != null) { diff --git a/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java b/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java index fe3c26a3b409d..cd087e69bb0c6 100644 --- a/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/ActivityStarterTests.java @@ -1406,7 +1406,7 @@ public class ActivityStarterTests extends WindowTestsBase { ActivityRecord source, ActivityOptions options, Task inTask, TaskFragment inTaskFragment) { starter.startActivityInner(target, source, null /* voiceSession */, - null /* voiceInteractor */, 0 /* startFlags */, true /* doResume */, + null /* voiceInteractor */, 0 /* startFlags */, options, inTask, inTaskFragment, false /* restrictedBgActivity */, null /* intentGrants */); } diff --git a/services/tests/wmtests/src/com/android/server/wm/DisplayWindowPolicyControllerTests.java b/services/tests/wmtests/src/com/android/server/wm/DisplayWindowPolicyControllerTests.java index 47c217651e786..21197baaf8cc5 100644 --- a/services/tests/wmtests/src/com/android/server/wm/DisplayWindowPolicyControllerTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/DisplayWindowPolicyControllerTests.java @@ -184,7 +184,6 @@ public class DisplayWindowPolicyControllerTests extends WindowTestsBase { /* voiceSession */null, /* voiceInteractor */ null, /* startFlags */ 0, - /* doResume */true, /* options */null, /* inTask */null, /* inTaskFragment */ null,