From ddd1da14db05df2ac195d10070fd4d6a880d5a26 Mon Sep 17 00:00:00 2001 From: Christopher Tate Date: Fri, 13 Apr 2018 13:41:51 -0700 Subject: [PATCH] Correct lookback for while-idle alarm rate limiting Lookback for while-idle alarm "last delivered?" bookkeeping was clamping at zero, but that means "when we rebooted" in the elapsed time base, not a point in the deep past. As a result, testing sequences that forced idle mode would behave unpredictably on devices that were rebooted recently. Change the lookback defaulting such that the first "while idle" alarm that triggers after reboot will always be considered deliverable. Change-Id: I0a35e046ecf858cd44523a6cece4a6a0779194bc Fixes: 77981603 Test: atest CtsAlarmManagerTestCases Test: POC app --- .../core/java/com/android/server/AlarmManagerService.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/services/core/java/com/android/server/AlarmManagerService.java b/services/core/java/com/android/server/AlarmManagerService.java index 6f4ae15bd9736..85eeba0565066 100644 --- a/services/core/java/com/android/server/AlarmManagerService.java +++ b/services/core/java/com/android/server/AlarmManagerService.java @@ -3077,10 +3077,10 @@ class AlarmManagerService extends SystemService { if ((alarm.flags&AlarmManager.FLAG_ALLOW_WHILE_IDLE) != 0) { // If this is an ALLOW_WHILE_IDLE alarm, we constrain how frequently the app can - // schedule such alarms. - final long lastTime = mLastAllowWhileIdleDispatch.get(alarm.creatorUid, 0); + // schedule such alarms. The first such alarm from an app is always delivered. + final long lastTime = mLastAllowWhileIdleDispatch.get(alarm.creatorUid, -1); final long minTime = lastTime + getWhileIdleMinIntervalLocked(alarm.creatorUid); - if (nowELAPSED < minTime) { + if (lastTime >= 0 && nowELAPSED < minTime) { // Whoops, it hasn't been long enough since the last ALLOW_WHILE_IDLE // alarm went off for this app. Reschedule the alarm to be in the // correct time period.