Colota - Android GPS Location tracker
fix(battery): stop the recovery worker restarting into another battery stop (#651)
Max Dietrich886ede982df9
diff --git a/apps/mobile/android/app/src/main/java/com/colota/service/BatteryRecoveryWorker.kt b/apps/mobile/android/app/src/main/java/com/colota/service/BatteryRecoveryWorker.ktindex 951d3d85..e8e5e298 100644--- a/apps/mobile/android/app/src/main/java/com/colota/service/BatteryRecoveryWorker.kt+++ b/apps/mobile/android/app/src/main/java/com/colota/service/BatteryRecoveryWorker.kt@@ -17,6 +17,7 @@ import androidx.work.WorkerParametersimport com.Colota.data.DatabaseHelperimport com.Colota.data.SettingsKeysimport com.Colota.util.AppLogger+import com.Colota.util.DeviceInfoHelper/*** Resumes the location foreground service after a battery-critical (<5%) stop,@@ -66,6 +67,13 @@ class BatteryRecoveryWorker(return Result.success()}+ // The charging constraint can fire before WorkManager's tracker sees the unplug; the+ // sticky broadcast cannot go stale that way. Retry, not success: success drops the recovery.+ if (DeviceInfoHelper(appContext).isBatteryCritical()) {+ AppLogger.d(TAG, "Battery still critical and unplugged - not resuming")+ return Result.retry()+ }+// Liveness, not the intent flag: a flag left true by a service the system killed would// make this recovery a permanent no-op, which is the window it exists to cover.if (LocationForegroundService.isRunning) {diff --git a/apps/mobile/android/app/src/test/java/com/Colota/service/BatteryRecoveryWorkerTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/service/BatteryRecoveryWorkerTest.ktindex 24fe0b59..14129f8a 100644--- a/apps/mobile/android/app/src/test/java/com/Colota/service/BatteryRecoveryWorkerTest.kt+++ b/apps/mobile/android/app/src/test/java/com/Colota/service/BatteryRecoveryWorkerTest.kt@@ -6,6 +6,8 @@package com.Colota.serviceimport android.app.Application+import android.content.Intent+import android.os.BatteryManagerimport androidx.test.core.app.ApplicationProviderimport androidx.work.ListenableWorkerimport androidx.work.testing.TestListenableWorkerBuilder@@ -120,6 +122,36 @@ class BatteryRecoveryWorkerTest {verify { LocationServiceModule.sendTrackingStartedEvent(any()) }}+ /**+ * WorkManager can run a charging-constrained worker before its tracker has observed the+ * unplug, and resuming there restarts a service that stops on battery again and re-arms this.+ */+ @Test+ fun `does not resume while the battery is still critical and unplugged`() {+ db.saveSetting(SettingsKeys.STOPPED_BY_BATTERY, "true")+ setBattery(level = 4, plugged = 0, status = BatteryManager.BATTERY_STATUS_DISCHARGING)++ val result = runWorker()++ // retry, not success: the recovery must survive to fire on a real charge.+ assertEquals(ListenableWorker.Result.retry(), result)+ assertNull("Still critical - must not restart the service", shadowOf(app).nextStartedService)+ verify(exactly = 0) { LocationServiceModule.sendTrackingStartedEvent(any()) }+ }++ /** The guard reads level and plug state together, so charging at 4% is the recovery, not the loop. */+ @Test+ fun `resumes at a critical level once the charger is connected`() {+ db.saveSetting(SettingsKeys.STOPPED_BY_BATTERY, "true")+ setBattery(level = 4, plugged = BatteryManager.BATTERY_PLUGGED_AC, status = BatteryManager.BATTERY_STATUS_CHARGING)++ val result = runWorker()++ assertEquals(ListenableWorker.Result.success(), result)+ assertNotNull("Charging at 4% is the recovery case", shadowOf(app).nextStartedService)+ verify { LocationServiceModule.sendTrackingStartedEvent(any()) }+ }+@Testfun `structural FGS-start failure fails instead of retrying forever`() {db.saveSetting(SettingsKeys.STOPPED_BY_BATTERY, "true")@@ -137,4 +169,15 @@ class BatteryRecoveryWorkerTest {unmockkObject(LocationForegroundService.Companion)}}++ @Suppress("DEPRECATION")+ private fun setBattery(level: Int, plugged: Int, status: Int) {+ app.sendStickyBroadcast(+ Intent(Intent.ACTION_BATTERY_CHANGED)+ .putExtra(BatteryManager.EXTRA_LEVEL, level)+ .putExtra(BatteryManager.EXTRA_SCALE, 100)+ .putExtra(BatteryManager.EXTRA_STATUS, status)+ .putExtra(BatteryManager.EXTRA_PLUGGED, plugged)+ )+ }}diff --git a/fastlane/metadata/android/en-US/changelogs/49.txt b/fastlane/metadata/android/en-US/changelogs/49.txtindex 685ec2b5..08ad3e6b 100644--- a/fastlane/metadata/android/en-US/changelogs/49.txt+++ b/fastlane/metadata/android/en-US/changelogs/49.txt@@ -1 +1,2 @@- A notification now alerts you when tracking stops on its own, instead of appearing silently+- Unplugging at a low battery no longer restarts tracking in a loop