Merge pull request #5489 from osmandapp/fix_location_provider

Fix methods for receiving location
This commit is contained in:
Alexey 2018-05-25 17:05:14 +03:00 committed by GitHub
commit 61fd91e9cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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;
@ -862,15 +863,14 @@ 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)) {
} if (System.currentTimeMillis() - loc.getTime() > LOCATION_TIMEOUT_TO_BE_STALE) {
if (locationRequestsCounter == REQUESTS_BEFORE_CHECK_LOCATION) { location = null;
locationRequestsCounter = 0; }
} else { }
locationRequestsCounter++;
} }
return location; return location;
} }
@ -878,19 +878,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;
} }