From ffb6f40fd08fdce5dfa658ff69fbdf4c4ea5ea1a Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Tue, 7 Jun 2016 19:58:41 -0700 Subject: [PATCH] Fixed active alarm notification dismiss bug found in RingtoneService --- .../clock2/ringtone/RingtoneActivity.java | 2 +- .../clock2/ringtone/RingtoneService.java | 23 +++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneActivity.java b/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneActivity.java index 6a950b3..d3f7e83 100644 --- a/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneActivity.java +++ b/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneActivity.java @@ -132,7 +132,7 @@ public class RingtoneActivity extends AppCompatActivity implements RingtoneServi private void snooze() { AlarmUtils.snoozeAlarm(this, mAlarm); // 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 immediately. + // 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(); } diff --git a/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneService.java b/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneService.java index c295932..47d7f74 100644 --- a/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneService.java +++ b/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneService.java @@ -77,19 +77,24 @@ public class RingtoneService extends Service { // TODO: abstract this, make subc // stopService(Intent) [or stopSelf()] is called, regardless of whether any clients are connected to it." // I have found the regardless part does not apply here. You MUST also unbind any clients from this service // at the same time you stop this service! - long id = intent.getLongExtra(EXTRA_ITEM_ID, -1); - if (id < 0) - throw new IllegalStateException("No item id set"); - Alarm alarm = checkNotNull(AlarmsRepository.getInstance(this).getItem(id)); + String action = intent.getAction(); + if (!action.equals(ACTION_SNOOZE) && !action.equals(ACTION_DISMISS)) + throw new UnsupportedOperationException(); if (ACTION_SNOOZE.equals(intent.getAction())) { + long id = intent.getLongExtra(EXTRA_ITEM_ID, -1); + if (id < 0) + throw new IllegalStateException("No item id set"); + Alarm alarm = checkNotNull(AlarmsRepository.getInstance(this).getItem(id)); AlarmUtils.snoozeAlarm(this, alarm); - } else if (ACTION_DISMISS.equals(intent.getAction())) { - AlarmUtils.cancelAlarm(this, alarm); - } else { - throw new UnsupportedOperationException(); } - + // ============================== WARNING =================================== + // DO NOT DO ANYTHING FOR ACTION_DISMISS. RingtoneActivity's current implementation of + // onServiceFinish() calls cancelAlarm for you! + // /*else if (ACTION_DISMISS.equals(intent.getAction())) { + // AlarmUtils.cancelAlarm(this, alarm); + // }*/ + // ========================================================================== stopSelf(startId); if (mRingtoneCallback != null) { mRingtoneCallback.onServiceFinish(); // tell client to unbind from this service