mirror of
https://github.com/element-hq/element-android
synced 2024-11-23 01:45:36 +03:00
Check if location tracking is started before starting it
This commit is contained in:
parent
25ca598414
commit
df2c3e7c07
1 changed files with 40 additions and 32 deletions
|
@ -66,6 +66,8 @@ class LocationTracker @Inject constructor(
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
var hasLocationFromGPSProvider = false
|
var hasLocationFromGPSProvider = false
|
||||||
|
|
||||||
|
private var isStarted = false
|
||||||
|
private var isStarting = false
|
||||||
private var firstLocationHandled = false
|
private var firstLocationHandled = false
|
||||||
private val _locations = MutableSharedFlow<Location>(replay = 1)
|
private val _locations = MutableSharedFlow<Location>(replay = 1)
|
||||||
|
|
||||||
|
@ -90,44 +92,48 @@ class LocationTracker @Inject constructor(
|
||||||
|
|
||||||
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])
|
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])
|
||||||
fun start() {
|
fun start() {
|
||||||
// TODO start only if not already started
|
if(!isStarting && !isStarted) {
|
||||||
Timber.d("start()")
|
isStarting = true
|
||||||
|
Timber.d("start()")
|
||||||
|
|
||||||
if (locationManager == null) {
|
if (locationManager == null) {
|
||||||
Timber.v("LocationManager is not available")
|
Timber.v("LocationManager is not available")
|
||||||
onNoLocationProviderAvailable()
|
onNoLocationProviderAvailable()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val providers = locationManager.allProviders
|
val providers = locationManager.allProviders
|
||||||
|
|
||||||
if (providers.isEmpty()) {
|
if (providers.isEmpty()) {
|
||||||
Timber.v("There is no location provider available")
|
Timber.v("There is no location provider available")
|
||||||
onNoLocationProviderAvailable()
|
onNoLocationProviderAvailable()
|
||||||
} else {
|
} else {
|
||||||
// Take GPS first
|
// Take GPS first
|
||||||
providers.sortedByDescending(::getProviderPriority)
|
providers.sortedByDescending(::getProviderPriority)
|
||||||
.mapNotNull { provider ->
|
.mapNotNull { provider ->
|
||||||
Timber.d("track location using $provider")
|
Timber.d("track location using $provider")
|
||||||
|
|
||||||
locationManager.requestLocationUpdates(
|
locationManager.requestLocationUpdates(
|
||||||
provider,
|
provider,
|
||||||
minDurationToUpdateLocationMillis,
|
minDurationToUpdateLocationMillis,
|
||||||
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
|
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
|
||||||
this
|
this
|
||||||
)
|
)
|
||||||
|
|
||||||
locationManager.getLastKnownLocation(provider)
|
locationManager.getLastKnownLocation(provider)
|
||||||
}
|
|
||||||
.maxByOrNull { location -> location.time }
|
|
||||||
?.let { latestKnownLocation ->
|
|
||||||
if (buildMeta.lowPrivacyLoggingEnabled) {
|
|
||||||
Timber.d("lastKnownLocation: $latestKnownLocation")
|
|
||||||
} else {
|
|
||||||
Timber.d("lastKnownLocation: ${latestKnownLocation.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()
|
callbacks.clear()
|
||||||
hasLocationFromGPSProvider = false
|
hasLocationFromGPSProvider = false
|
||||||
hasLocationFromFusedProvider = false
|
hasLocationFromFusedProvider = false
|
||||||
|
isStarting = false
|
||||||
|
isStarted = false
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue