Added dismiss now action to upcoming alarm notification
This commit is contained in:
parent
24a9d5e2d6
commit
9441b5da10
@ -2,13 +2,16 @@ package com.philliphsu.clock2;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
|
||||
import com.philliphsu.clock2.editalarm.AlarmUtils;
|
||||
import com.philliphsu.clock2.model.AlarmsRepository;
|
||||
|
||||
import static android.app.PendingIntent.FLAG_ONE_SHOT;
|
||||
import static com.philliphsu.clock2.util.DateFormatUtils.formatTime;
|
||||
import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
|
||||
|
||||
@ -19,6 +22,8 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
|
||||
= "com.philliphsu.clock2.action.CANCEL_NOTIFICATION";
|
||||
public static final String ACTION_SHOW_SNOOZING
|
||||
= "com.philliphsu.clock2.action.SHOW_SNOOZING";
|
||||
/*TOneverDO: not private*/ private static final String ACTION_DISMISS_NOW
|
||||
= "com.philliphsu.clock2.action.DISMISS_NOW";
|
||||
public static final String EXTRA_ALARM_ID
|
||||
= "com.philliphsu.clock2.extra.ALARM_ID";
|
||||
|
||||
@ -34,37 +39,39 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
|
||||
nm.cancel(getClass().getName(), (int) id);
|
||||
} else {
|
||||
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!");
|
||||
if (ACTION_DISMISS_NOW.equals(intent.getAction())) {
|
||||
AlarmUtils.cancelAlarm(context, alarm);
|
||||
} else {
|
||||
// Prepare notification
|
||||
String title;
|
||||
String text;
|
||||
if (ACTION_SHOW_SNOOZING.equals(intent.getAction())) {
|
||||
if (!alarm.isSnoozed())
|
||||
throw new IllegalStateException("Can't show snoozing notif. if alarm not snoozed!");
|
||||
title = alarm.label().isEmpty() ? context.getString(R.string.alarm) : alarm.label();
|
||||
text = context.getString(R.string.title_snoozing_until,
|
||||
formatTime(context, alarm.snoozingUntil()));
|
||||
} else {
|
||||
// No intent action required for default behavior
|
||||
title = context.getString(R.string.upcoming_alarm);
|
||||
text = formatTime(context, alarm.ringsAt());
|
||||
if (!alarm.label().isEmpty()) {
|
||||
text = alarm.label() + ", " + text;
|
||||
}
|
||||
}
|
||||
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()));
|
||||
|
||||
Intent in = new Intent(context, getClass())
|
||||
.putExtra(EXTRA_ALARM_ID, id) // TOneverDO: cast to int
|
||||
.setAction(ACTION_DISMISS_NOW);
|
||||
PendingIntent pi = PendingIntent.getBroadcast(context, (int) id, in, FLAG_ONE_SHOT);
|
||||
Notification note = new NotificationCompat.Builder(context)
|
||||
.setSmallIcon(R.mipmap.ic_launcher) // TODO: alarm icon
|
||||
.setContentTitle(title)
|
||||
.setContentText(text)
|
||||
.setOngoing(true)
|
||||
.addAction(R.mipmap.ic_launcher, context.getString(R.string.dismiss_now), pi)
|
||||
.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);
|
||||
nm.notify(getClass().getName(), (int) id, note);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,6 +50,7 @@ public final class AlarmUtils {
|
||||
}
|
||||
|
||||
public static void cancelAlarm(Context c, Alarm a) {
|
||||
Log.d(TAG, "Cancelling alarm " + a);
|
||||
AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
|
||||
|
||||
PendingIntent pi = alarmIntent(c, a, true);
|
||||
|
||||
@ -27,7 +27,7 @@ public class DurationUtils {
|
||||
if (abbreviate) {
|
||||
if (numHours == 0) {
|
||||
if (numMins == 0) {
|
||||
res = R.string.abbrev_less_than_one_minute; // TODO: verify formatArgs can be passed in but are ignored
|
||||
res = R.string.abbrev_less_than_one_minute;
|
||||
} else {
|
||||
res = R.string.abbrev_minutes;
|
||||
}
|
||||
@ -41,7 +41,7 @@ public class DurationUtils {
|
||||
} else {
|
||||
if (numHours == 0) {
|
||||
if (numMins == 0) {
|
||||
res = R.string.less_than_one_minute; // TODO: verify formatArgs can be passed in but are ignored
|
||||
res = R.string.less_than_one_minute;
|
||||
} else if (numMins == 1) {
|
||||
res = R.string.minute;
|
||||
} else {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user