Colota - Android GPS Location tracker
fix(location): force high accuracy on FOSS fresh-fix probe (#525)
Max Dietrich6ac56f5dce19
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.ktindex a2640975..0c65f306 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@@ -10,6 +10,7 @@ import android.content.Contextimport android.location.Locationimport android.location.LocationListenerimport android.location.LocationManager+import android.location.LocationRequestimport android.os.Buildimport android.os.CancellationSignalimport android.os.Handler@@ -91,11 +92,17 @@ class NativeLocationProvider(context: Context) : LocationProvider {onSuccess: (Location?) -> Unit,onFailure: (Exception) -> Unit) {- try {- onSuccess(locationManager.getLastKnownLocation(provider))+ val location = try {+ locationManager.getLastKnownLocation(provider)} catch (e: SecurityException) {onFailure(e)+ return+ } catch (e: IllegalArgumentException) {+ // The selected provider can be absent (GPS-less device, or deregistered mid-session).+ onFailure(e)+ return}+ onSuccess(location)}override fun getCurrentLocation(timeoutMs: Long, onResult: (Location?) -> Unit) {@@ -111,18 +118,36 @@ class NativeLocationProvider(context: Context) : LocationProvider {cancellationSignal.cancel()deliver(null)}+ val executor = Executor { handler.post(it) }+ fun onFix(location: Location?) {+ handler.removeCallbacks(timeoutRunnable)+ deliver(location)+ }try {- LocationManagerCompat.getCurrentLocation(- locationManager,- provider,- cancellationSignal,- Executor { handler.post(it) },- Consumer { location ->- handler.removeCallbacks(timeoutRunnable)- deliver(location)- }- )+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {+ // Compat's getCurrentLocation has no request param and defaults to balanced power, which can+ // keep the fused provider from engaging GNSS; the platform overload forces high accuracy.+ val request = LocationRequest.Builder(0)+ .setQuality(LocationRequest.QUALITY_HIGH_ACCURACY)+ .setDurationMillis(timeoutMs)+ .build()+ locationManager.getCurrentLocation(+ provider,+ request,+ cancellationSignal,+ executor,+ java.util.function.Consumer { onFix(it) }+ )+ } else {+ LocationManagerCompat.getCurrentLocation(+ locationManager,+ provider,+ cancellationSignal,+ executor,+ Consumer { onFix(it) }+ )+ }handler.postDelayed(timeoutRunnable, timeoutMs)AppLogger.d(TAG, "Requested fresh $provider fix (timeout=${timeoutMs}ms)")} catch (e: Exception) {