Skip to content

Colota - Android GPS Location tracker

fix(service): release a restored motionless hold on the next motion (#984)

Max Dietrich57badcfbf26b
diff --git a/apps/docs/docs/development/architecture.md b/apps/docs/docs/development/architecture.md
index 88191029..6a636cef 100644
--- a/apps/docs/docs/development/architecture.md
+++ b/apps/docs/docs/development/architecture.md
@@ -248,7 +248,7 @@ Each geofence supports three independent GPS pause modes, configured per zone:
- **Pause tracking** - Stops saving and syncing locations inside the zone. GPS continues running to detect exit.
- **WiFi pause** - Stops GPS entirely when connected to an unmetered network (WiFi/Ethernet). Implemented via `ConnectivityManager.NetworkCallback`, which fires immediately on network availability changes. An active network counter handles devices with multiple simultaneous unmetered networks - GPS only resumes once all of them are gone, after a short debounce.
-- **Motionless pause** - Stops GPS after the device has been still for the configured per-zone dwell window (default 1 minute). Stillness is detected by `RawSensorMotionDetector` (batched accelerometer variance + parallel SIG_MOTION); any motion above the variance threshold resets the timer. The same detector fires GPS resume when movement returns.
+- **Motionless pause** - Stops GPS after the device has been still for the configured per-zone dwell window (default 1 minute). Stillness is detected by `RawSensorMotionDetector` (batched accelerometer variance + parallel SIG_MOTION); any motion above the variance threshold resets the timer. The same detector fires GPS resume when movement returns. A hold restored after a service restart starts the detector from a STATIONARY baseline, because a fresh detector starts in MOVING and only a MOVING edge releases the hold.
A per-zone **stationary heartbeat** records a point at the geofence center at a relaxed interval while paused. The point is a synthetic fix, so no GPS wake is required. The save is unconditional and transmission goes through `SyncManager.queueAndSend`, so offline mode and sync conditions gate the upload without dropping the recording. It fires once on zone entry; restoring a zone after a service restart schedules the interval without recording, so repeated restarts cannot stack duplicate points. Configured via `heartbeatEnabled` and `heartbeatIntervalMinutes` per geofence.
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 68833d05..e0c034b3 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
@@ -345,6 +345,8 @@ class LocationForegroundService : Service() {
AppLogger.d(TAG, "Restored heartbeat: ${restoredGeofence.heartbeatIntervalMinutes}min")
}
ensureMotionDetectorRunning()
+ // The detector starts out moving and releases the hold only on a change to moving
+ if (isMotionlessPaused) (motionDetector as? RawSensorMotionDetector)?.resyncStationaryBaseline()
}
}
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 cb8364fa..bb97a4c8 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
@@ -1,8 +1,13 @@
package com.Colota.service
import android.app.NotificationManager
+import android.content.Context
import android.content.Intent
+import android.hardware.SensorManager
+import android.hardware.TriggerEventListener
import android.location.Location
+import android.os.Handler
+import android.os.HandlerThread
import android.os.SystemClock
import com.Colota.bridge.LocationServiceModule
import com.Colota.data.DatabaseHelper
@@ -3063,6 +3068,60 @@ class LocationForegroundServiceTest {
verify { dbHelper.saveSetting(SettingsKeys.PAUSE_ZONE_MOTIONLESS_ACTIVE, "false") }
}
+ @Test
+ fun `motion after a restart under a motionless hold turns GPS back on`() = runServiceTest {
+ val sensorManager = mockk<SensorManager>(relaxed = true)
+ val sigMotion = slot<TriggerEventListener>()
+ every { sensorManager.getDefaultSensor(any()) } returns mockk()
+ every { sensorManager.requestTriggerSensor(capture(sigMotion), any()) } returns true
+ val sensorContext = mockk<Context> { every { getSystemService(Context.SENSOR_SERVICE) } returns sensorManager }
+ mockkConstructor(Handler::class, HandlerThread::class)
+ every { anyConstructed<Handler>().post(any()) } answers { firstArg<Runnable>().run(); true }
+ every { anyConstructed<HandlerThread>().start() } just Runs
+ every { anyConstructed<HandlerThread>().looper } returns mockk(relaxed = true)
+ try {
+ makeCold()
+ setField("motionDetector", RawSensorMotionDetector(sensorContext))
+ every { dbHelper.getAllSettings() } returns mapOf(
+ SettingsKeys.TRACKING_ENABLED to "true",
+ SettingsKeys.PAUSE_ZONE_NAME to "Home",
+ SettingsKeys.PAUSE_ZONE_MOTIONLESS_ACTIVE to "true"
+ )
+ every { geofenceHelper.getGeofenceByName("Home") } returns geofence("Home", pauseOnMotionless = true)
+
+ service.onStartCommand(intentFor(null), 0, 1)
+ advanceUntilIdle()
+ verify(exactly = 0) { locationProvider.requestLocationUpdates(any(), any(), any(), any(), any()) }
+
+ sigMotion.captured.onTrigger(mockk(relaxed = true))
+ advanceUntilIdle()
+
+ assertFalse(getField("isMotionlessPaused"))
+ verify { locationProvider.requestLocationUpdates(any(), any(), any(), any(), any()) }
+ } finally {
+ unmockkConstructor(Handler::class, HandlerThread::class)
+ }
+ }
+
+ @Test
+ fun `a restart in a motionless zone without a hold leaves the detector to find stillness`() = runServiceTest {
+ // Starting from still would hide the change to still that engages the hold
+ makeCold()
+ val detector = mockk<RawSensorMotionDetector>(relaxed = true)
+ setField("motionDetector", detector)
+ every { dbHelper.getAllSettings() } returns mapOf(
+ SettingsKeys.TRACKING_ENABLED to "true",
+ SettingsKeys.PAUSE_ZONE_NAME to "Home"
+ )
+ every { geofenceHelper.getGeofenceByName("Home") } returns geofence("Home", pauseOnMotionless = true)
+
+ service.onStartCommand(intentFor(null), 0, 1)
+ advanceUntilIdle()
+
+ verify { detector.start(any()) }
+ verify(exactly = 0) { detector.resyncStationaryBaseline() }
+ }
+
@Test
fun `GPS starts again after a stale motionless hold is dropped`() = runServiceTest {
setField("currentZoneName", "Home")
diff --git a/fastlane/metadata/android/en-US/changelogs/53.txt b/fastlane/metadata/android/en-US/changelogs/53.txt
index d0ece16d..a7abc5ac 100644
--- a/fastlane/metadata/android/en-US/changelogs/53.txt
+++ b/fastlane/metadata/android/en-US/changelogs/53.txt
@@ -3,3 +3,4 @@
- Fixed a trip's average speed and its speed and elevation charts being skewed towards stretches recorded with more points
- Positioning accuracy setting in Tracking & sync: High accuracy (default) or Lower accuracy to save battery
- Fixed tracking recording nothing for hours after Android recreated it
+- Fixed GPS staying off when leaving a zone with motionless pause after the app was updated or restarted