Skip to content

Colota - Android GPS Location tracker

fix(profiles): re-read the Wi-Fi name when Location returns or the network changes (#945)

Max Dietrichd634feae84fe
diff --git a/apps/docs/docs/development/architecture.md b/apps/docs/docs/development/architecture.md
index a63746e4..526b90c6 100644
--- a/apps/docs/docs/development/architecture.md
+++ b/apps/docs/docs/development/architecture.md
@@ -258,7 +258,7 @@ Database access layer for tracking profiles and trip events. Maintains a `TimedC
### ConditionMonitor
-Monitors charging state via `BroadcastReceiver`, Android Auto connection via the `CarConnection` API, and Wi-Fi transport changes from `NetworkManager`, reading the network name one-shot when a named-network profile needs it. Forwards state changes to `ProfileManager` for condition evaluation.
+Monitors charging state via `BroadcastReceiver`, Android Auto connection via the `CarConnection` API, and Wi-Fi transport changes from `NetworkManager`, reading the network name one-shot when a named-network profile needs it: on every Wi-Fi network that comes or goes and when Location comes back on, because a name read while Location was off is blank. Forwards state changes to `ProfileManager` for condition evaluation.
### ProfileConstants
diff --git a/apps/docs/docs/guides/tracking-profiles.md b/apps/docs/docs/guides/tracking-profiles.md
index 691d289d..e7e1f346 100644
--- a/apps/docs/docs/guides/tracking-profiles.md
+++ b/apps/docs/docs/guides/tracking-profiles.md
@@ -59,7 +59,7 @@ Reading a network name counts as location access, so a **Wi-Fi network** profile
**Wi-Fi** means the phone is connected to a Wi-Fi network, whether or not a VPN owns the traffic; Ethernet does not match.
-SSID matching is case-insensitive. Without location permission or with Location turned off, Android hides the network name and the **Wi-Fi network** condition does not match.
+SSID matching is case-insensitive. The name is read when the network changes and when Location comes back on. Without location permission, or with Location turned off at that moment, Android hides the name and the **Wi-Fi network** condition does not match.
:::
diff --git a/apps/mobile/android/app/src/main/java/com/colota/service/ConditionMonitor.kt b/apps/mobile/android/app/src/main/java/com/colota/service/ConditionMonitor.kt
index 32031a77..0a4ec89a 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/service/ConditionMonitor.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/service/ConditionMonitor.kt
@@ -46,6 +46,7 @@ class ConditionMonitor(
private val mainHandler = Handler(Looper.getMainLooper())
// Main thread only: invalidates in-flight SSID probes when a newer network change arrives.
private var wifiPushGeneration = 0
+ private var wifiMonitorActive = false
fun start() {
// Unregister first to prevent duplicate observers on repeated start() calls
@@ -74,6 +75,7 @@ class ConditionMonitor(
chargingReceiver = unregisterSafely(chargingReceiver)
stopCarConnectionMonitor()
networkManager.setWifiStateListener(null)
+ wifiMonitorActive = false
AppLogger.d(TAG, "Condition monitors stopped")
}
@@ -152,9 +154,15 @@ class ConditionMonitor(
networkManager.setWifiStateListener {
mainHandler.post { pushWifiState() }
}
+ wifiMonitorActive = true
pushWifiState()
}
+ /** A name read while Location was off is blank. */
+ fun onLocationEnabled() {
+ if (wifiMonitorActive && ProfileConstants.CONDITION_WIFI_SSID in profileManager.getNeededConditionTypes()) pushWifiState()
+ }
+
/**
* The transport-only listener above carries no location flag and can stay registered; the
* network name is read one-shot only when a named-network profile needs it, so the indicator
diff --git a/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt b/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt
index 13b928f0..cfdb5cdc 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt
@@ -92,6 +92,7 @@ class LocationForegroundService : Service() {
AppLogger.d(TAG, "Location providers changed: enabled=$current")
LocationServiceModule.sendLocationStateEvent(current)
refreshNotificationForCurrentState()
+ if (current) conditionMonitor.onLocationEnabled()
}
}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/service/ProfileManager.kt b/apps/mobile/android/app/src/main/java/com/colota/service/ProfileManager.kt
index c86d6baf..da55ce6d 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/service/ProfileManager.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/service/ProfileManager.kt
@@ -80,7 +80,7 @@ class ProfileManager(
evaluate()
}
- /** Called on the main thread when the default network's transport or SSID changes. */
+ /** Called on the main thread when the Wi-Fi state may have changed or the SSID was read again. */
fun onWifiStateChanged(connected: Boolean, ssid: String) {
isWifiConnected = connected
currentSsid = ssid
diff --git a/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt b/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt
index 8e3eb76a..eadc6e2e 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/sync/NetworkManager.kt
@@ -82,14 +82,15 @@ class NetworkManager(private val context: Context) {
* sees Wi-Fi even when a VPN owns the default network.
*/
private val wifiTransportCallback = object : ConnectivityManager.NetworkCallback() {
+ // A switch between two networks keeps the count above zero, so notify even when isWifi stays true
override fun onAvailable(network: Network) {
availableWifiNetworks.incrementAndGet()
- refreshWifiTransport()
+ refreshWifiTransport(force = true)
}
override fun onLost(network: Network) {
availableWifiNetworks.updateAndGet { maxOf(0, it - 1) }
- refreshWifiTransport()
+ refreshWifiTransport(force = true)
}
}
@@ -227,16 +228,16 @@ class NetworkManager(private val context: Context) {
return if (ssid == UNKNOWN_SSID) "" else ssid
}
- private fun refreshWifiTransport() {
+ private fun refreshWifiTransport(force: Boolean) {
isWifi = availableWifiNetworks.get() > 0
- notifyWifiStateIfChanged()
+ notifyWifiStateIfChanged(force)
}
- /** Profile evaluation is not free, so only real state changes reach the listener. */
+ /** Profile evaluation is not free, so only real state changes reach the listener, unless forced. */
@Synchronized
- private fun notifyWifiStateIfChanged() {
+ private fun notifyWifiStateIfChanged(force: Boolean = false) {
val ssid = if (ssidTracking) currentSsid else ""
- if (isWifi == notifiedWifi && isVpn == notifiedVpn && ssid == notifiedSsid) return
+ if (!force && isWifi == notifiedWifi && isVpn == notifiedVpn && ssid == notifiedSsid) return
notifiedWifi = isWifi
notifiedVpn = isVpn
notifiedSsid = ssid
@@ -624,12 +625,13 @@ class NetworkManager(private val context: Context) {
return currentSsid.equals(ssid, ignoreCase = true)
}
- /** True while the default network is Wi-Fi. Needs no location-flagged callback. */
+ /** True while any Wi-Fi network is connected, also under a VPN. Needs no location-flagged callback. */
fun isWifiConnected(): Boolean = isWifi
/**
- * Registers a callback for default-network transport / SSID changes. The listener runs on the
- * ConnectivityManager callback thread; callers that touch app state must marshal it themselves.
+ * Registers a callback for Wi-Fi connection, VPN and SSID changes, and for every Wi-Fi network
+ * that comes or goes. The listener runs on the ConnectivityManager callback thread; callers that
+ * touch app state must marshal it themselves.
* Used by the profile Wi-Fi conditions, which care about more than sync's one boolean.
*/
fun setWifiStateListener(listener: (() -> Unit)?) {
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/service/ConditionMonitorTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/service/ConditionMonitorTest.kt
index 349b2716..b1aa8158 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/service/ConditionMonitorTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/service/ConditionMonitorTest.kt
@@ -208,6 +208,54 @@ class ConditionMonitorTest {
verify(exactly = 0) { mockNetworkManager.readSsidOnce(any()) }
}
+ @Test
+ fun `Location coming back re-reads the network name`() {
+ mockBatteryStatus(BatteryManager.BATTERY_STATUS_DISCHARGING)
+ every { mockProfileManager.getNeededConditionTypes() } returns setOf(ProfileConstants.CONDITION_WIFI_SSID)
+ every { mockNetworkManager.isWifiConnected() } returns true
+ var name = ""
+ every { mockNetworkManager.readSsidOnce(any()) } answers { firstArg<(String) -> Unit>().invoke(name) }
+
+ monitor.start()
+ verify { mockProfileManager.onWifiStateChanged(true, "") }
+
+ name = "HomeNet"
+ monitor.onLocationEnabled()
+
+ verify { mockProfileManager.onWifiStateChanged(true, "HomeNet") }
+ }
+
+ @Test
+ fun `Location coming back reads nothing without a Wi-Fi condition`() {
+ mockBatteryStatus(BatteryManager.BATTERY_STATUS_DISCHARGING)
+
+ monitor.start()
+ monitor.onLocationEnabled()
+
+ verify(exactly = 0) { mockNetworkManager.readSsidOnce(any()) }
+ verify(exactly = 0) { mockProfileManager.onWifiStateChanged(any(), any()) }
+ }
+
+ /** Probes can answer out of order. */
+ @Test
+ fun `a probe result superseded by a newer network change is dropped`() {
+ mockBatteryStatus(BatteryManager.BATTERY_STATUS_DISCHARGING)
+ every { mockProfileManager.getNeededConditionTypes() } returns setOf(ProfileConstants.CONDITION_WIFI_SSID)
+ every { mockNetworkManager.isWifiConnected() } returns true
+ var listener: (() -> Unit)? = null
+ every { mockNetworkManager.setWifiStateListener(any()) } answers { listener = firstArg() }
+ val pending = mutableListOf<(String) -> Unit>()
+ every { mockNetworkManager.readSsidOnce(any()) } answers { pending.add(firstArg()) }
+
+ monitor.start()
+ listener!!.invoke()
+ pending[1]("NewNet")
+ pending[0]("OldNet")
+
+ verify(exactly = 1) { mockProfileManager.onWifiStateChanged(true, "NewNet") }
+ verify(exactly = 0) { mockProfileManager.onWifiStateChanged(true, "OldNet") }
+ }
+
@Test
fun `start does not register the wifi listener without a wifi condition`() {
mockBatteryStatus(BatteryManager.BATTERY_STATUS_DISCHARGING)
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/service/LocationForegroundServiceTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/service/LocationForegroundServiceTest.kt
index 20bbbab9..cce01f07 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/service/LocationForegroundServiceTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/service/LocationForegroundServiceTest.kt
@@ -813,6 +813,17 @@ class LocationForegroundServiceTest {
verify(exactly = 2) { networkManager.setSsidTracking(true) }
}
+ /** Holding the flagged callback would keep the location indicator lit. */
+ @Test
+ fun `a Wi-Fi network profile never turns on SSID tracking`() {
+ every { profileManager.getNeededConditionTypes() } returns setOf(ProfileConstants.CONDITION_WIFI_SSID)
+ setField("config", ServiceConfig(endpoint = "https://example.com", syncCondition = "any"))
+
+ invokePushConfigToSyncManager()
+
+ verify(exactly = 0) { networkManager.setSsidTracking(true) }
+ }
+
@Test
fun `ACTION_RECHECK_ZONE during a hold with no cached fix still applies a zone edit`() {
setField("lastKnownLocation", null)
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/service/ProfileManagerTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/service/ProfileManagerTest.kt
index de2af937..3ac20a34 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/service/ProfileManagerTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/service/ProfileManagerTest.kt
@@ -261,7 +261,7 @@ class ProfileManagerTest {
// --- Wi-Fi conditions ---
@Test
- fun `activates wifi any profile while the default network is wifi`() = runTest {
+ fun `activates wifi any profile while connected to wifi`() = runTest {
every { profileHelper.getEnabledProfiles() } returns listOf(wifiAnyProfile())
val manager = createManager()
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt
index e3c37316..d03d9626 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/sync/NetworkManagerTest.kt
@@ -139,6 +139,24 @@ class NetworkManagerTest {
assertEquals(2, changes)
}
+ /** An automatic switch connects the new network before the old one goes. */
+ @Test
+ fun `a switch between two Wi-Fi networks still notifies`() {
+ val cm = mockk<ConnectivityManager>(relaxed = true)
+ val slot = slot<ConnectivityManager.NetworkCallback>()
+ every { cm.registerNetworkCallback(any<NetworkRequest>(), capture(slot)) } just Runs
+ val manager = newManagerWith(cm)
+ var changes = 0
+ manager.setWifiStateListener { changes++ }
+
+ slot.captured.onAvailable(mockk(relaxed = true))
+ slot.captured.onAvailable(mockk(relaxed = true))
+ slot.captured.onLost(mockk(relaxed = true))
+
+ assertTrue(manager.isWifiConnected())
+ assertEquals(3, changes)
+ }
+
@Test
fun `identical capability deliveries do not notify twice`() {
// Bandwidth and validation updates arrive without any transport or SSID change; each one