NSClient: more clear logic

This commit is contained in:
Milos Kozak 2022-05-30 19:12:20 +02:00
parent 3f3496426b
commit bbb49ccdab
7 changed files with 130 additions and 137 deletions

View file

@ -188,11 +188,33 @@ class MainApp : DaggerApplication() {
}
}
@Suppress("SpellCheckingInspection")
private fun doMigrations() {
// set values for different builds
if (!sp.contains(R.string.key_ns_alarms)) sp.putBoolean(R.string.key_ns_alarms, config.NSCLIENT)
if (!sp.contains(R.string.key_ns_announcements)) sp.putBoolean(R.string.key_ns_announcements, config.NSCLIENT)
if (!sp.contains(R.string.key_language)) sp.putString(R.string.key_language, "default")
// 3.1.0
if (sp.contains("ns_wifionly")) {
if (sp.getBoolean("ns_wifionly", false)) {
sp.putBoolean(R.string.key_ns_cellular, false)
sp.putBoolean(R.string.key_ns_wifi, true)
} else {
sp.putBoolean(R.string.key_ns_cellular, true)
sp.putBoolean(R.string.key_ns_wifi, false)
}
sp.remove("ns_wifionly")
}
if (sp.contains("ns_charginonly")) {
if (sp.getBoolean("ns_charginonly", false)) {
sp.putBoolean(R.string.key_ns_battery, false)
sp.putBoolean(R.string.key_ns_charging, true)
} else {
sp.putBoolean(R.string.key_ns_battery, true)
sp.putBoolean(R.string.key_ns_charging, true)
}
sp.remove("ns_charginonly")
}
}
override fun applicationInjector(): AndroidInjector<out DaggerApplication> {

View file

@ -28,13 +28,19 @@ class NsClientReceiverDelegate @Inject constructor(
}
fun onStatusEvent(ev: EventPreferenceChange) {
if (ev.isChanged(rh, R.string.key_ns_wifionly) ||
ev.isChanged(rh, R.string.key_ns_wifi_ssids) ||
ev.isChanged(rh, R.string.key_ns_allowroaming)) {
receiverStatusStore.updateNetworkStatus()
onStatusEvent(receiverStatusStore.lastNetworkEvent)
} else if (ev.isChanged(rh, R.string.key_ns_chargingonly)) {
receiverStatusStore.broadcastChargingState()
when {
ev.isChanged(rh, R.string.key_ns_wifi) ||
ev.isChanged(rh, R.string.key_ns_cellular) ||
ev.isChanged(rh, R.string.key_ns_wifi_ssids) ||
ev.isChanged(rh, R.string.key_ns_allow_roaming) -> {
receiverStatusStore.updateNetworkStatus()
receiverStatusStore.lastNetworkEvent?.let { onStatusEvent(it) }
}
ev.isChanged(rh, R.string.key_ns_charging) ||
ev.isChanged(rh, R.string.key_ns_battery) -> {
receiverStatusStore.broadcastChargingState()
}
}
}
@ -46,7 +52,7 @@ class NsClientReceiverDelegate @Inject constructor(
}
}
fun onStatusEvent(ev: EventNetworkChange?) {
fun onStatusEvent(ev: EventNetworkChange) {
val newNetworkState = calculateStatus(ev)
if (newNetworkState != allowedNetworkState) {
allowedNetworkState = newNetworkState
@ -62,30 +68,13 @@ class NsClientReceiverDelegate @Inject constructor(
}
}
fun calculateStatus(ev: EventChargingState): Boolean {
val chargingOnly = sp.getBoolean(R.string.key_ns_chargingonly, false)
var newAllowedState = true
if (!ev.isCharging && chargingOnly) {
newAllowedState = false
}
return newAllowedState
}
fun calculateStatus(ev: EventChargingState): Boolean =
!ev.isCharging && sp.getBoolean(R.string.key_ns_battery, true) ||
ev.isCharging && sp.getBoolean(R.string.key_ns_charging, true)
fun calculateStatus(ev: EventNetworkChange?): Boolean {
val wifiOnly = sp.getBoolean(R.string.key_ns_wifionly, false)
val allowedSsidString = sp.getString(R.string.key_ns_wifi_ssids, "")
val allowedSSIDs: List<String> = if (allowedSsidString.isEmpty()) List(0) { "" } else allowedSsidString.split(";")
val allowRoaming = sp.getBoolean(R.string.key_ns_allowroaming, true)
var newAllowedState = true
if (ev?.wifiConnected == true) {
if (allowedSSIDs.isNotEmpty() && !allowedSSIDs.contains(ev.ssid)) {
newAllowedState = false
}
} else {
if (!allowRoaming && ev?.roaming == true || wifiOnly) {
newAllowedState = false
}
}
return newAllowedState
}
fun calculateStatus(ev: EventNetworkChange): Boolean =
ev.mobileConnected && sp.getBoolean(R.string.key_ns_cellular, true) && !ev.roaming ||
ev.mobileConnected && sp.getBoolean(R.string.key_ns_cellular, true) && ev.roaming && sp.getBoolean(R.string.key_ns_allow_roaming, true) ||
ev.wifiConnected && sp.getBoolean(R.string.key_ns_wifi, true) && sp.getString(R.string.key_ns_wifi_ssids, "").isEmpty() ||
ev.wifiConnected && sp.getBoolean(R.string.key_ns_wifi, true) && sp.getString(R.string.key_ns_wifi_ssids, "").split(";").contains(ev.ssid)
}

View file

@ -733,13 +733,17 @@
<string name="secondcarbsincrement">Second carbs increment</string>
<string name="thirdcarbsincrement">Third carbs increment</string>
<string name="cgm">CGM</string>
<string name="key_ns_wifionly" translatable="false">ns_wifionly</string>
<string name="key_ns_cellular" translatable="false">ns_cellular</string>
<string name="key_ns_wifi" translatable="false">ns_wifi</string>
<string name="key_ns_wifi_ssids" translatable="false">ns_wifi_ssids</string>
<string name="key_ns_allowroaming" translatable="false">ns_allowroaming</string>
<string name="key_ns_chargingonly" translatable="false">ns_chargingonly</string>
<string name="ns_wifionly">Use WiFi connection only</string>
<string name="key_ns_allow_roaming" translatable="false">ns_allowroaming</string>
<string name="key_ns_battery" translatable="false">ns_battery</string>
<string name="key_ns_charging" translatable="false">ns_charging</string>
<string name="ns_cellular">Use Cellular connection</string>
<string name="ns_wifi">Use WiFi connection</string>
<string name="ns_wifi_ssids">WiFi SSID</string>
<string name="ns_chargingonly">Only if charging</string>
<string name="ns_charging">During charging</string>
<string name="ns_battery">On battery</string>
<string name="connectionsettings_title">Connection settings</string>
<string name="ns_wifi_allowedssids">Allowed SSIDs (semicolon separated)</string>
<string name="ns_allowroaming">Allow connection in roaming</string>

View file

@ -138,26 +138,36 @@
<SwitchPreference
android:defaultValue="false"
android:disableDependentsState="true"
android:key="@string/key_ns_wifionly"
android:title="@string/ns_wifionly" />
android:key="@string/key_ns_cellular"
android:title="@string/ns_cellular" />
<SwitchPreference
android:defaultValue="true"
android:dependency="@string/key_ns_cellular"
android:key="@string/key_ns_allow_roaming"
android:title="@string/ns_allowroaming" />
<SwitchPreference
android:defaultValue="true"
android:key="@string/key_ns_wifi"
android:title="@string/ns_wifi" />
<EditTextPreference
android:dialogMessage="@string/ns_wifi_allowedssids"
android:dependency="@string/key_ns_wifi"
android:inputType="text"
android:key="@string/key_ns_wifi_ssids"
android:title="@string/ns_wifi_ssids" />
<SwitchPreference
android:defaultValue="true"
android:dependency="@string/key_ns_wifionly"
android:key="@string/key_ns_allowroaming"
android:title="@string/ns_allowroaming" />
android:key="@string/key_ns_battery"
android:title="@string/ns_battery" />
<SwitchPreference
android:defaultValue="false"
android:key="@string/key_ns_chargingonly"
android:title="@string/ns_chargingonly" />
android:defaultValue="true"
android:key="@string/key_ns_charging"
android:title="@string/ns_charging" />
</androidx.preference.PreferenceScreen>

View file

@ -5,17 +5,13 @@ import info.nightscout.androidaps.R
import info.nightscout.androidaps.TestBase
import info.nightscout.androidaps.events.EventChargingState
import info.nightscout.androidaps.events.EventNetworkChange
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.receivers.ReceiverStatusStore
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.shared.sharedPreferences.SP
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.mockito.ArgumentMatchers.anyBoolean
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyLong
import org.mockito.ArgumentMatchers.anyString
import org.mockito.Mock
import org.mockito.Mockito.`when`
@ -28,94 +24,66 @@ class NsClientReceiverDelegateTest : TestBase() {
lateinit var receiverStatusStore: ReceiverStatusStore
val rxBus = RxBus(aapsSchedulers, aapsLogger)
private var sut: NsClientReceiverDelegate? = null
private lateinit var sut: NsClientReceiverDelegate
@Before fun prepare() {
@Before
fun prepare() {
receiverStatusStore = ReceiverStatusStore(context, rxBus)
`when`(sp.getLong(anyInt(), anyLong())).thenReturn(0L)
`when`(sp.getBoolean(anyInt(), anyBoolean())).thenReturn(false)
`when`(sp.getInt(anyInt(), anyInt())).thenReturn(0)
`when`(sp.getString(anyInt(), anyString())).thenReturn("")
// `when`(sp.getLong(anyInt(), anyLong())).thenReturn(0L)
// `when`(sp.getBoolean(anyInt(), anyBoolean())).thenReturn(false)
// `when`(sp.getInt(anyInt(), anyInt())).thenReturn(0)
// `when`(sp.getString(anyInt(), anyString())).thenReturn("")
sut = NsClientReceiverDelegate(rxBus, rh, sp, receiverStatusStore)
}
@Test fun testCalculateStatusChargingState() {
`when`(sp.getBoolean(anyInt(), anyBoolean())).thenReturn(false)
var ev = EventChargingState(true, 0)
Assert.assertTrue(sut!!.calculateStatus(ev))
ev = EventChargingState(false, 0)
Assert.assertTrue(sut!!.calculateStatus(ev))
`when`(sp.getBoolean(anyInt(), anyBoolean())).thenReturn(true)
ev = EventChargingState(true, 0)
Assert.assertTrue(sut!!.calculateStatus(ev))
ev = EventChargingState(false, 0)
Assert.assertTrue(!sut!!.calculateStatus(ev))
@Test
fun testCalculateStatusChargingState() {
`when`(sp.getBoolean(R.string.key_ns_battery, true)).thenReturn(true)
`when`(sp.getBoolean(R.string.key_ns_charging, true)).thenReturn(false)
Assert.assertTrue(sut.calculateStatus(EventChargingState(false, 0)))
Assert.assertFalse(sut.calculateStatus(EventChargingState(true, 0)))
`when`(sp.getBoolean(R.string.key_ns_battery, true)).thenReturn(false)
`when`(sp.getBoolean(R.string.key_ns_charging, true)).thenReturn(true)
Assert.assertTrue(sut.calculateStatus(EventChargingState(true, 0)))
Assert.assertFalse(sut.calculateStatus(EventChargingState(false, 0)))
}
@Test fun testCalculateStatusNetworkState() {
// wifiOnly = false
// allowRoaming = false as well
`when`(sp.getBoolean(anyInt(), anyBoolean())).thenReturn(false)
`when`(sp.getString(anyInt(), anyString())).thenReturn("")
val ev = EventNetworkChange()
ev.ssid = "<unknown ssid>"
ev.mobileConnected = true
ev.wifiConnected = true
Assert.assertTrue(sut!!.calculateStatus(ev))
ev.ssid = "test"
`when`(sp.getString(anyInt(), anyString())).thenReturn("test")
Assert.assertTrue(sut!!.calculateStatus(ev))
ev.ssid = "test"
Assert.assertTrue(sut!!.calculateStatus(ev))
ev.wifiConnected = false
Assert.assertTrue(sut!!.calculateStatus(ev))
@Test
fun testCalculateStatusNetworkState() {
`when`(sp.getBoolean(R.string.key_ns_cellular, true)).thenReturn(true)
`when`(sp.getBoolean(R.string.key_ns_wifi, true)).thenReturn(true)
`when`(sp.getString(R.string.key_ns_wifi_ssids, "")).thenReturn("")
Assert.assertTrue(sut.calculateStatus(EventNetworkChange(ssid = "<unknown ssid>", mobileConnected = true, wifiConnected = true)))
`when`(sp.getString(R.string.key_ns_wifi_ssids, "")).thenReturn("test")
Assert.assertTrue(sut.calculateStatus(EventNetworkChange(ssid = "test", mobileConnected = true, wifiConnected = true)))
Assert.assertFalse(sut.calculateStatus(EventNetworkChange(ssid = "unknown", mobileConnected = true, wifiConnected = true)))
Assert.assertTrue(sut.calculateStatus(EventNetworkChange(mobileConnected = true)))
// wifiOnly = true
// allowRoaming = true as well
`when`(sp.getBoolean(anyInt(), anyBoolean())).thenReturn(true)
ev.wifiConnected = true
Assert.assertTrue(sut!!.calculateStatus(ev))
ev.wifiConnected = false
Assert.assertTrue(!sut!!.calculateStatus(ev))
`when`(sp.getBoolean(R.string.key_ns_cellular, true)).thenReturn(false)
`when`(sp.getBoolean(R.string.key_ns_wifi, true)).thenReturn(true)
`when`(sp.getBoolean(R.string.key_ns_allow_roaming, true)).thenReturn(true)
Assert.assertTrue(sut.calculateStatus(EventNetworkChange(wifiConnected = true)))
Assert.assertFalse(sut.calculateStatus(EventNetworkChange()))
Assert.assertFalse(sut.calculateStatus(EventNetworkChange(mobileConnected = true)))
// wifiOnly = false
// allowRoaming = false as well
`when`(sp.getBoolean(anyInt(), anyBoolean())).thenReturn(false)
ev.wifiConnected = false
ev.roaming = true
Assert.assertTrue(!sut!!.calculateStatus(ev))
`when`(sp.getBoolean(R.string.key_ns_cellular, true)).thenReturn(true)
`when`(sp.getBoolean(R.string.key_ns_wifi, true)).thenReturn(true)
`when`(sp.getBoolean(R.string.key_ns_allow_roaming, true)).thenReturn(false)
Assert.assertTrue(sut.calculateStatus(EventNetworkChange(mobileConnected = true, roaming = false)))
Assert.assertFalse(sut.calculateStatus(EventNetworkChange(mobileConnected = true, roaming = true)))
// wifiOnly = false
// allowRoaming = true
`when`(sp.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(false)
`when`(sp.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true)
ev.wifiConnected = false
ev.roaming = true
Assert.assertTrue(sut!!.calculateStatus(ev))
// wifiOnly = true
// allowRoaming = true
`when`(sp.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(true)
`when`(sp.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true)
ev.wifiConnected = false
ev.roaming = true
Assert.assertTrue(!sut!!.calculateStatus(ev))
// wifiOnly = true
// allowRoaming = true
`when`(sp.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(true)
`when`(sp.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(true)
ev.wifiConnected = true
ev.roaming = true
Assert.assertTrue(sut!!.calculateStatus(ev))
// wifiOnly = false
// allowRoaming = false
`when`(sp.getBoolean(R.string.key_ns_wifionly, false)).thenReturn(false)
`when`(sp.getBoolean(R.string.key_ns_allowroaming, true)).thenReturn(false)
ev.wifiConnected = true
ev.roaming = true
Assert.assertTrue(sut!!.calculateStatus(ev))
`when`(sp.getBoolean(R.string.key_ns_cellular, true)).thenReturn(true)
`when`(sp.getBoolean(R.string.key_ns_wifi, true)).thenReturn(true)
`when`(sp.getBoolean(R.string.key_ns_allow_roaming, true)).thenReturn(true)
Assert.assertTrue(sut.calculateStatus(EventNetworkChange(mobileConnected = true, roaming = false)))
Assert.assertTrue(sut.calculateStatus(EventNetworkChange(mobileConnected = true, roaming = true)))
}
}

View file

@ -1,12 +1,10 @@
package info.nightscout.androidaps.events
class EventNetworkChange : Event() {
var mobileConnected = false
var wifiConnected = false
var vpnConnected = false
var ssid = ""
var roaming = false
var metered = false
}
class EventNetworkChange(
var mobileConnected: Boolean = false,
var wifiConnected: Boolean = false,
var vpnConnected: Boolean = false,
var ssid: String = "",
var roaming: Boolean = false,
var metered: Boolean = false
) : Event()

View file

@ -85,10 +85,12 @@ private val allowedKeys = """
nsalarm_staledatavalue
nsalarm_urgent_staledata
nsalarm_urgent_staledatavalue
ns_wifionly
ns_wifi_ssids
ns_allowroaming
ns_chargingonly
ns_cellular
ns_wifi
ns_allow_roaming
ns_battery
ns_charging
ns_autobackfill
ns_create_announcements_from_errors
nsclient_localbroadcasts