Do not allow app to start background activity after stop app switches triggered

Bug: 133450759
Bug: 132333960
Test: atest CtsActivityManagerBackgroundActivityTestCases
Change-Id: Ifec482d94437cb9e26fa30a1aa66d212c463cd99
diff --git a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
index 765c9d0..7284e0f 100644
--- a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
+++ b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
@@ -508,6 +508,12 @@
      */
     private boolean mDidAppSwitch;
 
+    /**
+     * Last stop app switches time, apps finished before this time cannot start background activity
+     * even if they are in grace period.
+     */
+    private long mLastStopAppSwitchesTime;
+
     IActivityController mController = null;
     boolean mControllerIsAMonkey = false;
 
@@ -4731,6 +4737,7 @@
         enforceCallerIsRecentsOrHasPermission(STOP_APP_SWITCHES, "stopAppSwitches");
         synchronized (mGlobalLock) {
             mAppSwitchesAllowedTime = SystemClock.uptimeMillis() + APP_SWITCH_DELAY_TIME;
+            mLastStopAppSwitchesTime = SystemClock.uptimeMillis();
             mDidAppSwitch = false;
             getActivityStartController().schedulePendingActivityLaunches(APP_SWITCH_DELAY_TIME);
         }
@@ -4747,6 +4754,10 @@
         }
     }
 
+    long getLastStopAppSwitchesTime() {
+        return mLastStopAppSwitchesTime;
+    }
+
     void onStartActivitySetDidAppSwitch() {
         if (mDidAppSwitch) {
             // This is the second allowed switch since we stopped switches, so now just generally
diff --git a/services/core/java/com/android/server/wm/WindowProcessController.java b/services/core/java/com/android/server/wm/WindowProcessController.java
index 2ad25cf..bc5e328 100644
--- a/services/core/java/com/android/server/wm/WindowProcessController.java
+++ b/services/core/java/com/android/server/wm/WindowProcessController.java
@@ -402,11 +402,17 @@
         if (mAllowBackgroundActivityStarts) {
             return true;
         }
-        // allow if any activity in the caller has either started or finished very recently
+        // allow if any activity in the caller has either started or finished very recently, and
+        // it must be started or finished after last stop app switches time.
         final long now = SystemClock.uptimeMillis();
         if (now - mLastActivityLaunchTime < ACTIVITY_BG_START_GRACE_PERIOD_MS
                 || now - mLastActivityFinishTime < ACTIVITY_BG_START_GRACE_PERIOD_MS) {
-            return true;
+            // if activity is started and finished before stop app switch time, we should not
+            // let app to be able to start background activity even it's in grace period.
+            if (mLastActivityLaunchTime > mAtm.getLastStopAppSwitchesTime()
+                    || mLastActivityFinishTime > mAtm.getLastStopAppSwitchesTime()) {
+                return true;
+            }
         }
         // allow if the proc is instrumenting with background activity starts privs
         if (mInstrumentingWithBackgroundActivityStartPrivileges) {