From d990d59ddfa8240686eb2f2e7f05d63db4b43589 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Mon, 6 Nov 2017 14:09:03 +0100 Subject: [PATCH] Dismiss notification service --- .../Overview/DismissNotificationService.java | 33 +++++++++++++++++++ .../plugins/Overview/NotificationStore.java | 29 +++++++++------- 2 files changed, 51 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Overview/DismissNotificationService.java diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/DismissNotificationService.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/DismissNotificationService.java new file mode 100644 index 0000000000..2e5e665425 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/DismissNotificationService.java @@ -0,0 +1,33 @@ +package info.nightscout.androidaps.plugins.Overview; + +import android.app.IntentService; +import android.app.PendingIntent; +import android.content.Intent; +import android.support.annotation.Nullable; + +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification; +import info.nightscout.utils.SafeParse; + +public class DismissNotificationService extends IntentService { + + /** + * Creates an IntentService. Invoked by your subclass's constructor. + * + * @param name Used to name the worker thread, important only for debugging. + */ + public DismissNotificationService(String name) { + super(name); + } + + @Override + protected void onHandleIntent(@Nullable Intent intent) { + MainApp.bus().post(new EventDismissNotification(intent.getIntExtra("alertID", -1))); + } + + public static PendingIntent deleteIntent(int id){ + Intent intent = new Intent(MainApp.instance(), DismissNotificationService.class); + intent.putExtra("alertID", id); + return PendingIntent.getService(MainApp.instance(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/NotificationStore.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/NotificationStore.java index 58978eeead..3956f29365 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/NotificationStore.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/NotificationStore.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.Overview; import android.app.NotificationManager; +import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; @@ -22,7 +23,6 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.Services.AlarmSoundService; import info.nightscout.androidaps.plugins.Wear.WearPlugin; -//Added by Rumen for snooze time import info.nightscout.utils.SP; /** @@ -57,20 +57,24 @@ public class NotificationStore { } } + store.add(n); + if (SP.getBoolean(MainApp.sResources.getString(R.string.key_raise_urgent_alarms_as_android_notification), false) && n.level == Notification.URGENT) { raiseSystemNotification(n); - } else if (n.soundId != null) { - Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class); - alarm.putExtra("soundid", n.soundId); - MainApp.instance().startService(alarm); - } + } else { + if (n.soundId != null) { + Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class); + alarm.putExtra("soundid", n.soundId); + MainApp.instance().startService(alarm); + } - store.add(n); + //Only pipe through to wear if no system notification is raised (should show on wear anyways) + WearPlugin wearPlugin = MainApp.getSpecificPlugin(WearPlugin.class); + if(wearPlugin!= null && wearPlugin.isEnabled()) { + wearPlugin.overviewNotification(n.id, "OverviewNotification:\n" + n.text); + } - WearPlugin wearPlugin = MainApp.getSpecificPlugin(WearPlugin.class); - if(wearPlugin!= null && wearPlugin.isEnabled()) { - wearPlugin.overviewNotification(n.id, "OverviewNotification:\n" + n.text); } Collections.sort(store, new NotificationComparator()); @@ -89,10 +93,12 @@ public class NotificationStore { .setContentText(n.text) .setPriority(NotificationCompat.PRIORITY_HIGH) .setVibrate(new long[] { 1000, 1000, 1000, 1000}) - .setSound(sound, AudioAttributes.USAGE_ALARM); + .setSound(sound, AudioAttributes.USAGE_ALARM) + .setDeleteIntent(DismissNotificationService.deleteIntent(n.id)); mgr.notify(n.id, notificationBuilder.build()); } + public boolean remove(int id) { for (int i = 0; i < store.size(); i++) { if (get(i).id == id) { @@ -131,3 +137,4 @@ public class NotificationStore { } } } +