diff --git a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt index 02f508316c..569fd47ab5 100644 --- a/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt +++ b/core/interfaces/src/main/java/info/nightscout/interfaces/ApsMode.kt @@ -3,5 +3,17 @@ package info.nightscout.interfaces enum class ApsMode { OPEN, CLOSED, - LGS; + LGS, + UNDEFINED; + + companion object { + + fun secureValueOf(stringValue: String): ApsMode { + return try { + valueOf(stringValue) + } catch (e: IllegalArgumentException) { + UNDEFINED + } + } + } } diff --git a/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt index fe11080b7f..b75f82f401 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/aps/loop/LoopPlugin.kt @@ -183,7 +183,7 @@ class LoopPlugin @Inject constructor( get() { val closedLoopEnabled = constraintChecker.isClosedLoopAllowed() val maxIobAllowed = constraintChecker.getMaxIOBAllowed().value() - val apsMode = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) val pump = activePlugin.activePump var isLGS = false if (!isSuspended && !pump.isSuspended()) if (closedLoopEnabled.value()) if (maxIobAllowed == HardLimits.MAX_IOB_LGS || apsMode == ApsMode.LGS) isLGS = true diff --git a/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt b/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt index 78be608347..6e9ecef1bc 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/constraints/objectives/objectives/Objective6.kt @@ -16,7 +16,7 @@ class Objective6(injector: HasAndroidInjector) : Objective(injector, "maxiob", R tasks.add(MinimumDurationTask(this, T.days(1).msecs())) tasks.add( object : Task(this, R.string.closedmodeenabled) { - override fun isCompleted(): Boolean = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) == ApsMode.CLOSED + override fun isCompleted(): Boolean = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) == ApsMode.CLOSED }) tasks.add( object : Task(this, R.string.maxiobset) { diff --git a/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt index 43e76f0fff..ec96275312 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/constraints/safety/SafetyPlugin.kt @@ -67,7 +67,7 @@ class SafetyPlugin @Inject constructor( } override fun isClosedLoopAllowed(value: Constraint): Constraint { - val mode = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + val mode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) if (mode == ApsMode.OPEN) value.set(aapsLogger, false, rh.gs(R.string.closedmodedisabledinpreferences), this) if (!config.isEngineeringModeOrRelease()) { if (value.value()) { @@ -160,7 +160,7 @@ class SafetyPlugin @Inject constructor( } override fun applyMaxIOBConstraints(maxIob: Constraint): Constraint { - val apsMode = ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) + val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) if (apsMode == ApsMode.LGS) maxIob.setIfSmaller(aapsLogger, HardLimits.MAX_IOB_LGS, rh.gs(R.string.limiting_iob, HardLimits.MAX_IOB_LGS, rh.gs(R.string.lowglucosesuspend)), this) return maxIob } diff --git a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index a92a6df507..d14ac5c35a 100644 --- a/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/plugins/main/src/main/java/info/nightscout/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -1288,7 +1288,7 @@ class SmsCommunicatorPlugin @Inject constructor( } private fun getApsModeText(): String = - when (ApsMode.valueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name))) { + when (ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name))) { ApsMode.OPEN -> rh.gs(R.string.openloop) ApsMode.CLOSED -> rh.gs(R.string.closedloop) ApsMode.LGS -> rh.gs(R.string.lowglucosesuspend) diff --git a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt index 03705264d6..cd8398a653 100644 --- a/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt +++ b/ui/src/main/java/info/nightscout/ui/dialogs/LoopDialog.kt @@ -162,7 +162,7 @@ class LoopDialog : DaggerDialogFragment() { val closedLoopAllowed = constraintChecker.isClosedLoopAllowed(Constraint(true)) val closedLoopAllowed2 = activePlugin.activeObjectives?.isAccomplished(Objectives.MAXIOB_OBJECTIVE) ?: false val lgsEnabled = constraintChecker.isLgsAllowed(Constraint(true)) - val apsMode = sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name) + val apsMode = ApsMode.secureValueOf(sp.getString(R.string.key_aps_mode, ApsMode.OPEN.name)) val pump = activePlugin.activePump binding.overviewDisconnect15m.visibility = pumpDescription.tempDurationStep15mAllowed.toVisibility() @@ -212,19 +212,19 @@ class LoopDialog : DaggerDialogFragment() { binding.overviewLoop.visibility = View.VISIBLE binding.overviewEnable.visibility = View.GONE when { - apsMode == "closed" -> { + apsMode == ApsMode.CLOSED -> { binding.overviewCloseloop.visibility = View.GONE binding.overviewLgsloop.visibility = View.VISIBLE binding.overviewOpenloop.visibility = View.VISIBLE } - apsMode == "lgs" -> { + apsMode == ApsMode.LGS -> { binding.overviewCloseloop.visibility = closedLoopAllowed.value().toVisibility() //show Close loop button only if Close loop allowed binding.overviewLgsloop.visibility = View.GONE binding.overviewOpenloop.visibility = View.VISIBLE } - apsMode == "open" -> { + apsMode == ApsMode.OPEN -> { binding.overviewCloseloop.visibility = closedLoopAllowed2.toVisibility() //show CloseLoop button only if Objective 6 is completed (closedLoopAllowed always false in open loop mode) binding.overviewLgsloop.visibility = lgsEnabled.value().toVisibility()