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.

Preserve device edits during scanning and history imports

greyxp1cd38905aa586
diff --git a/app/src/main/java/com/rfsentinel/app/data/DeviceNames.kt b/app/src/main/java/com/rfsentinel/app/data/DeviceNames.kt
index efc2797..2362510 100644
--- a/app/src/main/java/com/rfsentinel/app/data/DeviceNames.kt
+++ b/app/src/main/java/com/rfsentinel/app/data/DeviceNames.kt
@@ -2,6 +2,7 @@ package com.rfsentinel.app.data
import android.content.Context
import com.rfsentinel.app.detect.AdvertFingerprint
+import androidx.room.withTransaction
/** Your own names for devices (known_devices.alias), kept in memory so the list and map needn't read the database per device. */
object DeviceNames {
@@ -38,8 +39,12 @@ object DeviceNames {
*/
suspend fun set(context: Context, mac: String, alias: String?, name: String?, vendor: String?, type: String?, fingerprint: String? = null) {
val clean = alias?.trim()?.take(64)?.ifEmpty { null }
- ensureRow(context, mac, name, vendor, type)
- AppDatabase.getInstance(context).knownDeviceDao().setAlias(mac, clean)
+ val db = AppDatabase.getInstance(context)
+ db.withTransaction {
+ val dao = db.knownDeviceDao()
+ dao.ensureRow(mac, name, vendor, type)
+ dao.setAlias(mac, clean)
+ }
names = if (clean != null) names + (mac to clean) else names - mac
val kept = looks.filterValues { it != mac }
looks = if (clean != null && fingerprint != null) kept + (AdvertFingerprint.hash(fingerprint) to mac) else kept
@@ -49,15 +54,11 @@ object DeviceNames {
/** Saves your note on a device ([note] blank clears it). */
suspend fun setNote(context: Context, mac: String, note: String?, name: String?, vendor: String?, type: String?) {
- ensureRow(context, mac, name, vendor, type)
- AppDatabase.getInstance(context).knownDeviceDao().setNote(mac, note?.trim()?.take(1000)?.ifEmpty { null })
- }
-
- private suspend fun ensureRow(context: Context, mac: String, name: String?, vendor: String?, type: String?) {
- val dao = AppDatabase.getInstance(context).knownDeviceDao()
- if (dao.get(mac) == null) {
- val now = System.currentTimeMillis()
- dao.upsertAll(listOf(KnownDeviceEntity(mac, now, now, 1, 1, name, vendor, type)))
+ val db = AppDatabase.getInstance(context)
+ db.withTransaction {
+ val dao = db.knownDeviceDao()
+ dao.ensureRow(mac, name, vendor, type)
+ dao.setNote(mac, note?.trim()?.take(1000)?.ifEmpty { null })
}
}
}
diff --git a/app/src/main/java/com/rfsentinel/app/data/Favorites.kt b/app/src/main/java/com/rfsentinel/app/data/Favorites.kt
index ebce9e3..0b45a16 100644
--- a/app/src/main/java/com/rfsentinel/app/data/Favorites.kt
+++ b/app/src/main/java/com/rfsentinel/app/data/Favorites.kt
@@ -1,6 +1,7 @@
package com.rfsentinel.app.data
import android.content.Context
+import androidx.room.withTransaction
/** In-memory mirror of known_devices.favorite for fast list filtering. */
object Favorites {
@@ -14,11 +15,10 @@ object Favorites {
/** Stars / un-stars a device, creating its history row if it hasn't been flushed yet. */
suspend fun set(context: Context, mac: String, favorite: Boolean, name: String?, vendor: String?, type: String?) {
- val dao = AppDatabase.getInstance(context).knownDeviceDao()
- if (dao.get(mac) == null) {
- val now = System.currentTimeMillis()
- dao.upsertAll(listOf(KnownDeviceEntity(mac, now, now, 1, 1, name, vendor, type, favorite)))
- } else {
+ val db = AppDatabase.getInstance(context)
+ db.withTransaction {
+ val dao = db.knownDeviceDao()
+ dao.ensureRow(mac, name, vendor, type)
dao.setFavorite(mac, favorite)
}
macs = if (favorite) macs + mac else macs - mac
diff --git a/app/src/main/java/com/rfsentinel/app/data/KnownDeviceEntity.kt b/app/src/main/java/com/rfsentinel/app/data/KnownDeviceEntity.kt
index c535c68..0d6b571 100644
--- a/app/src/main/java/com/rfsentinel/app/data/KnownDeviceEntity.kt
+++ b/app/src/main/java/com/rfsentinel/app/data/KnownDeviceEntity.kt
@@ -4,8 +4,11 @@ import androidx.room.ColumnInfo
import androidx.room.Dao
import androidx.room.Entity
import androidx.room.Index
+import androidx.room.Insert
+import androidx.room.OnConflictStrategy
import androidx.room.PrimaryKey
import androidx.room.Query
+import androidx.room.Transaction
import androidx.room.Upsert
/**
@@ -53,6 +56,32 @@ interface KnownDeviceDao {
@Upsert
suspend fun upsertAll(rows: List<KnownDeviceEntity>)
+ @Insert(onConflict = OnConflictStrategy.IGNORE)
+ suspend fun insertIfMissing(row: KnownDeviceEntity)
+
+ /** Creates history for an unseen device without replacing a concurrent observation or user edit. */
+ suspend fun ensureRow(mac: String, name: String?, vendor: String?, type: String?) {
+ val now = System.currentTimeMillis()
+ insertIfMissing(KnownDeviceEntity(mac, now, now, 1, 1, name, vendor, type))
+ }
+
+ /** Merges observations without overwriting names, notes or favorites edited by the user. */
+ @Transaction
+ suspend fun recordSightings(rows: List<KnownDeviceEntity>, sessionStart: Long) {
+ val existing = getAll(rows.map { it.mac }).associateBy { it.mac }
+ upsertAll(rows.map { seen ->
+ val old = existing[seen.mac] ?: return@map seen
+ old.copy(
+ lastSeen = maxOf(old.lastSeen, seen.lastSeen),
+ sessions = old.sessions + if (old.lastSeen < sessionStart) 1 else 0,
+ detectCount = old.detectCount + 1,
+ name = seen.name ?: old.name,
+ vendor = seen.vendor ?: old.vendor,
+ deviceType = seen.deviceType
+ )
+ })
+ }
+
@Query("UPDATE known_devices SET favorite = :favorite WHERE mac = :mac")
suspend fun setFavorite(mac: String, favorite: Boolean)
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 33cc2f5..36480ee 100644
--- a/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
+++ b/app/src/main/java/com/rfsentinel/app/service/ScanForegroundService.kt
@@ -1241,23 +1241,18 @@ class ScanForegroundService : Service() {
val dao = AppDatabase.getInstance(this).knownDeviceDao()
val sessionStart = batch.sessionStart
batch.seen.chunked(400).forEach { chunk ->
- val existing = dao.getAll(chunk.map { it.mac }).associateBy { it.mac }
- dao.upsertAll(chunk.map { s ->
- val e = existing[s.mac]
+ dao.recordSightings(chunk.map { s ->
KnownDeviceEntity(
mac = s.mac,
- firstSeen = e?.firstSeen ?: s.firstSeen,
+ firstSeen = s.firstSeen,
lastSeen = s.lastSeen,
- sessions = (e?.sessions ?: 0) + if (e == null || e.lastSeen < sessionStart) 1 else 0,
- detectCount = (e?.detectCount ?: 0) + 1,
- name = s.name ?: e?.name,
- vendor = s.vendor ?: e?.vendor,
- deviceType = s.deviceType,
- favorite = e?.favorite ?: false,
- note = e?.note,
- alias = e?.alias
+ sessions = 1,
+ detectCount = 1,
+ name = s.name,
+ vendor = s.vendor,
+ deviceType = s.deviceType
)
- })
+ }, sessionStart)
}
batch.details.chunked(200).forEach { AppDatabase.getInstance(this).deviceDetailsDao().upsertAll(it) }
}
diff --git a/app/src/main/java/com/rfsentinel/app/ui/DeviceHistoryActivity.kt b/app/src/main/java/com/rfsentinel/app/ui/DeviceHistoryActivity.kt
index 1347bac..462e5c7 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/DeviceHistoryActivity.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/DeviceHistoryActivity.kt
@@ -134,9 +134,9 @@ class DeviceHistoryActivity : AppCompatActivity() {
lifecycleScope.launch {
val result = runCatching {
kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) {
- val dao = com.rfsentinel.app.data.AppDatabase.getInstance(this@DeviceHistoryActivity).knownDeviceDao()
+ val db = AppDatabase.getInstance(this@DeviceHistoryActivity)
contentResolver.openInputStream(uri)!!.use { raw ->
- com.rfsentinel.app.util.DeviceHistoryImport.importInto(dao, com.rfsentinel.app.util.DeviceHistoryImport.Limited(raw)) { n ->
+ com.rfsentinel.app.util.DeviceHistoryImport.importInto(db, com.rfsentinel.app.util.DeviceHistoryImport.Limited(raw)) { n ->
runOnUiThread { if (!isDestroyed) progress.text = "$n devices read..." }
}
}
diff --git a/app/src/main/java/com/rfsentinel/app/util/DeviceHistoryImport.kt b/app/src/main/java/com/rfsentinel/app/util/DeviceHistoryImport.kt
index 96aec76..607a8dc 100644
--- a/app/src/main/java/com/rfsentinel/app/util/DeviceHistoryImport.kt
+++ b/app/src/main/java/com/rfsentinel/app/util/DeviceHistoryImport.kt
@@ -1,8 +1,9 @@
package com.rfsentinel.app.util
+import androidx.room.withTransaction
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonToken
-import com.rfsentinel.app.data.KnownDeviceDao
+import com.rfsentinel.app.data.AppDatabase
import com.rfsentinel.app.data.KnownDeviceEntity
import java.io.BufferedReader
import java.io.InputStream
@@ -19,7 +20,7 @@ import java.util.TimeZone
* a star or note already there stays, and a name or vendor fills in only where there is none.
*
* Reading is streamed and rows are written in batches, so a history of a hundred thousand devices does not need to fit
- * in memory. Pure Kotlin apart from the DAO, so it is unit-tested.
+ * in memory. Parsing and merging are pure Kotlin.
*/
object DeviceHistoryImport {
@@ -190,7 +191,8 @@ object DeviceHistoryImport {
// ---- into the database -------------------------------------------------------------------------------
/** Merges the file into the history; [onProgress] gets the rows handled so far. Throws [NotHistory] for another kind of file. */
- suspend fun importInto(dao: KnownDeviceDao, input: InputStream, onProgress: (Int) -> Unit = {}): Result {
+ suspend fun importInto(db: AppDatabase, input: InputStream, onProgress: (Int) -> Unit = {}): Result {
+ val dao = db.knownDeviceDao()
var added = 0; var updated = 0; var same = 0; var skipped = 0; var seen = 0
val batch = ArrayList<KnownDeviceEntity>()
suspend fun flush() {
@@ -198,18 +200,20 @@ object DeviceHistoryImport {
// A file can list a device twice: the later rows merge into the earlier ones first.
val byMac = LinkedHashMap<String, KnownDeviceEntity>()
for (b in batch) byMac[b.mac] = merge(byMac[b.mac], b)
- val have = dao.getAll(byMac.keys.toList()).associateBy { it.mac }
- val write = ArrayList<KnownDeviceEntity>()
- for ((mac, incoming) in byMac) {
- val old = have[mac]
- val merged = merge(old, incoming)
- when {
- old == null -> { added++; write += merged }
- merged != old -> { updated++; write += merged }
- else -> same++
+ db.withTransaction {
+ val have = dao.getAll(byMac.keys.toList()).associateBy { it.mac }
+ val write = ArrayList<KnownDeviceEntity>()
+ for ((mac, incoming) in byMac) {
+ val old = have[mac]
+ val merged = merge(old, incoming)
+ when {
+ old == null -> { added++; write += merged }
+ merged != old -> { updated++; write += merged }
+ else -> same++
+ }
}
+ if (write.isNotEmpty()) dao.upsertAll(write)
}
- if (write.isNotEmpty()) dao.upsertAll(write)
batch.clear()
}
for (row in rows(input)) {
diff --git a/app/src/test/java/com/rfsentinel/app/util/DeviceHistoryImportTest.kt b/app/src/test/java/com/rfsentinel/app/util/DeviceHistoryImportTest.kt
index dc7491f..5d49a0c 100644
--- a/app/src/test/java/com/rfsentinel/app/util/DeviceHistoryImportTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/util/DeviceHistoryImportTest.kt
@@ -34,7 +34,7 @@ class DeviceHistoryImportTest {
"stray"
]"""
- private fun run(text: String) = runBlocking { DeviceHistoryImport.importInto(db.knownDeviceDao(), text.byteInputStream()) }
+ private fun run(text: String) = runBlocking { DeviceHistoryImport.importInto(db, text.byteInputStream()) }
@Test
fun theJsonExportIsReadBackWithItsDetails() {