Skip to content

Colota - Android GPS Location tracker

fix(geofence): send the pause-zone heartbeat past the sync interval (#594)

Max Dietrichf22e725cd69a
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 7f8c505e..dc2f19b5 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
@@ -1212,7 +1212,8 @@ class LocationForegroundService : Service() {
* saved unconditionally and handed to [SyncManager], which owns whether it goes out now,
* waits in the queue or stays local in offline mode. Recording and sending are separate
* concerns here - gating the save on a successful send loses the stay entirely whenever
- * the server is unreachable or sync is not allowed.
+ * the server is unreachable or sync is not allowed. The send skips the sync interval: on
+ * Power Saver a queued stay reaches the endpoint 15 minutes after the user got home.
*/
private suspend fun recordHeartbeatLocation() {
if (!::config.isInitialized) {
@@ -1255,7 +1256,7 @@ class LocationForegroundService : Service() {
dbHelper.saveSetting(SettingsKeys.HEARTBEAT_LAST_AT, location.time.toString())
val payload = PayloadBuilder.buildLocationPayload(location, timestampSec, battery, batteryStatus, payloadFieldMap, payloadCustomFields, config.apiFormat)
- syncManager.queueAndSend(locationId, payload)
+ syncManager.queueAndSend(locationId, payload, bypassInterval = true)
AppLogger.i(TAG, "Heartbeat recorded for zone '${zone.name}'")
}
diff --git a/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
index ce34cb8b..9abdbac4 100644
--- a/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
+++ b/apps/mobile/android/app/src/main/java/com/colota/sync/SyncManager.kt
@@ -128,7 +128,7 @@ class SyncManager(
}
}
- suspend fun queueAndSend(locationId: Long, payload: JSONObject) {
+ suspend fun queueAndSend(locationId: Long, payload: JSONObject, bypassInterval: Boolean = false) {
if (isOfflineMode) {
AppLogger.d(TAG, "Skipping queue for location $locationId - offline mode")
return
@@ -144,7 +144,7 @@ class SyncManager(
}
// Immediate send mode (syncInterval = 0)
- if (syncIntervalSeconds == 0 && isSyncAllowed()) {
+ if ((syncIntervalSeconds == 0 || bypassInterval) && isSyncAllowed()) {
AppLogger.d(TAG, "Instant send")
val success = networkManager.sendToEndpoint(payload, endpoint, authHeaders, httpMethod, apiFormat)
@@ -157,7 +157,7 @@ class SyncManager(
dbHelper.incrementRetryCount(queueId, "Send failed")
}
}
- // If syncInterval > 0, the periodic sync job will handle it
+ // Otherwise the periodic sync job will handle it
}
fun isSyncAllowed(): Boolean {
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 ea9faa47..c1bed727 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
@@ -2832,7 +2832,7 @@ class LocationForegroundServiceTest {
invokeRecordHeartbeatLocation()
verify { dbHelper.saveLocation(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) }
- coVerify { syncManager.queueAndSend(1L, any()) }
+ coVerify { syncManager.queueAndSend(1L, any(), true) }
}
@Test
@@ -2848,6 +2848,18 @@ class LocationForegroundServiceTest {
verify { AppLogger.i("LocationService", "Heartbeat recorded for zone 'Home'") }
}
+ @Test
+ fun `recordHeartbeatLocation asks to send past the sync interval`() = testScope.runTest {
+ // Queued like an ordinary fix, the stay arrives a full sync interval late
+ every { syncManager.isSyncAllowed() } returns true
+ every { dbHelper.saveLocation(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } returns 1L
+ setField("currentZoneGeofence", homeGeofence)
+
+ invokeRecordHeartbeatLocation()
+
+ coVerify { syncManager.queueAndSend(1L, any(), true) }
+ }
+
@Test
fun `recordHeartbeatLocation skips when no current zone`() = testScope.runTest {
setField("currentZoneGeofence", null)
diff --git a/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt
index 3c749f38..b59ac00f 100644
--- a/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt
+++ b/apps/mobile/android/app/src/test/java/com/Colota/sync/SyncManagerTest.kt
@@ -310,6 +310,78 @@ class SyncManagerTest {
coVerify(exactly = 0) { networkManager.sendToEndpoint(any(), any(), any(), any()) }
}
+ // --- queueAndSend: bypassInterval ---
+
+ @Test
+ fun `queueAndSend bypassInterval sends despite a periodic interval`() = scope.runTest {
+ // Before #579 the heartbeat POSTed directly, ignoring the interval. This restores that
+ syncManager.updateConfig(
+ endpoint = "https://example.com",
+ syncIntervalSeconds = 900,
+ retryIntervalSeconds = 30,
+ isOfflineMode = false,
+ syncCondition = "any",
+ syncSsid = "",
+ authHeaders = emptyMap()
+ )
+
+ coEvery { networkManager.isNetworkAvailable() } returns true
+ coEvery { networkManager.sendToEndpoint(any(), any(), any(), any()) } returns true
+
+ val payload = JSONObject().put("lat", 52.0)
+ syncManager.queueAndSend(1L, payload, bypassInterval = true)
+
+ coVerify { networkManager.sendToEndpoint(any(), "https://example.com", emptyMap(), "POST") }
+ verify { dbHelper.removeFromQueueByLocationId(1L) }
+ }
+
+ @Test
+ fun `queueAndSend bypassInterval still honours the sync condition`() = scope.runTest {
+ // bypassInterval covers the interval only, never the network the user chose
+ syncManager.updateConfig(
+ endpoint = "https://example.com",
+ syncIntervalSeconds = 900,
+ retryIntervalSeconds = 30,
+ isOfflineMode = false,
+ syncCondition = "wifi_ssid",
+ syncSsid = "HomeNetwork",
+ authHeaders = emptyMap()
+ )
+
+ coEvery { networkManager.isNetworkAvailable() } returns true
+ every { networkManager.isConnectedToSsid("HomeNetwork") } returns false
+
+ val payload = JSONObject().put("lat", 52.0)
+ syncManager.queueAndSend(1L, payload, bypassInterval = true)
+
+ coVerify(exactly = 0) { networkManager.sendToEndpoint(any(), any(), any(), any()) }
+ verify { dbHelper.addToQueue(1L, any()) }
+ }
+
+ @Test
+ fun `queueAndSend bypassInterval leaves a failed send in the queue`() = scope.runTest {
+ // An unreachable server must not lose the point (#579)
+ every { dbHelper.addToQueue(any(), any()) } returns 42L
+ syncManager.updateConfig(
+ endpoint = "https://example.com",
+ syncIntervalSeconds = 900,
+ retryIntervalSeconds = 30,
+ isOfflineMode = false,
+ syncCondition = "any",
+ syncSsid = "",
+ authHeaders = emptyMap()
+ )
+
+ coEvery { networkManager.isNetworkAvailable() } returns true
+ coEvery { networkManager.sendToEndpoint(any(), any(), any(), any()) } returns false
+
+ val payload = JSONObject().put("lat", 52.0)
+ syncManager.queueAndSend(1L, payload, bypassInterval = true)
+
+ verify { dbHelper.incrementRetryCount(42L, "Send failed") }
+ verify(exactly = 0) { dbHelper.removeFromQueueByLocationId(any()) }
+ }
+
// --- queueAndSend: auth headers and HTTP method ---
@Test