Lots of changes

This commit is contained in:
Phillip Hsu 2016-06-08 12:44:12 -07:00
parent 938f1a62a7
commit 8dc71ae34b
8 changed files with 42 additions and 24 deletions

View File

@ -37,7 +37,7 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
} else { } else {
Alarm alarm = checkNotNull(AlarmsRepository.getInstance(context).getItem(id)); Alarm alarm = checkNotNull(AlarmsRepository.getInstance(context).getItem(id));
if (ACTION_DISMISS_NOW.equals(intent.getAction())) { if (ACTION_DISMISS_NOW.equals(intent.getAction())) {
AlarmUtils.cancelAlarm(context, alarm); AlarmUtils.cancelAlarm(context, alarm, true);
} else { } else {
// Prepare notification // Prepare notification
String title; String title;

View File

@ -123,7 +123,7 @@ public class AlarmViewHolder extends BaseViewHolder<Alarm> implements AlarmCount
bindCountdown(true, alarm.ringsIn()); bindCountdown(true, alarm.ringsIn());
bindDismissButton(alarm); bindDismissButton(alarm);
} else { } else {
AlarmUtils.cancelAlarm(getContext(), alarm); // might save repo AlarmUtils.cancelAlarm(getContext(), alarm, true); // might save repo
bindCountdown(false, -1); bindCountdown(false, -1);
bindDismissButton(false, ""); bindDismissButton(false, "");
} }

View File

@ -7,5 +7,5 @@ import com.philliphsu.clock2.Alarm;
*/ */
public interface AlarmUtilsHelper { public interface AlarmUtilsHelper {
void scheduleAlarm(Alarm alarm); void scheduleAlarm(Alarm alarm);
void cancelAlarm(Alarm alarm); void cancelAlarm(Alarm alarm, boolean showToast);
} }

View File

@ -431,8 +431,8 @@ public class EditAlarmActivity extends BaseActivity implements AlarmNumpad.KeyLi
} }
@Override @Override
public void cancelAlarm(Alarm alarm) { public void cancelAlarm(Alarm alarm, boolean showToast) {
AlarmUtils.cancelAlarm(this, alarm); AlarmUtils.cancelAlarm(this, alarm, showToast);
} }
@Override @Override

View File

@ -75,7 +75,7 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
.build(); .build();
a.setEnabled(mView.isEnabled()); a.setEnabled(mView.isEnabled());
if (mAlarm != null) { if (mAlarm != null) {
mAlarmUtilsHelper.cancelAlarm(mAlarm); mAlarmUtilsHelper.cancelAlarm(mAlarm, false);
mRepository.updateItem(mAlarm, a); mRepository.updateItem(mAlarm, a);
} else { } else {
mRepository.addItem(a); mRepository.addItem(a);
@ -92,7 +92,7 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
public void delete() { public void delete() {
if (mAlarm != null) { if (mAlarm != null) {
if (mAlarm.isEnabled()) { if (mAlarm.isEnabled()) {
mAlarmUtilsHelper.cancelAlarm(mAlarm); // (1) mAlarmUtilsHelper.cancelAlarm(mAlarm, false); // (1)
} }
mRepository.deleteItem(mAlarm); // TOneverDO: before (1) mRepository.deleteItem(mAlarm); // TOneverDO: before (1)
} }
@ -101,7 +101,7 @@ public class EditAlarmPresenter implements EditAlarmContract.Presenter {
@Override @Override
public void dismissNow() { public void dismissNow() {
mAlarmUtilsHelper.cancelAlarm(checkNotNull(mAlarm)); mAlarmUtilsHelper.cancelAlarm(checkNotNull(mAlarm), true);
// cancelAlarm() should have turned off this alarm if appropriate // cancelAlarm() should have turned off this alarm if appropriate
mView.showEnabled(mAlarm.isEnabled()); mView.showEnabled(mAlarm.isEnabled());
} }

View File

@ -59,6 +59,7 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
Intent intent = new Intent(this, RingtoneService.class); Intent intent = new Intent(this, RingtoneService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE); bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
// TODO: Butterknife binding
Button snooze = (Button) findViewById(R.id.btn_snooze); Button snooze = (Button) findViewById(R.id.btn_snooze);
snooze.setOnClickListener(new View.OnClickListener() { snooze.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -80,10 +81,13 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
//super.onNewIntent(intent); // Not needed since no fragments hosted? //super.onNewIntent(intent); // Not needed since no fragments hosted?
if (mBound) { if (mBound) {
mBoundService.interrupt(); // prepare to notify the alarm was missed mBoundService.interrupt(); // prepare to notify the alarm was missed
/*
// Cannot rely on finish() to call onDestroy() on time before the activity is restarted. // Cannot rely on finish() to call onDestroy() on time before the activity is restarted.
unbindService(); unbindService();
// Calling recreate() would recreate this with its current intent, not the new intent passed in here. // Calling recreate() would recreate this with its current intent, not the new intent passed in here.
finish(); finish();
*/
dismiss(); // unbinds and finishes for you
startActivity(intent); startActivity(intent);
} }
} }
@ -131,15 +135,26 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi
private void snooze() { private void snooze() {
AlarmUtils.snoozeAlarm(this, mAlarm); 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, // 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. // 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() //unbindService(); // don't wait for finish() to call onDestroy()
finish(); //finish();
} }
private void dismiss() { private void dismiss() {
// TODO: Do we need to cancel the PendingIntent and the alarm in AlarmManager? // TODO: Do we really need to cancel the PendingIntent and the alarm in AlarmManager? They've
AlarmUtils.cancelAlarm(this, mAlarm); // 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() unbindService(); // don't wait for finish() to call onDestroy()
finish(); finish();
} }

View File

@ -89,8 +89,10 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc
AlarmUtils.snoozeAlarm(this, alarm); AlarmUtils.snoozeAlarm(this, alarm);
} }
// ============================== WARNING =================================== // ============================== WARNING ===================================
// DO NOT DO ANYTHING FOR ACTION_DISMISS. RingtoneActivity's current implementation of // I AM RECOMMENDING MYSELF TO NOT DO ANYTHING FOR ACTION_DISMISS.
// onServiceFinish() calls cancelAlarm for you! // 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())) { // /*else if (ACTION_DISMISS.equals(intent.getAction())) {
// AlarmUtils.cancelAlarm(this, alarm); // AlarmUtils.cancelAlarm(this, alarm);
// }*/ // }*/

View File

@ -68,7 +68,7 @@ public final class AlarmUtils {
Toast.makeText(context, message, Toast.LENGTH_LONG).show(); 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); Log.d(TAG, "Cancelling alarm " + a);
AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE); AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
@ -82,20 +82,21 @@ public final class AlarmUtils {
removeUpcomingAlarmNotification(c, a); 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()) { if (a.isSnoozed()) {
a.stopSnoozing(); a.stopSnoozing();
save(c); save(c); // TODO: not necessary?
} }
if (!a.hasRecurrence()) { if (!a.hasRecurrence()) {
a.setEnabled(false); a.setEnabled(false);
save(c); save(c); // TODO: not necessary?
}
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();
} }
// If service is not running, nothing happens // If service is not running, nothing happens
@ -108,7 +109,7 @@ public final class AlarmUtils {
public static void snoozeAlarm(Context c, Alarm a) { public static void snoozeAlarm(Context c, Alarm a) {
a.snooze(AlarmUtils.snoozeDuration(c)); a.snooze(AlarmUtils.snoozeDuration(c));
AlarmUtils.scheduleAlarm(c, a); AlarmUtils.scheduleAlarm(c, a);
save(c); save(c); // TODO: not necessary?
} }
public static void removeUpcomingAlarmNotification(Context c, Alarm a) { public static void removeUpcomingAlarmNotification(Context c, Alarm a) {