networklocation: honour interval changes while provider is running
- Commit
f5b72a02cdf765ac6b4e4f6a61173f5087a40136- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 20:10 UTC (28 days ago)
- Parents
05b6fca
Symptom: once the fix loop started, later provider requests asking for a different reporting interval had no effect until every client had stopped and the loop was torn down. A client asking for faster fixes was starved at the old cadence; a client settling for slower fixes left the provider scanning at the old, faster cadence, wasting battery. Root cause: LocationReportingTask.start() returned early whenever a scope already existed, discarding the new interval passed in from LocationProviderImpl.onSetRequest, which the framework re-invokes with an updated ProviderRequestUnbundled.interval as clients come and go. Fix: remember the clamped interval; a start() with the same interval is still a no-op, but a changed interval cancels the old loop and starts a fresh one at the new cadence (without closing the beacon store, which stop() alone owns). Verification: new regression test 'a changed interval takes effect while running' starts at 60s, re-starts at 1s and requires a second fix within 10s; module unit tests and compile run after this change.
Diff
networklocation/src/main/java/com/vayunmathur/networklocation/provider/LocationReportingTask.kt
diff --git a/networklocation/src/main/java/com/vayunmathur/networklocation/provider/LocationReportingTask.kt b/networklocation/src/main/java/com/vayunmathur/networklocation/provider/LocationReportingTask.ktindex b14a200709..ec9842bcbd 100644--- a/networklocation/src/main/java/com/vayunmathur/networklocation/provider/LocationReportingTask.kt+++ b/networklocation/src/main/java/com/vayunmathur/networklocation/provider/LocationReportingTask.kt@@ -40,15 +40,22 @@ class LocationReportingTask( private val offlineStore = OfflineBeaconStore(appContext) private var scope: CoroutineScope? = null+ private var intervalMs: Long = 0L fun start(intervalMillis: Long) {- if (scope != null) return+ val clamped = intervalMillis.coerceAtLeast(MIN_INTERVAL_MS)+ // The framework re-calls onSetRequest with a new interval as clients+ // come and go; keeping the first interval would report at a stale+ // cadence (wasting battery, or starving a client that asked faster).+ if (scope != null && clamped == intervalMs) return+ scope?.cancel()+ intervalMs = clamped val newScope = CoroutineScope(SupervisorJob() + Dispatchers.Default) scope = newScope newScope.launch { while (isActive) { runCatching { reportOnce() }.onFailure { Log.error(TAG, "fix attempt failed", it) }- delay(intervalMillis.coerceAtLeast(MIN_INTERVAL_MS))+ delay(clamped) } } }
networklocation/src/test/java/com/vayunmathur/networklocation/provider/LocationReportingTaskTest.kt
diff --git a/networklocation/src/test/java/com/vayunmathur/networklocation/provider/LocationReportingTaskTest.kt b/networklocation/src/test/java/com/vayunmathur/networklocation/provider/LocationReportingTaskTest.ktindex 5cf6f0978b..8ff0b423fa 100644--- a/networklocation/src/test/java/com/vayunmathur/networklocation/provider/LocationReportingTaskTest.kt+++ b/networklocation/src/test/java/com/vayunmathur/networklocation/provider/LocationReportingTaskTest.kt@@ -82,6 +82,28 @@ class LocationReportingTaskTest { assertTrue(fix.time > 0L) } + @Test+ fun `a changed interval takes effect while running`() {+ clearStores()+ clearTelephony()+ val mac = BeaconKeys.parseMac("00:11:22:33:44:55")!!+ writeWpsStore(+ OfflineDatabases.file(context, OfflineDatabases.WIFI),+ BeaconKeys.WIFI_UNIVERSE_BITS,+ listOf(WpsRec(0L, mac, 3_742_150_000L, -12_208_400_000L, 25L, null)),+ )+ setWifi(listOf("00:11:22:33:44:55"))++ val latch = CountDownLatch(2)+ val task = LocationReportingTask(context) { latch.countDown() }+ task.start(60_000L)+ // A re-request at a faster cadence must restart the loop; if the new+ // interval were ignored, the second fix would be 60 s away.+ task.start(1_000L)+ assertTrue(latch.await(10, TimeUnit.SECONDS), "expected two fixes after interval change")+ task.stop()+ }+ @Test fun `no scanned beacons produces no fix`() { clearStores()