From 049f105aa6aaa8fdc88fc25a66b50469b2e0bf4e Mon Sep 17 00:00:00 2001 From: Kweku Adams Date: Thu, 1 Jun 2023 19:23:42 +0000 Subject: [PATCH] Avoid incorrectly overriding while-in-use reason. Only set mAllowWhileInUsePermissionInFgsReason when the while-in-use permission flag (mAllowWhileInUsePermissionInFgs) is set to true. Bug: 285372293 Test: atest CtsJobSchedulerTestCases:UserInitiatedJobTest Change-Id: Ifcab71f6d4847d5338c628185887990c65bf73a6 --- .../com/android/server/am/ActiveServices.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java index 6db8ea7e536e7..7ede4614232f9 100644 --- a/services/core/java/com/android/server/am/ActiveServices.java +++ b/services/core/java/com/android/server/am/ActiveServices.java @@ -70,6 +70,7 @@ import static android.os.PowerExemptionManager.REASON_INSTR_BACKGROUND_FGS_PERMI import static android.os.PowerExemptionManager.REASON_OPT_OUT_REQUESTED; import static android.os.PowerExemptionManager.REASON_OP_ACTIVATE_PLATFORM_VPN; import static android.os.PowerExemptionManager.REASON_OP_ACTIVATE_VPN; +import static android.os.PowerExemptionManager.REASON_OTHER; import static android.os.PowerExemptionManager.REASON_PACKAGE_INSTALLER; import static android.os.PowerExemptionManager.REASON_PROC_STATE_PERSISTENT; import static android.os.PowerExemptionManager.REASON_PROC_STATE_PERSISTENT_UI; @@ -85,7 +86,6 @@ import static android.os.PowerExemptionManager.REASON_SYSTEM_MODULE; import static android.os.PowerExemptionManager.REASON_SYSTEM_UID; import static android.os.PowerExemptionManager.REASON_TEMP_ALLOWED_WHILE_IN_USE; import static android.os.PowerExemptionManager.REASON_UID_VISIBLE; -import static android.os.PowerExemptionManager.REASON_UNKNOWN; import static android.os.PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED; import static android.os.PowerExemptionManager.getReasonCodeFromProcState; import static android.os.PowerExemptionManager.reasonCodeToString; @@ -7428,33 +7428,34 @@ public final class ActiveServices { boolean isStartService) { // Check DeviceConfig flag. if (!mAm.mConstants.mFlagBackgroundFgsStartRestrictionEnabled) { + if (!r.mAllowWhileInUsePermissionInFgs) { + // BGFGS start restrictions are disabled. We're allowing while-in-use permissions. + // Note REASON_OTHER since there's no other suitable reason. + r.mAllowWhileInUsePermissionInFgsReason = REASON_OTHER; + } r.mAllowWhileInUsePermissionInFgs = true; } - final @ReasonCode int allowWhileInUse; - // Either (or both) mAllowWhileInUsePermissionInFgs or mAllowStartForeground is // newly allowed? boolean newlyAllowed = false; if (!r.mAllowWhileInUsePermissionInFgs || (r.mAllowStartForeground == REASON_DENIED)) { - allowWhileInUse = shouldAllowFgsWhileInUsePermissionLocked( + @ReasonCode final int allowWhileInUse = shouldAllowFgsWhileInUsePermissionLocked( callingPackage, callingPid, callingUid, r.app, backgroundStartPrivileges, isBindService); // We store them to compare the old and new while-in-use logics to each other. // (They're not used for any other purposes.) if (!r.mAllowWhileInUsePermissionInFgs) { r.mAllowWhileInUsePermissionInFgs = (allowWhileInUse != REASON_DENIED); + r.mAllowWhileInUsePermissionInFgsReason = allowWhileInUse; } if (r.mAllowStartForeground == REASON_DENIED) { r.mAllowStartForeground = shouldAllowFgsStartForegroundWithBindingCheckLocked( allowWhileInUse, callingPackage, callingPid, callingUid, intent, r, backgroundStartPrivileges, isBindService); } - } else { - allowWhileInUse = REASON_UNKNOWN; } - r.mAllowWhileInUsePermissionInFgsReason = allowWhileInUse; } /**