From df2c3e7c07eb7ba7da3ab19957453bb8037d9a2e Mon Sep 17 00:00:00 2001 From: Maxime NATUREL <46314705+mnaturel@users.noreply.github.com> Date: Fri, 17 Feb 2023 17:02:05 +0100 Subject: [PATCH] Check if location tracking is started before starting it --- .../app/features/location/LocationTracker.kt | 72 ++++++++++--------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/location/LocationTracker.kt b/vector/src/main/java/im/vector/app/features/location/LocationTracker.kt index 7e47600715..d80c0ffa8b 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationTracker.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationTracker.kt @@ -66,6 +66,8 @@ class LocationTracker @Inject constructor( @VisibleForTesting var hasLocationFromGPSProvider = false + private var isStarted = false + private var isStarting = false private var firstLocationHandled = false private val _locations = MutableSharedFlow(replay = 1) @@ -90,44 +92,48 @@ class LocationTracker @Inject constructor( @RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION]) fun start() { - // TODO start only if not already started - Timber.d("start()") + if(!isStarting && !isStarted) { + isStarting = true + Timber.d("start()") - if (locationManager == null) { - Timber.v("LocationManager is not available") - onNoLocationProviderAvailable() - return - } + if (locationManager == null) { + Timber.v("LocationManager is not available") + onNoLocationProviderAvailable() + return + } - val providers = locationManager.allProviders + val providers = locationManager.allProviders - if (providers.isEmpty()) { - Timber.v("There is no location provider available") - onNoLocationProviderAvailable() - } else { - // Take GPS first - providers.sortedByDescending(::getProviderPriority) - .mapNotNull { provider -> - Timber.d("track location using $provider") + if (providers.isEmpty()) { + Timber.v("There is no location provider available") + onNoLocationProviderAvailable() + } else { + // Take GPS first + providers.sortedByDescending(::getProviderPriority) + .mapNotNull { provider -> + Timber.d("track location using $provider") - locationManager.requestLocationUpdates( - provider, - minDurationToUpdateLocationMillis, - MIN_DISTANCE_TO_UPDATE_LOCATION_METERS, - this - ) + locationManager.requestLocationUpdates( + provider, + minDurationToUpdateLocationMillis, + MIN_DISTANCE_TO_UPDATE_LOCATION_METERS, + this + ) - locationManager.getLastKnownLocation(provider) - } - .maxByOrNull { location -> location.time } - ?.let { latestKnownLocation -> - if (buildMeta.lowPrivacyLoggingEnabled) { - Timber.d("lastKnownLocation: $latestKnownLocation") - } else { - Timber.d("lastKnownLocation: ${latestKnownLocation.provider}") + locationManager.getLastKnownLocation(provider) } - notifyLocation(latestKnownLocation) - } + .maxByOrNull { location -> location.time } + ?.let { latestKnownLocation -> + if (buildMeta.lowPrivacyLoggingEnabled) { + Timber.d("lastKnownLocation: $latestKnownLocation") + } else { + Timber.d("lastKnownLocation: ${latestKnownLocation.provider}") + } + notifyLocation(latestKnownLocation) + } + } + isStarted = true + isStarting = false } } @@ -149,6 +155,8 @@ class LocationTracker @Inject constructor( callbacks.clear() hasLocationFromGPSProvider = false hasLocationFromFusedProvider = false + isStarting = false + isStarted = false } /**