2016-07-07 00:56:31 +02:00
|
|
|
package info.nightscout.androidaps.receivers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by mike on 07.07.2016.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import android.app.AlarmManager;
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2017-01-28 23:45:14 +01:00
|
|
|
import android.content.SharedPreferences;
|
2016-07-07 00:56:31 +02:00
|
|
|
import android.os.PowerManager;
|
2017-01-28 23:45:14 +01:00
|
|
|
import android.preference.PreferenceManager;
|
2016-07-07 00:56:31 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-08-02 15:43:21 +02:00
|
|
|
import java.util.Date;
|
|
|
|
|
2016-07-07 10:34:20 +02:00
|
|
|
import info.nightscout.androidaps.Config;
|
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;
|
2016-07-11 14:04:57 +02:00
|
|
|
import info.nightscout.androidaps.interfaces.PluginBase;
|
2017-01-28 23:45:14 +01:00
|
|
|
import info.nightscout.androidaps.interfaces.PumpInterface;
|
2016-08-05 23:54:03 +02:00
|
|
|
import info.nightscout.androidaps.plugins.DanaR.DanaRPlugin;
|
2016-12-03 17:42:08 +01:00
|
|
|
import info.nightscout.androidaps.plugins.DanaRKorean.DanaRKoreanPlugin;
|
2017-01-28 23:45:14 +01:00
|
|
|
import info.nightscout.client.data.NSProfile;
|
2016-07-07 00:56:31 +02:00
|
|
|
|
|
|
|
public class KeepAliveReceiver extends BroadcastReceiver {
|
|
|
|
private static Logger log = LoggerFactory.getLogger(KeepAliveReceiver.class);
|
|
|
|
|
|
|
|
@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-01-29 11:11:07 +01:00
|
|
|
|
|
|
|
final PumpInterface pump = MainApp.getConfigBuilder();
|
|
|
|
final NSProfile profile = MainApp.getConfigBuilder().getActiveProfile().getProfile();
|
|
|
|
if (pump != null && profile != null) {
|
|
|
|
boolean isBasalOutdated = false;
|
|
|
|
boolean isStatusOutdated = false;
|
|
|
|
|
|
|
|
Date lastConnection = pump.lastStatusTime();
|
|
|
|
if (lastConnection.getTime() + 30 * 60 * 1000L < new Date().getTime())
|
|
|
|
isStatusOutdated = true;
|
|
|
|
if (Math.abs(profile.getBasal(NSProfile.secondsFromMidnight()) - pump.getBaseBasalRate()) > pump.getPumpDescription().basalStep)
|
|
|
|
isBasalOutdated = true;
|
|
|
|
|
|
|
|
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MainApp.instance().getApplicationContext());
|
2017-01-29 19:17:28 +01:00
|
|
|
if (SP.getBoolean("syncprofiletopump", false) && !pump.isThisProfileSet(profile)) {
|
2017-01-29 11:11:07 +01:00
|
|
|
Thread t = new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
pump.setNewBasalProfile(profile);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
t.start();
|
2017-01-29 11:24:11 +01:00
|
|
|
} else if (isStatusOutdated) {
|
2017-01-29 11:11:07 +01:00
|
|
|
Thread t = new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2017-01-29 11:24:11 +01:00
|
|
|
pump.updateStatus("KeepAlive. Status outdated.");
|
2017-01-29 11:11:07 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
t.start();
|
2017-01-29 11:24:11 +01:00
|
|
|
} else if (isBasalOutdated) {
|
2017-01-29 11:11:07 +01:00
|
|
|
Thread t = new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2017-01-29 11:24:11 +01:00
|
|
|
pump.updateStatus("KeepAlive. Basal outdated.");
|
2017-01-29 11:11:07 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
t.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 00:56:31 +02:00
|
|
|
log.debug("KeepAlive received");
|
|
|
|
wl.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setAlarm(Context context) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public 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);
|
|
|
|
}
|
|
|
|
}
|