diff --git a/app/src/main/java/com/philliphsu/clock2/alarms/AlarmActivity.java b/app/src/main/java/com/philliphsu/clock2/alarms/AlarmActivity.java index 38d48be..4b109ad 100644 --- a/app/src/main/java/com/philliphsu/clock2/alarms/AlarmActivity.java +++ b/app/src/main/java/com/philliphsu/clock2/alarms/AlarmActivity.java @@ -2,6 +2,7 @@ package com.philliphsu.clock2.alarms; import android.app.Notification; import android.app.NotificationManager; +import android.content.Intent; import android.os.Bundle; import android.support.v4.app.NotificationCompat; import android.view.ViewGroup; @@ -32,9 +33,25 @@ public class AlarmActivity extends RingtoneActivity { @Override public void finish() { super.finish(); + // If the presently ringing alarm is about to be superseded by a successive alarm, + // this, unfortunately, will cancel the missed alarm notification for the presently + // ringing alarm. + // + // A workaround is to override onNewIntent() and post the missed alarm notification again, + // AFTER calling through to its base implementation, because it calls finish(). mNotificationManager.cancel(TAG, getRingingObject().getIntId()); } + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + // -------------- TOneverDO: precede super --------------- + // Even though the base implementation calls finish() on this instance and starts a new + // instance, this instance will still be alive with all of its member state intact at + // this point. So this notification will still refer to the Alarm that was just missed. + postMissedAlarmNote(); + } + @Override protected Class getRingtoneServiceClass() { return AlarmRingtoneService.class; @@ -102,7 +119,10 @@ public class AlarmActivity extends RingtoneActivity { @Override protected void showAutoSilenced() { super.showAutoSilenced(); - // Post notification that alarm was missed + postMissedAlarmNote(); + } + + private void postMissedAlarmNote() { String alarmTime = DateFormatUtils.formatTime(this, getRingingObject().hour(), getRingingObject().minutes()); Notification note = new NotificationCompat.Builder(this) 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 f8ade06..4b286b7 100644 --- a/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneActivity.java +++ b/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneActivity.java @@ -1,6 +1,5 @@ package com.philliphsu.clock2.ringtone; -import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -130,10 +129,6 @@ public abstract class RingtoneActivity extends AppCompatAc @Override protected void onNewIntent(Intent intent) { //super.onNewIntent(intent); // Not needed since no fragments hosted? - - // TODO: Do we need this anymore? I think the broadcast that calls through to - // this was only sent from EditAlarmActivity? - // Notifies alarm missed and stops the service LocalBroadcastHelper.sendBroadcast(this, RingtoneService.ACTION_NOTIFY_MISSED); finish(); @@ -180,7 +175,6 @@ public abstract class RingtoneActivity extends AppCompatAc @OnClick(R.id.ok) public void finish() { super.finish(); - NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } public static boolean isAlive() { 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 bc516ed..f1233c0 100644 --- a/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneService.java +++ b/app/src/main/java/com/philliphsu/clock2/ringtone/RingtoneService.java @@ -3,6 +3,8 @@ package com.philliphsu.clock2.ringtone; import android.app.Notification; import android.app.PendingIntent; import android.app.Service; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.media.Ringtone; @@ -57,17 +59,16 @@ public abstract class RingtoneService extends Service { } }; - // Pretty sure this won't ever get called anymore... b/c EditAlarmActivity, the only component - // that sends such a broadcast, is deprecated. -// private final BroadcastReceiver mNotifyMissedReceiver = new BroadcastReceiver() { -// @Override -// public void onReceive(Context context, Intent intent) { -// // TODO: Do we need to call mAlarmController.cancelAlarm()? -// onAutoSilenced(); -// stopSelf(); -// // Activity finishes itself -// } -// }; +// // Pretty sure this won't ever get called anymore... b/c EditAlarmActivity, the only component +// // that sends such a broadcast, is deprecated. + private final BroadcastReceiver mNotifyMissedReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + onAutoSilenced(); + stopSelf(); + // Activity finishes itself + } + }; /** * Callback invoked when this Service is stopping and the corresponding @@ -137,7 +138,7 @@ public abstract class RingtoneService extends Service { super.onCreate(); // Pretty sure this won't ever get called anymore... b/c EditAlarmActivity, the only component // that sends such a broadcast, is deprecated. -// LocalBroadcastHelper.registerReceiver(this, mNotifyMissedReceiver, ACTION_NOTIFY_MISSED); + LocalBroadcastHelper.registerReceiver(this, mNotifyMissedReceiver, ACTION_NOTIFY_MISSED); } @Override @@ -152,7 +153,7 @@ public abstract class RingtoneService extends Service { stopForeground(true); // Pretty sure this won't ever get called anymore... b/c EditAlarmActivity, the only component // that sends such a broadcast, is deprecated. -// LocalBroadcastHelper.unregisterReceiver(this, mNotifyMissedReceiver); + LocalBroadcastHelper.unregisterReceiver(this, mNotifyMissedReceiver); } @Override