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.
Kotlin airtagalprandroidandroid-autoanti-surveillancebluetooth-low-energybody-cameracounter-surveillancedrone-detectionflock-safetyimsi-catcher-detectorpolice-detectorpolice-scannerprivacyremote-idrtl-sdrsdrsurveillance-detectiontracker-detectionwifi
Map: cameras load and draw sooner; small and full screen maps share their view and place card
CIS-C056df13eacb4d
diff --git a/app/src/main/java/com/rfsentinel/app/RFSentinelApp.kt b/app/src/main/java/com/rfsentinel/app/RFSentinelApp.ktindex 381a7a1..ea9cfdc 100644--- a/app/src/main/java/com/rfsentinel/app/RFSentinelApp.kt+++ b/app/src/main/java/com/rfsentinel/app/RFSentinelApp.kt@@ -67,6 +67,10 @@ class RFSentinelApp : Application() {// ~60k vendor rows: load off the main thread; lookups return null until ready.// A trace left "recording" by a crash / force-stop gets closed.appScope.launch { runCatching { com.rfsentinel.app.service.TripRecorder.closeStale(this@RFSentinelApp) } }+ // The CCTV cache too, at the same time, so the map has both ready when it opens.+ if (com.rfsentinel.app.util.Prefs.showCctv(this)) appScope.launch(Dispatchers.IO) {+ runCatching { com.rfsentinel.app.alpr.CctvStore.load(this@RFSentinelApp) }+ }appScope.launch(Dispatchers.IO) {runCatching { com.rfsentinel.app.alpr.AlprStore.load(this@RFSentinelApp) }runCatching { com.rfsentinel.app.alpr.DeflockBulk.refreshIfDue(this@RFSentinelApp) }diff --git a/app/src/main/java/com/rfsentinel/app/alpr/AlprStore.kt b/app/src/main/java/com/rfsentinel/app/alpr/AlprStore.ktindex f6ee0b2..9546ac6 100644--- a/app/src/main/java/com/rfsentinel/app/alpr/AlprStore.kt+++ b/app/src/main/java/com/rfsentinel/app/alpr/AlprStore.kt@@ -69,18 +69,23 @@ object AlprStore {* Reads the saved cache once (later calls return at once). Everything that changes the cache loads it first:* a download that ran before the file was read would merge into an empty list and save over it.*/- @Synchronizedfun load(context: Context) {- if (loaded) return- cameras = runCatching {- file(context).takeIf { it.exists() }?.readText()?.let { gson.fromJson<List<KnownCamera>>(it, listType) }- }.getOrNull().orEmpty()- areas = runCatching {- areasFile(context).takeIf { it.exists() }?.readText()?.let { gson.fromJson<List<CameraArea>>(it, areaListType) }- }.getOrNull().orEmpty()- loaded = true+ if (loaded) return // no lock once read: a merge writing the file must not hold up the map+ synchronized(this) {+ if (loaded) return+ val start = android.os.SystemClock.elapsedRealtime()+ // Streamed from the file: the US + Canada plate readers are megabytes, too big to read into one string first.+ cameras = runCatching { readJson<List<KnownCamera>>(file(context), listType) }.getOrNull().orEmpty()+ areas = runCatching { readJson<List<CameraArea>>(areasFile(context), areaListType) }.getOrNull().orEmpty()+ loaded = true+ android.util.Log.i("AlprStore", "Loaded ${cameras.size} cameras in ${android.os.SystemClock.elapsedRealtime() - start} ms")+ }}+ /** A saved JSON file parsed as it is read, or null when there is none. */+ internal fun <T> readJson(file: File, type: java.lang.reflect.Type): T? =+ file.takeIf { it.exists() }?.bufferedReader(Charsets.UTF_8, 64 * 1024)?.use { gson.fromJson<T>(it, type) }+/** True when the box isn't inside an area downloaded in the last [REFRESH_MS]. */fun needsDownload(south: Double, west: Double, north: Double, east: Double, now: Long = System.currentTimeMillis()) =CameraArea.needsDownload(areas, south, west, north, east, now, REFRESH_MS)diff --git a/app/src/main/java/com/rfsentinel/app/alpr/Cctv.kt b/app/src/main/java/com/rfsentinel/app/alpr/Cctv.ktindex bea376c..4dbea96 100644--- a/app/src/main/java/com/rfsentinel/app/alpr/Cctv.kt+++ b/app/src/main/java/com/rfsentinel/app/alpr/Cctv.kt@@ -153,14 +153,16 @@ object CctvStore {private fun file(c: Context) = File(c.filesDir, FILE)private fun areasFile(c: Context) = File(c.filesDir, AREAS_FILE)- @Synchronizedfun load(context: Context) {- if (loaded) return- cameras = runCatching { file(context).takeIf { it.exists() }?.readText()?.let { gson.fromJson<List<CctvCamera>>(it, listType) } }- .getOrNull().orEmpty()- areas = runCatching { areasFile(context).takeIf { it.exists() }?.readText()?.let { gson.fromJson<List<CameraArea>>(it, areaListType) } }- .getOrNull().orEmpty()- loaded = true+ if (loaded) return // no lock once read: a save in progress must not hold up the map+ synchronized(this) {+ if (loaded) return+ val start = android.os.SystemClock.elapsedRealtime()+ cameras = runCatching { AlprStore.readJson<List<CctvCamera>>(file(context), listType) }.getOrNull().orEmpty()+ areas = runCatching { AlprStore.readJson<List<CameraArea>>(areasFile(context), areaListType) }.getOrNull().orEmpty()+ loaded = true+ android.util.Log.i("CctvStore", "Loaded ${cameras.size} CCTV in ${android.os.SystemClock.elapsedRealtime() - start} ms")+ }}/**diff --git a/app/src/main/java/com/rfsentinel/app/ui/MapFragment.kt b/app/src/main/java/com/rfsentinel/app/ui/MapFragment.ktindex 9b05f42..0b7a2db 100644--- a/app/src/main/java/com/rfsentinel/app/ui/MapFragment.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/MapFragment.kt@@ -89,9 +89,6 @@ class MapFragment : Fragment() {const val EXTRA_NAV_LON = "nav_lon"/** Inside the main screen (its chips drive the filter; the menu is a button on the map). */const val ARG_EMBEDDED = "embedded"- /** The view the map was opened from (lat, lon, zoom, bearing, tilt) and whether it followed you: full screen carries on from it. */- const val EXTRA_CAMERA = "camera"- const val EXTRA_FOLLOWING = "following"/*** The last real heading, shared by every map (like [MapGps]): a map opened while stopped or crawling* turns the same way as the one before it, not north until you drive off.@@ -101,6 +98,8 @@ class MapFragment : Fragment() {private const val CAMERA_FAIL_SHOWN_MS = 90_000L/** [MapStart]'s levels are the classic raster map's: the vector map's are this much lower. */private const val ZOOM_SHIFT = MapStart.VECTOR_SHIFT+ /** A map screen opening carries on from the other one's view if it was on screen this recently; older, it starts on where you are. */+ private const val SHARED_VIEW_MS = 30 * 60_000L/** 3D: your arrow this far down the map (navigation apps: about 70 %), so you see more road ahead. */internal const val ARROW_HEIGHT = 0.72@@ -169,6 +168,9 @@ class MapFragment : Fragment() {private var following = false/** 3D: you pinched - your zoom until the follow button. */private var userZoom = false+ /** The [MapScreens] write this map has shown (its view; its place card): a newer one is picked up on resume. */+ private var viewSeen = -1+ private var cardSeen = -1private var engine: MapEngine? = nullprivate var nav: MapNav? = null@@ -231,11 +233,8 @@ class MapFragment : Fragment() {})}- /** The map on its own screen, looking where this one looks: same place, zoom, heading and tilt, still following you if this one was. */- private fun fullMapIntent() = Intent(act, MapActivity::class.java).apply {- map?.camera?.let { c -> putExtra(EXTRA_CAMERA, doubleArrayOf(c.lat, c.lon, c.zoom, c.bearing, c.tilt)) }- putExtra(EXTRA_FOLLOWING, following)- }+ /** The map on its own screen, looking where this one looks (see [MapScreens], written as this one pauses). */+ private fun fullMapIntent() = Intent(act, MapActivity::class.java)override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {_binding = ActivityMapBinding.inflate(inflater, container, false)@@ -313,6 +312,8 @@ class MapFragment : Fragment() {try {while (isActive) {renderLive()+ // Cameras, CCTV and towers for the view: following you, the map never goes idle to ask for them.+ rf?.safe("static") { rf?.maybeRedrawStatic() }delay(3_000)}} finally {@@ -466,15 +467,12 @@ class MapFragment : Fragment() {return}if (tripId != null) return // framed on the trace once it's loaded- val from = args.getDoubleArray(EXTRA_CAMERA)?.takeIf { it.size == 5 }- if (from != null) {- // Opened full screen from the small map: carry on from its view rather than snap north.- m.jumpTo(from[0], from[1], from[2])- m.easeTo(bearing = from[3], tilt = from[4], ms = 0)- centeredOnce = true- lastFix = knownFix()- following = args.getBoolean(EXTRA_FOLLOWING) && Permissions.granted(act, android.Manifest.permission.ACCESS_FINE_LOCATION)- styleFollowButton()+ // The other map screen was looked at not long ago (opened full screen from the small map, or back to it):+ // carry on from its view rather than snap north.+ val shared = MapScreens.view?.takeIf { System.currentTimeMillis() - it.at < SHARED_VIEW_MS }+ if (shared != null) {+ viewSeen = MapScreens.version+ lookAsShared(m, shared)return}val knownFix = knownFix()@@ -490,6 +488,37 @@ class MapFragment : Fragment() {styleFollowButton()}+ /** A live map, not a trace or a map opened on one thing (an aircraft, a Waze report, a place): its view is the one both map screens share. */+ private fun liveMap() = tripId == null &&+ !args.containsKey(EXTRA_CENTER_LAT) && !args.containsKey(EXTRA_AIRCRAFT) && !args.containsKey(EXTRA_WAZE)++ private fun lookAsShared(m: MapEngine, v: MapScreens.View) {+ m.jumpTo(v.lat, v.lon, v.zoom)+ m.easeTo(bearing = v.bearing, tilt = v.tilt, ms = 0)+ centeredOnce = true+ lastFix = knownFix()+ following = v.following && Permissions.granted(act, android.Manifest.permission.ACCESS_FINE_LOCATION)+ userZoom = following && v.userZoom+ styleFollowButton()+ }++ /** Leaving the screen: the other map screen opens on what this one shows. */+ private fun shareView() {+ if (!liveMap()) return+ val cam = map?.camera ?: return+ if (kotlin.math.abs(cam.lat) < 1e-4 && kotlin.math.abs(cam.lon) < 1e-4) return // it never looked anywhere+ MapScreens.save(MapScreens.View(cam.lat, cam.lon, cam.zoom, cam.bearing, cam.tilt, following, userZoom, System.currentTimeMillis()), nav?.card())+ viewSeen = MapScreens.version; cardSeen = MapScreens.version+ }++ /** Back on screen: what the other map screen showed since (its view, its place card and routes). */+ private fun pickUpShared() {+ if (!liveMap()) return+ if (cardSeen != MapScreens.version) { cardSeen = MapScreens.version; nav?.restore(MapScreens.card) }+ val m = map ?: return // not up yet: it starts on the shared view+ if (viewSeen != MapScreens.version) { viewSeen = MapScreens.version; MapScreens.view?.let { lookAsShared(m, it) } }+ }+/** Your position (lat, lon) for distances in the details, when known. */private fun myPosition(): Pair<Double, Double>? =(lastFix ?: ScanForegroundService.lastFix)?.let { it.latitude to it.longitude }@@ -1181,6 +1210,7 @@ class MapFragment : Fragment() {super.onResume()engine?.onResume()swapEngineIfChanged()+ pickUpShared()applyMarker() // back from Settings > Map: a new marker shows at oncestartGps()// Precise GPS while the map is on screen, so devices land where they were heard.@@ -1189,11 +1219,13 @@ class MapFragment : Fragment() {// Back from Settings > Map: show what's switched on now (and nothing that was switched off).// The saved cameras first: right after the app starts they may still be loading, and a map drawn without// them would stay empty until a new download finished.+ // Each kind is drawn as soon as it is read (the plate readers don't wait for the CCTV).viewLifecycleOwner.lifecycleScope.launch {- kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) {- com.rfsentinel.app.alpr.AlprStore.load(act)- if (Prefs.showCctv(act)) com.rfsentinel.app.alpr.CctvStore.load(act)- }+ kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) { com.rfsentinel.app.alpr.AlprStore.load(act) }+ rf?.safe("static") { rf?.maybeRedrawStatic(force = true) }+ }+ if (Prefs.showCctv(act)) viewLifecycleOwner.lifecycleScope.launch {+ kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) { com.rfsentinel.app.alpr.CctvStore.load(act) }rf?.safe("static") { rf?.maybeRedrawStatic(force = true) }}if (rf != null && rf?.isDark != darkTiles()) loadStyle()@@ -1205,6 +1237,7 @@ class MapFragment : Fragment() {override fun onPause() {if (safetyArmed) { safetyArmed = false; MapSafety.disarm(act) } // paused normally: not a crashsaveView()+ shareView()stopGps()if (tripId == null) ScanForegroundService.mapHidden(act)engine?.onPause()diff --git a/app/src/main/java/com/rfsentinel/app/ui/MapNav.kt b/app/src/main/java/com/rfsentinel/app/ui/MapNav.ktindex 94b6d42..d2f7c90 100644--- a/app/src/main/java/com/rfsentinel/app/ui/MapNav.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/MapNav.kt@@ -346,6 +346,23 @@ internal class MapNav(closeCard(keepRoute = true)}+ /** The place card as it is, for the other map screen to show the same; null when none shows. */+ fun card(): MapScreens.Card? = place?.takeIf { cardShown }?.let { MapScreens.Card(it, routes, selected, options) }++ /** The card as the other map screen left it: the same place and routes (not looked for again), or none. */+ fun restore(card: MapScreens.Card?) {+ if (card == null) { if (cardShown) closeCard(); return }+ planJob?.cancel()+ place = card.place; routes = card.routes; selected = card.selected.coerceIn(0, (card.routes.size - 1).coerceAtLeast(0)); options = card.options+ b.navCard.visibility = View.VISIBLE+ b.navRoutesScroll.visibility = View.GONE+ b.navAvoidScroll.visibility = View.GONE+ styleBackdrop()+ bindCard()+ if (routes.isNotEmpty()) showRoutes(refit = false)+ else { rf()?.drawRoutes(emptyList(), 0); rf()?.drawNavPlaces(emptyList(), card.place.lat to card.place.lon) }+ }+fun closeCard(keepRoute: Boolean = false) {planJob?.cancel()b.navCard.visibility = View.GONEdiff --git a/app/src/main/java/com/rfsentinel/app/ui/MapScreens.kt b/app/src/main/java/com/rfsentinel/app/ui/MapScreens.ktnew file mode 100644index 0000000..ceab884--- /dev/null+++ b/app/src/main/java/com/rfsentinel/app/ui/MapScreens.kt@@ -0,0 +1,33 @@+package com.rfsentinel.app.ui++import com.rfsentinel.app.nav.Place+import com.rfsentinel.app.nav.Route+import com.rfsentinel.app.nav.Valhalla++/**+ * What the live map shows, kept for both of its screens: the small map on the main screen and the full screen+ * map. The one leaving the screen writes it; the other, coming back, shows the same: the same place, zoom,+ * heading and tilt, following you or not, and the same place card and routes.+ */+internal object MapScreens {+ class View(+ val lat: Double, val lon: Double, val zoom: Double, val bearing: Double, val tilt: Double,+ val following: Boolean, val userZoom: Boolean, val at: Long+ )++ class Card(val place: Place, val routes: List<Route>, val selected: Int, val options: Valhalla.Options)++ /** Counts the writes: a map that has seen the latest one has nothing to pick up. */+ var version = 0+ private set+ var view: View? = null+ private set+ var card: Card? = null+ private set++ fun save(view: View, card: Card?) {+ this.view = view+ this.card = card+ version+++ }+}diff --git a/app/src/main/java/com/rfsentinel/app/ui/RfMap.kt b/app/src/main/java/com/rfsentinel/app/ui/RfMap.ktindex 9790491..999895d 100644--- a/app/src/main/java/com/rfsentinel/app/ui/RfMap.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/RfMap.kt@@ -334,6 +334,7 @@ internal class RfMap(private val activity: AppCompatActivity, val map: MapEngineval now = System.currentTimeMillis()val box = staticBoxif (!force && box != null && box.contains(view) && now - staticAt < 15_000) 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@@ -382,6 +383,8 @@ internal class RfMap(private val activity: AppCompatActivity, val map: MapEngine})maybeFetchCameras(view)+ val took = android.os.SystemClock.elapsedRealtime() - drawStart+ if (took > 100) android.util.Log.i("RfMap", "Cameras, CCTV and towers drawn in $took ms (${cameras.size} + ${cctvs.size} + ${towers.size})")}private fun clearStatic() {