Lots of changes
This commit is contained in:
parent
938f1a62a7
commit
8dc71ae34b
@ -37,7 +37,7 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
|
||||
} else {
|
||||
Alarm alarm = checkNotNull(AlarmsRepository.getInstance(context).getItem(id));
|
||||
if (ACTION_DISMISS_NOW.equals(intent.getAction())) {
|
||||
AlarmUtils.cancelAlarm(context, alarm);
|
||||
AlarmUtils.cancelAlarm(context, alarm, true);
|
||||
} else {
|
||||
// Prepare notification
|
||||
String title;
|
||||
|
||||
@ -123,7 +123,7 @@ public class AlarmViewHolder extends BaseViewHolder<Alarm> implements AlarmCount
|
||||
bindCountdown(true, alarm.ringsIn());
|
||||
bindDismissButton(alarm);
|
||||
} else {
|
||||
AlarmUtils.cancelAlarm(getContext(), alarm); // might save repo
|
||||
AlarmUtils.cancelAlarm(getContext(), alarm, true); // might save repo
|
||||
bindCountdown(false, -1);
|
||||
bindDismissButton(false, "");
|
||||
}
|
||||
|
||||
@ -7,5 +7,5 @@ import com.philliphsu.clock2.Alarm;
|
||||
*/
|
||||
public interface AlarmUtilsHelper {
|
||||
void scheduleAlarm(Alarm alarm);
|
||||
void cancelAlarm(Alarm alarm);
|
||||
void cancelAlarm(Alarm alarm, boolean showToast);
|
||||
}
|
||||
|
||||
@ -431,8 +431,8 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cancelAlarm(Alarm alarm) {
|
||||
AlarmUtils.cancelAlarm(this, alarm);
|
||||
public void cancelAlarm(Alarm alarm, boolean showToast) {
|
||||
AlarmUtils.cancelAlarm(this, alarm, showToast);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -75,7 +75,7 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
|
||||
.build();
|
||||
a.setEnabled(mView.isEnabled());
|
||||
if (mAlarm != null) {
|
||||
mAlarmUtilsHelper.cancelAlarm(mAlarm);
|
||||
mAlarmUtilsHelper.cancelAlarm(mAlarm, false);
|
||||
mRepository.updateItem(mAlarm, a);
|
||||
} else {
|
||||
mRepository.addItem(a);
|
||||
@ -92,7 +92,7 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
|
||||
public void delete() {
|
||||
if (mAlarm != null) {
|
||||
if (mAlarm.isEnabled()) {
|
||||
mAlarmUtilsHelper.cancelAlarm(mAlarm); // (1)
|
||||
mAlarmUtilsHelper.cancelAlarm(mAlarm, false); // (1)
|
||||
}
|
||||
mRepository.deleteItem(mAlarm); // TOneverDO: before (1)
|
||||
}
|
||||
@ -101,7 +101,7 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
|
||||
|
||||
@Override
|
||||
public void dismissNow() {
|
||||
mAlarmUtilsHelper.cancelAlarm(checkNotNull(mAlarm));
|
||||
mAlarmUtilsHelper.cancelAlarm(checkNotNull(mAlarm), true);
|
||||
// cancelAlarm() should have turned off this alarm if appropriate
|
||||
mView.showEnabled(mAlarm.isEnabled());
|
||||
}
|
||||
|
||||
@ -59,6 +59,7 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
|
||||
Intent intent = new Intent(this, RingtoneService.class);
|
||||
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
|
||||
|
||||
// TODO: Butterknife binding
|
||||
Button snooze = (Button) findViewById(R.id.btn_snooze);
|
||||
snooze.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
@ -80,10 +81,13 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
|
||||
//super.onNewIntent(intent); // Not needed since no fragments hosted?
|
||||
if (mBound) {
|
||||
mBoundService.interrupt(); // prepare to notify the alarm was missed
|
||||
/*
|
||||
// Cannot rely on finish() to call onDestroy() on time before the activity is restarted.
|
||||
unbindService();
|
||||
// Calling recreate() would recreate this with its current intent, not the new intent passed in here.
|
||||
finish();
|
||||
*/
|
||||
dismiss(); // unbinds and finishes for you
|
||||
startActivity(intent);
|
||||
}
|
||||
}
|
||||
@ -131,15 +135,26 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
|
||||
|
||||
private void snooze() {
|
||||
AlarmUtils.snoozeAlarm(this, mAlarm);
|
||||
// TODO: If dismiss() calls AlarmUtils.cancelAlarm(), don't call dismiss().
|
||||
dismiss();
|
||||
// Can't call dismiss() because we don't want to also call cancelAlarm()! Why? For example,
|
||||
// we don't want the alarm, if it has no recurrence, to be turned off right now.
|
||||
unbindService(); // don't wait for finish() to call onDestroy()
|
||||
finish();
|
||||
//unbindService(); // don't wait for finish() to call onDestroy()
|
||||
//finish();
|
||||
}
|
||||
|
||||
private void dismiss() {
|
||||
// TODO: Do we need to cancel the PendingIntent and the alarm in AlarmManager?
|
||||
AlarmUtils.cancelAlarm(this, mAlarm);
|
||||
// TODO: Do we really need to cancel the PendingIntent and the alarm in AlarmManager? They've
|
||||
// already fired, so what point is there to cancelling them?
|
||||
// ===================================== WARNING ==========================================
|
||||
// If you call cancelAlarm(), then you MUST make sure you are not interfering with a recent
|
||||
// scheduleAlarm() or snoozeAlarm() call. This can actually be the case, so I recommend you
|
||||
// do NOT call it! A PendingIntent and alarm that have already been fired won't bother
|
||||
// you, so just let it sit until the next time the same Alarm is scheduled and they subsequently
|
||||
// get cancelled!
|
||||
// ========================================================================================
|
||||
//AlarmUtils.cancelAlarm(this, mAlarm); // not necessary?
|
||||
|
||||
unbindService(); // don't wait for finish() to call onDestroy()
|
||||
finish();
|
||||
}
|
||||
|
||||
@ -89,8 +89,10 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc
|
||||
AlarmUtils.snoozeAlarm(this, alarm);
|
||||
}
|
||||
// ============================== WARNING ===================================
|
||||
// DO NOT DO ANYTHING FOR ACTION_DISMISS. RingtoneActivity's current implementation of
|
||||
// onServiceFinish() calls cancelAlarm for you!
|
||||
// I AM RECOMMENDING MYSELF TO NOT DO ANYTHING FOR ACTION_DISMISS.
|
||||
// We don't really need to cancel the PendingIntent and the alarm in AlarmManager if
|
||||
// they've already been fired. We can just let it sit! See the similar warning
|
||||
// in RingtoneActivity#dismiss().
|
||||
// /*else if (ACTION_DISMISS.equals(intent.getAction())) {
|
||||
// AlarmUtils.cancelAlarm(this, alarm);
|
||||
// }*/
|
||||
|
||||
@ -68,7 +68,7 @@ public final class AlarmUtils {
|
||||
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
public static void cancelAlarm(Context c, Alarm a) {
|
||||
public static void cancelAlarm(Context c, Alarm a, boolean showToast) {
|
||||
Log.d(TAG, "Cancelling alarm " + a);
|
||||
AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
|
||||
|
||||
@ -82,20 +82,21 @@ public final class AlarmUtils {
|
||||
|
||||
removeUpcomingAlarmNotification(c, a);
|
||||
|
||||
// TOneverDO: Place block after making value changes to the alarm.
|
||||
if (showToast && (a.ringsIn() <= HOURS.toMillis(hoursBeforeUpcoming(c)) || a.isSnoozed())) {
|
||||
String time = formatTime(c, a.isSnoozed() ? a.snoozingUntil() : a.ringsAt());
|
||||
String text = c.getString(R.string.upcoming_alarm_dismissed, time);
|
||||
Toast.makeText(c, text, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
if (a.isSnoozed()) {
|
||||
a.stopSnoozing();
|
||||
save(c);
|
||||
save(c); // TODO: not necessary?
|
||||
}
|
||||
|
||||
if (!a.hasRecurrence()) {
|
||||
a.setEnabled(false);
|
||||
save(c);
|
||||
}
|
||||
|
||||
if (a.ringsIn() <= HOURS.toMillis(hoursBeforeUpcoming(c)) || a.isSnoozed()) {
|
||||
String time = formatTime(c, a.isSnoozed() ? a.snoozingUntil() : a.ringsAt());
|
||||
String text = c.getString(R.string.upcoming_alarm_dismissed, time);
|
||||
Toast.makeText(c, text, Toast.LENGTH_LONG).show();
|
||||
save(c); // TODO: not necessary?
|
||||
}
|
||||
|
||||
// If service is not running, nothing happens
|
||||
@ -108,7 +109,7 @@ public final class AlarmUtils {
|
||||
public static void snoozeAlarm(Context c, Alarm a) {
|
||||
a.snooze(AlarmUtils.snoozeDuration(c));
|
||||
AlarmUtils.scheduleAlarm(c, a);
|
||||
save(c);
|
||||
save(c); // TODO: not necessary?
|
||||
}
|
||||
|
||||
public static void removeUpcomingAlarmNotification(Context c, Alarm a) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user