forked from Blendman974/faceclaw
Fix bug where Navigate app could decide you'd arrived based on a stale fix
This commit is contained in:
1 parent
758cf7de3b
commit
6051be0456
2 files changed
+55
-7
No files matched your search
@@ -9,6 +9,7 @@ import android.location.LocationManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
@@ -26,6 +27,14 @@ public final class FaceclawLocationTracker implements LocationListener {
|
||||
private final LocationManager locationManager;
|
||||
private final Handler callbackHandler;
|
||||
|
||||
/**
|
||||
* Only seed from the platform's cached fix when it is this fresh. An older
|
||||
* one is likely wherever the phone was last used (for example the previous
|
||||
* navigation's destination), and guidance built on it is wrong from the
|
||||
* first frame: the route starts there and arrival fires immediately.
|
||||
*/
|
||||
private static final long MAX_SEED_AGE_MS = 60L * 1000L;
|
||||
|
||||
private FaceclawLocationTrackerListener listener;
|
||||
private boolean running;
|
||||
|
||||
@@ -83,11 +92,9 @@ public final class FaceclawLocationTracker implements LocationListener {
|
||||
locationManager.requestLocationUpdates(provider, Math.max(500L, intervalMs), 0f, this,
|
||||
callbackHandler.getLooper());
|
||||
// Seed with the freshest cached fix so the UI has a position
|
||||
// before the first live fix (GPS cold starts can take a while).
|
||||
Location cached = locationManager.getLastKnownLocation(provider);
|
||||
if (cached == null) {
|
||||
cached = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
|
||||
}
|
||||
// before the first live fix (GPS cold starts can take a while),
|
||||
// but only if it's recent enough to still be where the user is.
|
||||
Location cached = freshestCachedLocation(provider);
|
||||
if (cached != null) {
|
||||
deliverLocation(cached);
|
||||
}
|
||||
@@ -100,6 +107,32 @@ public final class FaceclawLocationTracker implements LocationListener {
|
||||
}
|
||||
}
|
||||
|
||||
private Location freshestCachedLocation(String primaryProvider) {
|
||||
Location best = null;
|
||||
for (String provider : new String[] {primaryProvider, LocationManager.NETWORK_PROVIDER,
|
||||
LocationManager.GPS_PROVIDER}) {
|
||||
Location candidate;
|
||||
try {
|
||||
candidate = locationManager.getLastKnownLocation(provider);
|
||||
} catch (Throwable ignored) {
|
||||
continue;
|
||||
}
|
||||
if (candidate == null || ageMs(candidate) > MAX_SEED_AGE_MS) {
|
||||
continue;
|
||||
}
|
||||
if (best == null || candidate.getElapsedRealtimeNanos() > best.getElapsedRealtimeNanos()) {
|
||||
best = candidate;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/** Age via the monotonic clock, so a wall-clock change can't make an old fix look fresh. */
|
||||
private static long ageMs(Location location) {
|
||||
long ageNanos = SystemClock.elapsedRealtimeNanos() - location.getElapsedRealtimeNanos();
|
||||
return ageNanos / 1_000_000L;
|
||||
}
|
||||
|
||||
private String chooseProvider() {
|
||||
try {
|
||||
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||
|
||||
@@ -90,6 +90,13 @@ const MAP_RETRY_BACKOFF_MS = 5_000;
|
||||
const REROUTE_MIN_INTERVAL_MS = 20_000;
|
||||
const FIRST_FIX_TIMEOUT_MS = 10_000;
|
||||
const LOCATION_INTERVAL_MS = 1_000;
|
||||
/**
|
||||
* A fix older than this is not where the user is now. The tracker seeds each
|
||||
* start with the platform's cached fix, and after a previous trip that can be
|
||||
* the old destination: routing from it, or feeding it to the follower, ends a
|
||||
* fresh navigation with "Arrived" before a live fix ever comes in.
|
||||
*/
|
||||
const FIX_MAX_AGE_MS = 60_000;
|
||||
/** Above this speed a GPS bearing is trustworthy for heading-up. */
|
||||
const BEARING_MIN_SPEED_MPS = 2.5;
|
||||
/** The head-direction chevron only redraws once the wearer has turned this far. */
|
||||
@@ -467,8 +474,16 @@ function stopNavigation(finalStatus: string): void {
|
||||
ensureTickTimer();
|
||||
}
|
||||
|
||||
function isFreshFix(fix: TrackedLocation): boolean {
|
||||
return Date.now() - fix.timestampMs < FIX_MAX_AGE_MS;
|
||||
}
|
||||
|
||||
function handleFix(fix: TrackedLocation): void {
|
||||
lastFix = fix;
|
||||
if (!lastFix || fix.timestampMs >= lastFix.timestampMs) lastFix = fix;
|
||||
// A stale fix (a cached seed from a previous outing) may position the map
|
||||
// until something better arrives, but it must not stand in for the user's
|
||||
// current location: it neither starts a route nor advances guidance.
|
||||
if (!isFreshFix(fix)) return;
|
||||
if (fix.bearingDeg !== null && (fix.speedMps ?? 0) >= BEARING_MIN_SPEED_MPS) {
|
||||
lastBearingDeg = fix.bearingDeg;
|
||||
}
|
||||
@@ -522,7 +537,7 @@ function stopTrackingIfIdle(): void {
|
||||
}
|
||||
|
||||
function waitForFix(): Promise<TrackedLocation> {
|
||||
if (lastFix && Date.now() - lastFix.timestampMs < 60_000) {
|
||||
if (lastFix && isFreshFix(lastFix)) {
|
||||
return Promise.resolve(lastFix);
|
||||
}
|
||||
return new Promise<TrackedLocation>((resolve, reject) => {
|
||||
|
||||
Reference in new issue
Block a user