2016-07-07 00:56:31 +02:00
|
|
|
package info.nightscout.androidaps.receivers;
|
|
|
|
|
|
|
|
import android.app.AlarmManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.PowerManager;
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-08-02 15:43:21 +02:00
|
|
|
import java.util.Date;
|
|
|
|
|
2016-07-07 00:56:31 +02:00
|
|
|
import info.nightscout.androidaps.Constants;
|
2016-07-18 12:02:33 +02:00
|
|
|
import info.nightscout.androidaps.MainApp;
|
2018-07-30 15:46:20 +02:00
|
|
|
import info.nightscout.androidaps.data.Profile;
|
2018-03-28 19:33:41 +02:00
|
|
|
import info.nightscout.androidaps.events.EventProfileSwitchChange;
|
2017-01-28 23:45:14 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.PumpInterface;
|
2018-07-30 15:46:20 +02:00
|
|
|
import info.nightscout.androidaps.logging.L;
|
2017-11-20 23:39:12 +01:00
|
|
|
import info.nightscout.androidaps.plugins.ConfigBuilder.ConfigBuilderPlugin;
|
2018-07-30 15:46:20 +02:00
|
|
|
import info.nightscout.androidaps.plugins.ConfigBuilder.ProfileFunctions;
|
2018-03-28 22:49:43 +02:00
|
|
|
import info.nightscout.androidaps.queue.commands.Command;
|
2018-07-30 15:46:20 +02:00
|
|
|
import info.nightscout.utils.FabricPrivacy;
|
2017-12-17 01:37:27 +01:00
|
|
|
import info.nightscout.utils.LocalAlertUtils;
|
2016-07-07 00:56:31 +02:00
|
|
|
|
2018-07-09 23:05:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mike on 07.07.2016.
|
|
|
|
*/
|
2016-07-07 00:56:31 +02:00
|
|
|
public class KeepAliveReceiver extends BroadcastReceiver {
|
2018-07-30 15:46:20 +02:00
|
|
|
private static Logger log = LoggerFactory.getLogger(L.CORE);
|
2017-11-28 21:48:46 +01:00
|
|
|
public static final long STATUS_UPDATE_FREQUENCY = 15 * 60 * 1000L;
|
|
|
|
|
2017-12-17 01:40:27 +01:00
|
|
|
public static void cancelAlarm(Context context) {
|
|
|
|
Intent intent = new Intent(context, KeepAliveReceiver.class);
|
|
|
|
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
|
|
|
|
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
alarmManager.cancel(sender);
|
|
|
|
}
|
|
|
|
|
2016-07-07 00:56:31 +02:00
|
|
|
@Override
|
2016-07-07 10:34:20 +02:00
|
|
|
public void onReceive(Context context, Intent rIntent) {
|
2016-07-07 00:56:31 +02:00
|
|
|
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
|
|
|
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
|
|
|
|
wl.acquire();
|
|
|
|
|
2017-12-17 01:37:27 +01:00
|
|
|
LocalAlertUtils.shortenSnoozeInterval();
|
|
|
|
LocalAlertUtils.checkStaleBGAlert();
|
2017-11-28 21:48:46 +01:00
|
|
|
checkPump();
|
2018-07-11 20:19:42 +02:00
|
|
|
FabricPrivacy.uploadDailyStats();
|
2017-01-29 11:11:07 +01:00
|
|
|
|
2018-07-30 15:46:20 +02:00
|
|
|
if (L.isEnabled(L.CORE))
|
|
|
|
log.debug("KeepAlive received");
|
2017-11-28 21:48:46 +01:00
|
|
|
wl.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkPump() {
|
2017-11-20 23:39:12 +01:00
|
|
|
final PumpInterface pump = ConfigBuilderPlugin.getActivePump();
|
2018-07-30 14:21:11 +02:00
|
|
|
final Profile profile = ProfileFunctions.getInstance().getProfile();
|
2018-03-18 15:02:21 +01:00
|
|
|
if (pump != null && profile != null) {
|
2017-02-19 19:15:14 +01:00
|
|
|
Date lastConnection = pump.lastDataTime();
|
2017-11-28 21:48:46 +01:00
|
|
|
boolean isStatusOutdated = lastConnection.getTime() + STATUS_UPDATE_FREQUENCY < System.currentTimeMillis();
|
|
|
|
boolean isBasalOutdated = Math.abs(profile.getBasal() - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep;
|
|
|
|
|
2017-12-17 01:37:27 +01:00
|
|
|
LocalAlertUtils.checkPumpUnreachableAlarm(lastConnection, isStatusOutdated);
|
2017-01-29 11:11:07 +01:00
|
|
|
|
2018-03-28 22:49:43 +02:00
|
|
|
if (!pump.isThisProfileSet(profile) && !ConfigBuilderPlugin.getCommandQueue().isRunning(Command.CommandType.BASALPROFILE)) {
|
2018-03-28 19:33:41 +02:00
|
|
|
MainApp.bus().post(new EventProfileSwitchChange());
|
2017-02-12 19:51:13 +01:00
|
|
|
} else if (isStatusOutdated && !pump.isBusy()) {
|
2018-03-28 19:33:41 +02:00
|
|
|
ConfigBuilderPlugin.getCommandQueue().readStatus("KeepAlive. Status outdated.", null);
|
2017-02-12 19:51:13 +01:00
|
|
|
} else if (isBasalOutdated && !pump.isBusy()) {
|
2018-03-28 19:33:41 +02:00
|
|
|
ConfigBuilderPlugin.getCommandQueue().readStatus("KeepAlive. Basal outdated.", null);
|
2017-01-29 11:11:07 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-07 00:56:31 +02:00
|
|
|
}
|
|
|
|
|
2017-11-28 21:48:46 +01:00
|
|
|
//called by MainApp at first app start
|
2016-07-07 00:56:31 +02:00
|
|
|
public void setAlarm(Context context) {
|
2017-11-28 21:48:46 +01:00
|
|
|
|
2017-12-17 01:37:27 +01:00
|
|
|
LocalAlertUtils.shortenSnoozeInterval();
|
|
|
|
LocalAlertUtils.presnoozeAlarms();
|
2017-11-28 21:48:46 +01:00
|
|
|
|
2016-07-07 00:56:31 +02:00
|
|
|
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|
|
|
Intent i = new Intent(context, KeepAliveReceiver.class);
|
|
|
|
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
|
|
|
|
try {
|
|
|
|
pi.send();
|
|
|
|
} catch (PendingIntent.CanceledException e) {
|
|
|
|
}
|
|
|
|
am.cancel(pi);
|
|
|
|
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), Constants.keepAliveMsecs, pi);
|
|
|
|
}
|
|
|
|
|
2017-12-02 10:21:42 +01:00
|
|
|
}
|