Modern Apps git

← Modern-Apps.git

findfamily: drop location fixes worse than 100m accuracy

Commit
c147730fea0c41df311f3bcef235bd3f8cc54244
Author
vayun-mathur <[email protected]>
When
2026-09-23 16:17 UTC (2 months ago)
Parents
34235f5

Diff

findfamily/src/main/java/com/vayunmathur/findfamily/domain/NoShowPolicy.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/domain/NoShowPolicy.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/domain/NoShowPolicy.ktindex a607529d9b..b490a852fc 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/domain/NoShowPolicy.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/domain/NoShowPolicy.kt@@ -18,7 +18,12 @@ import kotlin.time.Instant  * (`data/Coord.kt:havershine` + `util/LocationTrackingInbound.kt`):  * entering needs the whole error circle inside the radius, leaving needs it  * wholly outside a hysteresis margin, and fixes worse than- * [NO_SHOW_MAX_ACCURACY_METERS] cannot move the answer. The distance itself is+ * [NO_SHOW_MAX_ACCURACY_METERS] cannot move the answer. The same constant+ * also gates the whole fix pipeline (`util/LocationTrackingHeartbeat.kt`):+ * intake drops anything coarser at `util/recordFix`, inbound drops coarser peer+ * fixes before Room, and the crowd-finding sighting paths drop them before+ * reporting. Shutdown / battery-low parting reports are the one bypass — they+ * republish the last held fix however stale, marked as such. The distance itself is  * a pure haversine here rather than the platform geodesic, so values agree to  * within the sub-meter noise the strict inequalities already tolerate.  */@@ -109,11 +114,16 @@ object NoShowPolicy {     }      /**-     * Arrival when the alert watches no waypoint: any fresh-enough location-     * update counts, regardless of position or accuracy.+     * Arrival when the alert watches no waypoint: a fresh fix that is also accurate+     * enough. A coarse fix cannot say where the person is, so it must not suppress+     * the alert.      */-    fun hasAnyFreshFix(fixTimestamp: Instant, expectedAt: Instant): Boolean =-        isFixFreshForArrival(fixTimestamp, expectedAt)+    fun hasAnyFreshFix(+        fixTimestamp: Instant,+        expectedAt: Instant,+        fixAccuracyMeters: Double,+    ): Boolean =+        isFixFreshForArrival(fixTimestamp, expectedAt) && isAccurateEnough(fixAccuracyMeters)      /**      * Fire-once guard for the scheduler: fires only for an unfired alert whose
findfamily/src/main/java/com/vayunmathur/findfamily/platform/NoShowCheckScheduler.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/platform/NoShowCheckScheduler.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/platform/NoShowCheckScheduler.ktindex 488fb9c462..cdf9edc038 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/platform/NoShowCheckScheduler.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/platform/NoShowCheckScheduler.kt@@ -119,7 +119,11 @@ object NoShowCheckScheduler {         if (fix == null) return false         val waypointId = alert.waypointId         if (waypointId == null) {-            return NoShowPolicy.hasAnyFreshFix(fix.timestamp, alert.expectedAt)+            return NoShowPolicy.hasAnyFreshFix(+                fixTimestamp = fix.timestamp,+                expectedAt = alert.expectedAt,+                fixAccuracyMeters = fix.acc.toDouble(),+            )         }         val waypoint = runCatching { repository.getWaypoint(waypointId) }.getOrNull()             ?: return false
findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingCrowdFinding.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingCrowdFinding.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingCrowdFinding.ktindex 3ba5fa2fd5..1d4f36bb5b 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingCrowdFinding.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingCrowdFinding.kt@@ -31,8 +31,8 @@ internal fun LocationTrackingService.startTrackerScanner() {                     Log.i("FF-Tracker", "sighting dropped: no location fix yet")                     return@collect                 }-                if (loc.accuracy > 100f) {-                    Log.i("FF-Tracker", "sighting dropped: accuracy ${loc.accuracy}m > 100m")+                if (loc.accuracy > MAX_FIX_ACCURACY_METERS) {+                    Log.i("FF-Tracker", "sighting dropped: accuracy ${loc.accuracy}m > ${MAX_FIX_ACCURACY_METERS}m")                     return@collect                 }                 val battery = runCatching {@@ -96,10 +96,10 @@ internal fun LocationTrackingService.startPoweredOffScanner() {                             return@collect                         }                         // A sighting is only ever "the finder was near here". Reporting one-                        // from a 500m-accurate fix would add noise the owner cannot tell+                        // from a coarse fix would add noise the owner cannot tell                         // apart from a good one, so drop it rather than dilute the answer.-                        if (loc.accuracy > 100f) {-                            Log.i(LocationTrackingService.TAG_POWERED_OFF, "sighting dropped: accuracy ${loc.accuracy}m > 100m")+                        if (loc.accuracy > MAX_FIX_ACCURACY_METERS) {+                            Log.i(LocationTrackingService.TAG_POWERED_OFF, "sighting dropped: accuracy ${loc.accuracy}m > ${MAX_FIX_ACCURACY_METERS}m")                             return@collect                         }                         val lv = LocationValue(
findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingHeartbeat.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingHeartbeat.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingHeartbeat.ktindex 18f97c3fa6..3ddc9130bd 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingHeartbeat.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingHeartbeat.kt@@ -9,6 +9,7 @@ import com.vayunmathur.findfamily.data.LocationValue import com.vayunmathur.findfamily.data.User import com.vayunmathur.findfamily.data.RequestStatus import com.vayunmathur.findfamily.data.havershine+import com.vayunmathur.findfamily.domain.NoShowPolicy import com.vayunmathur.findfamily.R import kotlin.time.Clock import kotlin.time.Duration.Companion.minutes@@ -21,6 +22,19 @@ import kotlin.time.Duration.Companion.minutes  */ private val FIX_MAX_AGE_NANOS = 2.minutes.inWholeNanoseconds +/**+ * Fixes worse than this are never held, published, or used for crowd reports.+ *+ * Applies at intake ([recordFix]) and to inbound peer fixes before they reach+ * Room. Shutdown and low-battery parting reports bypass it: they deliberately+ * republish the last held fix however stale, so the last-seen stays truthful.+ *+ * Delegates to [NoShowPolicy.NO_SHOW_MAX_ACCURACY_METERS] so the fix pipeline,+ * the live geofence, and the no-show path can never drift apart.+ */+internal val MAX_FIX_ACCURACY_METERS: Float+    get() = NoShowPolicy.NO_SHOW_MAX_ACCURACY_METERS.toFloat()+ /**  * Keeps the best recent fix rather than simply the newest one.  *@@ -28,8 +42,16 @@ private val FIX_MAX_AGE_NANOS = 2.minutes.inWholeNanoseconds  * to hundreds of metres between them, so overwriting a recent GPS fix with one of those  * made a phone sitting on a table appear to move around the city. A coarser fix only  * wins once the one being held has gone stale enough to be the worse answer.+ *+ * Fixes worse than [MAX_FIX_ACCURACY_METERS] are dropped here and never become+ * [LocationTrackingService.lastKnownLocation].  */ internal fun LocationTrackingService.recordFix(location: Location) {+    if (!location.hasAccuracy() || !location.accuracy.isFinite() ||+        location.accuracy < 0f || location.accuracy > MAX_FIX_ACCURACY_METERS+    ) {+        return+    }     val held = lastKnownLocation     if (held == null) {         lastKnownLocation = location
findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingInbound.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingInbound.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingInbound.ktindex 4d8fd39d35..49ca9cbadc 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingInbound.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingInbound.kt@@ -7,6 +7,7 @@ import com.vayunmathur.findfamily.data.LocationValue import com.vayunmathur.findfamily.data.User import com.vayunmathur.findfamily.data.havershine import com.vayunmathur.findfamily.data.RequestStatus+import com.vayunmathur.findfamily.domain.NoShowPolicy import com.vayunmathur.findfamily.uwb.UwbEnvelope import com.vayunmathur.findfamily.uwb.UwbEnvelopeKind import com.vayunmathur.findfamily.uwb.UwbInbox@@ -17,10 +18,12 @@ import kotlin.io.encoding.Base64 import kotlin.time.Clock  /**- * Accuracy beyond which a fix is not used to decide geofence membership. Matches the- * gate the tracker sighting path already applies.+ * Accuracy beyond which a fix is not used to decide geofence membership.+ * Delegates to [NoShowPolicy.NO_SHOW_MAX_ACCURACY_METERS] so the live path and+ * the no-show path can never drift apart.  */-internal const val GEOFENCE_MAX_ACCURACY_METERS = 100.0+internal val GEOFENCE_MAX_ACCURACY_METERS: Double+    get() = NoShowPolicy.NO_SHOW_MAX_ACCURACY_METERS  /** How far past a geofence's radius a fix has to be before it counts as having left. */ internal const val WAYPOINT_EXIT_HYSTERESIS = 1.2@@ -42,10 +45,18 @@ internal suspend fun LocationTrackingService.processIncomingLocations(incoming:     // stream, that person silently disappears from this map, and "Alice was dropped" is the     // only thread anyone will have to pull on. See the note on LocationSource before adding     // a value that could cause it.-    val locList = incoming.filter { it.source != LocationSource.UNKNOWN }-    if (locList.size != incoming.size) {+    val knownSource = incoming.filter { it.source != LocationSource.UNKNOWN }+    if (knownSource.size != incoming.size) {         val dropped = incoming.filter { it.source == LocationSource.UNKNOWN }.map { it.userid.toULong() }.distinct()-        Log.w("FF-Heartbeat", "dropped ${incoming.size - locList.size} fix(es) from newer peer(s) with an unrecognised source: $dropped")+        Log.w("FF-Heartbeat", "dropped ${incoming.size - knownSource.size} fix(es) from newer peer(s) with an unrecognised source: $dropped")+    }+    // Low-accuracy fixes are never used: anything worse than the intake gate never+    // reaches Room, the map, or latestLocations.+    val locList = knownSource.filter {+        it.acc.isFinite() && it.acc >= 0f && it.acc <= MAX_FIX_ACCURACY_METERS+    }+    if (locList.size != knownSource.size) {+        Log.i("FF-Heartbeat", "dropped ${knownSource.size - locList.size} fix(es) worse than ${MAX_FIX_ACCURACY_METERS}m")     }     if (locList.isEmpty()) return     val currentUsers = repository.getAllUsers()
findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt
diff --git a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.ktindex d8cabbec19..2b9a91ddcf 100644--- a/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt+++ b/findfamily/src/main/java/com/vayunmathur/findfamily/util/LocationTrackingService.kt@@ -180,7 +180,7 @@ class LocationTrackingService : Service(), SensorEventListener {     private val networkListener = LocationListener { location ->         lastNetworkFixElapsedMs = SystemClock.elapsedRealtime()         recordFix(location)-        if (location.accuracy > 100f) {+        if (!location.hasAccuracy() || location.accuracy > MAX_FIX_ACCURACY_METERS) {             if (!isGpsRunning && isMoving) startGps()         } else {             if (isGpsRunning) stopGps()
findfamily/src/test/java/com/vayunmathur/findfamily/domain/NoShowPolicyTest.kt
diff --git a/findfamily/src/test/java/com/vayunmathur/findfamily/domain/NoShowPolicyTest.kt b/findfamily/src/test/java/com/vayunmathur/findfamily/domain/NoShowPolicyTest.ktindex 734a5c7513..4ec51b38e7 100644--- a/findfamily/src/test/java/com/vayunmathur/findfamily/domain/NoShowPolicyTest.kt+++ b/findfamily/src/test/java/com/vayunmathur/findfamily/domain/NoShowPolicyTest.kt@@ -220,9 +220,15 @@ class NoShowPolicyTest {      @Test     fun anyFreshFixCountsWhenNoWaypointWatched() {-        assertTrue(NoShowPolicy.hasAnyFreshFix(expectedAt, expectedAt))-        assertTrue(NoShowPolicy.hasAnyFreshFix(expectedAt + 1.hours, expectedAt))-        assertFalse(NoShowPolicy.hasAnyFreshFix(expectedAt - 1.seconds, expectedAt))+        assertTrue(NoShowPolicy.hasAnyFreshFix(expectedAt, expectedAt, 10.0))+        assertTrue(NoShowPolicy.hasAnyFreshFix(expectedAt + 1.hours, expectedAt, 100.0))+        assertFalse(NoShowPolicy.hasAnyFreshFix(expectedAt - 1.seconds, expectedAt, 10.0))+    }++    @Test+    fun coarseFixDoesNotCountWhenNoWaypointWatched() {+        assertFalse(NoShowPolicy.hasAnyFreshFix(expectedAt, expectedAt, 100.01))+        assertFalse(NoShowPolicy.hasAnyFreshFix(expectedAt + 1.hours, expectedAt, Double.POSITIVE_INFINITY))     }      // --- fire-once guard ---