Skip to content

Colota - Android GPS Location tracker

feat: add passive location listener for free GPS fixes between active intervals (#218)

Max Dietrich80fe50c87199
diff --git a/apps/docs/docs/configuration/gps-settings.md b/apps/docs/docs/configuration/gps-settings.md
index fb4e7d45..48e3e516 100644
--- a/apps/docs/docs/configuration/gps-settings.md
+++ b/apps/docs/docs/configuration/gps-settings.md
@@ -15,7 +15,7 @@ sidebar_position: 2
## Tracking Interval
-How often the app requests a GPS fix. Shorter intervals give denser track points but drain more battery.
+How often the app requests a GPS fix. Shorter intervals give denser track points but drain more battery. The actual recording frequency may be higher due to the passive location listener picking up free fixes from other apps.
- **1-5 seconds**: High detail, suitable for driving or cycling
- **15-30 seconds**: Good balance for walking or commuting
@@ -33,3 +33,9 @@ Only records a new location if you've moved at least this many meters since the
When enabled, GPS fixes with accuracy worse than the threshold are discarded. This prevents recording poor-quality positions from indoor or urban environments.
The Google Play variant uses Android's `HIGH_ACCURACY` positioning mode via FusedLocationProvider, which combines GPS, Wi-Fi, and cellular data. The FOSS variant uses Android's native `LocationManager` with `GPS_PROVIDER` directly.
+
+## Passive Location Listener
+
+In addition to the configured tracking interval, Colota listens for location fixes already requested by other apps on your device (such as maps or navigation apps). These passive fixes are recorded at no extra battery cost and may result in additional data points between your configured interval.
+
+This is intentional behavior - it improves track density when other apps happen to request a location. If you see more points than expected, this is why. The accuracy filter still applies, so poor-quality passive fixes are discarded.
diff --git a/apps/docs/docs/guides/battery-optimization.md b/apps/docs/docs/guides/battery-optimization.md
index ec9849c0..b4a8c486 100644
--- a/apps/docs/docs/guides/battery-optimization.md
+++ b/apps/docs/docs/guides/battery-optimization.md
@@ -12,6 +12,7 @@ Settings and tips to reduce battery usage without losing GPS fixes.
- **Batch processing**: 50 items per batch, 10 concurrent network requests
- **Smart sync**: Only syncs when queue has items and network is available
- **Battery critical shutdown**: Stops tracking below 5% (when unplugged)
+- **Passive location listener**: Records free fixes from other apps at zero battery cost, improving track density without extra GPS usage
## Tips
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 e9e58e07..74bf2fb7 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
@@ -18,6 +18,8 @@ import com.Colota.util.DeviceInfoHelper
import com.Colota.util.SecureStorageHelper
import android.content.Intent
import android.content.pm.ServiceInfo
+import android.location.LocationListener
+import android.location.LocationManager
import android.os.*
import androidx.core.app.ServiceCompat
import com.Colota.location.LocationProvider
@@ -45,6 +47,7 @@ class LocationForegroundService : Service() {
@Volatile private var serviceScope: CoroutineScope? = null
@Volatile private var locationUpdateCallback: LocationUpdateCallback? = null
+ @Volatile private var passiveLocationListener: LocationListener? = null
@Volatile private var locationRestartJob: Job? = null
@Volatile private var insidePauseZone = false
@Volatile private var currentZoneName: String? = null
@@ -277,6 +280,7 @@ class LocationForegroundService : Service() {
},
onFailure = { /* initial location unavailable, updates will arrive */ }
)
+ startPassiveListener()
} catch (e: SecurityException) {
AppLogger.e(TAG, "Location permission missing", e)
stopForegroundServiceWithReason("Location permission missing")
@@ -289,6 +293,33 @@ class LocationForegroundService : Service() {
private fun stopLocationUpdates() {
locationUpdateCallback?.let { locationProvider.removeLocationUpdates(it) }
locationUpdateCallback = null
+ stopPassiveListener()
+ }
+
+ private fun startPassiveListener() {
+ try {
+ val locationManager = getSystemService(LOCATION_SERVICE) as LocationManager
+ val listener = LocationListener { location -> handleLocationUpdate(location) }
+ locationManager.requestLocationUpdates(
+ LocationManager.PASSIVE_PROVIDER, 1000L, 0f, listener, Looper.getMainLooper()
+ )
+ passiveLocationListener = listener
+ AppLogger.d(TAG, "Passive location listener started")
+ } catch (e: Exception) {
+ AppLogger.w(TAG, "Failed to start passive listener: ${e.message}")
+ }
+ }
+
+ private fun stopPassiveListener() {
+ passiveLocationListener?.let {
+ try {
+ val locationManager = getSystemService(LOCATION_SERVICE) as LocationManager
+ locationManager.removeUpdates(it)
+ } catch (e: Exception) {
+ AppLogger.w(TAG, "Failed to remove passive listener: ${e.message}")
+ }
+ }
+ passiveLocationListener = null
}
private fun handleZoneRecheckAction() {
@@ -386,6 +417,15 @@ class LocationForegroundService : Service() {
return
}
+ // Software-side distance filter (passive fixes bypass the OS-level distance filter)
+ if (config.minUpdateDistance > 0f && prev != null) {
+ val distance = prev.distanceTo(location)
+ if (distance < config.minUpdateDistance) {
+ AppLogger.d(TAG, "Location filtered: distance ${String.format(Locale.US, "%.1f", distance)}m < threshold ${config.minUpdateDistance}m")
+ return
+ }
+ }
+
AppLogger.d(TAG, "Location received: ${String.format(Locale.US, "%.5f, %.5f", location.latitude, location.longitude)} acc=${location.accuracy}m provider=${location.provider}")
applySpeedFallback(location)