Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 592e3ecb authored by Annie Chin's avatar Annie Chin Committed by Nir Bruderman
Browse files

DeskClock: Correctly reschedule alarms on system time change

Reported issue: AlarmManager would not properly reschedule repeat alarm
if the user manually adjusted date/time to before the next scheduled
instance of the alarm.

Steps to repro issue:
1.Set an alarm for 18:00 when system time is 19:00.
2.Modify the system time to 17:50
3.Note that alarm does not fire at 18:00

Thanks to Guotao Deng for identification and initial fix.

Based on this patch, and adapted for CM:
https://android-review.googlesource.com/#/c/147815/

Change-Id: Ifa7b85ab93583afaf4a248e13d726997b68822ed
(cherry picked from commit d31b7009)
parent 028eade4
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -678,6 +678,9 @@ public final class AlarmStateManager extends BroadcastReceiver {
        // TODO: Refactor this code to not use the overloaded registerInstance method.
        ContentResolver contentResolver = context.getContentResolver();
        for (AlarmInstance instance : AlarmInstance.getInstances(contentResolver, null)) {
            final Alarm alarm = Alarm.getAlarm(contentResolver, instance.mAlarmId);
            instance.setAlarmTime(alarm.getNextAlarmTime(Calendar.getInstance()));
            AlarmInstance.updateInstance(contentResolver, instance);
            AlarmStateManager.registerInstance(context, instance, false);
        }
        AlarmStateManager.updateNextAlarm(context);
+17 −13
Original line number Diff line number Diff line
@@ -326,17 +326,28 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
    }

    public AlarmInstance createInstanceAfter(Calendar time) {
        Calendar nextInstanceTime = getNextAlarmTime(time);
        AlarmInstance result = new AlarmInstance(nextInstanceTime, id);
        result.mVibrate = vibrate;
        result.mLabel = label;
        result.mRingtone = alert;
        result.mIncreasingVolume = increasingVolume;
        result.mProfile = profile;
        return result;
    }

    public Calendar getNextAlarmTime(Calendar currentTime) {
        Calendar nextInstanceTime = Calendar.getInstance();
        nextInstanceTime.set(Calendar.YEAR, time.get(Calendar.YEAR));
        nextInstanceTime.set(Calendar.MONTH, time.get(Calendar.MONTH));
        nextInstanceTime.set(Calendar.DAY_OF_MONTH, time.get(Calendar.DAY_OF_MONTH));
        nextInstanceTime.set(Calendar.YEAR, currentTime.get(Calendar.YEAR));
        nextInstanceTime.set(Calendar.MONTH, currentTime.get(Calendar.MONTH));
        nextInstanceTime.set(Calendar.DAY_OF_MONTH, currentTime.get(Calendar.DAY_OF_MONTH));
        nextInstanceTime.set(Calendar.HOUR_OF_DAY, hour);
        nextInstanceTime.set(Calendar.MINUTE, minutes);
        nextInstanceTime.set(Calendar.SECOND, 0);
        nextInstanceTime.set(Calendar.MILLISECOND, 0);

        // If we are still behind the passed in time, then add a day
        if (nextInstanceTime.getTimeInMillis() <= time.getTimeInMillis()) {
        // If we are still behind the passed in currentTime, then add a day
        if (nextInstanceTime.getTimeInMillis() <= currentTime.getTimeInMillis()) {
            nextInstanceTime.add(Calendar.DAY_OF_YEAR, 1);
        }

@@ -345,14 +356,7 @@ public final class Alarm implements Parcelable, ClockContract.AlarmsColumns {
        if (addDays > 0) {
            nextInstanceTime.add(Calendar.DAY_OF_WEEK, addDays);
        }

        AlarmInstance result = new AlarmInstance(nextInstanceTime, id);
        result.mVibrate = vibrate;
        result.mLabel = label;
        result.mRingtone = alert;
        result.mIncreasingVolume = increasingVolume;
        result.mProfile = profile;
        return result;
        return nextInstanceTime;
    }

    @Override