Skip to content

Commit df2c3e7

Browse files
committed
Check if location tracking is started before starting it
1 parent 25ca598 commit df2c3e7

File tree

1 file changed

+43
-35
lines changed

1 file changed

+43
-35
lines changed

vector/src/main/java/im/vector/app/features/location/LocationTracker.kt

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class LocationTracker @Inject constructor(
6666
@VisibleForTesting
6767
var hasLocationFromGPSProvider = false
6868

69+
private var isStarted = false
70+
private var isStarting = false
6971
private var firstLocationHandled = false
7072
private val _locations = MutableSharedFlow<Location>(replay = 1)
7173

@@ -90,44 +92,48 @@ class LocationTracker @Inject constructor(
9092

9193
@RequiresPermission(anyOf = [Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION])
9294
fun start() {
93-
// TODO start only if not already started
94-
Timber.d("start()")
95-
96-
if (locationManager == null) {
97-
Timber.v("LocationManager is not available")
98-
onNoLocationProviderAvailable()
99-
return
100-
}
95+
if(!isStarting && !isStarted) {
96+
isStarting = true
97+
Timber.d("start()")
98+
99+
if (locationManager == null) {
100+
Timber.v("LocationManager is not available")
101+
onNoLocationProviderAvailable()
102+
return
103+
}
101104

102-
val providers = locationManager.allProviders
105+
val providers = locationManager.allProviders
103106

104-
if (providers.isEmpty()) {
105-
Timber.v("There is no location provider available")
106-
onNoLocationProviderAvailable()
107-
} else {
108-
// Take GPS first
109-
providers.sortedByDescending(::getProviderPriority)
110-
.mapNotNull { provider ->
111-
Timber.d("track location using $provider")
112-
113-
locationManager.requestLocationUpdates(
114-
provider,
115-
minDurationToUpdateLocationMillis,
116-
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
117-
this
118-
)
119-
120-
locationManager.getLastKnownLocation(provider)
121-
}
122-
.maxByOrNull { location -> location.time }
123-
?.let { latestKnownLocation ->
124-
if (buildMeta.lowPrivacyLoggingEnabled) {
125-
Timber.d("lastKnownLocation: $latestKnownLocation")
126-
} else {
127-
Timber.d("lastKnownLocation: ${latestKnownLocation.provider}")
107+
if (providers.isEmpty()) {
108+
Timber.v("There is no location provider available")
109+
onNoLocationProviderAvailable()
110+
} else {
111+
// Take GPS first
112+
providers.sortedByDescending(::getProviderPriority)
113+
.mapNotNull { provider ->
114+
Timber.d("track location using $provider")
115+
116+
locationManager.requestLocationUpdates(
117+
provider,
118+
minDurationToUpdateLocationMillis,
119+
MIN_DISTANCE_TO_UPDATE_LOCATION_METERS,
120+
this
121+
)
122+
123+
locationManager.getLastKnownLocation(provider)
124+
}
125+
.maxByOrNull { location -> location.time }
126+
?.let { latestKnownLocation ->
127+
if (buildMeta.lowPrivacyLoggingEnabled) {
128+
Timber.d("lastKnownLocation: $latestKnownLocation")
129+
} else {
130+
Timber.d("lastKnownLocation: ${latestKnownLocation.provider}")
131+
}
132+
notifyLocation(latestKnownLocation)
128133
}
129-
notifyLocation(latestKnownLocation)
130-
}
134+
}
135+
isStarted = true
136+
isStarting = false
131137
}
132138
}
133139

@@ -149,6 +155,8 @@ class LocationTracker @Inject constructor(
149155
callbacks.clear()
150156
hasLocationFromGPSProvider = false
151157
hasLocationFromFusedProvider = false
158+
isStarting = false
159+
isStarted = false
152160
}
153161

154162
/**

0 commit comments

Comments
 (0)