Fixed NPE when sending broadcast to UpcomingAlarmReceiver to cancel the notification

This commit is contained in:
Phillip Hsu 2016-06-03 20:59:01 -07:00
parent a4b54bd935
commit 6064f4975a

View File

@ -6,7 +6,6 @@ import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.philliphsu.clock2.model.AlarmsRepository; import com.philliphsu.clock2.model.AlarmsRepository;
@ -27,17 +26,15 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
long id = intent.getLongExtra(EXTRA_ALARM_ID, -1); long id = intent.getLongExtra(EXTRA_ALARM_ID, -1);
if (id < 0) { if (id < 0) {
Log.e(TAG, "No alarm id received"); throw new IllegalStateException("No alarm id received");
} }
Alarm alarm = checkNotNull(AlarmsRepository.getInstance(context).getItem(id));
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (intent.getAction() != null) { if (ACTION_CANCEL_NOTIFICATION.equals(intent.getAction())) {
// TODO: Verify that no java/project configuration is needed for strings to work in switch nm.cancel(getClass().getName(), (int) id);
switch (intent.getAction()) { } else {
case ACTION_CANCEL_NOTIFICATION: Alarm alarm = checkNotNull(AlarmsRepository.getInstance(context).getItem(id));
nm.cancel(getClass().getName(), alarm.intId()); if (ACTION_SHOW_SNOOZING.equals(intent.getAction())) {
break;
case ACTION_SHOW_SNOOZING:
if (!alarm.isSnoozed()) { if (!alarm.isSnoozed()) {
throw new IllegalStateException("Can't show snoozing notif. if alarm not snoozed!"); throw new IllegalStateException("Can't show snoozing notif. if alarm not snoozed!");
} }
@ -54,10 +51,6 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
.build(); .build();
// todo actions // todo actions
nm.notify(getClass().getName(), alarm.intId(), note); nm.notify(getClass().getName(), alarm.intId(), note);
break;
default:
break;
}
} else { } else {
// No intent action required for default behavior // No intent action required for default behavior
String text = formatTime(context, alarm.ringsAt()); String text = formatTime(context, alarm.ringsAt());
@ -75,3 +68,4 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
} }
} }
} }
}