@@ -66,6 +66,8 @@ class LocationTracker @Inject constructor(
66
66
@VisibleForTesting
67
67
var hasLocationFromGPSProvider = false
68
68
69
+ private var isStarted = false
70
+ private var isStarting = false
69
71
private var firstLocationHandled = false
70
72
private val _locations = MutableSharedFlow <Location >(replay = 1 )
71
73
@@ -90,44 +92,48 @@ class LocationTracker @Inject constructor(
90
92
91
93
@RequiresPermission(anyOf = [Manifest .permission.ACCESS_COARSE_LOCATION , Manifest .permission.ACCESS_FINE_LOCATION ])
92
94
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
+ }
101
104
102
- val providers = locationManager.allProviders
105
+ val providers = locationManager.allProviders
103
106
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)
128
133
}
129
- notifyLocation(latestKnownLocation)
130
- }
134
+ }
135
+ isStarted = true
136
+ isStarting = false
131
137
}
132
138
}
133
139
@@ -149,6 +155,8 @@ class LocationTracker @Inject constructor(
149
155
callbacks.clear()
150
156
hasLocationFromGPSProvider = false
151
157
hasLocationFromFusedProvider = false
158
+ isStarting = false
159
+ isStarted = false
152
160
}
153
161
154
162
/* *
0 commit comments