diff options
| author | 2019-05-21 15:18:44 +0100 | |
|---|---|---|
| committer | 2019-05-23 13:12:22 +0100 | |
| commit | fc6872ee834463ca733c5507fd26f55dbc22fea8 (patch) | |
| tree | 2144f0d2eb33128029cda4cda87d85c6b60c055a | |
| parent | fe0bfe66e95128f09744ddd52268e07bc947ce55 (diff) | |
Introduce a grace period for bg activity starts
We allow processes that either had an activity started in them
recently, or recently finished an activity while they were
participating in a visible task to start activities from background
within 10 seconds.
Bug: 132333960
Test: atest WmTests:ActivityStarterTests
Test: atest CtsWindowManagerDeviceTestCases:ActivityStarterTests
Test: atest BackgroundActivityLaunchTest
Test: manual with bugs on b/hotlistid:1573032
Change-Id: Ica2c1abecf881ff9bb0959f17456d816350e4e96
| -rw-r--r-- | services/core/java/com/android/server/wm/ActivityTaskManagerService.java | 10 | ||||
| -rw-r--r-- | services/core/java/com/android/server/wm/WindowProcessController.java | 29 |
2 files changed, 39 insertions, 0 deletions
diff --git a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java index 772e5e646825..765c9d0a5864 100644 --- a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java +++ b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java @@ -311,6 +311,9 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub { public static final int KEY_DISPATCHING_TIMEOUT_MS = 5 * 1000; // How long we wait until we timeout on key dispatching during instrumentation. static final int INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT_MS = 60 * 1000; + // How long we permit background activity starts after an activity in the process + // started or finished. + static final long ACTIVITY_BG_START_GRACE_PERIOD_MS = 10 * 1000; /** Used to indicate that an app transition should be animated. */ static final boolean ANIMATE = true; @@ -1575,6 +1578,13 @@ public class ActivityTaskManagerService extends IActivityTaskManager.Stub { } } } + + // note down that the process has finished an activity and is in background activity + // starts grace period + if (r.app != null) { + r.app.setLastActivityFinishTimeIfNeeded(SystemClock.uptimeMillis()); + } + final long origId = Binder.clearCallingIdentity(); try { boolean res; diff --git a/services/core/java/com/android/server/wm/WindowProcessController.java b/services/core/java/com/android/server/wm/WindowProcessController.java index 12b62b99b3e2..2ad25cf123e2 100644 --- a/services/core/java/com/android/server/wm/WindowProcessController.java +++ b/services/core/java/com/android/server/wm/WindowProcessController.java @@ -34,6 +34,7 @@ import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_CONFI import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_RELEASE; import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM; import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME; +import static com.android.server.wm.ActivityTaskManagerService.ACTIVITY_BG_START_GRACE_PERIOD_MS; import static com.android.server.wm.ActivityTaskManagerService.INSTRUMENTATION_KEY_DISPATCHING_TIMEOUT_MS; import static com.android.server.wm.ActivityTaskManagerService.KEY_DISPATCHING_TIMEOUT_MS; import static com.android.server.wm.ActivityTaskManagerService.RELAUNCH_REASON_NONE; @@ -50,6 +51,7 @@ import android.content.pm.ApplicationInfo; import android.content.res.Configuration; import android.os.Message; import android.os.RemoteException; +import android.os.SystemClock; import android.util.ArraySet; import android.util.Log; import android.util.Slog; @@ -163,6 +165,11 @@ public class WindowProcessController extends ConfigurationContainer<Configuratio private final ArrayList<TaskRecord> mRecentTasks = new ArrayList<>(); // The most recent top-most activity that was resumed in the process for pre-Q app. private ActivityRecord mPreQTopResumedActivity = null; + // The last time an activity was launched in the process + private long mLastActivityLaunchTime; + // The last time an activity was finished in the process while the process participated + // in a visible task + private long mLastActivityFinishTime; // Last configuration that was reported to the process. private final Configuration mLastReportedConfiguration; @@ -372,6 +379,20 @@ public class WindowProcessController extends ConfigurationContainer<Configuratio return mUsingWrapper; } + void setLastActivityLaunchTime(long launchTime) { + if (launchTime <= mLastActivityLaunchTime) { + return; + } + mLastActivityLaunchTime = launchTime; + } + + void setLastActivityFinishTimeIfNeeded(long finishTime) { + if (finishTime <= mLastActivityFinishTime || !hasActivityInVisibleTask()) { + return; + } + mLastActivityFinishTime = finishTime; + } + public void setAllowBackgroundActivityStarts(boolean allowBackgroundActivityStarts) { mAllowBackgroundActivityStarts = allowBackgroundActivityStarts; } @@ -381,6 +402,12 @@ public class WindowProcessController extends ConfigurationContainer<Configuratio if (mAllowBackgroundActivityStarts) { return true; } + // allow if any activity in the caller has either started or finished very recently + final long now = SystemClock.uptimeMillis(); + if (now - mLastActivityLaunchTime < ACTIVITY_BG_START_GRACE_PERIOD_MS + || now - mLastActivityFinishTime < ACTIVITY_BG_START_GRACE_PERIOD_MS) { + return true; + } // allow if the proc is instrumenting with background activity starts privs if (mInstrumentingWithBackgroundActivityStartPrivileges) { return true; @@ -457,6 +484,8 @@ public class WindowProcessController extends ConfigurationContainer<Configuratio } void addActivityIfNeeded(ActivityRecord r) { + // even if we already track this activity, note down that it has been launched + setLastActivityLaunchTime(r.lastLaunchTime); if (mActivities.contains(r)) { return; } |