Skip to content

Colota - Android GPS Location tracker

Fix/motionless restore guard (#584)

Max Dietrich236e59e673a1
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 845534ce..f43efb38 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
@@ -294,10 +294,10 @@ class LocationForegroundService : Service() {
// so setupLocationUpdates() won't start GPS before onAvailable fires.
registerWifiPause()
}
- if (savedSettings[SettingsKeys.PAUSE_ZONE_MOTIONLESS_ACTIVE]?.toBoolean() == true) {
- isMotionlessPaused = true
- AppLogger.d(TAG, "Restored motionless pause state")
- }
+ restoreMotionlessHold(
+ restoredGeofence,
+ savedSettings[SettingsKeys.PAUSE_ZONE_MOTIONLESS_ACTIVE]?.toBoolean() == true
+ )
if (restoredGeofence?.heartbeatEnabled == true) {
startHeartbeat(
restoredGeofence.heartbeatIntervalMinutes,
@@ -1115,6 +1115,23 @@ class LocationForegroundService : Service() {
dbHelper.saveSetting(SettingsKeys.PAUSE_ZONE_MOTIONLESS_ACTIVE, "false")
}
+ /**
+ * Only the motion detector clears a motionless hold, and [ensureMotionDetectorRunning] starts it
+ * only while the zone still pauses on motionless. Restoring the flag without that zone strands it:
+ * [setupLocationUpdates] returns early, so GPS, the heartbeat logger and the pause watchdog never
+ * start, and nothing is left that could release it - not a restart, not force-stopping the app.
+ */
+ private fun restoreMotionlessHold(restoredGeofence: GeofenceHelper.Geofence?, savedActive: Boolean) {
+ if (!savedActive) return
+ if (restoredGeofence?.pauseOnMotionless == true) {
+ isMotionlessPaused = true
+ AppLogger.d(TAG, "Restored motionless pause state")
+ } else {
+ clearMotionlessPauseState()
+ AppLogger.w(TAG, "Dropped stale motionless pause for '$currentZoneName' - zone no longer pauses on motionless")
+ }
+ }
+
/**
* Starts a heartbeat that records a point at the zone center at a relaxed interval
* while paused in a geofence zone.
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 2fef344b..dc3ea4dc 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
@@ -2468,6 +2468,14 @@ class LocationForegroundServiceTest {
method.invoke(service)
}
+ private fun invokeRestoreMotionlessHold(geofence: GeofenceHelper.Geofence?, savedActive: Boolean) {
+ val method = LocationForegroundService::class.java.getDeclaredMethod(
+ "restoreMotionlessHold", GeofenceHelper.Geofence::class.java, Boolean::class.java
+ )
+ method.isAccessible = true
+ method.invoke(service, geofence, savedActive)
+ }
+
private fun invokeSetupLocationUpdates() {
val method = LocationForegroundService::class.java
.getDeclaredMethod("setupLocationUpdates")
@@ -2537,6 +2545,54 @@ class LocationForegroundServiceTest {
verify { locationProvider.requestLocationUpdates(any(), any(), any(), any()) }
}
+ // =========================================================================
+ // Motionless pause restore on service restart
+ // =========================================================================
+
+ @Test
+ fun `restore keeps the motionless hold while the zone still pauses on motionless`() {
+ setField("currentZoneName", "Home")
+
+ invokeRestoreMotionlessHold(geofence("Home", pauseOnMotionless = true), savedActive = true)
+
+ assertTrue(getField("isMotionlessPaused"))
+ verify(exactly = 0) { dbHelper.saveSetting(SettingsKeys.PAUSE_ZONE_MOTIONLESS_ACTIVE, "false") }
+ }
+
+ @Test
+ fun `restore drops a motionless hold whose geofence is gone`() {
+ // Only the motion detector clears this hold and it is never started for a zone that no longer
+ // exists, so keeping the flag would leave GPS off for good - surviving restarts and force-stops.
+ setField("currentZoneName", "Home")
+
+ invokeRestoreMotionlessHold(null, savedActive = true)
+
+ assertFalse(getField("isMotionlessPaused"))
+ verify { dbHelper.saveSetting(SettingsKeys.PAUSE_ZONE_MOTIONLESS_ACTIVE, "false") }
+ }
+
+ @Test
+ fun `restore drops a motionless hold when the zone no longer pauses on motionless`() {
+ setField("currentZoneName", "Home")
+
+ invokeRestoreMotionlessHold(geofence("Home", pauseOnMotionless = false), savedActive = true)
+
+ assertFalse(getField("isMotionlessPaused"))
+ verify { dbHelper.saveSetting(SettingsKeys.PAUSE_ZONE_MOTIONLESS_ACTIVE, "false") }
+ }
+
+ @Test
+ fun `GPS starts again after a stale motionless hold is dropped`() = runServiceTest {
+ setField("currentZoneName", "Home")
+ setField("insidePauseZone", true)
+ setField("isMotionlessPaused", true)
+
+ invokeRestoreMotionlessHold(null, savedActive = true)
+ invokeSetupLocationUpdates()
+
+ verify { locationProvider.requestLocationUpdates(any(), any(), any(), any()) }
+ }
+
// =========================================================================
// WiFi pause event reason
// =========================================================================
diff --git a/fastlane/metadata/android/en-US/changelogs/45.txt b/fastlane/metadata/android/en-US/changelogs/45.txt
index 62430ad5..59eb98e0 100644
--- a/fastlane/metadata/android/en-US/changelogs/45.txt
+++ b/fastlane/metadata/android/en-US/changelogs/45.txt
@@ -4,3 +4,4 @@
- Merge adjacent trips from the Trips selection bar when one journey got split up
- Split a trip by tapping the point on the map where the new trip should start
- Trips show an average speed instead of 0 km/h when the points carry no speed, including imported tracks
+- Tracking no longer gets stuck on Pause after a geofence is deleted or its motionless pause is turned off
\ No newline at end of file