BG-FGS while-in-use restriction uses the same grace period mechanism as BG-Activity-Launch. am: 28644e3b9e

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/13857624

Change-Id: Ie81a9bad37152aef941d072c12e45e4d16516927
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index bfaf4e8..9cecd8b 100644
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -4891,6 +4891,10 @@
                 if (!pr.mAllowBackgroundActivityStartsTokens.isEmpty()) {
                     return true;
                 }
+                if (pr.getWindowProcessController()
+                        .areBackgroundActivityStartsAllowedByGracePeriodSafe()) {
+                    return true;
+                }
             }
         }
 
diff --git a/services/core/java/com/android/server/wm/WindowProcessController.java b/services/core/java/com/android/server/wm/WindowProcessController.java
index df49ac7..e6a35f1 100644
--- a/services/core/java/com/android/server/wm/WindowProcessController.java
+++ b/services/core/java/com/android/server/wm/WindowProcessController.java
@@ -455,15 +455,13 @@
         mAllowBackgroundActivityStarts = allowBackgroundActivityStarts;
     }
 
-    boolean areBackgroundActivityStartsAllowed() {
-        // allow if the whitelisting flag was explicitly set
-        if (mAllowBackgroundActivityStarts) {
-            if (DEBUG_ACTIVITY_STARTS) {
-                Slog.d(TAG, "[WindowProcessController(" + mPid
-                        + ")] Activity start allowed: mAllowBackgroundActivityStarts = true");
-            }
-            return true;
+    public boolean areBackgroundActivityStartsAllowedByGracePeriodSafe() {
+        synchronized (mAtm.mGlobalLockWithoutBoost) {
+            return areBackgroundActivityStartsAllowedByGracePeriod();
         }
+    }
+
+    boolean areBackgroundActivityStartsAllowedByGracePeriod() {
         // 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();
@@ -485,8 +483,24 @@
                         + ACTIVITY_BG_START_GRACE_PERIOD_MS
                         + "ms grace period but also within stop app switch window");
             }
-
         }
+        return false;
+    }
+
+    boolean areBackgroundActivityStartsAllowed() {
+        // allow if the whitelisting flag was explicitly set
+        if (mAllowBackgroundActivityStarts) {
+            if (DEBUG_ACTIVITY_STARTS) {
+                Slog.d(TAG, "[WindowProcessController(" + mPid
+                        + ")] Activity start allowed: mAllowBackgroundActivityStarts = true");
+            }
+            return true;
+        }
+
+        if (areBackgroundActivityStartsAllowedByGracePeriod()) {
+            return true;
+        }
+
         // allow if the proc is instrumenting with background activity starts privs
         if (mInstrumentingWithBackgroundActivityStartPrivileges) {
             if (DEBUG_ACTIVITY_STARTS) {