KeepAliveReceiver: clean up, categorize Android notification as alarm so it plays sound even in silent mode.
This commit is contained in:
parent
d423c8ae2a
commit
550ed57ca5
3 changed files with 24 additions and 14 deletions
|
@ -18,7 +18,6 @@ public class Constants {
|
||||||
public static final Integer carbsOnlyForCheckLimit = 10101010;
|
public static final Integer carbsOnlyForCheckLimit = 10101010;
|
||||||
|
|
||||||
public static final Integer notificationID = 556677;
|
public static final Integer notificationID = 556677;
|
||||||
public static final Integer urgentAlarmNotificationID = 556678;
|
|
||||||
|
|
||||||
public static final int hoursToKeepInDatabase = 72;
|
public static final int hoursToKeepInDatabase = 72;
|
||||||
public static final int daysToKeepHistoryInDatabase = 30;
|
public static final int daysToKeepHistoryInDatabase = 30;
|
||||||
|
|
|
@ -3,6 +3,9 @@ package info.nightscout.androidaps.plugins.Overview;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.media.AudioAttributes;
|
||||||
import android.media.RingtoneManager;
|
import android.media.RingtoneManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
|
@ -15,7 +18,6 @@ import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import info.nightscout.androidaps.Constants;
|
|
||||||
import info.nightscout.androidaps.MainApp;
|
import info.nightscout.androidaps.MainApp;
|
||||||
import info.nightscout.androidaps.R;
|
import info.nightscout.androidaps.R;
|
||||||
import info.nightscout.androidaps.Services.AlarmSoundService;
|
import info.nightscout.androidaps.Services.AlarmSoundService;
|
||||||
|
@ -75,17 +77,20 @@ public class NotificationStore {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void raiseSystemNotification(Notification n) {
|
private void raiseSystemNotification(Notification n) {
|
||||||
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
|
|
||||||
Context context = MainApp.instance().getApplicationContext();
|
Context context = MainApp.instance().getApplicationContext();
|
||||||
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
|
Bitmap largeIcon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.blueowl);
|
||||||
|
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
|
||||||
NotificationCompat.Builder notificationBuilder =
|
NotificationCompat.Builder notificationBuilder =
|
||||||
new NotificationCompat.Builder(context)
|
new NotificationCompat.Builder(context)
|
||||||
.setSmallIcon(R.drawable.notif_icon)
|
.setSmallIcon(R.drawable.ic_notification)
|
||||||
|
.setLargeIcon(largeIcon)
|
||||||
.setContentTitle("Urgent alarm")
|
.setContentTitle("Urgent alarm")
|
||||||
.setContentText(n.text)
|
.setContentText(n.text)
|
||||||
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
.setPriority(NotificationCompat.PRIORITY_HIGH)
|
||||||
.setSound(uri);
|
.setVibrate(new long[] { 1000, 1000, 1000, 1000})
|
||||||
mgr.notify(Constants.urgentAlarmNotificationID, notificationBuilder.build());
|
.setSound(sound, AudioAttributes.USAGE_ALARM);
|
||||||
|
mgr.notify(n.id, notificationBuilder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean remove(int id) {
|
public boolean remove(int id) {
|
||||||
|
|
|
@ -29,10 +29,14 @@ import info.nightscout.androidaps.plugins.Overview.Notification;
|
||||||
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
|
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
|
||||||
|
|
||||||
public class KeepAliveReceiver extends BroadcastReceiver {
|
public class KeepAliveReceiver extends BroadcastReceiver {
|
||||||
|
public static final long STATUS_UPDATE_FREQUENCY = 15 * 60 * 1000L;
|
||||||
private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class);
|
private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class);
|
||||||
|
|
||||||
private long nextPumpDisconnectedAlarm = System.currentTimeMillis() + 24 * 60 * 1000;
|
// TODO consider moving this into an Alarms plugin that works offline and can be configured
|
||||||
private long nextMissedReadingsAlarm = System.currentTimeMillis() + 24 * 60 * 1000;
|
// (e.g. override silent mode at night only)
|
||||||
|
public static final int ALARM_FREQUENCY = 25 * 60 * 1000;
|
||||||
|
private static long nextPumpDisconnectedAlarm = System.currentTimeMillis() + ALARM_FREQUENCY;
|
||||||
|
private static long nextMissedReadingsAlarm = System.currentTimeMillis() + ALARM_FREQUENCY;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent rIntent) {
|
public void onReceive(Context context, Intent rIntent) {
|
||||||
|
@ -49,11 +53,11 @@ public class KeepAliveReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
private void checkBg() {
|
private void checkBg() {
|
||||||
BgReading bgReading = DatabaseHelper.lastBg();
|
BgReading bgReading = DatabaseHelper.lastBg();
|
||||||
if (bgReading != null && bgReading.date + 25 * 60 * 1000 < System.currentTimeMillis()
|
if (bgReading != null && bgReading.date + ALARM_FREQUENCY < System.currentTimeMillis()
|
||||||
&& nextMissedReadingsAlarm < System.currentTimeMillis()) {
|
&& nextMissedReadingsAlarm < System.currentTimeMillis()) {
|
||||||
Notification n = new Notification(Notification.BG_READINGS_MISSED, MainApp.sResources.getString(R.string.missed_bg_readings), Notification.URGENT);
|
Notification n = new Notification(Notification.BG_READINGS_MISSED, MainApp.sResources.getString(R.string.missed_bg_readings), Notification.URGENT);
|
||||||
n.soundId = R.raw.alarm;
|
n.soundId = R.raw.alarm;
|
||||||
nextMissedReadingsAlarm = System.currentTimeMillis() + 24 * 60 * 1000;
|
nextMissedReadingsAlarm = System.currentTimeMillis() + ALARM_FREQUENCY;
|
||||||
MainApp.bus().post(new EventNewNotification(n));
|
MainApp.bus().post(new EventNewNotification(n));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,15 +68,17 @@ public class KeepAliveReceiver extends BroadcastReceiver {
|
||||||
if (pump != null && profile != null && profile.getBasal() != null) {
|
if (pump != null && profile != null && profile.getBasal() != null) {
|
||||||
Date lastConnection = pump.lastDataTime();
|
Date lastConnection = pump.lastDataTime();
|
||||||
|
|
||||||
boolean isStatusOutdated = lastConnection.getTime() + 15 * 60 * 1000L < System.currentTimeMillis();
|
boolean isStatusOutdated = lastConnection.getTime() + STATUS_UPDATE_FREQUENCY < System.currentTimeMillis();
|
||||||
boolean isBasalOutdated = Math.abs(profile.getBasal() - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep;
|
boolean isBasalOutdated = Math.abs(profile.getBasal() - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep;
|
||||||
|
|
||||||
|
boolean alarmTimeoutExpired = lastConnection.getTime() + ALARM_FREQUENCY < System.currentTimeMillis();
|
||||||
|
boolean nextAlarmOccurrenceReached = nextPumpDisconnectedAlarm < System.currentTimeMillis();
|
||||||
|
|
||||||
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
|
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
|
||||||
if (isStatusOutdated && lastConnection.getTime() + 25 * 60 * 1000 < System.currentTimeMillis()
|
if (isStatusOutdated && alarmTimeoutExpired && nextAlarmOccurrenceReached) {
|
||||||
&& nextPumpDisconnectedAlarm < System.currentTimeMillis()) {
|
|
||||||
Notification n = new Notification(Notification.PUMP_UNREACHABLE, MainApp.sResources.getString(R.string.pump_unreachable), Notification.URGENT);
|
Notification n = new Notification(Notification.PUMP_UNREACHABLE, MainApp.sResources.getString(R.string.pump_unreachable), Notification.URGENT);
|
||||||
n.soundId = R.raw.alarm;
|
n.soundId = R.raw.alarm;
|
||||||
nextPumpDisconnectedAlarm = System.currentTimeMillis() + 24 * 60 * 1000;
|
nextPumpDisconnectedAlarm = System.currentTimeMillis() + ALARM_FREQUENCY;
|
||||||
MainApp.bus().post(new EventNewNotification(n));
|
MainApp.bus().post(new EventNewNotification(n));
|
||||||
} else if (SP.getBoolean("syncprofiletopump", false) && !pump.isThisProfileSet(profile)) {
|
} else if (SP.getBoolean("syncprofiletopump", false) && !pump.isThisProfileSet(profile)) {
|
||||||
Thread t = new Thread(new Runnable() {
|
Thread t = new Thread(new Runnable() {
|
||||||
|
|
Loading…
Reference in a new issue