From 0d1c54f534a3821f1d29205e612f4498ba910e77 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 25 May 2018 15:38:50 +0300 Subject: [PATCH] Fix methods for receiving location --- .../osmand/plus/OsmAndLocationProvider.java | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java index 5b2611e2c2..16f2538a1f 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import net.osmand.GeoidAltitudeCorrection; import net.osmand.PlatformUtil; @@ -70,12 +71,12 @@ public class OsmAndLocationProvider implements SensorEventListener { private static final int GPS_DIST_REQUEST = 0; private static final int NOT_SWITCH_TO_NETWORK_WHEN_GPS_LOST_MS = 12000; - private static final long STALE_LOCATION_TIMEOUT = 1000 * 60 * 5; // 5 minutes - private static final long STALE_LOCATION_TIMEOUT_FOR_UI = 1000 * 60 * 15; // 15 minutes + private static final long LOCATION_TIMEOUT_TO_BE_STALE = 1000 * 60 * 2; // 2 minutes + private static final long STALE_LOCATION_TIMEOUT_TO_BE_GONE = 1000 * 60 * 20; // 20 minutes private static final int REQUESTS_BEFORE_CHECK_LOCATION = 100; - private int locationRequestsCounter; - private int staleLocationRequestsCounter; + private AtomicInteger locationRequestsCounter = new AtomicInteger(); + private AtomicInteger staleLocationRequestsCounter = new AtomicInteger(); private net.osmand.Location cachedLocation; @@ -869,15 +870,15 @@ public class OsmAndLocationProvider implements SensorEventListener { } public net.osmand.Location getLastKnownLocation() { - net.osmand.Location location = this.location; - if (location != null && locationRequestsCounter == 0 - && System.currentTimeMillis() - location.getTime() > STALE_LOCATION_TIMEOUT) { - location = null; - } - if (locationRequestsCounter == REQUESTS_BEFORE_CHECK_LOCATION) { - locationRequestsCounter = 0; - } else { - locationRequestsCounter++; + net.osmand.Location loc = this.location; + if (loc != null) { + int counter = locationRequestsCounter.incrementAndGet(); + if (counter >= REQUESTS_BEFORE_CHECK_LOCATION && locationRequestsCounter.compareAndSet(counter, 0)) { + net.osmand.Location cached = cachedLocation; + if (cached != null && System.currentTimeMillis() - cached.getTime() > LOCATION_TIMEOUT_TO_BE_STALE) { + location = null; + } + } } return location; } @@ -885,19 +886,17 @@ public class OsmAndLocationProvider implements SensorEventListener { @Nullable public net.osmand.Location getLastStaleKnownLocation() { net.osmand.Location newLoc = getLastKnownLocation(); - if (newLoc == null) { - if (staleLocationRequestsCounter == 0 && cachedLocation != null - && System.currentTimeMillis() - cachedLocation.getTime() > STALE_LOCATION_TIMEOUT_FOR_UI) { - cachedLocation = null; + if (newLoc == null && cachedLocation != null) { + int counter = staleLocationRequestsCounter.incrementAndGet(); + if (counter >= REQUESTS_BEFORE_CHECK_LOCATION && staleLocationRequestsCounter.compareAndSet(counter, 0)) { + net.osmand.Location cached = cachedLocation; + if (cached != null && System.currentTimeMillis() - cached.getTime() > STALE_LOCATION_TIMEOUT_TO_BE_GONE) { + cachedLocation = null; + } } } else { cachedLocation = newLoc; } - if (staleLocationRequestsCounter == REQUESTS_BEFORE_CHECK_LOCATION) { - staleLocationRequestsCounter = 0; - } else { - staleLocationRequestsCounter++; - } return cachedLocation; }