Skip to content

RF Sentinel (RFSentinel): free, open-source police detector app for Android. Spots police body cams, Flock ALPR cameras, AirTag trackers, Remote ID drones, smart glasses and fake cell towers from Bluetooth LE and WiFi. No root.

Follow alert for a flagged device that keeps turning up, not only one that stays

CIS-C05c8062f0a9e5
diff --git a/app/src/main/java/com/rfsentinel/app/service/DeviceRegistry.kt b/app/src/main/java/com/rfsentinel/app/service/DeviceRegistry.kt
index 3f6285a..4517d0c 100644
--- a/app/src/main/java/com/rfsentinel/app/service/DeviceRegistry.kt
+++ b/app/src/main/java/com/rfsentinel/app/service/DeviceRegistry.kt
@@ -342,6 +342,16 @@ object DeviceRegistry {
}
}
+ /** Marks [mac] as following you ([PlaceFollow] found it at several places). True the first time only. */
+ fun markFollowing(mac: String): Boolean {
+ val t = tracks[mac] ?: return false
+ synchronized(t) {
+ if (t.following) return false
+ t.following = true
+ return true
+ }
+ }
+
/** Light snapshots for the live list; pruned first. */
fun snapshot(now: Long = System.currentTimeMillis()): List<Snapshot> {
tracks.values.removeIf { now - it.lastSeen > PRUNE_AFTER_MS }
diff --git a/app/src/main/java/com/rfsentinel/app/service/PlaceFollow.kt b/app/src/main/java/com/rfsentinel/app/service/PlaceFollow.kt
new file mode 100644
index 0000000..09bd4bf
--- /dev/null
+++ b/app/src/main/java/com/rfsentinel/app/service/PlaceFollow.kt
@@ -0,0 +1,78 @@
+package com.rfsentinel.app.service
+
+/**
+ * The follow check for a device that keeps turning up rather than staying in range (after
+ * Chasing Your Tail's multi-location tracking): a flagged device heard at [MIN_PLACES] or more
+ * separate places along your way within [WINDOW_MS], even with gaps between, is following you
+ * as surely as one that never left. [DeviceRegistry.checkFollowing] covers the one that stays.
+ *
+ * A place is where *you* were when it was heard, so a fixed camera you pass by twice is one place.
+ * Kept in memory only, for flagged devices, and forgotten after [WINDOW_MS].
+ */
+class PlaceFollow {
+
+ private class Visit(val at: Long, val lat: Double, val lon: Double)
+
+ private val visits = HashMap<String, ArrayDeque<Visit>>()
+ /** When each device's follow alert was given: once per [WINDOW_MS], however often it drops out and returns. */
+ private val alerted = HashMap<String, Long>()
+
+ /** [mac] (flagged) was heard with you at [lat], [lon]; [previous] is its address before a rotation. */
+ fun heard(mac: String, previous: String?, now: Long, lat: Double, lon: Double) {
+ if (previous != null && previous != mac) {
+ visits.remove(previous)?.let { old -> visits.getOrPut(mac) { ArrayDeque() }.addAll(0, old) }
+ alerted.remove(previous)?.let { alerted[mac] = it }
+ }
+ val list = visits.getOrPut(mac) { ArrayDeque() }
+ val last = list.lastOrNull()
+ if (last != null && now - last.at < SAMPLE_MS) return
+ list.addLast(Visit(now, lat, lon))
+ while (list.isNotEmpty() && now - list.first().at > WINDOW_MS) list.removeFirst()
+ if (visits.size > MAX_DEVICES) {
+ visits.entries.removeIf { now - (it.value.lastOrNull()?.at ?: 0L) > WINDOW_MS }
+ if (visits.size > MAX_DEVICES) visits.remove(visits.keys.first())
+ alerted.entries.removeIf { now - it.value > WINDOW_MS }
+ }
+ }
+
+ /**
+ * How many separate places [mac] was heard at, when that makes it a follower: at least
+ * [MIN_PLACES], spread over [minDurationMs] and [minDistanceM] (the follow settings). Else 0.
+ */
+ fun places(mac: String, minDurationMs: Long, minDistanceM: Double): Int {
+ val list = visits[mac] ?: return 0
+ if (list.size < MIN_PLACES || list.last().at - list.first().at < minDurationMs) return 0
+ val places = ArrayList<Visit>()
+ for (v in list) if (places.none { metres(it, v) < PLACE_RADIUS_M }) places += v
+ if (places.size < MIN_PLACES) return 0
+ val spread = places.maxOf { a -> places.maxOf { b -> metres(a, b) } }
+ return if (spread >= minDistanceM) places.size else 0
+ }
+
+ /** True once per [WINDOW_MS] when [mac] is a follower by [places]: time to alert. */
+ fun follower(mac: String, now: Long, minDurationMs: Long, minDistanceM: Double): Boolean {
+ if (alerted[mac]?.let { now - it < WINDOW_MS } == true) return false
+ if (places(mac, minDurationMs, minDistanceM) == 0) return false
+ alerted[mac] = now
+ return true
+ }
+
+ /** The follow alert for [mac] was given another way (it stayed in range): don't repeat it. */
+ fun alertedNow(mac: String, now: Long) { alerted[mac] = now }
+
+ private fun metres(a: Visit, b: Visit): Double {
+ val r = 6_371_000.0
+ val dLat = Math.toRadians(b.lat - a.lat)
+ val dLon = Math.toRadians(b.lon - a.lon) * Math.cos(Math.toRadians((a.lat + b.lat) / 2))
+ return r * Math.sqrt(dLat * dLat + dLon * dLon)
+ }
+
+ companion object {
+ const val MIN_PLACES = 3
+ const val PLACE_RADIUS_M = 300.0
+ const val WINDOW_MS = 2 * 60 * 60_000L
+ /** At most one visit a minute per device. */
+ private const val SAMPLE_MS = 60_000L
+ private const val MAX_DEVICES = 500
+ }
+}
diff --git a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
index 9b58f6e..54880b7 100644
--- a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
+++ b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
@@ -167,6 +167,8 @@ class ScanForegroundService : Service() {
private lateinit var bleEngine: BleScanEngine
private lateinit var wifiEngine: WifiScanEngine
+ /** Flagged devices that keep turning up at places along your way (pipeline thread only). */
+ private val placeFollow = PlaceFollow()
/** Drone Remote ID over Wi-Fi Aware, on phones that have it. */
private var nanRemoteId: NanRemoteId? = null
@@ -612,12 +614,13 @@ class ScanForegroundService : Service() {
maybeAlert(a, best, now)
val trackerPaused = best.category == Category.TRACKER && now < Prefs.trackerFollowPausedUntil(this)
+ val followMs = Prefs.followMinMinutes(this) * 60_000L
+ val followM = Prefs.followMinMeters(this).toDouble()
+ // Followers that stay in range, and ones that keep turning up at places along your way.
+ if (loc != null && !best.listOnly) placeFollow.heard(mac, DeviceRegistry.linkedFrom(mac), now, loc.latitude, loc.longitude)
if (loc != null && Prefs.followerAlerts(this) && !trackerPaused && !best.listOnly &&
- DeviceRegistry.checkFollowing(
- mac,
- Prefs.followMinMinutes(this) * 60_000L,
- Prefs.followMinMeters(this).toDouble()
- )
+ (DeviceRegistry.checkFollowing(mac, followMs, followM).also { if (it) placeFollow.alertedNow(mac, now) } ||
+ placeFollow.follower(mac, now, followMs, followM).also { if (it) DeviceRegistry.markFollowing(mac) })
) {
NotificationHelper.sendAlert(this, mac, best, a.rssi, following = true)
AlertPlayer.play(this, best.tier, com.rfsentinel.app.util.Spoken.device(this, best, following = true), following = true)
diff --git a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
index 398d688..561702d 100644
--- a/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
+++ b/app/src/main/java/com/rfsentinel/app/settings/SettingsActivity.kt
@@ -772,7 +772,7 @@ class SettingsActivity : AppCompatActivity() {
info(binding.shortVoiceSwitch, "\"Body cam\", \"Police car\", \"Speed camera, 50\" instead of whole sentences.")
info(binding.bubbleSwitch, "A small bubble over other apps (Waze, Google Maps...) showing the threat level. Needs \"Display over other apps\".")
info(binding.floatingMapSwitch, "A small map over other apps, centred on you, with the same device dots as RF Sentinel's map. It always uses OpenFreeMap: a window this small has no room for Mapbox's logo and info button.")
- info(binding.followSwitch, "Warns when a tracker or flagged device keeps moving with you, for at least the time and distance below.")
+ info(binding.followSwitch, "Warns when a tracker or flagged device keeps moving with you, for at least the time and distance below - whether it stays in range or keeps turning up again: heard at 3 or more places along your way within 2 hours.")
withUnit(binding.followMinutesInput, "min")
withUnit(binding.followMetersInput, "m")
info(binding.mapProviderLabel, "OpenFreeMap: free and open, OpenStreetMap data.\n\nMapbox: Mapbox's Standard map with 3D buildings and day / night light.\n\nThe floating mini map and the Android Auto map always use OpenStreetMap maps.")
diff --git a/app/src/test/java/com/rfsentinel/app/service/PlaceFollowTest.kt b/app/src/test/java/com/rfsentinel/app/service/PlaceFollowTest.kt
new file mode 100644
index 0000000..fa020e8
--- /dev/null
+++ b/app/src/test/java/com/rfsentinel/app/service/PlaceFollowTest.kt
@@ -0,0 +1,57 @@
+package com.rfsentinel.app.service
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class PlaceFollowTest {
+
+ private val min = 60_000L
+ /** About 1.1 km of latitude. */
+ private val step = 0.01
+
+ @Test
+ fun turningUpAtThreePlacesWithGapsIsFollowing() {
+ val p = PlaceFollow()
+ p.heard("aa", null, 0, 10.5, -20.5)
+ p.heard("aa", null, 6 * min, 10.5 + step, -20.5) // gone in between: a new track each time
+ assertFalse(p.follower("aa", 6 * min, 10 * min, 800.0))
+ p.heard("aa", null, 14 * min, 10.5 + 2 * step, -20.5)
+ assertEquals(3, p.places("aa", 10 * min, 800.0))
+ assertTrue(p.follower("aa", 14 * min, 10 * min, 800.0))
+ p.heard("aa", null, 30 * min, 10.5 + 3 * step, -20.5)
+ assertFalse("once per window", p.follower("aa", 30 * min, 10 * min, 800.0))
+ }
+
+ @Test
+ fun aFixedCameraPassedRepeatedlyIsOnePlace() {
+ val p = PlaceFollow()
+ for (i in 0 until 6) p.heard("cam", null, i * 10 * min, 10.5 + (i % 2) * 0.001, -20.5)
+ assertEquals(0, p.places("cam", 10 * min, 800.0))
+ }
+
+ @Test
+ fun tooQuickOrTooCloseIsNot() {
+ val p = PlaceFollow()
+ p.heard("bb", null, 0, 10.5, -20.5)
+ p.heard("bb", null, 2 * min, 10.504, -20.5)
+ p.heard("bb", null, 4 * min, 10.508, -20.5)
+ assertEquals("under 10 min", 0, p.places("bb", 10 * min, 800.0))
+ assertEquals("under 1500 m apart", 0, p.places("bb", 0, 1500.0))
+ }
+
+ @Test
+ fun anAddressRotationKeepsItsPlacesAndItsAlert() {
+ val p = PlaceFollow()
+ p.heard("old", null, 0, 10.5, -20.5)
+ p.heard("old", null, 6 * min, 10.5 + step, -20.5)
+ p.heard("new", "old", 14 * min, 10.5 + 2 * step, -20.5)
+ assertTrue(p.follower("new", 14 * min, 10 * min, 800.0))
+ p.alertedNow("x", 0)
+ p.heard("y", "x", 1 * min, 10.5, -20.5)
+ p.heard("y", null, 7 * min, 10.5 + step, -20.5)
+ p.heard("y", null, 15 * min, 10.5 + 2 * step, -20.5)
+ assertFalse("alert carried with the rotation", p.follower("y", 15 * min, 10 * min, 800.0))
+ }
+}