keep alive dagger

This commit is contained in:
AdrianLxM 2020-01-06 15:22:28 +01:00
parent 1eb375af09
commit fa8197f7f9
2 changed files with 17 additions and 22 deletions

View file

@ -179,6 +179,7 @@ public class MainApp extends DaggerApplication {
@Inject VirtualPumpPlugin virtualPumpPlugin;
@Inject VersionCheckerPlugin versionCheckerPlugin;
@Inject WearPlugin wearPlugin;
@Inject KeepAliveReceiver.KeepAliveManager keepAliveManager;
@Override
public void onCreate() {
@ -302,7 +303,7 @@ public class MainApp extends DaggerApplication {
}).start();
}
new Thread(() -> KeepAliveReceiver.setAlarm(this)).start();
new Thread(() -> keepAliveManager.setAlarm(this)).start();
doMigrations();
}
@ -508,7 +509,7 @@ public class MainApp extends DaggerApplication {
if (timeDateOrTZChangeReceiver != null)
unregisterReceiver(timeDateOrTZChangeReceiver);
unregisterActivityLifecycleCallbacks(activityMonitor);
KeepAliveReceiver.cancelAlarm(this);
keepAliveManager.cancelAlarm(this);
super.onTerminate();
}

View file

@ -12,7 +12,7 @@ import info.nightscout.androidaps.Config
import info.nightscout.androidaps.db.DatabaseHelper
import info.nightscout.androidaps.events.EventProfileNeedsUpdate
import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin
import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin
@ -23,7 +23,6 @@ import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.LocalAlertUtils
import info.nightscout.androidaps.utils.T
import org.slf4j.LoggerFactory
import javax.inject.Inject
import kotlin.math.abs
@ -40,8 +39,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
if (L.isEnabled(L.CORE))
log.debug("KeepAlive received")
aapsLogger.debug(LTag.CORE, "KeepAlive received");
val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager
val wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "AndroidAPS:KeepAliveReceiver")
wl.acquire(T.mins(2).msecs())
@ -52,18 +50,10 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
wl.release()
}
companion object {
private val log = LoggerFactory.getLogger(L.CORE)
private val KEEP_ALIVE_MILLISECONDS = T.mins(5).msecs()
private val STATUS_UPDATE_FREQUENCY = T.mins(15).msecs()
private val IOB_UPDATE_FREQUENCY = T.mins(5).msecs()
class KeepAliveManager @Inject constructor(val aapsLogger: AAPSLogger) {
//called by MainApp at first app start
@JvmStatic
fun setAlarm(context: Context) {
if (L.isEnabled(L.CORE))
log.debug("KeepAlive scheduled")
aapsLogger.debug(LTag.CORE, "KeepAlive scheduled")
SystemClock.sleep(5000) // wait for app initialization
LocalAlertUtils.shortenSnoozeInterval()
LocalAlertUtils.presnoozeAlarms()
@ -78,10 +68,8 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), KEEP_ALIVE_MILLISECONDS, pi)
}
@JvmStatic
fun cancelAlarm(context: Context) {
if (L.isEnabled(L.CORE))
log.debug("KeepAlive canceled")
aapsLogger.debug(LTag.CORE, "KeepAlive canceled")
val intent = Intent(context, KeepAliveReceiver::class.java)
val sender = PendingIntent.getBroadcast(context, 0, intent, 0)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
@ -89,6 +77,13 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
}
}
companion object {
private val KEEP_ALIVE_MILLISECONDS = T.mins(5).msecs()
private val STATUS_UPDATE_FREQUENCY = T.mins(15).msecs()
private val IOB_UPDATE_FREQUENCY = T.mins(5).msecs()
}
// Usually devicestatus is uploaded through LoopPlugin after every loop cycle.
// if there is no BG available, we have to upload anyway to have correct
// IOB displayed in NS
@ -112,8 +107,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
val lastConnection = pump.lastDataTime()
val isStatusOutdated = lastConnection + STATUS_UPDATE_FREQUENCY < System.currentTimeMillis()
val isBasalOutdated = abs(profile.basal - pump.baseBasalRate) > pump.pumpDescription.basalStep
if (L.isEnabled(L.CORE))
log.debug("Last connection: " + DateUtil.dateAndTimeString(lastConnection))
aapsLogger.debug(LTag.CORE, "Last connection: " + DateUtil.dateAndTimeString(lastConnection))
// sometimes keep alive broadcast stops
// as as workaround test if readStatus was requested before an alarm is generated
if (lastReadStatus != 0L && lastReadStatus > System.currentTimeMillis() - T.mins(5).msecs()) {
@ -129,7 +123,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
configBuilderPlugin.commandQueue.readStatus("KeepAlive. Basal outdated.", null)
}
if (lastRun != 0L && System.currentTimeMillis() - lastRun > T.mins(10).msecs()) {
log.error("KeepAlive fail")
aapsLogger.error(LTag.CORE, "KeepAlive fail")
FabricPrivacy.getInstance().logCustom("KeepAliveFail")
}
lastRun = System.currentTimeMillis()