Skip to content

Commit

Permalink
Merge pull request #3547 from streetcomplete/location-bugfix
Browse files Browse the repository at this point in the history
Fix location updates lost when StreetComplete is sent to the background
  • Loading branch information
westnordost authored Nov 29, 2021
2 parents 9669fc2 + 1018c8c commit 2ff9849
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ class FineLocationManager(context: Context, locationUpdateCallback: (Location) -
}
}

// Both signals are refreshed regardless of whether the device has both providers, because
// they are both canceled in removeUpdates and both checked in the locationListener
private fun refreshCancellationSignals() {
if (gpsCancellationSignal.isCanceled) {
gpsCancellationSignal = CancellationSignal()
}
if (networkCancellationSignal.isCanceled) {
networkCancellationSignal = CancellationSignal()
}
}

@RequiresPermission(ACCESS_FINE_LOCATION)
fun requestUpdates(minTime: Long, minDistance: Float) {
if (deviceHasGPS)
Expand All @@ -52,18 +63,13 @@ class FineLocationManager(context: Context, locationUpdateCallback: (Location) -

@RequiresPermission(ACCESS_FINE_LOCATION)
@Synchronized fun getCurrentLocation() {
refreshCancellationSignals()
if (deviceHasGPS) {
if (gpsCancellationSignal.isCanceled) {
gpsCancellationSignal = CancellationSignal()
}
LocationManagerCompat.getCurrentLocation(
locationManager, GPS_PROVIDER, gpsCancellationSignal, mainExecutor, currentLocationConsumer
)
}
if (deviceHasNetworkLocationProvider) {
if (networkCancellationSignal.isCanceled) {
networkCancellationSignal = CancellationSignal()
}
LocationManagerCompat.getCurrentLocation(
locationManager, NETWORK_PROVIDER, networkCancellationSignal, mainExecutor, currentLocationConsumer
)
Expand Down

0 comments on commit 2ff9849

Please sign in to comment.