Colota - Android GPS Location tracker
fix: update speed buffer before distance filter to prevent stale speed profile. Closes #256
Max Dietrich296d509edb22
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 b3b7d455..d9b220b1 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@@ -464,6 +464,9 @@ class LocationForegroundService : Service() {applySpeedFallback(location)+ // Before distance filter so stationary locations still update the speed buffer+ profileManager.onLocationUpdate(location)+// Software-side distance filter (FLP bypasses the OS-level distance filter for some fixes)// Bypassed during geofence entry delay so stationary arrival points are loggedif (pendingPauseZone == null && config.minUpdateDistance > 0f && prev != null) {@@ -476,9 +479,6 @@ class LocationForegroundService : Service() {AppLogger.d(TAG, "Location received: acc=${location.accuracy}m provider=${location.provider}")- // After accuracy filter so bad GPS doesn't pollute speed average- profileManager.onLocationUpdate(location)-lastKnownLocation = locationval zone = geofenceHelper.getPauseZone(location)@@ -597,6 +597,8 @@ class LocationForegroundService : Service() {if (geofence.pauseOnWifi) registerWifiPause()if (geofence.pauseOnMotionless) startMotionlessCountdown(geofence.motionlessTimeoutMinutes)+ profileManager.clearSpeedBuffer()+AppLogger.d(TAG, "Entered pause zone: ${geofence.name}")}diff --git a/apps/mobile/android/app/src/main/java/com/colota/service/ProfileManager.kt b/apps/mobile/android/app/src/main/java/com/colota/service/ProfileManager.ktindex 673aa05c..9f21ad57 100644--- a/apps/mobile/android/app/src/main/java/com/colota/service/ProfileManager.kt+++ b/apps/mobile/android/app/src/main/java/com/colota/service/ProfileManager.kt@@ -173,6 +173,15 @@ class ProfileManager(return result}+ /** Clears stale speed readings (e.g. when entering a geofence pause zone). */+ fun clearSpeedBuffer() {+ synchronized(speedLock) {+ speedBuffer.clear()+ }+ AppLogger.d(TAG, "Speed buffer cleared")+ evaluate()+ }+private fun getAverageSpeed(): Float? {synchronized(speedLock) {if (speedBuffer.isEmpty()) return nulldiff --git a/apps/mobile/android/app/src/test/java/com/Colota/service/ProfileManagerTest.kt b/apps/mobile/android/app/src/test/java/com/Colota/service/ProfileManagerTest.ktindex c0b4912e..f054083c 100644--- a/apps/mobile/android/app/src/test/java/com/Colota/service/ProfileManagerTest.kt+++ b/apps/mobile/android/app/src/test/java/com/Colota/service/ProfileManagerTest.kt@@ -757,6 +757,62 @@ class ProfileManagerTest {assertFalse(manager.isStationary)}+ // --- Clear speed buffer ---++ @Test+ fun `clearSpeedBuffer causes speed profile to deactivate`() = testScope.runTest {+ val profile = speedAboveProfile(threshold = 10f, priority = 10)+ every { profileHelper.getEnabledProfiles() } returns listOf(profile)++ val manager = createManager()+ manager.defaultInterval = 5000L+ manager.defaultDistance = 0f+ manager.defaultSyncInterval = 0++ // Fill buffer with fast speeds - profile activates+ repeat(ProfileConstants.SPEED_BUFFER_SIZE) {+ manager.onLocationUpdate(mockLocation(15f))+ }+ assertEquals("Fast", switchedProfileName)++ // Clear buffer (e.g. entering geofence pause zone)+ manager.clearSpeedBuffer()++ // Wait past deactivation delay+ advanceTimeBy(31_000)++ // Should have reverted to defaults+ assertNull(switchedProfileName)+ assertEquals(5000L, switchedInterval)+ }++ @Test+ fun `clearSpeedBuffer allows speed below profile to stop matching`() = testScope.runTest {+ val profile = speedBelowProfile(threshold = 10f, priority = 5)+ every { profileHelper.getEnabledProfiles() } returns listOf(profile)++ val manager = createManager()+ manager.defaultInterval = 5000L+ manager.defaultDistance = 0f+ manager.defaultSyncInterval = 0++ // Fill buffer with slow speeds - profile activates+ repeat(ProfileConstants.SPEED_BUFFER_SIZE) {+ manager.onLocationUpdate(mockLocation(5f))+ }+ assertEquals("Slow", switchedProfileName)++ // Clear buffer+ manager.clearSpeedBuffer()++ // Wait past deactivation delay+ advanceTimeBy(61_000)++ // Should have reverted to defaults (avgSpeed is null, condition doesn't match)+ assertNull(switchedProfileName)+ assertEquals(5000L, switchedInterval)+ }+// --- Unknown condition type ---@Test