KeepAliveReceiver: clean up, categorize Android notification as alarm so it plays sound even in silent mode.

This commit is contained in:
Johannes Mockenhaupt 2017-10-30 16:20:08 +01:00
parent d423c8ae2a
commit 550ed57ca5
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1
3 changed files with 24 additions and 14 deletions

View file

@ -18,7 +18,6 @@ public class Constants {
public static final Integer carbsOnlyForCheckLimit = 10101010;
public static final Integer notificationID = 556677;
public static final Integer urgentAlarmNotificationID = 556678;
public static final int hoursToKeepInDatabase = 72;
public static final int daysToKeepHistoryInDatabase = 30;

View file

@ -3,6 +3,9 @@ package info.nightscout.androidaps.plugins.Overview;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioAttributes;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
@ -15,7 +18,6 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import info.nightscout.androidaps.Constants;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.Services.AlarmSoundService;
@ -75,17 +77,20 @@ public class NotificationStore {
}
private void raiseSystemNotification(Notification n) {
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
Context context = MainApp.instance().getApplicationContext();
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 =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notif_icon)
.setSmallIcon(R.drawable.ic_notification)
.setLargeIcon(largeIcon)
.setContentTitle("Urgent alarm")
.setContentText(n.text)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(uri);
mgr.notify(Constants.urgentAlarmNotificationID, notificationBuilder.build());
.setVibrate(new long[] { 1000, 1000, 1000, 1000})
.setSound(sound, AudioAttributes.USAGE_ALARM);
mgr.notify(n.id, notificationBuilder.build());
}
public boolean remove(int id) {

View file

@ -29,10 +29,14 @@ import info.nightscout.androidaps.plugins.Overview.Notification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
public class KeepAliveReceiver extends BroadcastReceiver {
public static final long STATUS_UPDATE_FREQUENCY = 15 * 60 * 1000L;
private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class);
private long nextPumpDisconnectedAlarm = System.currentTimeMillis() + 24 * 60 * 1000;
private long nextMissedReadingsAlarm = System.currentTimeMillis() + 24 * 60 * 1000;
// TODO consider moving this into an Alarms plugin that works offline and can be configured
// (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
public void onReceive(Context context, Intent rIntent) {
@ -49,11 +53,11 @@ public class KeepAliveReceiver extends BroadcastReceiver {
private void checkBg() {
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()) {
Notification n = new Notification(Notification.BG_READINGS_MISSED, MainApp.sResources.getString(R.string.missed_bg_readings), Notification.URGENT);
n.soundId = R.raw.alarm;
nextMissedReadingsAlarm = System.currentTimeMillis() + 24 * 60 * 1000;
nextMissedReadingsAlarm = System.currentTimeMillis() + ALARM_FREQUENCY;
MainApp.bus().post(new EventNewNotification(n));
}
}
@ -64,15 +68,17 @@ public class KeepAliveReceiver extends BroadcastReceiver {
if (pump != null && profile != null && profile.getBasal() != null) {
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 alarmTimeoutExpired = lastConnection.getTime() + ALARM_FREQUENCY < System.currentTimeMillis();
boolean nextAlarmOccurrenceReached = nextPumpDisconnectedAlarm < System.currentTimeMillis();
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
if (isStatusOutdated && lastConnection.getTime() + 25 * 60 * 1000 < System.currentTimeMillis()
&& nextPumpDisconnectedAlarm < System.currentTimeMillis()) {
if (isStatusOutdated && alarmTimeoutExpired && nextAlarmOccurrenceReached) {
Notification n = new Notification(Notification.PUMP_UNREACHABLE, MainApp.sResources.getString(R.string.pump_unreachable), Notification.URGENT);
n.soundId = R.raw.alarm;
nextPumpDisconnectedAlarm = System.currentTimeMillis() + 24 * 60 * 1000;
nextPumpDisconnectedAlarm = System.currentTimeMillis() + ALARM_FREQUENCY;
MainApp.bus().post(new EventNewNotification(n));
} else if (SP.getBoolean("syncprofiletopump", false) && !pump.isThisProfileSet(profile)) {
Thread t = new Thread(new Runnable() {