Fix calendar alarms not being triggered after 22h

Fix two bugs:

1) scheduleNextAlarmLocked is supposed to schedule the next check for
   alarms in (24h - 15min) if it can't find any alarms within the next
   24h. It doesn't, because the comparison at the end

   if (nextAlarmTime == Long.MAX_VALUE)

   is never true, since nextAlarmTime is set to the value of the
   variable end at the beginning.

   Fix this by introducing a new boolean, mAlarmScheduled, which is
   set true by scheduleAlarm.

2) With the first issue fixed, we still have a blind spot of 1h 45min
   where no alarm would be triggered, because the end variable is
   calculated from start, which is always set to two hours before the
   function is run; end adds 24h to this, so we end up 22h in the
   future and don't look for any alarms past that. When we schedule
   the next check, we do so 23h 45min in the future and thereby end up
   with a 1h 45min window in which no alarm would be triggered.

   Fix this by calculating end from currentMillis instead. Also use
   DateUtils.DAY_IN_MILLIS for consistency.

Also, schedule the next alarm based on the value of end instead of a
constant for more flexibility and less risk of future breakage.

Test: Tested using the android emulator and an offline calendar app
(https://play.google.com/store/apps/details?id=org.sufficientlysecure
.localcalendar&hl=en) in the following manner:

1) Temporarily changed the time scale to 10 minutes instead of 24
   hours for feasibility. To do this, the second fix had to be applied,
   since without it the window we're looking at is two hours back. That
   is, line 281 was changed to

   final long end = currentMillis + (10 * 60 * 1000);

   and lines 478-479 to

   scheduleNextAlarmCheck(end);

2) With no further changes I verified the bug by adding one event with
   the alarm set within ten minutes from the current time and one
   with the alarm set for later than ten minutes after the first
   alarm. The first triggers, but the second doesn't.

3) Applied the rest and did the same test again to verify that both
   alarms now trigger.

Bug: 69456806
Test: atest packages/providers/CalendarProvider/tests/
Change-Id: I7635258dc55257e5c8616a07f611d20b04fe0ff8
Signed-off-by: Kim Lindberger <kim.lindberger@gmail.com>
1 file changed