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.

Whitelist network names by their start, like the watchlist

CIS-C07c7f28eacc02
diff --git a/app/src/main/java/com/rfsentinel/app/data/TrustedNetworks.kt b/app/src/main/java/com/rfsentinel/app/data/TrustedNetworks.kt
index 7626efa..d8ae347 100644
--- a/app/src/main/java/com/rfsentinel/app/data/TrustedNetworks.kt
+++ b/app/src/main/java/com/rfsentinel/app/data/TrustedNetworks.kt
@@ -14,12 +14,21 @@ import com.rfsentinel.app.service.DeviceRegistry
*/
object TrustedNetworks {
- data class Rule(
- val text: String,
- /** The whole SSID must be [text]; else any SSID containing it matches. Case never matters. */
- val exact: Boolean
- ) {
- val label get() = if (exact) "Exact name" else "Name contains"
+ /** How a rule's text is matched against an SSID; case never matters. */
+ enum class Match(val label: String) {
+ STARTS("Name starts with"), CONTAINS("Name contains"), EXACT("Exact name")
+ }
+
+ data class Rule(val text: String, private val match: Match?) {
+ /** Rules saved without one (none were released) match by containing. */
+ val how: Match get() = match ?: Match.CONTAINS
+ val label get() = how.label
+
+ fun matches(name: String): Boolean = when (how) {
+ Match.STARTS -> name.startsWith(text, ignoreCase = true)
+ Match.CONTAINS -> name.contains(text, ignoreCase = true)
+ Match.EXACT -> name.equals(text, ignoreCase = true)
+ }
}
private const val PREFS = "trusted_networks"
@@ -35,7 +44,7 @@ object TrustedNetworks {
fun matches(rules: List<Rule>, ssid: String?): Boolean {
val name = ssid?.trim().orEmpty()
if (name.isEmpty()) return false
- return rules.any { r -> if (r.exact) name.equals(r.text, ignoreCase = true) else name.contains(r.text, ignoreCase = true) }
+ return rules.any { it.matches(name) }
}
fun contains(mac: String): Boolean = mac in trusted
@@ -60,7 +69,7 @@ object TrustedNetworks {
fun save(context: Context, rule: Rule, replacing: Rule? = null) {
val clean = rule.copy(text = rule.text.trim())
if (clean.text.isEmpty()) return
- val others = rules.filterNot { it == replacing || it.text.equals(clean.text, ignoreCase = true) && it.exact == clean.exact }
+ val others = rules.filterNot { it == replacing || it.text.equals(clean.text, ignoreCase = true) && it.how == clean.how }
write(context, others + clean)
}
diff --git a/app/src/main/java/com/rfsentinel/app/ui/DeviceActions.kt b/app/src/main/java/com/rfsentinel/app/ui/DeviceActions.kt
index 86150d3..a4e7260 100644
--- a/app/src/main/java/com/rfsentinel/app/ui/DeviceActions.kt
+++ b/app/src/main/java/com/rfsentinel/app/ui/DeviceActions.kt
@@ -372,15 +372,37 @@ object DeviceActions {
.show()
return
}
- // A network on many vehicles (a bus fleet's routers) has a new address on each: its name catches them all.
- var byName = false
+ // A network on many vehicles (a bus fleet's routers) has a new address on each: the start of its name
+ // catches them all ("LAUSD-Bus-123" -> "LAUSD-"), as on the watchlist.
+ val pad = (20 * activity.resources.displayMetrics.density).toInt()
+ val device = android.widget.RadioButton(activity).apply { id = View.generateViewId(); text = "Only this device ($mac)" }
+ val starts = android.widget.RadioButton(activity).apply { id = View.generateViewId(); text = "Every network whose name (SSID) starts with:" }
+ val startInput = EditText(activity).apply {
+ setText(name); setSelection(name.length); visibility = View.GONE
+ inputType = android.text.InputType.TYPE_CLASS_TEXT or android.text.InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
+ }
+ val group = android.widget.RadioGroup(activity).apply {
+ addView(device); addView(starts); check(device.id)
+ setOnCheckedChangeListener { _, id ->
+ startInput.visibility = if (id == starts.id) View.VISIBLE else View.GONE
+ if (id == starts.id) startInput.requestFocus()
+ }
+ }
+ val box = LinearLayout(activity).apply {
+ orientation = LinearLayout.VERTICAL
+ setPadding(pad, pad / 2, pad, 0)
+ addView(TextView(activity).apply { text = "Trusted, never logged, flashed or alerted on." })
+ addView(group); addView(startInput)
+ }
MaterialAlertDialogBuilder(activity)
- .setTitle("Whitelist: trusted, never logged, flashed or alerted on")
- .setSingleChoiceItems(arrayOf("Only this device ($mac)", "Every network named \"$name\""), 0) { _, which -> byName = which == 1 }
+ .setTitle("Whitelist")
+ .setView(box)
.setPositiveButton("Whitelist") { _, _ ->
- if (!byName) { addDevice(); return@setPositiveButton }
- TrustedNetworks.save(activity, TrustedNetworks.Rule(name, exact = true))
- toast(activity, "Every network named $name is whitelisted")
+ if (group.checkedRadioButtonId != starts.id) { addDevice(); return@setPositiveButton }
+ val start = startInput.text.toString().trim()
+ if (start.isEmpty()) { toast(activity, "Not whitelisted: enter the start of the name"); return@setPositiveButton }
+ TrustedNetworks.save(activity, TrustedNetworks.Rule(start, TrustedNetworks.Match.STARTS))
+ toast(activity, "Networks starting with $start are whitelisted")
}
.setNegativeButton("Cancel", null)
.show()
diff --git a/app/src/main/java/com/rfsentinel/app/whitelist/WhitelistActivity.kt b/app/src/main/java/com/rfsentinel/app/whitelist/WhitelistActivity.kt
index 4a63746..07d2231 100644
--- a/app/src/main/java/com/rfsentinel/app/whitelist/WhitelistActivity.kt
+++ b/app/src/main/java/com/rfsentinel/app/whitelist/WhitelistActivity.kt
@@ -24,8 +24,8 @@ class WhitelistActivity : AppCompatActivity() {
private lateinit var adapter: WhitelistAdapter<WhitelistEntity>
private val networks = mutableListOf<TrustedNetworks.Rule>()
private lateinit var networkAdapter: WhitelistAdapter<TrustedNetworks.Rule>
- /** The Match dropdown: whole name (true) or part of it. */
- private var exact = false
+ /** The Match dropdown's choice. */
+ private var match = TrustedNetworks.Match.STARTS
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -76,10 +76,10 @@ class WhitelistActivity : AppCompatActivity() {
}
private fun setupNetworks(savedInstanceState: Bundle?) {
- val choices = listOf("Name contains", "Exact name")
- binding.networkMatch.setAdapter(ArrayAdapter(this, android.R.layout.simple_list_item_1, choices))
- binding.networkMatch.setOnItemClickListener { _, _, position, _ -> setExact(position == 1) }
- setExact(savedInstanceState?.getBoolean(STATE_EXACT) ?: false)
+ val choices = TrustedNetworks.Match.entries
+ binding.networkMatch.setAdapter(ArrayAdapter(this, android.R.layout.simple_list_item_1, choices.map { it.label }))
+ binding.networkMatch.setOnItemClickListener { _, _, position, _ -> setMatch(choices[position]) }
+ setMatch(savedInstanceState?.getInt(STATE_MATCH)?.let { choices.getOrNull(it) } ?: TrustedNetworks.Match.STARTS)
networkAdapter = WhitelistAdapter(networks, { it.text }, { it.label }, { editNetwork(it) }) { rule ->
TrustedNetworks.remove(this, rule)
@@ -98,16 +98,21 @@ class WhitelistActivity : AppCompatActivity() {
binding.networkInput.error = "Type a network name or part of one"
return@setOnClickListener
}
- TrustedNetworks.save(this, TrustedNetworks.Rule(text, exact))
+ TrustedNetworks.save(this, TrustedNetworks.Rule(text, match))
binding.networkInput.text?.clear()
showNetworks()
}
showNetworks()
}
- private fun setExact(on: Boolean) {
- exact = on
- binding.networkMatch.setText(if (on) "Exact name" else "Name contains", false) // false: keep every choice in the list
+ private fun setMatch(m: TrustedNetworks.Match) {
+ match = m
+ binding.networkMatch.setText(m.label, false) // false: keep every choice in the list
+ binding.networkInput.hint = when (m) {
+ TrustedNetworks.Match.STARTS -> "Start of the network name, e.g. LAUSD-"
+ TrustedNetworks.Match.CONTAINS -> "Part of the network name, e.g. LAUSD"
+ TrustedNetworks.Match.EXACT -> "The whole network name"
+ }
}
private fun showNetworks() {
@@ -120,9 +125,10 @@ class WhitelistActivity : AppCompatActivity() {
private fun editNetwork(rule: TrustedNetworks.Rule) {
val pad = (16 * resources.displayMetrics.density).toInt()
val input = EditText(this).apply { setText(rule.text); setSelection(rule.text.length); hint = "Network name or keyword" }
- val contains = RadioButton(this).apply { id = android.view.View.generateViewId(); text = "Name contains" }
- val whole = RadioButton(this).apply { id = android.view.View.generateViewId(); text = "Exact name" }
- val group = RadioGroup(this).apply { addView(contains); addView(whole); check(if (rule.exact) whole.id else contains.id) }
+ val buttons = TrustedNetworks.Match.entries.associateWith { m ->
+ RadioButton(this).apply { id = android.view.View.generateViewId(); text = m.label }
+ }
+ val group = RadioGroup(this).apply { buttons.values.forEach { addView(it) }; check(buttons.getValue(rule.how).id) }
val box = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
setPadding(pad, pad / 2, pad, 0)
@@ -133,7 +139,8 @@ class WhitelistActivity : AppCompatActivity() {
.setView(box)
.setPositiveButton("Save") { _, _ ->
val text = input.text.toString().trim()
- if (text.isNotEmpty()) TrustedNetworks.save(this, TrustedNetworks.Rule(text, group.checkedRadioButtonId == whole.id), replacing = rule)
+ val how = buttons.entries.firstOrNull { it.value.id == group.checkedRadioButtonId }?.key ?: rule.how
+ if (text.isNotEmpty()) TrustedNetworks.save(this, TrustedNetworks.Rule(text, how), replacing = rule)
showNetworks()
}
.setNegativeButton("Cancel", null)
@@ -142,10 +149,10 @@ class WhitelistActivity : AppCompatActivity() {
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
- outState.putBoolean(STATE_EXACT, exact)
+ outState.putInt(STATE_MATCH, match.ordinal)
}
private companion object {
- const val STATE_EXACT = "networkExact"
+ const val STATE_MATCH = "networkMatch"
}
}
diff --git a/app/src/main/res/layout/activity_whitelist.xml b/app/src/main/res/layout/activity_whitelist.xml
index 5d32185..ea9ff87 100644
--- a/app/src/main/res/layout/activity_whitelist.xml
+++ b/app/src/main/res/layout/activity_whitelist.xml
@@ -60,7 +60,7 @@
android:layout_height="wrap_content"
android:paddingTop="2dp"
android:paddingBottom="8dp"
- android:text="WiFi networks with a matching name are trusted like a whitelisted device. Name contains also matches part of a name: LAUSD matches LAUSD-Bus-123 and WiFi-LAUSD."
+ android:text="WiFi networks with a matching name are trusted like a whitelisted device. Name starts with LAUSD- matches LAUSD-Bus-123; Name contains LAUSD also matches WiFi-LAUSD."
android:textColor="?android:attr/textColorSecondary"
android:textSize="@dimen/text_caption" />
@@ -85,7 +85,7 @@
android:layout_height="wrap_content"
android:inputType="text|textNoSuggestions"
android:importantForAutofill="no"
- android:hint="Network name or keyword, e.g. LAUSD" />
+ android:hint="Start of the network name, e.g. LAUSD-" />
<Button
android:id="@+id/addNetworkButton"
diff --git a/app/src/test/java/com/rfsentinel/app/data/TrustedNetworksTest.kt b/app/src/test/java/com/rfsentinel/app/data/TrustedNetworksTest.kt
index 98198b8..3ec419c 100644
--- a/app/src/test/java/com/rfsentinel/app/data/TrustedNetworksTest.kt
+++ b/app/src/test/java/com/rfsentinel/app/data/TrustedNetworksTest.kt
@@ -1,5 +1,6 @@
package com.rfsentinel.app.data
+import com.rfsentinel.app.data.TrustedNetworks.Match
import com.rfsentinel.app.data.TrustedNetworks.Rule
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
@@ -8,7 +9,7 @@ import org.junit.Test
class TrustedNetworksTest {
// Made-up network names.
- private val rules = listOf(Rule("FLEET", exact = false), Rule("Depot WiFi", exact = true))
+ private val rules = listOf(Rule("FLEET", Match.CONTAINS), Rule("Depot WiFi", Match.EXACT), Rule("Metro-", Match.STARTS))
@Test
fun keywordMatchesAnyPartOfTheNameWhateverTheCase() {
@@ -17,6 +18,19 @@ class TrustedNetworksTest {
assertTrue(TrustedNetworks.matches(rules, "myFleetNet"))
}
+ @Test
+ fun startMatchesOnlyTheBeginningOfTheName() {
+ assertTrue(TrustedNetworks.matches(rules, "Metro-Bus-42"))
+ assertTrue(TrustedNetworks.matches(rules, "metro-rail"))
+ assertFalse(TrustedNetworks.matches(rules, "MyMetro-Bus"))
+ assertFalse(TrustedNetworks.matches(rules, "Metro Bus"))
+ }
+
+ @Test
+ fun aRuleSavedWithoutAMatchTypeMatchesByContaining() {
+ assertTrue(TrustedNetworks.matches(listOf(Rule("FLEET", null)), "Bus-fleet-3"))
+ }
+
@Test
fun exactNameMatchesOnlyTheWholeName() {
assertTrue(TrustedNetworks.matches(rules, "depot wifi"))