Skip to content

Colota - Android GPS Location tracker

refactor: drop NETWORK_PROVIDER from FOSS location provider

Max Dietrich24d4132082bc
diff --git a/apps/docs/docs/development/architecture.md b/apps/docs/docs/development/architecture.md
index caa1967f..42a8e684 100644
--- a/apps/docs/docs/development/architecture.md
+++ b/apps/docs/docs/development/architecture.md
@@ -77,7 +77,7 @@ Emits events back to JavaScript:
Location services are abstracted behind a `LocationProvider` interface (`location/LocationProvider.kt`), with flavor-specific implementations:
- **GMS** (`src/gms/`) - `GmsLocationProvider` wraps Google Play Services `FusedLocationProviderClient`
-- **FOSS** (`src/foss/`) - `NativeLocationProvider` wraps Android's native `LocationManager` with `GPS_PROVIDER` and `NETWORK_PROVIDER` fallback
+- **FOSS** (`src/foss/`) - `NativeLocationProvider` wraps Android's native `LocationManager` with `GPS_PROVIDER`
Each flavor provides a `LocationProviderFactory` that instantiates and returns the correct implementation at runtime. The service and bridge code in `src/main/` depends only on the `LocationProvider` interface, never on a concrete class.
diff --git a/apps/mobile/android/app/src/foss/java/com/Colota/location/NativeLocationProvider.kt b/apps/mobile/android/app/src/foss/java/com/Colota/location/NativeLocationProvider.kt
index 38a0f322..89ae6b8d 100644
--- a/apps/mobile/android/app/src/foss/java/com/Colota/location/NativeLocationProvider.kt
+++ b/apps/mobile/android/app/src/foss/java/com/Colota/location/NativeLocationProvider.kt
@@ -15,26 +15,19 @@ import android.os.Looper
import com.Colota.util.AppLogger
/**
- * LocationManager-based provider for FOSS flavor (no Google Play Services).
- * Uses GPS as primary source with NETWORK_PROVIDER as coarse fallback.
+ * LocationManager-based GPS provider for FOSS flavor (no Google Play Services).
*/
@SuppressLint("MissingPermission")
class NativeLocationProvider(context: Context) : LocationProvider {
companion object {
private const val TAG = "NativeLocationProvider"
- /** Ignore network updates for this long after the last GPS fix. */
- private const val GPS_SUPPRESSION_MS = 10_000L
}
private val locationManager: LocationManager =
context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
- /** Maps callback -> [GPS listener, network listener?]. */
- private val listenerMap = java.util.concurrent.ConcurrentHashMap<LocationUpdateCallback, List<LocationListener>>()
-
- /** Last GPS fix time; suppresses redundant network updates. */
- @Volatile private var lastGpsTime: Long = 0
+ private val listenerMap = java.util.concurrent.ConcurrentHashMap<LocationUpdateCallback, LocationListener>()
override fun requestLocationUpdates(
intervalMs: Long,
@@ -44,7 +37,6 @@ class NativeLocationProvider(context: Context) : LocationProvider {
) {
val listener = object : LocationListener {
override fun onLocationChanged(location: Location) {
- lastGpsTime = System.currentTimeMillis()
callback.onLocationUpdate(location)
}
@Deprecated("Deprecated in API 29")
@@ -53,8 +45,6 @@ class NativeLocationProvider(context: Context) : LocationProvider {
override fun onProviderDisabled(provider: String) {}
}
- val listeners = mutableListOf<LocationListener>(listener)
-
try {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
@@ -63,46 +53,19 @@ class NativeLocationProvider(context: Context) : LocationProvider {
listener,
looper
)
-
AppLogger.d(TAG, "Started GPS_PROVIDER updates: interval=${intervalMs}ms, distance=${minDistanceMeters}m")
-
- // NETWORK_PROVIDER as coarse fallback for faster initial fix
- if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
- val networkListener = object : LocationListener {
- override fun onLocationChanged(location: Location) {
- if (System.currentTimeMillis() - lastGpsTime > GPS_SUPPRESSION_MS) {
- callback.onLocationUpdate(location)
- }
- }
- @Deprecated("Deprecated in API 29")
- override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {}
- override fun onProviderEnabled(provider: String) {}
- override fun onProviderDisabled(provider: String) {}
- }
- listeners.add(networkListener)
- locationManager.requestLocationUpdates(
- LocationManager.NETWORK_PROVIDER,
- intervalMs,
- minDistanceMeters,
- networkListener,
- looper
- )
- AppLogger.d(TAG, "Also started NETWORK_PROVIDER as fallback")
- } else {
- AppLogger.d(TAG, "NETWORK_PROVIDER not available")
- }
} catch (e: SecurityException) {
- listeners.forEach { locationManager.removeUpdates(it) }
+ locationManager.removeUpdates(listener)
throw e
}
- listenerMap[callback] = listeners
+ listenerMap[callback] = listener
}
override fun removeLocationUpdates(callback: LocationUpdateCallback) {
- listenerMap.remove(callback)?.let { listeners ->
- listeners.forEach { locationManager.removeUpdates(it) }
- AppLogger.d(TAG, "Stopped location updates (${listeners.size} listener(s))")
+ listenerMap.remove(callback)?.let { listener ->
+ locationManager.removeUpdates(listener)
+ AppLogger.d(TAG, "Stopped GPS_PROVIDER updates")
}
}
@@ -111,9 +74,7 @@ class NativeLocationProvider(context: Context) : LocationProvider {
onFailure: (Exception) -> Unit
) {
try {
- val location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
- ?: locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
- onSuccess(location)
+ onSuccess(locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER))
} catch (e: SecurityException) {
onFailure(e)
}