Skip to content

Colota - Android GPS Location tracker

fix(service): keep the entry delay through a profile switch (#930)

Max Dietrich9ed73239c0fe
diff --git a/apps/docs/docs/guides/geofencing.md b/apps/docs/docs/guides/geofencing.md
index 9276d965..30a750f8 100644
--- a/apps/docs/docs/guides/geofencing.md
+++ b/apps/docs/docs/guides/geofencing.md
@@ -85,6 +85,7 @@ See the [Deep Link Setup](deep-link-setup.md#geofences) guide for the full paylo
- The notification shows "Paused" with "Inside zone", "Zone WiFi" or "No movement" and when tracking resumes. The zone name only appears on the Dashboard
- By default, GPS continues running inside the zone to detect when you leave. If **WiFi pause** or **motionless pause** is enabled, GPS stops entirely inside the zone and zone exit is detected when GPS resumes
- If you leave before the entry delay completes, the delay is cancelled and tracking continues uninterrupted
+- Switching tracking profiles during the entry delay does not skip or restart it
- A stationary profile stays active inside a zone, but records nothing while WiFi pause or motionless pause has GPS off
- When exiting the zone, tracking automatically resumes
- If GPS goes quiet while paused (for example, indoors for a long stretch), Colota periodically requests a fresh position to check whether you have left
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 de235b37..d1ae63de 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
@@ -438,7 +438,7 @@ class LocationForegroundService : Service() {
stopLocationUpdates()
syncManager.stopPeriodicSync()
- setupLocationUpdates()
+ setupLocationUpdates(skipEntryDelay = true)
syncManager.startPeriodicSync()
// Start after setup so profile evaluations don't race
@@ -498,7 +498,7 @@ class LocationForegroundService : Service() {
override fun onBind(intent: Intent?): IBinder? = null
- private fun setupLocationUpdates() {
+ private fun setupLocationUpdates(skipEntryDelay: Boolean = false) {
if (isWifiPaused || isMotionlessPaused) return // GPS intentionally stopped by a zone pause hold
// The resume paths reach here without a stop, and the assignment below would orphan the old
@@ -547,7 +547,8 @@ class LocationForegroundService : Service() {
// A restored pause must be re-checked against a fresh fix: a cached fix can only re-enter,
// never exit, so a departed user would stay latched. A usable fix resumes only if it places
// us outside the zone (via recheck); with no usable fix, hold the pause - the watchdog resumes
- // once a real fix confirms a departure. Cold start latches a pause only from a fresh last-known fix.
+ // once a real fix confirms a departure. Only a full start latches a pause from a fresh last-known fix;
+ // any other restart goes through the entry delay, so the arrival points are still recorded.
val restoredPause = insidePauseZone
if (restoredPause) {
requestFreshOrLastLocation { location, _ ->
@@ -559,10 +560,12 @@ class LocationForegroundService : Service() {
} else {
locationProvider.getLastLocation(
onSuccess = { location ->
+ if (insidePauseZone) return@getLastLocation
if (location != null && isFixFresh(location)) {
lastKnownLocation = location
geofenceHelper.getPauseZone(location)?.let { zone ->
- enterPauseZone(zone)
+ if (skipEntryDelay) enterPauseZone(zone)
+ else startEntryDelay(zone)
} ?: run {
updateNotification(forceUpdate = true)
}
@@ -892,7 +895,7 @@ class LocationForegroundService : Service() {
private fun applyZoneTransition(zone: GeofenceHelper.Geofence?): Job? {
return when {
zone != null && (!insidePauseZone || zone.name != currentZoneName) -> {
- if (pendingPauseZone?.name != zone.name) startEntryDelay(zone)
+ startEntryDelay(zone)
null
}
zone == null && pendingPauseZone != null -> { cancelEntryDelay(); null }
@@ -1019,6 +1022,7 @@ class LocationForegroundService : Service() {
* If the device exits the zone before the delay completes, the delay is cancelled.
*/
private fun startEntryDelay(geofence: GeofenceHelper.Geofence) {
+ if (pendingPauseZone?.name == geofence.name) return
entryDelayJob?.cancel()
pendingPauseZone = geofence
@@ -1584,7 +1588,6 @@ class LocationForegroundService : Service() {
// the old listener firing during an async coroutine window.
locationRestartJob?.cancel()
locationRestartJob = null
- if (pendingPauseZone != null) cancelEntryDelay()
stopLocationUpdates()
setupLocationUpdates()
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 a9a568fc..5ab8d3a5 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
@@ -1678,6 +1678,18 @@ class LocationForegroundServiceTest {
assertEquals(officeGeofence, getField<GeofenceHelper.Geofence?>("pendingPauseZone"))
}
+ // Every fix inside the zone calls it again.
+ @Test
+ fun `startEntryDelay keeps the running delay when asked again for the same zone`() {
+ invokeStartEntryDelay(homeGeofence)
+ val firstJob: Job? = getField("entryDelayJob")
+
+ invokeStartEntryDelay(homeGeofence)
+
+ assertSame(firstJob, getField<Job?>("entryDelayJob"))
+ assertFalse(firstJob?.isCancelled == true)
+ }
+
@Test
fun `cancelEntryDelay clears pendingPauseZone and job`() {
invokeStartEntryDelay(homeGeofence)
@@ -1748,6 +1760,21 @@ class LocationForegroundServiceTest {
assertEquals(60, config.syncIntervalSeconds)
}
+ @Test
+ fun `a profile switch with the last fix inside a zone starts the entry delay instead of pausing at once`() {
+ setField("insidePauseZone", false)
+ val atHome = mockLocation()
+ every { geofenceHelper.getPauseZone(atHome) } returns homeGeofence
+ every { locationProvider.getLastLocation(any(), any()) } answers {
+ firstArg<(Location?) -> Unit>()(atHome)
+ }
+
+ invokeApplyProfileConfig(interval = 300_000L, distance = 0f, syncInterval = 0)
+
+ assertFalse(getField("insidePauseZone"))
+ assertEquals(homeGeofence, getField<GeofenceHelper.Geofence?>("pendingPauseZone"))
+ }
+
@Test
fun `applyProfileConfig arms the stationary heartbeat for a stationary profile`() {
invokeApplyProfileConfig(
@@ -1851,15 +1878,51 @@ class LocationForegroundServiceTest {
}
@Test
- fun `applyProfileConfig cancels pending entry delay`() {
+ fun `a profile switch keeps a pending entry delay running`() {
val mockJob = mockk<Job>(relaxed = true)
setField("entryDelayJob", mockJob)
setField("pendingPauseZone", homeGeofence)
+ val atHome = mockLocation()
+ every { geofenceHelper.getPauseZone(atHome) } returns homeGeofence
+ every { locationProvider.getLastLocation(any(), any()) } answers { firstArg<(Location?) -> Unit>()(atHome) }
invokeApplyProfileConfig(interval = 2000L, distance = 5f, syncInterval = 30)
+ invokeApplyProfileConfig(interval = 300_000L, distance = 0f, syncInterval = 0)
+
+ verify(exactly = 0) { mockJob.cancel() }
+ assertSame(mockJob, getField<Job?>("entryDelayJob"))
+ assertEquals(homeGeofence, getField<GeofenceHelper.Geofence?>("pendingPauseZone"))
+ assertFalse(getField("insidePauseZone"))
+ }
+
+ // The GMS last fix can arrive after the delay has already paused.
+ @Test
+ fun `a last fix that lands after the entry delay paused starts no second delay`() {
+ setField("insidePauseZone", false)
+ val atHome = mockLocation()
+ every { geofenceHelper.getPauseZone(atHome) } returns homeGeofence
+ val onSuccess = slot<(Location?) -> Unit>()
+ every { locationProvider.getLastLocation(capture(onSuccess), any()) } just Runs
+
+ invokeApplyProfileConfig(interval = 300_000L, distance = 0f, syncInterval = 0)
+ setField("insidePauseZone", true)
+ setField("pendingPauseZone", null)
+ onSuccess.captured(atHome)
- verify { mockJob.cancel() }
assertNull(getField<GeofenceHelper.Geofence?>("pendingPauseZone"))
+ assertNull(getField<Job?>("entryDelayJob"))
+ }
+
+ @Test
+ fun `a full start with a fresh fix inside a zone pauses at once`() {
+ setField("insidePauseZone", false)
+ val atHome = mockLocation()
+ every { geofenceHelper.getPauseZone(atHome) } returns homeGeofence
+ every { locationProvider.getLastLocation(any(), any()) } answers { firstArg<(Location?) -> Unit>()(atHome) }
+
+ invokeSetupLocationUpdates(skipEntryDelay = true)
+
+ assertTrue(getField("insidePauseZone"))
}
@Test
@@ -2830,11 +2893,11 @@ class LocationForegroundServiceTest {
method.invoke(service, geofence, savedActive)
}
- private fun invokeSetupLocationUpdates() {
+ private fun invokeSetupLocationUpdates(skipEntryDelay: Boolean = false) {
val method = LocationForegroundService::class.java
- .getDeclaredMethod("setupLocationUpdates")
+ .getDeclaredMethod("setupLocationUpdates", Boolean::class.javaPrimitiveType)
method.isAccessible = true
- method.invoke(service)
+ method.invoke(service, skipEntryDelay)
}
private fun invokeHandleRecheckProfiles() {
diff --git a/fastlane/metadata/android/en-US/changelogs/52.txt b/fastlane/metadata/android/en-US/changelogs/52.txt
index 7111ccb0..f31a4e40 100644
--- a/fastlane/metadata/android/en-US/changelogs/52.txt
+++ b/fastlane/metadata/android/en-US/changelogs/52.txt
@@ -1 +1,2 @@
- Colota is now available in Spanish
+- Profiles no longer skip a pause zone's entry delay