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() { private fun doMigrations() {
// set values for different builds // 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_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_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") 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> { override fun applicationInjector(): AndroidInjector<out DaggerApplication> {

View file

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

View file

@ -733,13 +733,17 @@
<string name="secondcarbsincrement">Second carbs increment</string> <string name="secondcarbsincrement">Second carbs increment</string>
<string name="thirdcarbsincrement">Third carbs increment</string> <string name="thirdcarbsincrement">Third carbs increment</string>
<string name="cgm">CGM</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_wifi_ssids" translatable="false">ns_wifi_ssids</string>
<string name="key_ns_allowroaming" translatable="false">ns_allowroaming</string> <string name="key_ns_allow_roaming" translatable="false">ns_allowroaming</string>
<string name="key_ns_chargingonly" translatable="false">ns_chargingonly</string> <string name="key_ns_battery" translatable="false">ns_battery</string>
<string name="ns_wifionly">Use WiFi connection only</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_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="connectionsettings_title">Connection settings</string>
<string name="ns_wifi_allowedssids">Allowed SSIDs (semicolon separated)</string> <string name="ns_wifi_allowedssids">Allowed SSIDs (semicolon separated)</string>
<string name="ns_allowroaming">Allow connection in roaming</string> <string name="ns_allowroaming">Allow connection in roaming</string>

View file

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

View file

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

View file

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

View file

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