Try to get location by using all available providers.

This commit is contained in:
Onuray Sahin 2022-01-28 14:17:07 +03:00
parent 26c0fee56d
commit 0f8c3bcc17
1 changed files with 20 additions and 21 deletions

View File

@ -55,15 +55,10 @@ class LocationTracker @Inject constructor(
Timber.d("## LocationTracker. isGpsEnabled: $isGpsEnabled - isNetworkEnabled: $isNetworkEnabled")
val provider = when {
isGpsEnabled -> LocationManager.GPS_PROVIDER
isNetworkEnabled -> LocationManager.NETWORK_PROVIDER
else -> {
callback?.onLocationProviderIsNotAvailable()
Timber.v("## LocationTracker. There is no location provider available")
return
}
}
locationManager.allProviders
.takeIf { it.isNotEmpty() }
?.forEach { provider ->
Timber.d("## LocationTracker. track location using $provider")
// Send last known location without waiting location updates
locationManager.getLastKnownLocation(provider)?.let { lastKnownLocation ->
@ -71,7 +66,6 @@ class LocationTracker @Inject constructor(
callback?.onLocationUpdate(lastKnownLocation.toLocationData())
}
Timber.d("## LocationTracker. track location using $provider")
locationManager.requestLocationUpdates(
provider,
MIN_TIME_TO_UPDATE_LOCATION_MILLIS,
@ -79,6 +73,11 @@ class LocationTracker @Inject constructor(
this
)
}
?: run {
callback?.onLocationProviderIsNotAvailable()
Timber.v("## LocationTracker. There is no location provider available")
}
}
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])
fun stop() {