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
Device lists: Sort by and Group by in the menu (category, radio, maker, distance), shared by the main screen and snapshots; alerts stay on top
CIS-C022804a2dfb68
diff --git a/app/src/main/java/com/rfsentinel/app/MainActivity.kt b/app/src/main/java/com/rfsentinel/app/MainActivity.ktindex 4bbd41e..211e087 100644--- a/app/src/main/java/com/rfsentinel/app/MainActivity.kt+++ b/app/src/main/java/com/rfsentinel/app/MainActivity.kt@@ -695,14 +695,20 @@ class MainActivity : AppCompatActivity() {if (chip.text != label) chip.text = labelstyleFilterChip(chip, visualChips, if (f == com.rfsentinel.app.ui.DeviceFilter.FLAGGED && n > 0) 0xFFD0503C.toInt() else null)}- val visible = all.filter { filter.matches(it) && matchesQuery(it) }- .sortedWith(- compareByDescending<DeviceRegistry.Snapshot> { it.best != null && !WhitelistCache.contains(it.mac) }- .thenByDescending { it.following }- .thenByDescending { it.best?.confidence ?: 0 }- .thenBy { bandOrder(ProximityUtil.band(it.rssi)) }- .thenBy { it.firstSeen }- )+ // Sort by / Group by from the ⋮ menu (alerts stay on top); the default is alerts, then closest.+ val sections = com.rfsentinel.app.ui.DeviceOrder.arrange(+ all.filter { filter.matches(it) && matchesQuery(it) }.map { s ->+ com.rfsentinel.app.ui.DeviceOrder.Item(s, com.rfsentinel.app.ui.DeviceFilter.seen(s),+ com.rfsentinel.app.data.DeviceNames.of(s.mac) ?: s.best?.label ?: s.name ?: s.deviceType,+ s.vendor, s.rssi, s.firstSeen, s.lastSeen)+ },+ com.rfsentinel.app.ui.DeviceOrder.sort(this), com.rfsentinel.app.ui.DeviceOrder.group(this),+ compareByDescending<com.rfsentinel.app.ui.DeviceOrder.Item<DeviceRegistry.Snapshot>> { it.value.following }+ .thenByDescending { it.value.best?.confidence ?: 0 }+ .thenBy { bandOrder(ProximityUtil.band(it.rssi)) }+ .thenBy { it.firstSeen }+ )+ val visible = sections.flatMap { it.items }shownDevices = visiblerefreshMuteButton()@@ -732,7 +738,11 @@ class MainActivity : AppCompatActivity() {val (urgent, quiet) = waze.partition { it.flashing }val (radioLive, radioOld) = radio.partition { it.flashing }val (airUrgent, airQuiet) = air.partition { it.flashing }- submitRows(airUrgent + radioLive + urgent + visible.map { toRow(it, now, threshold) } + airQuiet + radioOld + cells + quiet)+ val deviceRows = sections.flatMap { sec ->+ listOfNotNull(sec.header?.let { DeviceRow.header(com.rfsentinel.app.ui.DeviceOrder.headerLabel(sec)) }) ++ sec.items.map { toRow(it, now, threshold) }+ }+ submitRows(airUrgent + radioLive + urgent + deviceRows + airQuiet + radioOld + cells + quiet)shownCells = cells.size + waze.size + radio.size + air.size}binding.emptyState.visibility = if (visible.isEmpty() && (!panes.listShown || shownCells == 0) &&@@ -1203,6 +1213,7 @@ class MainActivity : AppCompatActivity() {override fun onCreateOptionsMenu(menu: Menu): Boolean {menuInflater.inflate(R.menu.menu_main, menu)+ com.rfsentinel.app.ui.DeviceOrder.addMenu(menu, this)(menu.findItem(R.id.action_search)?.actionView as? SearchView)?.apply {queryHint = "Name, address, vendor, type..."setOnQueryTextListener(object : SearchView.OnQueryTextListener {@@ -1255,6 +1266,7 @@ class MainActivity : AppCompatActivity() {}override fun onOptionsItemSelected(item: MenuItem): Boolean {+ if (com.rfsentinel.app.ui.DeviceOrder.onMenuItem(item, this)) { render(); return true }return when (item.itemId) {R.id.action_waze_now -> { com.rfsentinel.app.ui.WazeUi.checkNow(this); true }R.id.action_waze_status -> { com.rfsentinel.app.ui.WazeUi.openStatus(this); true }diff --git a/app/src/main/java/com/rfsentinel/app/ui/DeviceAdapter.kt b/app/src/main/java/com/rfsentinel/app/ui/DeviceAdapter.ktindex 6b96ae7..ffa4277 100644--- a/app/src/main/java/com/rfsentinel/app/ui/DeviceAdapter.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/DeviceAdapter.kt@@ -6,6 +6,7 @@ import android.graphics.Typefaceimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroup+import android.widget.TextViewimport androidx.recyclerview.widget.DiffUtilimport androidx.recyclerview.widget.ListAdapterimport androidx.recyclerview.widget.RecyclerView@@ -35,8 +36,14 @@ data class DeviceRow(val rssi: Int? = null,val confidence: Int? = null,/** Added to [rssi] for the bars only: cell signals run about 10 dB lower than WiFi and Bluetooth. */- val barBoost: Int = 0-)+ val barBoost: Int = 0,+ /** A group's header ("Trackers · 3", Group by in the ⋮ menu): [title] only, not a device. */+ val header: Boolean = false+) {+ companion object {+ fun header(label: String) = DeviceRow("header:$label", label, "", "", null, 0, null, false, true, header = true)+ }+}private const val ESP_BADGE = 0xFF1E88E5.toInt()private const val PHONE_BADGE = 0xFF2E7D32.toInt()@@ -66,7 +73,7 @@ private fun metaWithBadges(row: DeviceRow): CharSequence {class DeviceAdapter(private val onClick: (DeviceRow) -> Unit,private val onLongPress: (DeviceRow) -> Unit-) : ListAdapter<DeviceRow, DeviceAdapter.VH>(Diff) {+) : ListAdapter<DeviceRow, RecyclerView.ViewHolder>(Diff) {/** Visual style: signal bars and a confidence bar on each row (Settings > Appearance, or the list's own choice). */@android.annotation.SuppressLint("NotifyDataSetChanged")@@ -118,11 +125,19 @@ class DeviceAdapter(}}- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH =- VH(ItemDeviceBinding.inflate(LayoutInflater.from(parent.context), parent, false))+ /** A group's header: bold, in the accent colour, like the settings' section headers. */+ class HeaderVH(val text: TextView) : RecyclerView.ViewHolder(text)++ override fun getItemViewType(position: Int) = if (getItem(position).header) HEADER else DEVICE++ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder =+ if (viewType == HEADER) HeaderVH(groupHeader(parent))+ else VH(ItemDeviceBinding.inflate(LayoutInflater.from(parent.context), parent, false))- override fun onBindViewHolder(holder: VH, position: Int) {+ override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {val row = getItem(position)+ if (holder is HeaderVH) { holder.text.text = row.title; return }+ holder as VHval b = holder.bindingif (row.tag != null) {b.tagText.visibility = View.VISIBLE@@ -148,8 +163,25 @@ class DeviceAdapter(holder.itemView.setOnLongClickListener { onLongPress(row); true }}- override fun onViewRecycled(holder: VH) {- holder.stopFlash()+ override fun onViewRecycled(holder: RecyclerView.ViewHolder) {+ (holder as? VH)?.stopFlash()super.onViewRecycled(holder)}++ companion object {+ private const val DEVICE = 0+ private const val HEADER = 1++ /** The header row of a group, shared with the snapshot list so both look the same. */+ fun groupHeader(parent: ViewGroup): TextView = TextView(parent.context).apply {+ val dp = resources.displayMetrics.density+ layoutParams = RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)+ setPaddingRelative((12 * dp).toInt(), (14 * dp).toInt(), (12 * dp).toInt(), (4 * dp).toInt())+ textSize = 14f+ setTypeface(typeface, Typeface.BOLD)+ val tv = android.util.TypedValue()+ if (context.theme.resolveAttribute(com.google.android.material.R.attr.colorSecondary, tv, true)) setTextColor(tv.data)+ isClickable = false+ }+ }}diff --git a/app/src/main/java/com/rfsentinel/app/ui/DeviceFilter.kt b/app/src/main/java/com/rfsentinel/app/ui/DeviceFilter.ktindex fece903..87dee0f 100644--- a/app/src/main/java/com/rfsentinel/app/ui/DeviceFilter.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/DeviceFilter.kt@@ -60,17 +60,10 @@ enum class DeviceFilter(val label: String, val liveOnly: Boolean = false) {val remoteId: Boolean = false)- fun matches(s: DeviceRegistry.Snapshot): Boolean = matches(Seen(- s.mac, s.deviceType, s.name, s.hits, s.sources, s.best != null && !WhitelistCache.contains(s.mac),- s.facts, s.isNew, s.remoteId != null- ))+ fun matches(s: DeviceRegistry.Snapshot): Boolean = matches(seen(s))/** A device saved in a snapshot or a trace (its type read with today's names, see [DeviceIntel.savedType]). */- fun matches(d: TripDeviceEntity): Boolean = !liveOnly && matches(Seen(- d.mac, DeviceIntel.savedType(d.deviceType, d.vendor, d.mac).orEmpty(), d.name,- listOfNotNull(Category.parse(d.category)?.let { Hit(it, d.label, d.confidence, d.evidence.orEmpty(), "") }),- setOfNotNull(Advert.Source.entries.firstOrNull { it.name == d.source }), d.flagged- ))+ fun matches(d: TripDeviceEntity): Boolean = !liveOnly && matches(seen(d))fun matches(s: Seen): Boolean = when (this) {ALL -> true@@ -126,6 +119,17 @@ enum class DeviceFilter(val label: String, val liveOnly: Boolean = false) {s.type.contains("Find My", true) || s.type.contains("AirTag", true) ||s.hits.any { com.rfsentinel.app.detect.SignatureEngine.isAppleFindMy(it) }+ fun seen(s: DeviceRegistry.Snapshot) = Seen(+ s.mac, s.deviceType, s.name, s.hits, s.sources, s.best != null && !WhitelistCache.contains(s.mac),+ s.facts, s.isNew, s.remoteId != null+ )++ fun seen(d: TripDeviceEntity) = Seen(+ d.mac, DeviceIntel.savedType(d.deviceType, d.vendor, d.mac).orEmpty(), d.name,+ listOfNotNull(Category.parse(d.category)?.let { Hit(it, d.label, d.confidence, d.evidence.orEmpty(), "") }),+ setOfNotNull(Advert.Source.entries.firstOrNull { it.name == d.source }), d.flagged+ )+fun parse(name: String?) = entries.firstOrNull { it.name == name } ?: ALL/**diff --git a/app/src/main/java/com/rfsentinel/app/ui/DeviceOrder.kt b/app/src/main/java/com/rfsentinel/app/ui/DeviceOrder.ktnew file mode 100644index 0000000..6192279--- /dev/null+++ b/app/src/main/java/com/rfsentinel/app/ui/DeviceOrder.kt@@ -0,0 +1,134 @@+package com.rfsentinel.app.ui++import android.content.Context+import android.view.Menu+import android.view.MenuItem+import com.rfsentinel.app.detect.Advert+import com.rfsentinel.app.detect.DeviceIntel+import com.rfsentinel.app.util.Prefs+import com.rfsentinel.app.util.ProximityUtil++/**+ * How the device lists are sorted and grouped (the ⋮ menu's Sort by / Group by), shared by the main+ * screen and saved snapshots so one choice applies to both. Alerts always stay on top.+ */+object DeviceOrder {+ enum class Sort(val label: String) {+ ALERTS("Alerts, then closest"),+ SIGNAL("Signal strength"),+ NAME("Name"),+ RECENT("Recently seen"),+ FIRST_SEEN("First seen"),+ TYPE("Type & maker")+ }++ enum class Group(val label: String) {+ NONE("None"),+ CATEGORY("Category"),+ RADIO("Radio"),+ MAKER("Maker"),+ DISTANCE("Distance")+ }++ /** What sorting and grouping look at, for a device heard now or one saved. */+ class Item<T>(+ val value: T,+ val seen: DeviceFilter.Seen,+ val title: String,+ val vendor: String?,+ val rssi: Int,+ val firstSeen: Long,+ val lastSeen: Long+ )++ /** A group of the list: its header (null when not grouped) and its devices, in order. */+ class Section<T>(val header: String?, val items: List<T>)++ fun sort(context: Context) = Sort.entries.firstOrNull { it.name == Prefs.listSort(context) } ?: Sort.ALERTS+ fun group(context: Context) = Group.entries.firstOrNull { it.name == Prefs.listGroup(context) } ?: Group.NONE++ /** Category groups, in the filter chips' spirit: what matters most first. */+ private val CATEGORY_ORDER = listOf(+ DeviceFilter.TRACKERS, DeviceFilter.DRONES, DeviceFilter.CAMERAS, DeviceFilter.VEHICLES, DeviceFilter.PHONES,+ DeviceFilter.AUDIO, DeviceFilter.ROUTERS, DeviceFilter.HOTSPOTS, DeviceFilter.TVS, DeviceFilter.SMART_HOME+ )+ private const val OTHER = "Other"+ private const val UNKNOWN_MAKER = "Unknown maker"+ private val BANDS = listOf("Near", "Nearby", "Far")++ /**+ * Sorts and groups [items]. [alertsOrder] is the list's own order for Sort.ALERTS (the main screen's:+ * flagged, following, confidence, closeness). Flagged devices come first whatever the choice, in an+ * "Alerts" group of their own when grouped.+ */+ fun <T> arrange(items: List<Item<T>>, sort: Sort, group: Group, alertsOrder: Comparator<Item<T>>): List<Section<T>> {+ val order: Comparator<Item<T>> = compareByDescending<Item<T>> { it.seen.flagged }.then(+ when (sort) {+ Sort.ALERTS -> alertsOrder+ Sort.SIGNAL -> compareByDescending { it.rssi }+ Sort.NAME -> compareBy<Item<T>> { it.seen.name.isNullOrBlank() }.thenBy { it.title.lowercase() }+ Sort.RECENT -> compareByDescending { it.lastSeen }+ Sort.FIRST_SEEN -> compareByDescending { it.firstSeen }+ Sort.TYPE -> compareBy<Item<T>> { it.seen.type.lowercase() }.thenBy { maker(it).lowercase() }+ }+ )+ val sorted = items.sortedWith(order)+ if (group == Group.NONE) return listOf(Section(null, sorted.map { it.value }))+ val (alerts, rest) = sorted.partition { it.seen.flagged }+ val groups = rest.groupBy { key(it, group) }+ val keys = when (group) {+ Group.CATEGORY -> CATEGORY_ORDER.map { it.label }.filter { it in groups } + listOfNotNull(OTHER.takeIf { it in groups })+ Group.RADIO -> listOf("Bluetooth + WiFi", "WiFi", "Bluetooth", OTHER).filter { it in groups }+ Group.DISTANCE -> BANDS.filter { it in groups }+ // Most common makers first, unknown last.+ else -> groups.keys.sortedWith(compareBy<String> { it == UNKNOWN_MAKER }.thenByDescending { groups[it]!!.size }.thenBy { it.lowercase() })+ }+ return listOfNotNull(alerts.takeIf { it.isNotEmpty() }?.let { Section("Alerts", it.map { a -> a.value }) }) ++ keys.map { k -> Section(k, groups.getValue(k).map { it.value }) }+ }++ private fun <T> key(i: Item<T>, group: Group): String = when (group) {+ Group.CATEGORY -> CATEGORY_ORDER.firstOrNull { it.matches(i.seen) }?.label ?: OTHER+ Group.RADIO -> {+ val ble = Advert.Source.BLE in i.seen.sources+ val wifi = Advert.Source.WIFI in i.seen.sources+ when { ble && wifi -> "Bluetooth + WiFi"; wifi -> "WiFi"; ble -> "Bluetooth"; else -> OTHER }+ }+ Group.MAKER -> maker(i)+ Group.DISTANCE -> ProximityUtil.band(i.rssi)+ Group.NONE -> ""+ }++ private fun maker(i: Item<*>) = i.vendor?.takeIf { it.isNotBlank() }?.let { DeviceIntel.shortVendor(it) } ?: UNKNOWN_MAKER++ /** "Trackers · 3": the header of a group. */+ fun headerLabel(s: Section<*>) = "${s.header} · ${s.items.size}"++ // The ⋮ menu: Sort by and Group by, each a list to pick one from.+ private const val SORT_GROUP = 7100+ private const val GROUP_GROUP = 7200++ fun addMenu(menu: Menu, context: Context, order: Int = Menu.NONE) {+ val sortMenu = menu.addSubMenu(Menu.NONE, SORT_GROUP, order, "Sort by")+ val sort = sort(context)+ Sort.entries.forEach { s -> sortMenu.add(SORT_GROUP, SORT_GROUP + 1 + s.ordinal, s.ordinal, s.label).isChecked = s == sort }+ sortMenu.setGroupCheckable(SORT_GROUP, true, true)+ val groupMenu = menu.addSubMenu(Menu.NONE, GROUP_GROUP, order, "Group by")+ val group = group(context)+ Group.entries.forEach { g -> groupMenu.add(GROUP_GROUP, GROUP_GROUP + 1 + g.ordinal, g.ordinal, g.label).isChecked = g == group }+ groupMenu.setGroupCheckable(GROUP_GROUP, true, true)+ }++ /** Handles a Sort by / Group by pick; true when it was one. */+ fun onMenuItem(item: MenuItem, context: Context): Boolean {+ val sort = Sort.entries.firstOrNull { item.itemId == SORT_GROUP + 1 + it.ordinal }+ val group = Group.entries.firstOrNull { item.itemId == GROUP_GROUP + 1 + it.ordinal }+ when {+ sort != null -> Prefs.setListSort(context, sort.name)+ group != null -> Prefs.setListGroup(context, group.name)+ else -> return false+ }+ item.isChecked = true+ return true+ }+}diff --git a/app/src/main/java/com/rfsentinel/app/ui/SnapshotActivity.kt b/app/src/main/java/com/rfsentinel/app/ui/SnapshotActivity.ktindex e18e59d..b29573f 100644--- a/app/src/main/java/com/rfsentinel/app/ui/SnapshotActivity.kt+++ b/app/src/main/java/com/rfsentinel/app/ui/SnapshotActivity.kt@@ -48,15 +48,21 @@ class SnapshotActivity : AppCompatActivity() {private val timeFmt = DateFormat.getTimeInstance(DateFormat.SHORT)private val adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {- var items: List<TripDeviceEntity> = emptyList()+ /** Devices, and group headers (Group by) as their text. */+ var items: List<Any> = emptyList()override fun getItemCount() = items.size+ override fun getItemViewType(position: Int) = if (items[position] is String) 1 else 0+override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =- object : RecyclerView.ViewHolder(ItemDetectionBinding.inflate(LayoutInflater.from(parent.context), parent, false).root) {}+ if (viewType == 1) object : RecyclerView.ViewHolder(DeviceAdapter.groupHeader(parent)) {}+ else object : RecyclerView.ViewHolder(ItemDetectionBinding.inflate(LayoutInflater.from(parent.context), parent, false).root) {}override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {val d = items[position]+ if (d is String) { (holder.itemView as android.widget.TextView).text = d; return }+ d as TripDeviceEntityval b = ItemDetectionBinding.bind(holder.itemView)val category = Category.parse(d.category)val type = DeviceIntel.savedType(d.deviceType, d.vendor, d.mac)@@ -132,7 +138,17 @@ class SnapshotActivity : AppCompatActivity() {filter.matches(d) && (q.isEmpty() ||listOfNotNull(d.mac, d.label, d.name, d.vendor, d.deviceType, DeviceIntel.savedType(d.deviceType, d.vendor, d.mac), d.evidence).any { it.contains(q, ignoreCase = true) })}- adapter.items = list+ // Sort by / Group by, as on the main screen; by default matches first, then the strongest signal.+ val sections = DeviceOrder.arrange(+ list.map { d ->+ DeviceOrder.Item(d, DeviceFilter.seen(d), DeviceIntel.savedLabel(d.label, d.deviceType, d.vendor, d.mac),+ d.vendor, d.bestRssi, d.firstSeen, d.lastSeen)+ },+ DeviceOrder.sort(this), DeviceOrder.group(this),+ compareBy<DeviceOrder.Item<TripDeviceEntity>> { it.value.category == null }.thenByDescending { it.value.confidence }+ .thenByDescending { it.rssi }+ )+ adapter.items = sections.flatMap { sec -> listOfNotNull(sec.header?.let { DeviceOrder.headerLabel(sec) }) + sec.items }for ((f, chip) in filterChips) {val n = devices.count { f.matches(it) }chip.text = if (f == DeviceFilter.ALL || n > 0) "${f.label} $n" else f.label@@ -149,10 +165,11 @@ class SnapshotActivity : AppCompatActivity() {menu.add(0, 2, 1, "Export...")menu.add(0, 3, 2, "Rename")menu.add(0, 4, 3, "Delete")+ DeviceOrder.addMenu(menu, this)return true}- override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {+ override fun onOptionsItemSelected(item: MenuItem): Boolean = if (DeviceOrder.onMenuItem(item, this)) { show(); true } else when (item.itemId) {1 -> { startActivity(Intent(this, MapActivity::class.java).putExtra(MapActivity.EXTRA_TRIP_ID, tripId)); true }2 -> { Exporter.showTripExport(this, tripId, snapshot = true); true }3 -> { rename(); true }diff --git a/app/src/main/java/com/rfsentinel/app/util/Prefs.kt b/app/src/main/java/com/rfsentinel/app/util/Prefs.ktindex fe39e88..e4e9ae2 100644--- a/app/src/main/java/com/rfsentinel/app/util/Prefs.kt+++ b/app/src/main/java/com/rfsentinel/app/util/Prefs.kt@@ -284,6 +284,12 @@ object Prefs {sp(context).getString("map_filter", null) ?: if (bool(context, "map_show_all", true)) "ALL" else "FLAGGED"fun setMapFilter(context: Context, name: String) = sp(context).edit { putString("map_filter", name) }+ /** The device lists' Sort by / Group by (see ui.DeviceOrder), shared by the main screen and snapshots. */+ fun listSort(context: Context): String? = sp(context).getString("list_sort", null)+ fun setListSort(context: Context, name: String) = sp(context).edit { putString("list_sort", name) }+ fun listGroup(context: Context): String? = sp(context).getString("list_group", null)+ fun setListGroup(context: Context, name: String) = sp(context).edit { putString("list_group", name) }+/** Last destinations picked (newest first, kept on this phone only). */fun recentDestinations(context: Context): List<com.rfsentinel.app.nav.Place> = places(context, "recent_destinations")fun addRecentDestination(context: Context, d: com.rfsentinel.app.nav.Place) {diff --git a/app/src/test/java/com/rfsentinel/app/detect/IdentifyMoreTest.kt b/app/src/test/java/com/rfsentinel/app/detect/IdentifyMoreTest.ktindex f517eab..7deda54 100644--- a/app/src/test/java/com/rfsentinel/app/detect/IdentifyMoreTest.kt+++ b/app/src/test/java/com/rfsentinel/app/detect/IdentifyMoreTest.kt@@ -3,6 +3,7 @@ package com.rfsentinel.app.detectimport com.rfsentinel.app.service.DeviceRegistryimport com.rfsentinel.app.service.DeviceRegistry.GeoSampleimport com.rfsentinel.app.ui.DeviceFilter+import com.rfsentinel.app.ui.DeviceOrderimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrue@@ -242,4 +243,35 @@ class IdentifyMoreTest {assertEquals(emptyList<DeviceFilter>(), kinds("Business access point - Ruckus"))assertEquals(emptyList<DeviceFilter>(), kinds("Business WiFi (account sign-in)"))}++ @Test+ fun listSortAndGroupKeepAlertsOnTop() {+ fun item(id: String, type: String, rssi: Int, vendor: String?, flagged: Boolean = false, src: Advert.Source = Advert.Source.BLE,+ name: String? = null, seen: Long = 0L) =+ DeviceOrder.Item(id, DeviceFilter.Seen("02:00:00:00:00:$id", type, name, emptyList(), setOf(src), flagged),+ name ?: type, vendor, rssi, seen, seen)+ val items = listOf(+ item("01", "Phone", -50, "Samsung Electronics Co.,Ltd", name = "Zed"),+ item("02", "AirTag (Find My)", -85, "Apple, Inc.", flagged = true),+ item("03", "Home router - eero", -70, "eero inc.", src = Advert.Source.WIFI, name = "Alpha", seen = 5L),+ item("04", "Phone", -90, null)+ )+ val none = Comparator<DeviceOrder.Item<String>> { _, _ -> 0 }+ fun ids(sort: DeviceOrder.Sort, group: DeviceOrder.Group) =+ DeviceOrder.arrange(items, sort, group, none).map { (it.header ?: "") to it.items }++ // The flagged tracker stays first; then the chosen order.+ assertEquals(listOf("" to listOf("02", "01", "03", "04")), ids(DeviceOrder.Sort.SIGNAL, DeviceOrder.Group.NONE))+ assertEquals(listOf("" to listOf("02", "03", "01", "04")), ids(DeviceOrder.Sort.NAME, DeviceOrder.Group.NONE))+ assertEquals(listOf("" to listOf("02", "03", "01", "04")), ids(DeviceOrder.Sort.RECENT, DeviceOrder.Group.NONE))+ // Grouped: the alerts in their own group first, then the groups in order.+ assertEquals(listOf("Alerts" to listOf("02"), "Phones" to listOf("01", "04"), "Routers" to listOf("03")),+ ids(DeviceOrder.Sort.SIGNAL, DeviceOrder.Group.CATEGORY))+ assertEquals(listOf("Alerts" to listOf("02"), "WiFi" to listOf("03"), "Bluetooth" to listOf("01", "04")),+ ids(DeviceOrder.Sort.SIGNAL, DeviceOrder.Group.RADIO))+ assertEquals(listOf("Alerts" to listOf("02"), "Near" to listOf("01"), "Nearby" to listOf("03"), "Far" to listOf("04")),+ ids(DeviceOrder.Sort.SIGNAL, DeviceOrder.Group.DISTANCE))+ assertEquals(listOf("Alerts", "eero", "Samsung", "Unknown maker"),+ ids(DeviceOrder.Sort.SIGNAL, DeviceOrder.Group.MAKER).map { it.first }.map { if (it.startsWith("Samsung")) "Samsung" else it })+ }}