Notify of the latest known location among all last known locations

This commit is contained in:
Maxime NATUREL 2022-05-03 16:35:53 +02:00
parent 0011eda8e0
commit 7860173fa2

View File

@ -57,34 +57,36 @@ class LocationTracker @Inject constructor(
return return
} }
locationManager.allProviders val providers = 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 if (providers.isEmpty()) {
locationManager.getLastKnownLocation(provider)?.let { lastKnownLocation -> callbacks.forEach { it.onLocationProviderIsNotAvailable() }
if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) { Timber.v("## LocationTracker. There is no location provider available")
Timber.d("## LocationTracker. lastKnownLocation: $lastKnownLocation") } else {
} else { // Take GPS first
Timber.d("## LocationTracker. lastKnownLocation: ${lastKnownLocation.provider}") providers.sortedByDescending { if (it == LocationManager.GPS_PROVIDER) 1 else 0 }
} .mapNotNull { provider ->
notifyLocation(lastKnownLocation, isLive = false) Timber.d("## LocationTracker. track location using $provider")
locationManager.requestLocationUpdates(
provider,
MIN_TIME_TO_UPDATE_LOCATION_MILLIS,
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
this
)
locationManager.getLastKnownLocation(provider)
} }
.maxByOrNull { location -> location.time }
locationManager.requestLocationUpdates( ?.let { latestKnownLocation ->
provider, if (BuildConfig.LOW_PRIVACY_LOG_ENABLE) {
MIN_TIME_TO_UPDATE_LOCATION_MILLIS, Timber.d("## LocationTracker. lastKnownLocation: $latestKnownLocation")
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS, } else {
this Timber.d("## LocationTracker. lastKnownLocation: ${latestKnownLocation.provider}")
) }
} notifyLocation(latestKnownLocation, isLive = false)
?: run { }
callbacks.forEach { it.onLocationProviderIsNotAvailable() } }
Timber.v("## LocationTracker. There is no location provider available")
}
} }
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION]) @RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])
@ -125,6 +127,7 @@ class LocationTracker @Inject constructor(
} }
private fun notifyLocation(location: Location, isLive: Boolean) { private fun notifyLocation(location: Location, isLive: Boolean) {
Timber.d("## LocationTracker. notify location")
when (location.provider) { when (location.provider) {
LocationManager.GPS_PROVIDER -> { LocationManager.GPS_PROVIDER -> {
hasGpsProviderLiveLocation = isLive hasGpsProviderLiveLocation = isLive