Fix methods for receiving location

This commit is contained in:
Alex Sytnyk 2018-05-25 15:38:50 +03:00
parent 5e26ec37d9
commit 0d1c54f534

View file

@ -5,6 +5,7 @@ import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import net.osmand.GeoidAltitudeCorrection; import net.osmand.GeoidAltitudeCorrection;
import net.osmand.PlatformUtil; 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 GPS_DIST_REQUEST = 0;
private static final int NOT_SWITCH_TO_NETWORK_WHEN_GPS_LOST_MS = 12000; 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 LOCATION_TIMEOUT_TO_BE_STALE = 1000 * 60 * 2; // 2 minutes
private static final long STALE_LOCATION_TIMEOUT_FOR_UI = 1000 * 60 * 15; // 15 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 static final int REQUESTS_BEFORE_CHECK_LOCATION = 100;
private int locationRequestsCounter; private AtomicInteger locationRequestsCounter = new AtomicInteger();
private int staleLocationRequestsCounter; private AtomicInteger staleLocationRequestsCounter = new AtomicInteger();
private net.osmand.Location cachedLocation; private net.osmand.Location cachedLocation;
@ -869,15 +870,15 @@ public class OsmAndLocationProvider implements SensorEventListener {
} }
public net.osmand.Location getLastKnownLocation() { public net.osmand.Location getLastKnownLocation() {
net.osmand.Location location = this.location; net.osmand.Location loc = this.location;
if (location != null && locationRequestsCounter == 0 if (loc != null) {
&& System.currentTimeMillis() - location.getTime() > STALE_LOCATION_TIMEOUT) { int counter = locationRequestsCounter.incrementAndGet();
location = null; if (counter >= REQUESTS_BEFORE_CHECK_LOCATION && locationRequestsCounter.compareAndSet(counter, 0)) {
} net.osmand.Location cached = cachedLocation;
if (locationRequestsCounter == REQUESTS_BEFORE_CHECK_LOCATION) { if (cached != null && System.currentTimeMillis() - cached.getTime() > LOCATION_TIMEOUT_TO_BE_STALE) {
locationRequestsCounter = 0; location = null;
} else { }
locationRequestsCounter++; }
} }
return location; return location;
} }
@ -885,19 +886,17 @@ public class OsmAndLocationProvider implements SensorEventListener {
@Nullable @Nullable
public net.osmand.Location getLastStaleKnownLocation() { public net.osmand.Location getLastStaleKnownLocation() {
net.osmand.Location newLoc = getLastKnownLocation(); net.osmand.Location newLoc = getLastKnownLocation();
if (newLoc == null) { if (newLoc == null && cachedLocation != null) {
if (staleLocationRequestsCounter == 0 && cachedLocation != null int counter = staleLocationRequestsCounter.incrementAndGet();
&& System.currentTimeMillis() - cachedLocation.getTime() > STALE_LOCATION_TIMEOUT_FOR_UI) { if (counter >= REQUESTS_BEFORE_CHECK_LOCATION && staleLocationRequestsCounter.compareAndSet(counter, 0)) {
cachedLocation = null; net.osmand.Location cached = cachedLocation;
if (cached != null && System.currentTimeMillis() - cached.getTime() > STALE_LOCATION_TIMEOUT_TO_BE_GONE) {
cachedLocation = null;
}
} }
} else { } else {
cachedLocation = newLoc; cachedLocation = newLoc;
} }
if (staleLocationRequestsCounter == REQUESTS_BEFORE_CHECK_LOCATION) {
staleLocationRequestsCounter = 0;
} else {
staleLocationRequestsCounter++;
}
return cachedLocation; return cachedLocation;
} }