Reschedule alarm on every change so previous PendingIntent that launches RingtoneActivity is cancelled and updated

This commit is contained in:
Phillip Hsu 2016-09-04 23:11:18 -07:00
parent ecf41785c3
commit d44caf5255
2 changed files with 22 additions and 10 deletions

View File

@ -46,8 +46,7 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder<Alarm> {
private static final String TAG = "BaseAlarmViewHolder"; private static final String TAG = "BaseAlarmViewHolder";
private static final String TAG_ADD_LABEL_DIALOG = "add_label_dialog"; private static final String TAG_ADD_LABEL_DIALOG = "add_label_dialog";
// Visible for subclasses. private final AlarmController mAlarmController;
final AlarmController mAlarmController;
// TODO: Should we use VectorDrawable type? // TODO: Should we use VectorDrawable type?
private final Drawable mDismissNowDrawable; private final Drawable mDismissNowDrawable;
@ -183,8 +182,7 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder<Alarm> {
alarm.setEnabled(checked); alarm.setEnabled(checked);
if (alarm.isEnabled()) { if (alarm.isEnabled()) {
// TODO: On 21+, upcoming notification doesn't post immediately // TODO: On 21+, upcoming notification doesn't post immediately
mAlarmController.scheduleAlarm(alarm, true); persistUpdatedAlarm(alarm, true);
mAlarmController.save(alarm);
} else { } else {
mAlarmController.cancelAlarm(alarm, true); mAlarmController.cancelAlarm(alarm, true);
// cancelAlarm() already calls save() for you. // cancelAlarm() already calls save() for you.
@ -207,6 +205,21 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder<Alarm> {
dialog.show(mFragmentManager, TAG_ADD_LABEL_DIALOG); dialog.show(mFragmentManager, TAG_ADD_LABEL_DIALOG);
} }
/**
* Helper method that should be called each time a change is made to the underlying alarm.
* We should schedule a new alarm with the AlarmManager any time a change is made, even when
* it was not the alarm's time that changed. This is so that we cancel and update the
* PendingIntent's extra data with the most up-to-date Alarm's values. The effect of this
* is to guarantee that the Intent that will launch RingtoneActivity has the most up-to-date
* extra data about the updated alarm.
*
* @param newAlarm The new alarm that has the updated values
*/
final void persistUpdatedAlarm(Alarm newAlarm, boolean showSnackbar) {
mAlarmController.scheduleAlarm(newAlarm, showSnackbar);
mAlarmController.save(newAlarm);
}
private void bindTime(Alarm alarm) { private void bindTime(Alarm alarm) {
String time = DateFormat.getTimeFormat(getContext()).format(new Date(alarm.ringsAt())); String time = DateFormat.getTimeFormat(getContext()).format(new Date(alarm.ringsAt()));
if (DateFormat.is24HourFormat(getContext())) { if (DateFormat.is24HourFormat(getContext())) {
@ -276,7 +289,7 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder<Alarm> {
.label(label) .label(label)
.build(); .build();
oldAlarm.copyMutableFieldsTo(newAlarm); oldAlarm.copyMutableFieldsTo(newAlarm);
mAlarmController.save(newAlarm); persistUpdatedAlarm(newAlarm, false);
} }
}; };
} }
@ -301,8 +314,7 @@ public abstract class BaseAlarmViewHolder extends BaseViewHolder<Alarm> {
.minutes(minute) .minutes(minute)
.build(); .build();
oldAlarm.copyMutableFieldsTo(newAlarm); oldAlarm.copyMutableFieldsTo(newAlarm);
mAlarmController.scheduleAlarm(newAlarm, true); persistUpdatedAlarm(newAlarm, true);
mAlarmController.save(newAlarm);
} }
}; };
} }

View File

@ -143,7 +143,7 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder {
.vibrates(mVibrate.isChecked()) .vibrates(mVibrate.isChecked())
.build(); .build();
oldAlarm.copyMutableFieldsTo(newAlarm); oldAlarm.copyMutableFieldsTo(newAlarm);
mAlarmController.save(newAlarm); persistUpdatedAlarm(newAlarm, false);
} }
@OnClick({ R.id.day0, R.id.day1, R.id.day2, R.id.day3, R.id.day4, R.id.day5, R.id.day6 }) @OnClick({ R.id.day0, R.id.day1, R.id.day2, R.id.day3, R.id.day4, R.id.day5, R.id.day6 })
@ -159,7 +159,7 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder {
+ weekDayAtPosition + " relative to a week starting on Sunday"); + weekDayAtPosition + " relative to a week starting on Sunday");
newAlarm.setRecurring(weekDayAtPosition, view.isChecked()); newAlarm.setRecurring(weekDayAtPosition, view.isChecked());
// --------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------
mAlarmController.save(newAlarm); persistUpdatedAlarm(newAlarm, true);
} }
private void bindDays(Alarm alarm) { private void bindDays(Alarm alarm) {
@ -217,7 +217,7 @@ public class ExpandedAlarmViewHolder extends BaseAlarmViewHolder {
.ringtone(ringtoneUri.toString()) .ringtone(ringtoneUri.toString())
.build(); .build();
oldAlarm.copyMutableFieldsTo(newAlarm); oldAlarm.copyMutableFieldsTo(newAlarm);
mAlarmController.save(newAlarm); persistUpdatedAlarm(newAlarm, false);
} }
}; };
} }