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.

CCTV: show again at once when zooming back in; download what's on screen and around you first

CIS-C0f6263fe45529
diff --git a/app/src/main/java/com/rfsentinel/app/alpr/Cctv.kt b/app/src/main/java/com/rfsentinel/app/alpr/Cctv.kt
index 4dbea96..259dd16 100644
--- a/app/src/main/java/com/rfsentinel/app/alpr/Cctv.kt
+++ b/app/src/main/java/com/rfsentinel/app/alpr/Cctv.kt
@@ -174,7 +174,7 @@ object CctvStore {
if (busy.get()) return
scope.launch {
withContext(Dispatchers.IO) { load(app) }
- val tiles = CameraArea.tilesAround(lat, lon, radiusKm, MAX_SPAN_DEG)
+ val tiles = nearestFirst(CameraArea.tilesAround(lat, lon, radiusKm, MAX_SPAN_DEG), lat, lon)
.filter { needsDownload(it[0], it[1], it[2], it[3]) }
if (tiles.isEmpty()) return@launch
runCatching { downloadTiles(app, tiles) }
@@ -184,7 +184,17 @@ object CctvStore {
/** "Download cameras around me" (Settings): the CCTV within the radius; how many were found. */
suspend fun downloadAround(context: Context, lat: Double, lon: Double, radiusKm: Double, onProgress: (String) -> Unit = {}): Int {
withContext(Dispatchers.IO) { load(context) }
- return downloadTiles(context, CameraArea.tilesAround(lat, lon, radiusKm, MAX_SPAN_DEG), onProgress)
+ return downloadTiles(context, nearestFirst(CameraArea.tilesAround(lat, lon, radiusKm, MAX_SPAN_DEG), lat, lon), onProgress)
+ }
+
+ /** Where you are first, then outwards: the cameras around you show in the first minute, not the last. */
+ fun nearestFirst(tiles: List<DoubleArray>, lat: Double, lon: Double): List<DoubleArray> {
+ val cos = kotlin.math.cos(Math.toRadians(lat))
+ return tiles.sortedBy { t ->
+ val dLat = (t[0] + t[2]) / 2 - lat
+ val dLon = ((t[1] + t[3]) / 2 - lon) * cos
+ dLat * dLat + dLon * dLon
+ }
}
/** Areas still missing after the last download around you (retried the next time the map opens). */
diff --git a/app/src/main/java/com/rfsentinel/app/ui/RfMap.kt b/app/src/main/java/com/rfsentinel/app/ui/RfMap.kt
index 999895d..c589e15 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/RfMap.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/RfMap.kt
@@ -90,6 +90,8 @@ internal class RfMap(private val activity: AppCompatActivity, val map: MapEngine
/** The area the camera, CCTV and tower layers were drawn for (redrawn when the view leaves it). */
private var staticBox: MapEngine.Box? = null
private var staticAt = 0L
+ /** Whether the CCTV were drawn (zoomed in enough): zooming back in past that level redraws them at once. */
+ private var staticCctv = false
private var lastCameraFetch = 0L
private var lastCctvFetch = 0L
@@ -333,14 +335,19 @@ internal class RfMap(private val activity: AppCompatActivity, val map: MapEngine
val view = map.visibleBox() ?: return
val now = System.currentTimeMillis()
val box = staticBox
- if (!force && box != null && box.contains(view) && now - staticAt < 15_000) return
+ val cctvZoom = map.camera.zoom >= CCTV_MIN_ZOOM
+ if (!force && box != null && box.contains(view) && now - staticAt < 15_000 && cctvZoom == staticCctv) return
val drawStart = android.os.SystemClock.elapsedRealtime()
val (n, e, so, w) = grownBox(view.north, view.east, view.south, view.west, 0.5)
val big = MapEngine.Box(n, e, so, w)
- staticBox = big; staticAt = now
+ staticBox = big; staticAt = now; staticCctv = cctvZoom
fun inside(lat: Double, lon: Double) = big.contains(lat, lon)
+ // Over the limit, the ones on screen come first (not whichever the file lists first).
+ fun <T> onScreenFirst(list: List<T>, max: Int, lat: (T) -> Double, lon: (T) -> Double): List<T> =
+ if (list.size <= max) list else list.partition { view.contains(lat(it), lon(it)) }.let { (seen, around) -> (seen + around).take(max) }
- val cams = if (Prefs.showKnownAlpr(activity)) AlprStore.cameras.filter { inside(it.lat, it.lon) }.take(1500) else emptyList()
+ val cams = if (Prefs.showKnownAlpr(activity))
+ onScreenFirst(AlprStore.cameras.filter { inside(it.lat, it.lon) }, 1500, { it.lat }, { it.lon }) else emptyList()
cameras = cams.associateBy { it.osmId }
set(CAMERAS, cams.map { c ->
GeoJson.feature(GeoJson.point(c.lat, c.lon), "id" to c.osmId, "icon" to "rf-cam-${c.type.name}",
@@ -355,8 +362,8 @@ internal class RfMap(private val activity: AppCompatActivity, val map: MapEngine
val showCctv = Prefs.showCctv(activity)
val private = Prefs.cctvPrivate(activity)
- val cctv = if (showCctv && map.camera.zoom >= CCTV_MIN_ZOOM)
- CctvStore.cameras.filter { (private || !it.isPrivate) && inside(it.lat, it.lon) }.take(3000) else emptyList()
+ val cctv = if (showCctv && cctvZoom)
+ onScreenFirst(CctvStore.cameras.filter { (private || !it.isPrivate) && inside(it.lat, it.lon) }, 3000, { it.lat }, { it.lon }) else emptyList()
cctvs = cctv.associateBy { it.osmId }
set(CCTV, cctv.map { c ->
GeoJson.feature(GeoJson.point(c.lat, c.lon), "id" to c.osmId, "icon" to if (c.isPrivate) "rf-cctv-private" else "rf-cctv-public")
@@ -427,11 +434,18 @@ internal class RfMap(private val activity: AppCompatActivity, val map: MapEngine
val small = view.north - view.south <= span && view.east - view.west <= span
if (Prefs.showCctv(activity) && now - lastCctvFetch > 20_000 && !CctvStore.isBusy) {
lastCctvFetch = now
- val here = ScanForegroundService.lastFix
- if (here != null) CctvStore.ensureAround(activity, here.latitude, here.longitude, Prefs.cameraRadiusKm(activity).toDouble())
- if (!CctvStore.isBusy && small && map.camera.zoom >= CCTV_MIN_ZOOM) activity.lifecycleScope.launch {
- runCatching { CctvStore.autoDownload(activity, view.south, view.west, view.north, view.east) }
- safe("static") { maybeRedrawStatic(force = true); onStaticChanged?.invoke() }
+ // What's on screen first; the area around you (many tiles, minutes) only once that is there.
+ // (Unless the servers just refused it: then the next tile around you is worth a try meanwhile.)
+ if (small && map.camera.zoom >= CCTV_MIN_ZOOM && now - CctvStore.lastFailureAt > 60_000 &&
+ CctvStore.needsDownload(view.south, view.west, view.north, view.east)) {
+ activity.lifecycleScope.launch {
+ val changed = runCatching { CctvStore.autoDownload(activity, view.south, view.west, view.north, view.east) }.getOrDefault(false)
+ if (changed) lastCctvFetch = 0L // straight on to the area around you
+ safe("static") { maybeRedrawStatic(force = true); onStaticChanged?.invoke() }
+ }
+ } else {
+ val here = ScanForegroundService.lastFix
+ if (here != null) CctvStore.ensureAround(activity, here.latitude, here.longitude, Prefs.cameraRadiusKm(activity).toDouble())
}
}
if (now - lastCameraFetch < 20_000) return
diff --git a/app/src/test/java/com/rfsentinel/app/alpr/CameraAreaTest.kt b/app/src/test/java/com/rfsentinel/app/alpr/CameraAreaTest.kt
index 2104622..0f75a9b 100644
--- a/app/src/test/java/com/rfsentinel/app/alpr/CameraAreaTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/alpr/CameraAreaTest.kt
@@ -56,4 +56,16 @@ class CameraAreaTest {
val max = CameraArea.expand(10.0, -21.0, 12.0, -19.0, maxSpan = 2.0)
assertEquals(listOf(10.0, -21.0, 12.0, -19.0), max)
}
+
+ @Test
+ fun cctvTilesStartWhereYouAre() {
+ // A point near the north-east corner of a 4 x 4 grid: its own tile first, the far corner last.
+ val tiles = CameraArea.tilesAround(10.0, -20.0, radiusKm = 200.0, maxTileDeg = 1.0)
+ val ordered = CctvStore.nearestFirst(tiles, 10.9, -19.1)
+ assertEquals(tiles.size, ordered.size)
+ val first = ordered.first()
+ assertTrue(10.9 in first[0]..first[2] && -19.1 in first[1]..first[3])
+ val last = ordered.last()
+ assertTrue(last[0] == tiles.minOf { it[0] } && last[1] == tiles.minOf { it[1] })
+ }
}