From 8652e483b098203cda9af80124592db64dd773af Mon Sep 17 00:00:00 2001 From: Beverly Date: Thu, 18 Mar 2021 10:28:45 -0400 Subject: [PATCH] Next alarm is definitive info from clock Even if the next alarm is further in the future than the alarm on the ScheduleCalendar, update the next alarm b/c this means that our current nextAlarm is old info Test: atest ScheduleCalendarTest Test: manual 1. enable DND that ends at alarm 2. create an alarm that goes off in 5 min 3. change alarm to go off in 11 min 4. in the 6 minute difference, notice that DND is still on and didn't end earlier than execpted. Fixes: 181665547 Change-Id: Ieb2fc7269c4a9b1bc109941d16ce191012d338c4 --- .../service/notification/ScheduleCalendar.java | 17 +++++------------ .../notification/ScheduleCalendarTest.java | 3 ++- .../ScheduleConditionProviderTest.java | 10 +++++----- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/core/java/android/service/notification/ScheduleCalendar.java b/core/java/android/service/notification/ScheduleCalendar.java index 6ed966e0bbbd0..314c97db4e7b0 100644 --- a/core/java/android/service/notification/ScheduleCalendar.java +++ b/core/java/android/service/notification/ScheduleCalendar.java @@ -57,25 +57,18 @@ public class ScheduleCalendar { } /** - * Sets next alarm of the schedule if the saved next alarm has passed or is further - * in the future than given nextAlarm + * Sets next alarm of the schedule * @param now current time in milliseconds * @param nextAlarm time of next alarm in milliseconds */ public void maybeSetNextAlarm(long now, long nextAlarm) { if (mSchedule != null && mSchedule.exitAtAlarm) { - // alarm canceled if (nextAlarm == 0) { + // alarm canceled mSchedule.nextAlarm = 0; - } - // only allow alarms in the future - if (nextAlarm > now) { - if (mSchedule.nextAlarm == 0 || mSchedule.nextAlarm < now) { - mSchedule.nextAlarm = nextAlarm; - } else { - // store earliest alarm - mSchedule.nextAlarm = Math.min(mSchedule.nextAlarm, nextAlarm); - } + } else if (nextAlarm > now) { + // only allow alarms in the future + mSchedule.nextAlarm = nextAlarm; } else if (mSchedule.nextAlarm < now) { if (DEBUG) { Log.d(TAG, "All alarms are in the past " + mSchedule.nextAlarm); diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ScheduleCalendarTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ScheduleCalendarTest.java index 3b15376399732..11162043bb27f 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/ScheduleCalendarTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/ScheduleCalendarTest.java @@ -316,9 +316,10 @@ public class ScheduleCalendarTest extends UiServiceTestCase { mScheduleCalendar.setSchedule(mScheduleInfo); mScheduleInfo.nextAlarm = 2000; + // next alarm updated to 3000 (alarm for 2000 was changed to 3000) mScheduleCalendar.maybeSetNextAlarm(1000, 3000); - assertEquals(2000, mScheduleInfo.nextAlarm); + assertEquals(3000, mScheduleInfo.nextAlarm); } @Test diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ScheduleConditionProviderTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ScheduleConditionProviderTest.java index 5a527a2190552..7446e9e0bb9f6 100644 --- a/services/tests/uiservicestests/src/com/android/server/notification/ScheduleConditionProviderTest.java +++ b/services/tests/uiservicestests/src/com/android/server/notification/ScheduleConditionProviderTest.java @@ -279,17 +279,17 @@ public class ScheduleConditionProviderTest extends UiServiceTestCase { conditionId, cal, now.getTimeInMillis(), now.getTimeInMillis() + 500); assertEquals(Condition.STATE_TRUE, condition.state); - // in schedule, update with later alarm time, should be in dnd + // in schedule, update with nextAlarm = later alarm time (1000), should be in dnd condition = mService.evaluateSubscriptionLocked( conditionId, cal, now.getTimeInMillis() + 250, now.getTimeInMillis() + 1000); assertEquals(Condition.STATE_TRUE, condition.state); - // at earliest alarm fire time, should exit dnd - assertTrue(cal.isInSchedule(now.getTimeInMillis() + 500)); + // at next alarm fire time (1000), should exit dnd + assertTrue(cal.isInSchedule(now.getTimeInMillis() + 1000)); assertTrue("" + info.nextAlarm + " " + now.getTimeInMillis(), - cal.shouldExitForAlarm(now.getTimeInMillis() + 500)); + cal.shouldExitForAlarm(now.getTimeInMillis() + 1000)); condition = mService.evaluateSubscriptionLocked( - conditionId, cal, now.getTimeInMillis() + 500, 0); + conditionId, cal, now.getTimeInMillis() + 1000, 0); assertEquals(Condition.STATE_FALSE, condition.state); }