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,29 +55,28 @@ class LocationTracker @Inject constructor(
Timber.d("## LocationTracker. isGpsEnabled: $isGpsEnabled - isNetworkEnabled: $isNetworkEnabled") Timber.d("## LocationTracker. isGpsEnabled: $isGpsEnabled - isNetworkEnabled: $isNetworkEnabled")
val provider = when { locationManager.allProviders
isGpsEnabled -> LocationManager.GPS_PROVIDER .takeIf { it.isNotEmpty() }
isNetworkEnabled -> LocationManager.NETWORK_PROVIDER ?.forEach { provider ->
else -> { Timber.d("## LocationTracker. track location using $provider")
callback?.onLocationProviderIsNotAvailable()
Timber.v("## LocationTracker. There is no location provider available")
return
}
}
// Send last known location without waiting location updates // Send last known location without waiting location updates
locationManager.getLastKnownLocation(provider)?.let { lastKnownLocation -> locationManager.getLastKnownLocation(provider)?.let { lastKnownLocation ->
Timber.d("## LocationTracker. lastKnownLocation") Timber.d("## LocationTracker. lastKnownLocation")
callback?.onLocationUpdate(lastKnownLocation.toLocationData()) callback?.onLocationUpdate(lastKnownLocation.toLocationData())
} }
Timber.d("## LocationTracker. track location using $provider") locationManager.requestLocationUpdates(
locationManager.requestLocationUpdates( provider,
provider, MIN_TIME_TO_UPDATE_LOCATION_MILLIS,
MIN_TIME_TO_UPDATE_LOCATION_MILLIS, MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS, this
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]) @RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])