Make NightDisplayService robust to early alarms

Bug: 30743700

Use the same time for calculating the next activated state and
scheduling the next alarm. This prevents the alarm from being scheduled
incorrectly if the activated state is updated slightly before the
scheduled start or end time.

Change-Id: I690aebddd28547a25b507c227c4527ffdd7c6a02
This commit is contained in:
Justin Klaassen
2016-08-08 15:01:47 -07:00
parent 262f0186bd
commit 4346f63dbf

View File

@@ -20,6 +20,7 @@ import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.AlarmManager;
import android.content.BroadcastReceiver;
@@ -361,13 +362,12 @@ public final class NightDisplayService extends SystemService
if (setActivated) {
mController.setActivated(activated);
}
updateNextAlarm();
updateNextAlarm(mIsActivated, now);
}
private void updateNextAlarm() {
if (mIsActivated != null) {
final Calendar now = Calendar.getInstance();
final Calendar next = mIsActivated ? mEndTime.getDateTimeAfter(now)
private void updateNextAlarm(@Nullable Boolean activated, @NonNull Calendar now) {
if (activated != null) {
final Calendar next = activated ? mEndTime.getDateTimeAfter(now)
: mStartTime.getDateTimeAfter(now);
mAlarmManager.setExact(AlarmManager.RTC, next.getTimeInMillis(), TAG, this, null);
}
@@ -396,10 +396,11 @@ public final class NightDisplayService extends SystemService
@Override
public void onActivated(boolean activated) {
final Calendar now = Calendar.getInstance();
if (mIsActivated != null) {
mLastActivatedTime = Calendar.getInstance();
mLastActivatedTime = now;
}
updateNextAlarm();
updateNextAlarm(activated, now);
}
@Override