diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 56eb96faf7..57ad69b62a 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -133,6 +133,7 @@ public class MainApp extends DaggerApplication { @Inject DanaRSPlugin danaRSPlugin; @Inject DanaRv2Plugin danaRv2Plugin; @Inject DanaRKoreanPlugin danaRKoreanPlugin; + @Inject DstHelperPlugin dstHelperPlugin; @Inject InsulinOrefFreePeakPlugin insulinOrefFreePeakPlugin; @Inject InsulinOrefRapidActingPlugin insulinOrefRapidActingPlugin; @Inject InsulinOrefUltraRapidActingPlugin insulinOrefUltraRapidActingPlugin; @@ -267,12 +268,10 @@ public class MainApp extends DaggerApplication { // if (engineeringMode) pluginsList.add(tidepoolPlugin); pluginsList.add(maintenancePlugin); pluginsList.add(automationPlugin); + pluginsList.add(dstHelperPlugin); pluginsList.add(configBuilderPlugin); - pluginsList.add(DstHelperPlugin.getPlugin()); - - configBuilderPlugin.initialize(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/dstHelper/DstHelperPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/dstHelper/DstHelperPlugin.java deleted file mode 100644 index d6458465af..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/dstHelper/DstHelperPlugin.java +++ /dev/null @@ -1,96 +0,0 @@ -package info.nightscout.androidaps.plugins.constraints.dstHelper; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.Calendar; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.interfaces.Constraint; -import info.nightscout.androidaps.interfaces.ConstraintsInterface; -import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.PluginDescription; -import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; -import info.nightscout.androidaps.plugins.bus.RxBus; -import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin; -import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification; -import info.nightscout.androidaps.plugins.general.overview.notifications.Notification; - -public class DstHelperPlugin extends PluginBase implements ConstraintsInterface { - public static final int DISABLE_TIMEFRAME_HOURS = -3; - public static final int WARN_PRIOR_TIMEFRAME_HOURS = 12; - private static Logger log = LoggerFactory.getLogger(L.CONSTRAINTS); - - static DstHelperPlugin plugin = null; - - @Deprecated - public static DstHelperPlugin getPlugin() { - if (plugin == null) - plugin = new DstHelperPlugin(); - return plugin; - } - - public DstHelperPlugin() { - super(new PluginDescription() - .mainType(PluginType.CONSTRAINTS) - .neverVisible(true) - .alwaysEnabled(true) - .showInList(false) - .pluginName(R.string.dst_plugin_name) - ); - } - - public static boolean wasDST(Calendar now) { - Calendar ago = (Calendar) now.clone(); - ago.add(Calendar.HOUR, DISABLE_TIMEFRAME_HOURS); - return now.get(Calendar.DST_OFFSET) != ago.get(Calendar.DST_OFFSET); - } - - public static boolean willBeDST(Calendar now) { - Calendar ago = (Calendar) now.clone(); - ago.add(Calendar.HOUR, WARN_PRIOR_TIMEFRAME_HOURS); - return now.get(Calendar.DST_OFFSET) != ago.get(Calendar.DST_OFFSET); - } - - //Return false if time to DST change happened in the last 3 hours. - @Override - public Constraint isLoopInvocationAllowed(Constraint value) { - - PumpInterface pump = ConfigBuilderPlugin.getPlugin().getActivePump(); - if (pump == null || pump.canHandleDST()) { - log.debug("Pump can handle DST"); - return value; - } - - Calendar cal = Calendar.getInstance(); - - if (willBeDST(cal)) { - warnUser(Notification.DST_IN_24H, MainApp.gs(R.string.dst_in_24h_warning)); - } - - if (!value.value()) { - log.debug("Already not allowed - don't check further"); - return value; - } - - if (wasDST(cal)) { - LoopPlugin loopPlugin = LoopPlugin.getPlugin(); - if (!loopPlugin.isSuspended()) { - warnUser(Notification.DST_LOOP_DISABLED, MainApp.gs(R.string.dst_loop_disabled_warning)); - } else { - log.debug("Loop already suspended"); - } - value.set(false, "DST in last 3 hours.", this); - } - return value; - } - - private void warnUser(int id, String warningText) { - Notification notification = new Notification(id, warningText, Notification.LOW); - RxBus.Companion.getINSTANCE().send(new EventNewNotification(notification)); - } -} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/constraints/dstHelper/DstHelperPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/dstHelper/DstHelperPlugin.kt new file mode 100644 index 0000000000..91925e598c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/constraints/dstHelper/DstHelperPlugin.kt @@ -0,0 +1,96 @@ +package info.nightscout.androidaps.plugins.constraints.dstHelper + +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.interfaces.Constraint +import info.nightscout.androidaps.interfaces.ConstraintsInterface +import info.nightscout.androidaps.interfaces.PluginBase +import info.nightscout.androidaps.interfaces.PluginDescription +import info.nightscout.androidaps.interfaces.PluginType +import info.nightscout.androidaps.logging.AAPSLogger +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 +import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification +import info.nightscout.androidaps.plugins.general.overview.notifications.Notification +import info.nightscout.androidaps.plugins.general.overview.notifications.NotificationWithAction +import info.nightscout.androidaps.utils.T +import info.nightscout.androidaps.utils.resources.ResourceHelper +import info.nightscout.androidaps.utils.sharedPreferences.SP +import java.util.* +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class DstHelperPlugin @Inject constructor(): PluginBase(PluginDescription() + .mainType(PluginType.CONSTRAINTS) + .neverVisible(true) + .alwaysEnabled(true) + .showInList(false) + .pluginName(R.string.dst_plugin_name) +), ConstraintsInterface { + + @Inject lateinit var aapsLogger: AAPSLogger + @Inject lateinit var rxBus: RxBusWrapper + @Inject lateinit var resourceHelper: ResourceHelper + @Inject lateinit var mainApp: MainApp + @Inject lateinit var sp : SP + @Inject lateinit var configBuilderPlugin: ConfigBuilderPlugin + @Inject lateinit var loopPlugin: LoopPlugin + + private val DISABLE_TIMEFRAME_HOURS = -3 + private val WARN_PRIOR_TIMEFRAME_HOURS = 12 + + //Return false if time to DST change happened in the last 3 hours. + override fun isLoopInvocationAllowed(value: Constraint): Constraint { + val pump = configBuilderPlugin.activePump + if (pump == null || pump.canHandleDST()) { + aapsLogger.debug(LTag.CONSTRAINTS, "Pump can handle DST") + return value + } + val cal = Calendar.getInstance() + if (willBeDST(cal)) { + val snoozedTo: Long = sp.getLong(R.string.key_snooze_dst_in24h, 0L) + if (snoozedTo == 0L || System.currentTimeMillis() > snoozedTo) { + val notification = NotificationWithAction(mainApp, Notification.DST_IN_24H, resourceHelper.gs(R.string.dst_in_24h_warning), Notification.LOW) + notification.action(R.string.snooze, Runnable { + sp.putLong(R.string.key_snooze_dst_in24h, System.currentTimeMillis() + T.hours(24).msecs()) + }) + rxBus.send(EventNewNotification(notification)) + } + } + if (!value.value()) { + aapsLogger.debug(LTag.CONSTRAINTS, "Already not allowed - don't check further") + return value + } + if (wasDST(cal)) { + if (!loopPlugin.isSuspended) { + val snoozedTo: Long = sp.getLong(R.string.key_snooze_loopdisabled, 0L) + if (snoozedTo == 0L || System.currentTimeMillis() > snoozedTo) { + val notification = NotificationWithAction(mainApp, Notification.DST_LOOP_DISABLED, resourceHelper.gs(R.string.dst_loop_disabled_warning), Notification.LOW) + notification.action(R.string.snooze, Runnable { + sp.putLong(R.string.key_snooze_loopdisabled, System.currentTimeMillis() + T.hours(24).msecs()) + }) + rxBus.send(EventNewNotification(notification)) + } + } else { + aapsLogger.debug(LTag.CONSTRAINTS, "Loop already suspended") + } + value.set(false, "DST in last 3 hours.", this) + } + return value + } + + fun wasDST(now: Calendar): Boolean { + val ago = now.clone() as Calendar + ago.add(Calendar.HOUR, DISABLE_TIMEFRAME_HOURS) + return now[Calendar.DST_OFFSET] != ago[Calendar.DST_OFFSET] + } + + fun willBeDST(now: Calendar): Boolean { + val ago = now.clone() as Calendar + ago.add(Calendar.HOUR, WARN_PRIOR_TIMEFRAME_HOURS) + return now[Calendar.DST_OFFSET] != ago[Calendar.DST_OFFSET] + } +} \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationWithAction.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationWithAction.kt index 12af540779..2836b2f516 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationWithAction.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/notifications/NotificationWithAction.kt @@ -72,7 +72,7 @@ class NotificationWithAction constructor( } } - fun action(buttonText: Int, action: Runnable?) { + fun action(buttonText: Int, action: Runnable) { this.buttonText = buttonText this.action = action } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 37a9a277c7..aa48438209 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1689,5 +1689,7 @@ xdripstatus_showbgi wear_detailed_delta snoozedTo + snooze_dst_in24h + snooze_loopdisabled