From 3e7c4b13506a8125d75f092e8423bc8621c1e4f4 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 21 Jun 2017 07:28:04 +0200 Subject: [PATCH] NS high & low alarms with sound --- .../nightscout/androidaps/MainActivity.java | 3 -- .../Services/AlarmSoundService.java | 6 ++- .../androidaps/Services/DataService.java | 18 +++---- .../NSClientInternal/data/NSStatus.java | 33 ++++++++++++- .../plugins/Overview/Notification.java | 46 ++++++++++++++++++ .../plugins/Overview/NotificationStore.java | 15 ++++++ .../androidaps/receivers/NSAlarmReceiver.java | 3 +- .../res/raw/{staledataalarm.mp3 => alarm.mp3} | Bin .../res/raw/{bgalarm.mp3 => urgentalarm.mp3} | Bin 9 files changed, 104 insertions(+), 20 deletions(-) rename app/src/main/res/raw/{staledataalarm.mp3 => alarm.mp3} (100%) rename app/src/main/res/raw/{bgalarm.mp3 => urgentalarm.mp3} (100%) diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index f1e80abeba..c46a13c91e 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -80,9 +80,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe registerBus(); setUpTabs(false); - Intent alarm = new Intent(this, AlarmSoundService.class); - alarm.putExtra("soundid", R.raw.staledataalarm); - //startService(alarm); } @Subscribe diff --git a/app/src/main/java/info/nightscout/androidaps/Services/AlarmSoundService.java b/app/src/main/java/info/nightscout/androidaps/Services/AlarmSoundService.java index 79b647b5f5..41587e79b9 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/AlarmSoundService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/AlarmSoundService.java @@ -18,7 +18,7 @@ public class AlarmSoundService extends Service { private static Logger log = LoggerFactory.getLogger(AlarmSoundService.class); MediaPlayer player; - int resourceId = R.raw.bgalarm; + int resourceId = R.raw.error; public AlarmSoundService() { } @@ -36,9 +36,11 @@ public class AlarmSoundService extends Service { } public int onStartCommand(Intent intent, int flags, int startId) { + if (player != null && player.isPlaying()) + player.stop(); log.debug("onStartCommand"); if (intent != null && intent.hasExtra("soundid")) - resourceId = intent.getIntExtra("soundid", R.raw.bgalarm); + resourceId = intent.getIntExtra("soundid", R.raw.error); player = new MediaPlayer(); AssetFileDescriptor afd = MainApp.sResources.openRawResourceFd(resourceId); diff --git a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java index 33b56d125f..4d0685a00f 100644 --- a/app/src/main/java/info/nightscout/androidaps/Services/DataService.java +++ b/app/src/main/java/info/nightscout/androidaps/Services/DataService.java @@ -262,18 +262,12 @@ public class DataService extends IntentService { NSStatus.getInstance().setData(statusJson); if (Config.logIncommingData) log.debug("Received status: " + statusJson.toString()); - if (statusJson.has("settings")) { - JSONObject settings = statusJson.getJSONObject("settings"); - if (settings.has("thresholds")) { - JSONObject thresholds = settings.getJSONObject("thresholds"); - if (thresholds.has("bgTargetTop")) { - OverviewPlugin.bgTargetHigh = thresholds.getDouble("bgTargetTop"); - } - if (thresholds.has("bgTargetBottom")) { - OverviewPlugin.bgTargetLow = thresholds.getDouble("bgTargetBottom"); - } - } - } + Double targetHigh = NSStatus.getInstance().getThreshold("bgTargetTop"); + Double targetlow = NSStatus.getInstance().getThreshold("bgTargetBottom"); + if (targetHigh != null) + OverviewPlugin.bgTargetHigh = targetHigh; + if (targetlow != null) + OverviewPlugin.bgTargetLow = targetlow; } catch (JSONException e) { e.printStackTrace(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSStatus.java b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSStatus.java index 3261e32318..e89dee8db5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSStatus.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/NSClientInternal/data/NSStatus.java @@ -1,5 +1,7 @@ package info.nightscout.androidaps.plugins.NSClientInternal.data; +import android.support.annotation.Nullable; + import org.json.JSONException; import org.json.JSONObject; @@ -104,9 +106,10 @@ public class NSStatus { return instance; } - private JSONObject data; + private JSONObject data = null; - public NSStatus() {} + public NSStatus() { + } public NSStatus setData(JSONObject obj) { this.data = obj; @@ -157,6 +160,32 @@ public class NSStatus { return getStringOrNull("activeProfile"); } + // "bgHigh": 252, + // "bgTargetTop": 180, + // "bgTargetBottom": 72, + // "bgLow": 71 + @Nullable + public Double getThreshold(String what) { + try { + if (data == null) + return null; + String settings = getSettings(); + if (settings != null) { + JSONObject settingsO = new JSONObject(settings); + if (settingsO.has("thresholds")) { + JSONObject tObject = settingsO.getJSONObject("thresholds"); + if (tObject.has(what)) { + Double result = tObject.getDouble(what); + return result; + } + } + } + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } + private String getStringOrNull(String key) { String ret = null; if (data.has(key)) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java index 23e72a0446..bfa1f6e3f5 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/Notification.java @@ -2,7 +2,12 @@ package info.nightscout.androidaps.plugins.Overview; import java.util.Date; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.plugins.NSClientInternal.data.NSAlarm; +import info.nightscout.androidaps.plugins.NSClientInternal.data.NSStatus; +import info.nightscout.utils.SP; /** * Created by mike on 03.12.2016. @@ -44,6 +49,7 @@ public class Notification { public Date validTo = new Date(0); public NSAlarm nsAlarm = null; + public Integer soundId = null; public Notification() { } @@ -87,12 +93,52 @@ public class Notification { this.id = NSALARM; this.level = NORMAL; this.text = nsAlarm.getTile(); + if (isAlarmForLow() && SP.getBoolean(R.string.key_nsalarm_low, false) || isAlarmForHigh() && SP.getBoolean(R.string.key_nsalarm_high, false)) + this.soundId = R.raw.alarm; break; case 2: this.id = NSURGENTALARM; this.level = URGENT; this.text = nsAlarm.getTile(); + if (isAlarmForLow() && SP.getBoolean(R.string.key_nsalarm_urgent_low, false) || isAlarmForHigh() && SP.getBoolean(R.string.key_nsalarm_urgent_high, false)) + this.soundId = R.raw.urgentalarm; break; } } + + public boolean isEnabled() { + if (nsAlarm == null) + return true; + if (level == ANNOUNCEMENT) + return true; + if (level == NORMAL && isAlarmForLow() && SP.getBoolean(R.string.key_nsalarm_low, false) || isAlarmForHigh() && SP.getBoolean(R.string.key_nsalarm_high, false)) + return true; + if (level == URGENT && isAlarmForLow() && SP.getBoolean(R.string.key_nsalarm_urgent_low, false) || isAlarmForHigh() && SP.getBoolean(R.string.key_nsalarm_urgent_high, false)) + return true; + return false; + } + + boolean isAlarmForLow() { + BgReading bgReading = MainApp.getDbHelper().lastBg(); + if (bgReading == null) + return false; + Double threshold = NSStatus.getInstance().getThreshold("bgTargetTop"); + if (threshold == null) + return false; + if (bgReading.value <= threshold) + return true; + return false; + } + + boolean isAlarmForHigh() { + BgReading bgReading = MainApp.getDbHelper().lastBg(); + if (bgReading == null) + return false; + Double threshold = NSStatus.getInstance().getThreshold("bgTargetBottom"); + if (threshold == null) + return false; + if (bgReading.value >= threshold) + return true; + return false; + } } 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 8a046c59e5..b836f99be5 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,5 +1,7 @@ package info.nightscout.androidaps.plugins.Overview; +import android.content.Intent; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -9,6 +11,10 @@ import java.util.Comparator; import java.util.Date; import java.util.List; +import info.nightscout.androidaps.MainApp; +import info.nightscout.androidaps.R; +import info.nightscout.androidaps.Services.AlarmSoundService; + /** * Created by mike on 03.12.2016. @@ -41,6 +47,11 @@ public class NotificationStore { return; } } + 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); Collections.sort(store, new NotificationComparator()); } @@ -48,6 +59,10 @@ public class NotificationStore { public boolean remove(int id) { for (int i = 0; i < store.size(); i++) { if (get(i).id == id) { + if (get(i).soundId != null) { + Intent alarm = new Intent(MainApp.instance().getApplicationContext(), AlarmSoundService.class); + MainApp.instance().stopService(alarm); + } store.remove(i); return true; } diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/NSAlarmReceiver.java b/app/src/main/java/info/nightscout/androidaps/receivers/NSAlarmReceiver.java index 41f8eb5297..bff2e6695a 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/NSAlarmReceiver.java +++ b/app/src/main/java/info/nightscout/androidaps/receivers/NSAlarmReceiver.java @@ -36,7 +36,8 @@ public class NSAlarmReceiver extends BroadcastReceiver { case Intents.ACTION_ALARM: case Intents.ACTION_URGENT_ALARM: Notification notification = new Notification(nsAlarm); - MainApp.bus().post(new EventNewNotification(notification)); + if (notification.isEnabled()) + MainApp.bus().post(new EventNewNotification(notification)); break; case Intents.ACTION_CLEAR_ALARM: MainApp.bus().post(new EventDismissNotification(Notification.NSALARM)); diff --git a/app/src/main/res/raw/staledataalarm.mp3 b/app/src/main/res/raw/alarm.mp3 similarity index 100% rename from app/src/main/res/raw/staledataalarm.mp3 rename to app/src/main/res/raw/alarm.mp3 diff --git a/app/src/main/res/raw/bgalarm.mp3 b/app/src/main/res/raw/urgentalarm.mp3 similarity index 100% rename from app/src/main/res/raw/bgalarm.mp3 rename to app/src/main/res/raw/urgentalarm.mp3