Skip to content

Colota - Android GPS Location tracker

fix: respect sync condition in heartbeat and zone entry flush

Max Dietrich984e0641c212
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 2e4f8850..e69f3421 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
@@ -664,7 +664,9 @@ class LocationForegroundService : Service() {
profileManager.clearSpeedBuffer()
// Flush any queued points so the backend shows the arrival position
- serviceScope?.launch { syncManager.manualFlush() }
+ if (syncManager.isSyncAllowed()) {
+ serviceScope?.launch { syncManager.manualFlush() }
+ }
AppLogger.d(TAG, "Entered pause zone: ${geofence.name} (heartbeat=${geofence.heartbeatEnabled}, wifi=${geofence.pauseOnWifi}, motionless=${geofence.pauseOnMotionless})")
}
@@ -874,8 +876,11 @@ class LocationForegroundService : Service() {
customFields
)
- // Send immediately regardless of sync condition - the whole point of
- // a heartbeat is keeping the device visible on the server.
+ if (!syncManager.isSyncAllowed()) {
+ AppLogger.d(TAG, "Heartbeat skipped: sync condition not met")
+ return
+ }
+
val sent = networkManager.sendToEndpoint(
payload, config.endpoint, secureStorage.getAuthHeaders(),
config.httpMethod, config.apiFormat
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 e755061b..33da13f8 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
@@ -1910,6 +1910,82 @@ class LocationForegroundServiceTest {
verify { payloadBuilder.buildPayload(any(), any(), any(), emptyMap(), any(), any()) }
}
+ // =========================================================================
+ // enterPauseZone - flush respects sync condition
+ // =========================================================================
+
+ @Test
+ fun `enterPauseZone flushes queue when sync is allowed`() = testScope.runTest {
+ every { syncManager.isSyncAllowed() } returns true
+ val location = mockLocation()
+ setField("lastKnownLocation", location)
+
+ invokeEnterPauseZone(homeGeofence)
+
+ coVerify { syncManager.manualFlush() }
+ }
+
+ @Test
+ fun `enterPauseZone skips flush when sync condition not met`() = testScope.runTest {
+ every { syncManager.isSyncAllowed() } returns false
+ val location = mockLocation()
+ setField("lastKnownLocation", location)
+
+ invokeEnterPauseZone(homeGeofence)
+
+ coVerify(exactly = 0) { syncManager.manualFlush() }
+ }
+
+ // =========================================================================
+ // sendHeartbeatLocation - sync condition check
+ // =========================================================================
+
+ @Test
+ fun `sendHeartbeatLocation skips send when sync condition not met`() = testScope.runTest {
+ every { networkManager.isNetworkAvailable() } returns true
+ every { syncManager.isSyncAllowed() } returns false
+ setField("currentZoneGeofence", homeGeofence)
+
+ invokeSendHeartbeatLocation()
+
+ coVerify(exactly = 0) { networkManager.sendToEndpoint(any(), any(), any(), any(), any()) }
+ verify { AppLogger.d("LocationService", "Heartbeat skipped: sync condition not met") }
+ }
+
+ @Test
+ fun `sendHeartbeatLocation sends when sync condition is met`() = testScope.runTest {
+ every { networkManager.isNetworkAvailable() } returns true
+ every { syncManager.isSyncAllowed() } returns true
+ coEvery { networkManager.sendToEndpoint(any(), any(), any(), any(), any()) } returns true
+ every { dbHelper.saveLocation(any(), any(), any(), any(), any(), any(), any(), any(), any(), any()) } returns 1L
+ setField("currentZoneGeofence", homeGeofence)
+
+ invokeSendHeartbeatLocation()
+
+ coVerify { networkManager.sendToEndpoint(any(), "https://example.com", any(), any(), any()) }
+ }
+
+ @Test
+ fun `sendHeartbeatLocation skips when no network available`() = testScope.runTest {
+ every { networkManager.isNetworkAvailable() } returns false
+ setField("currentZoneGeofence", homeGeofence)
+
+ invokeSendHeartbeatLocation()
+
+ coVerify(exactly = 0) { networkManager.sendToEndpoint(any(), any(), any(), any(), any()) }
+ }
+
+ @Test
+ fun `sendHeartbeatLocation skips when no current zone`() = testScope.runTest {
+ every { networkManager.isNetworkAvailable() } returns true
+ every { syncManager.isSyncAllowed() } returns true
+ setField("currentZoneGeofence", null)
+
+ invokeSendHeartbeatLocation()
+
+ coVerify(exactly = 0) { networkManager.sendToEndpoint(any(), any(), any(), any(), any()) }
+ }
+
// =========================================================================
// Reflection helpers
// =========================================================================
@@ -1970,6 +2046,12 @@ class LocationForegroundServiceTest {
motionlessTimeoutMinutes: Int = 10
) = GeofenceHelper.CachedGeofence(name, lat, lon, radius, pauseOnWifi, pauseOnMotionless, motionlessTimeoutMinutes)
+ private fun invokeSendHeartbeatLocation() {
+ val method = LocationForegroundService::class.java.getDeclaredMethod("sendHeartbeatLocation")
+ method.isAccessible = true
+ kotlinx.coroutines.runBlocking { method.invoke(service) }
+ }
+
private val homeGeofence = geofence("Home", 52.50, 13.40, 150.0)
private val officeGeofence = geofence("Office", 48.14, 11.58, 200.0)
private val parkGeofence = geofence("Park", 52.51, 13.35, 100.0)