From cefa3d77000be86668ca5837ed2d6c6a7280e4ec Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Tue, 13 Jun 2023 00:33:06 +0000 Subject: [PATCH] Allow deviceProvisioningPackage to start foreground service from the background. Even when the deviceProvisioningPackage is background-restricted aka in the forced-app-standby mode. Bug: 286115374 Bug: 279767668 Test: atest cts/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java Change-Id: I114d168943fa721375bacd0762d9b66fec6f6a86 --- .../com/android/server/am/ActiveServices.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index 884656bde67f7..038bb64515090 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -406,6 +406,8 @@ public final class ActiveServices { // allowlisted packageName. ArraySet mAllowListWhileInUsePermissionInFgs = new ArraySet<>(); + String mCachedDeviceProvisioningPackage; + // TODO: remove this after feature development is done private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @@ -480,7 +482,8 @@ public final class ActiveServices { // (REASON_ALARM_MANAGER_ALARM_CLOCK), allow it to continue and do not stop it, // even the app is background-restricted. if (r.isForeground - && r.mAllowStartForegroundAtEntering != REASON_ALARM_MANAGER_ALARM_CLOCK) { + && r.mAllowStartForegroundAtEntering != REASON_ALARM_MANAGER_ALARM_CLOCK + && !isDeviceProvisioningPackage(r.packageName)) { toStop.add(r); } } @@ -879,7 +882,8 @@ public final class ActiveServices { boolean forcedStandby = false; if (bgLaunch && appRestrictedAnyInBackground(appUid, appPackageName) - && !isTempAllowedByAlarmClock(appUid)) { + && !isTempAllowedByAlarmClock(appUid) + && !isDeviceProvisioningPackage(appPackageName)) { if (DEBUG_FOREGROUND_SERVICE) { Slog.d(TAG, "Forcing bg-only service start only for " + r.shortInstanceName + " : bgLaunch=" + bgLaunch + " callerFg=" + callerFg); @@ -1917,6 +1921,9 @@ public final class ActiveServices { */ private boolean isForegroundServiceAllowedInBackgroundRestricted(ProcessRecord app) { final ProcessStateRecord state = app.mState; + if (isDeviceProvisioningPackage(app.info.packageName)) { + return true; + } if (!state.isBackgroundRestricted() || state.getSetProcState() <= ActivityManager.PROCESS_STATE_BOUND_TOP) { return true; @@ -8528,4 +8535,13 @@ public final class ActiveServices { } return results; } + + private boolean isDeviceProvisioningPackage(String packageName) { + if (mCachedDeviceProvisioningPackage == null) { + mCachedDeviceProvisioningPackage = mAm.mContext.getResources().getString( + com.android.internal.R.string.config_deviceProvisioningPackage); + } + return mCachedDeviceProvisioningPackage != null + && mCachedDeviceProvisioningPackage.equals(packageName); + } }