Colota - Android GPS Location tracker
fix(service): say the location stream never started instead of printing uptime (#619)
Max Dietrich4b0c786a5768
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.ktindex 0b5d4d98..2a08de9d 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@@ -613,11 +613,10 @@ class LocationForegroundService : Service() {// Catch here so a DB or system-service hiccup doesn't kill the heartbeat loop.try {val state = trackingStateLabel()- val sinceLastFix = SystemClock.elapsedRealtime() - lastFixAtMsval (battery, _) = deviceInfoHelper.getCachedBatteryStatus()val doze = (getSystemService(POWER_SERVICE) as? PowerManager)?.isDeviceIdleMode ?: falseAppLogger.i(TAG,- "Heartbeat state=$state, ${sinceLastFix / 1000}s since last fix, " ++ "Heartbeat state=$state, ${sinceLastFixLabel()}, " +"queue=${dbHelper.getQueuedCount()}, batt=$battery%, " +"profile=${profileManager.getActiveProfileName() ?: "default"}, doze=$doze")@@ -661,6 +660,12 @@ class LocationForegroundService : Service() {else -> "ACTIVE"}+ /** 0 means nothing has stamped the field since this instance started (a restart under a hold never+ * registers the stream), and elapsedRealtime() - 0 is the phone's uptime, not a fix age. */+ private fun sinceLastFixLabel(): String =+ if (lastFixAtMs == 0L) "no location stream this instance"+ else "${(SystemClock.elapsedRealtime() - lastFixAtMs) / 1000}s since last fix"+private fun cancelTrackingHeartbeatLogger() {trackingHeartbeatJob?.cancel()trackingHeartbeatJob = nulldiff --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.ktindex 1e79c427..decb207e 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@@ -3,6 +3,7 @@ package com.Colota.serviceimport android.app.NotificationManagerimport android.content.Intentimport android.location.Location+import android.os.SystemClockimport com.Colota.bridge.LocationServiceModuleimport com.Colota.data.DatabaseHelperimport com.Colota.data.GeofenceHelper@@ -2137,6 +2138,22 @@ class LocationForegroundServiceTest {assertEquals("ACTIVE", invokeTrackingStateLabel())}+ /** A restart under a hold never registers the stream, so nothing stamps lastFixAtMs and the raw+ * subtraction prints the phone's uptime - a healthy soak read "137019s since last fix". */+ @Test+ fun `sinceLastFixLabel says the stream never started instead of printing uptime`() {+ setField("lastFixAtMs", 0L)++ assertEquals("no location stream this instance", invokeSinceLastFixLabel())+ }++ @Test+ fun `sinceLastFixLabel reports the age of a stamped fix`() {+ setField("lastFixAtMs", SystemClock.elapsedRealtime() - 30_000L)++ assertEquals("30s since last fix", invokeSinceLastFixLabel())+ }+// =========================================================================// stopForegroundServiceWithReason - battery critical path// =========================================================================@@ -2663,6 +2680,12 @@ class LocationForegroundServiceTest {return method.invoke(service) as String}+ private fun invokeSinceLastFixLabel(): String {+ val method = LocationForegroundService::class.java.getDeclaredMethod("sinceLastFixLabel")+ method.isAccessible = true+ return method.invoke(service) as String+ }+private fun invokeOnDestroy() {val method = LocationForegroundService::class.java.getDeclaredMethod("onDestroy")method.isAccessible = true