Avoid taking into account any provider location if we have gps location.

This commit is contained in:
Benoit Marty 2022-01-29 08:51:30 +01:00
parent 99f82d9691
commit a8c251f6f5
1 changed files with 23 additions and 6 deletions

View File

@ -56,13 +56,19 @@ class LocationTracker @Inject constructor(
locationManager.allProviders
.takeIf { it.isNotEmpty() }
// Take GPS first
?.sortedByDescending { if (it == LocationManager.GPS_PROVIDER) 1 else 0 }
?.forEach { provider ->
Timber.d("## LocationTracker. track location using $provider")
// Send last known location without waiting location updates
locationManager.getLastKnownLocation(provider)?.let { lastKnownLocation ->
if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) {
Timber.d("## LocationTracker. lastKnownLocation: $lastKnownLocation")
} else {
Timber.d("## LocationTracker. lastKnownLocation")
callback?.onLocationUpdate(lastKnownLocation.toLocationData())
}
onLocationChanged(lastKnownLocation)
}
locationManager.requestLocationUpdates(
@ -91,11 +97,22 @@ class LocationTracker @Inject constructor(
} else {
Timber.d("## LocationTracker. onLocationChanged")
}
if (location.provider != LocationManager.GPS_PROVIDER && hasGpsProviderLocation) {
notifyLocation(location)
}
private fun notifyLocation(location: Location) {
when (location.provider) {
LocationManager.GPS_PROVIDER -> {
hasGpsProviderLocation = true
}
else -> {
if (hasGpsProviderLocation) {
// Ignore this update
Timber.d("## LocationTracker. ignoring location from ${location.provider}, we have gps location")
return
}
}
}
callback?.onLocationUpdate(location.toLocationData())
}