Skip to content

Commit

Permalink
Merge branch 'upstream-westnordost' into mnalis-everything
Browse files Browse the repository at this point in the history
  • Loading branch information
mnalis committed Nov 29, 2021
2 parents 141a50a + bad8131 commit a8f102a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ Always remember to pay attention to [the quest guidelines](https://github.com/st

Inheritance and class hierarchy should be avoided if possible. It is preferable to extract shared code to helper file such as [KerbUtil.kt](app/src/main/java/de/westnordost/streetcomplete/osm/kerb/KerbUtil.kt).

### Hints for more active people

See ["Efficiently working together"](https://github.com/streetcomplete/StreetComplete/discussions/3450) which is addressed to people highly active in this project.

If you are making your first issue or pull request then you can definitely skip reading it.

## StreetComplete-related projects

### Dependencies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class FineLocationManager(context: Context, locationUpdateCallback: (Location) -
private val mainExecutor = ContextCompat.getMainExecutor(context)
private val currentLocationConsumer = Consumer<Location?> {
if (it != null) {
locationUpdateCallback(it)
if (!networkCancellationSignal.isCanceled && !gpsCancellationSignal.isCanceled) {
locationUpdateCallback(it)
}
}
}
private var gpsCancellationSignal = CancellationSignal()
Expand All @@ -33,14 +35,24 @@ class FineLocationManager(context: Context, locationUpdateCallback: (Location) -

private val locationListener = object : LocationUpdateListener {
override fun onLocationChanged(location: Location) {
if (networkCancellationSignal.isCanceled || gpsCancellationSignal.isCanceled) return
if (!isBetterLocation(location, lastLocation)) return

lastLocation = location
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 @@ -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
)
Expand Down

0 comments on commit a8f102a

Please sign in to comment.