From 82626aa84c00fcc4dea70ea87507b9594906fec3 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 3 Jun 2018 17:41:56 +0200 Subject: [PATCH] Fix #5535 - do not use getTime from location comparing with system time --- .../osmand/plus/OsmAndLocationProvider.java | 23 +++++++++++-------- .../osmand/plus/activities/MapActivity.java | 1 - 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java index bbbd20378b..45b5235197 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndLocationProvider.java @@ -78,12 +78,16 @@ public class OsmAndLocationProvider implements SensorEventListener { private AtomicInteger locationRequestsCounter = new AtomicInteger(); private AtomicInteger staleLocationRequestsCounter = new AtomicInteger(); - private net.osmand.Location cachedLocation; + private long lastTimeGPSLocationFixed = 0; + private long lastTimeLocationFixed = 0; private boolean gpsSignalLost; private SimulationProvider simulatePosition = null; + private long cachedLocationTimeFix = 0; + private net.osmand.Location cachedLocation; + private boolean sensorRegistered = false; private float[] mGravs = new float[3]; private float[] mGeoMags = new float[3]; @@ -578,7 +582,8 @@ public class OsmAndLocationProvider implements SensorEventListener { @Override public void onLocationChanged(Location location) { if (location != null) { - lastTimeGPSLocationFixed = location.getTime(); + // lastTimeGPSLocationFixed = location.getTime(); + lastTimeGPSLocationFixed = System.currentTimeMillis(); } if(!locationSimulation.isRouteAnimating()) { setLocation(convertLocation(location, app)); @@ -766,7 +771,10 @@ public class OsmAndLocationProvider implements SensorEventListener { if(location == null){ updateGPSInfo(null); } + if(location != null) { + // // use because there is a bug on some devices with location.getTime() + lastTimeLocationFixed = System.currentTimeMillis(); simulatePosition = null; if(gpsSignalLost) { gpsSignalLost = false; @@ -811,12 +819,6 @@ public class OsmAndLocationProvider implements SensorEventListener { } } - public void checkIfLastKnownLocationIsValid() { - net.osmand.Location loc = getLastKnownLocation(); - if (loc != null && (System.currentTimeMillis() - loc.getTime()) > INTERVAL_TO_CLEAR_SET_LOCATION) { - setLocation(null); - } - } public NavigationInfo getNavigationInfo() { return navigationInfo; @@ -867,7 +869,7 @@ public class OsmAndLocationProvider implements SensorEventListener { if (loc != null) { int counter = locationRequestsCounter.incrementAndGet(); if (counter >= REQUESTS_BEFORE_CHECK_LOCATION && locationRequestsCounter.compareAndSet(counter, 0)) { - if (System.currentTimeMillis() - loc.getTime() > LOCATION_TIMEOUT_TO_BE_STALE) { + if (System.currentTimeMillis() - lastTimeGPSLocationFixed > LOCATION_TIMEOUT_TO_BE_STALE) { location = null; } } @@ -882,12 +884,13 @@ public class OsmAndLocationProvider implements SensorEventListener { 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) { + if (cached != null && System.currentTimeMillis() - cachedLocationTimeFix > STALE_LOCATION_TIMEOUT_TO_BE_GONE) { cachedLocation = null; } } } else { cachedLocation = newLoc; + cachedLocationTimeFix = lastTimeLocationFixed; } return cachedLocation; } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index 2a898bdfaf..d2e4be7ffe 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -655,7 +655,6 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven getSupportActionBar().hide(); } - app.getLocationProvider().checkIfLastKnownLocationIsValid(); // for voice navigation ApplicationMode routingAppMode = getRoutingHelper().getAppMode(); if (routingAppMode != null && settings.AUDIO_STREAM_GUIDANCE.getModeValue(routingAppMode) != null) {