Fixed NPE when sending broadcast to UpcomingAlarmReceiver to cancel the notification
This commit is contained in:
parent
a4b54bd935
commit
6064f4975a
@ -6,7 +6,6 @@ import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.util.Log;
|
||||
|
||||
import com.philliphsu.clock2.model.AlarmsRepository;
|
||||
|
||||
@ -27,51 +26,46 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
long id = intent.getLongExtra(EXTRA_ALARM_ID, -1);
|
||||
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);
|
||||
if (intent.getAction() != null) {
|
||||
// TODO: Verify that no java/project configuration is needed for strings to work in switch
|
||||
switch (intent.getAction()) {
|
||||
case ACTION_CANCEL_NOTIFICATION:
|
||||
nm.cancel(getClass().getName(), alarm.intId());
|
||||
break;
|
||||
case ACTION_SHOW_SNOOZING:
|
||||
if (!alarm.isSnoozed()) {
|
||||
throw new IllegalStateException("Can't show snoozing notif. if alarm not snoozed!");
|
||||
}
|
||||
String title = alarm.label().isEmpty()
|
||||
? context.getString(R.string.alarm)
|
||||
: alarm.label();
|
||||
String text = context.getString(R.string.title_snoozing_until,
|
||||
formatTime(context, alarm.snoozingUntil()));
|
||||
Notification note = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.mipmap.ic_launcher) // TODO: alarm icon
|
||||
.setContentTitle(title)
|
||||
.setContentText(text)
|
||||
.setOngoing(true)
|
||||
.build();
|
||||
// todo actions
|
||||
nm.notify(getClass().getName(), alarm.intId(), note);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (ACTION_CANCEL_NOTIFICATION.equals(intent.getAction())) {
|
||||
nm.cancel(getClass().getName(), (int) id);
|
||||
} else {
|
||||
// No intent action required for default behavior
|
||||
String text = formatTime(context, alarm.ringsAt());
|
||||
if (!alarm.label().isEmpty()) {
|
||||
text = alarm.label() + ", " + text;
|
||||
Alarm alarm = checkNotNull(AlarmsRepository.getInstance(context).getItem(id));
|
||||
if (ACTION_SHOW_SNOOZING.equals(intent.getAction())) {
|
||||
if (!alarm.isSnoozed()) {
|
||||
throw new IllegalStateException("Can't show snoozing notif. if alarm not snoozed!");
|
||||
}
|
||||
String title = alarm.label().isEmpty()
|
||||
? context.getString(R.string.alarm)
|
||||
: alarm.label();
|
||||
String text = context.getString(R.string.title_snoozing_until,
|
||||
formatTime(context, alarm.snoozingUntil()));
|
||||
Notification note = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.mipmap.ic_launcher) // TODO: alarm icon
|
||||
.setContentTitle(title)
|
||||
.setContentText(text)
|
||||
.setOngoing(true)
|
||||
.build();
|
||||
// todo actions
|
||||
nm.notify(getClass().getName(), alarm.intId(), note);
|
||||
} else {
|
||||
// No intent action required for default behavior
|
||||
String text = formatTime(context, alarm.ringsAt());
|
||||
if (!alarm.label().isEmpty()) {
|
||||
text = alarm.label() + ", " + text;
|
||||
}
|
||||
Notification note = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentTitle(context.getString(R.string.upcoming_alarm))
|
||||
.setContentText(text)
|
||||
.setOngoing(true)
|
||||
.build();
|
||||
// todo actions
|
||||
nm.notify(getClass().getName(), alarm.intId(), note);
|
||||
}
|
||||
Notification note = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.mipmap.ic_launcher)
|
||||
.setContentTitle(context.getString(R.string.upcoming_alarm))
|
||||
.setContentText(text)
|
||||
.setOngoing(true)
|
||||
.build();
|
||||
// todo actions
|
||||
nm.notify(getClass().getName(), alarm.intId(), note);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user