From c7a13af455b6c44444243ad21bccac95002fbd76 Mon Sep 17 00:00:00 2001 From: Suprabh Shukla Date: Tue, 10 May 2022 17:28:41 -0700 Subject: [PATCH] Fix alarm manager FGS reason codes The reason code for alarm_clock was never used as intended. This means we cannot differentiate between the two types in metrics. Also fixing the reason code to be used while denying fgs. This should not cause any functional change but is better for consistency and catching inadvertent bugs in the future. Test: atest CtsAlarmManagerTestCases:ExactAlarmsTest ./out/host/linux-x86/bin/statsd_testdrive 60 Should show reason code 301 after running ExactAlarmsTest#alarmClockAllowsFGS Bug: 231661615 Change-Id: I8c20f6b040d9fbdb0808769a4fa0eae7c63d5e07 --- .../server/alarm/AlarmManagerService.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java b/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java index 2ea8592e883e8..ca62eacd19f90 100644 --- a/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java +++ b/apex/jobscheduler/service/java/com/android/server/alarm/AlarmManagerService.java @@ -30,6 +30,8 @@ import static android.app.AlarmManager.INTERVAL_HOUR; import static android.app.AlarmManager.RTC; import static android.app.AlarmManager.RTC_WAKEUP; import static android.content.pm.PackageManager.MATCH_SYSTEM_ONLY; +import static android.os.PowerExemptionManager.REASON_ALARM_MANAGER_ALARM_CLOCK; +import static android.os.PowerExemptionManager.REASON_DENIED; import static android.os.PowerExemptionManager.REASON_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED; import static android.os.PowerExemptionManager.TEMPORARY_ALLOW_LIST_TYPE_FOREGROUND_SERVICE_ALLOWED; import static android.os.PowerWhitelistManager.REASON_ALARM_MANAGER_WHILE_IDLE; @@ -329,6 +331,7 @@ public class AlarmManagerService extends SystemService { }); BroadcastOptions mOptsWithFgs = BroadcastOptions.makeBasic(); + BroadcastOptions mOptsWithFgsForAlarmClock = BroadcastOptions.makeBasic(); BroadcastOptions mOptsWithoutFgs = BroadcastOptions.makeBasic(); BroadcastOptions mOptsTimeBroadcast = BroadcastOptions.makeBasic(); ActivityOptions mActivityOptsRestrictBal = ActivityOptions.makeBasic(); @@ -743,9 +746,12 @@ public class AlarmManagerService extends SystemService { mOptsWithFgs.setTemporaryAppAllowlist(ALLOW_WHILE_IDLE_WHITELIST_DURATION, TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, REASON_ALARM_MANAGER_WHILE_IDLE, ""); + mOptsWithFgsForAlarmClock.setTemporaryAppAllowlist( + ALLOW_WHILE_IDLE_WHITELIST_DURATION, + TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_ALLOWED, + REASON_ALARM_MANAGER_ALARM_CLOCK, ""); mOptsWithoutFgs.setTemporaryAppAllowlist(ALLOW_WHILE_IDLE_WHITELIST_DURATION, - TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED, - REASON_ALARM_MANAGER_WHILE_IDLE, ""); + TEMPORARY_ALLOWLIST_TYPE_FOREGROUND_SERVICE_NOT_ALLOWED, REASON_DENIED, ""); } } @@ -1744,6 +1750,7 @@ public class AlarmManagerService extends SystemService { public void onStart() { mInjector.init(); mOptsWithFgs.setPendingIntentBackgroundActivityLaunchAllowed(false); + mOptsWithFgsForAlarmClock.setPendingIntentBackgroundActivityLaunchAllowed(false); mOptsWithoutFgs.setPendingIntentBackgroundActivityLaunchAllowed(false); mOptsTimeBroadcast.setPendingIntentBackgroundActivityLaunchAllowed(false); mActivityOptsRestrictBal.setPendingIntentBackgroundActivityLaunchAllowed(false); @@ -2741,7 +2748,12 @@ public class AlarmManagerService extends SystemService { if (isExactAlarmChangeEnabled(callingPackage, callingUserId)) { needsPermission = exact; lowerQuota = !exact; - idleOptions = exact ? mOptsWithFgs.toBundle() : mOptsWithoutFgs.toBundle(); + if (exact) { + idleOptions = (alarmClock != null) ? mOptsWithFgsForAlarmClock.toBundle() + : mOptsWithFgs.toBundle(); + } else { + idleOptions = mOptsWithoutFgs.toBundle(); + } } else { changeDisabled = true; needsPermission = false;