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.Notification;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
|
import android.app.PendingIntent;
|
||||||
import android.content.BroadcastReceiver;
|
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 com.philliphsu.clock2.editalarm.AlarmUtils;
|
||||||
import com.philliphsu.clock2.model.AlarmsRepository;
|
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.DateFormatUtils.formatTime;
|
||||||
import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
|
import static com.philliphsu.clock2.util.Preconditions.checkNotNull;
|
||||||
|
|
||||||
@ -19,6 +22,8 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
|
|||||||
= "com.philliphsu.clock2.action.CANCEL_NOTIFICATION";
|
= "com.philliphsu.clock2.action.CANCEL_NOTIFICATION";
|
||||||
public static final String ACTION_SHOW_SNOOZING
|
public static final String ACTION_SHOW_SNOOZING
|
||||||
= "com.philliphsu.clock2.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
|
public static final String EXTRA_ALARM_ID
|
||||||
= "com.philliphsu.clock2.extra.ALARM_ID";
|
= "com.philliphsu.clock2.extra.ALARM_ID";
|
||||||
|
|
||||||
@ -34,37 +39,39 @@ public class UpcomingAlarmReceiver extends BroadcastReceiver {
|
|||||||
nm.cancel(getClass().getName(), (int) id);
|
nm.cancel(getClass().getName(), (int) id);
|
||||||
} else {
|
} else {
|
||||||
Alarm alarm = checkNotNull(AlarmsRepository.getInstance(context).getItem(id));
|
Alarm alarm = checkNotNull(AlarmsRepository.getInstance(context).getItem(id));
|
||||||
if (ACTION_SHOW_SNOOZING.equals(intent.getAction())) {
|
if (ACTION_DISMISS_NOW.equals(intent.getAction())) {
|
||||||
if (!alarm.isSnoozed()) {
|
AlarmUtils.cancelAlarm(context, alarm);
|
||||||
throw new IllegalStateException("Can't show snoozing notif. if alarm not snoozed!");
|
} 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)
|
Intent in = new Intent(context, getClass())
|
||||||
: alarm.label();
|
.putExtra(EXTRA_ALARM_ID, id) // TOneverDO: cast to int
|
||||||
String text = context.getString(R.string.title_snoozing_until,
|
.setAction(ACTION_DISMISS_NOW);
|
||||||
formatTime(context, alarm.snoozingUntil()));
|
PendingIntent pi = PendingIntent.getBroadcast(context, (int) id, in, FLAG_ONE_SHOT);
|
||||||
Notification note = new NotificationCompat.Builder(context)
|
Notification note = new NotificationCompat.Builder(context)
|
||||||
.setSmallIcon(R.mipmap.ic_launcher) // TODO: alarm icon
|
.setSmallIcon(R.mipmap.ic_launcher) // TODO: alarm icon
|
||||||
.setContentTitle(title)
|
.setContentTitle(title)
|
||||||
.setContentText(text)
|
.setContentText(text)
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
|
.addAction(R.mipmap.ic_launcher, context.getString(R.string.dismiss_now), pi)
|
||||||
.build();
|
.build();
|
||||||
// todo actions
|
nm.notify(getClass().getName(), (int) id, note);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -50,6 +50,7 @@ public final class AlarmUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void cancelAlarm(Context c, Alarm a) {
|
public static void cancelAlarm(Context c, Alarm a) {
|
||||||
|
Log.d(TAG, "Cancelling alarm " + a);
|
||||||
AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
|
AlarmManager am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
|
||||||
|
|
||||||
PendingIntent pi = alarmIntent(c, a, true);
|
PendingIntent pi = alarmIntent(c, a, true);
|
||||||
|
|||||||
@ -27,7 +27,7 @@ public class DurationUtils {
|
|||||||
if (abbreviate) {
|
if (abbreviate) {
|
||||||
if (numHours == 0) {
|
if (numHours == 0) {
|
||||||
if (numMins == 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 {
|
} else {
|
||||||
res = R.string.abbrev_minutes;
|
res = R.string.abbrev_minutes;
|
||||||
}
|
}
|
||||||
@ -41,7 +41,7 @@ public class DurationUtils {
|
|||||||
} else {
|
} else {
|
||||||
if (numHours == 0) {
|
if (numHours == 0) {
|
||||||
if (numMins == 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) {
|
} else if (numMins == 1) {
|
||||||
res = R.string.minute;
|
res = R.string.minute;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user