From c942de5ae01dbe8a9cc7370fc5e5f3eef4987d29 Mon Sep 17 00:00:00 2001 From: Stephen Michel Date: Sun, 28 Nov 2021 09:15:51 -0500 Subject: [PATCH 1/2] Fix location updates lost when app is backgrounded This fixes two separate bugs: 1. The signals were canceled in `removeUpdates` but not reset in `requestUpdates` 2. The signals were reset conditionally based on whether the provider was available, but the `locationListener` always checked both. So, on devices with only GPS, the network signal would never be un-canceled and location updates would never be received. Fixes #3543 --- .../location/FineLocationManager.kt | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/de/westnordost/streetcomplete/location/FineLocationManager.kt b/app/src/main/java/de/westnordost/streetcomplete/location/FineLocationManager.kt index 86bb54554f..699dafec6e 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/location/FineLocationManager.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/location/FineLocationManager.kt @@ -41,8 +41,20 @@ 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) { + refreshCancellationSignals() if (deviceHasGPS) locationManager.requestLocationUpdates(GPS_PROVIDER, minTime, minDistance, locationListener, Looper.getMainLooper()) if (deviceHasNetworkLocationProvider) @@ -51,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 ) From 1018c8ccf1fa8cb512df9da172167d65027b76c0 Mon Sep 17 00:00:00 2001 From: Tobias Zwick Date: Mon, 29 Nov 2021 11:29:46 +0100 Subject: [PATCH 2/2] Update app/src/main/java/de/westnordost/streetcomplete/location/FineLocationManager.kt --- .../westnordost/streetcomplete/location/FineLocationManager.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/de/westnordost/streetcomplete/location/FineLocationManager.kt b/app/src/main/java/de/westnordost/streetcomplete/location/FineLocationManager.kt index 699dafec6e..404e132a2a 100644 --- a/app/src/main/java/de/westnordost/streetcomplete/location/FineLocationManager.kt +++ b/app/src/main/java/de/westnordost/streetcomplete/location/FineLocationManager.kt @@ -54,7 +54,6 @@ class FineLocationManager(context: Context, locationUpdateCallback: (Location) - @RequiresPermission(ACCESS_FINE_LOCATION) fun requestUpdates(minTime: Long, minDistance: Float) { - refreshCancellationSignals() if (deviceHasGPS) locationManager.requestLocationUpdates(GPS_PROVIDER, minTime, minDistance, locationListener, Looper.getMainLooper()) if (deviceHasNetworkLocationProvider)