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.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;
}