Refine TetherType matcher

This commit is contained in:
Mygod
2019-01-22 23:46:51 +08:00
parent 9277cd1e8b
commit 116902493c

View File

@@ -21,10 +21,10 @@ enum class TetherType {
} }
companion object { companion object {
private val usbRegexes: List<Pattern> private val usbRegexs: List<Pattern>
private val wifiRegexes: List<Pattern> private val wifiRegexs: List<Pattern>
private val wimaxRegexes: List<Pattern> private val wimaxRegexs: List<Pattern>
private val bluetoothRegexes: List<Pattern> private val bluetoothRegexs: List<Pattern>
/** /**
* Source: https://android.googlesource.com/platform/frameworks/base/+/61fa313/core/res/res/values/config.xml#328 * Source: https://android.googlesource.com/platform/frameworks/base/+/61fa313/core/res/res/values/config.xml#328
@@ -32,31 +32,34 @@ enum class TetherType {
init { init {
val appRes = app.resources val appRes = app.resources
val sysRes = Resources.getSystem() val sysRes = Resources.getSystem()
usbRegexes = appRes.getStringArray(sysRes usbRegexs = appRes.getStringArray(sysRes
.getIdentifier("config_tether_usb_regexs", "array", "android")) .getIdentifier("config_tether_usb_regexs", "array", "android"))
.filterNotNull() .filterNotNull()
.map { it.toPattern() } .map { it.toPattern() }
wifiRegexes = appRes.getStringArray(sysRes wifiRegexs = appRes.getStringArray(sysRes
.getIdentifier("config_tether_wifi_regexs", "array", "android")) .getIdentifier("config_tether_wifi_regexs", "array", "android"))
.filterNotNull() .filterNotNull()
.map { it.toPattern() } .map { it.toPattern() }
wimaxRegexes = appRes.getStringArray(sysRes wimaxRegexs = appRes.getStringArray(sysRes
.getIdentifier("config_tether_wimax_regexs", "array", "android")) .getIdentifier("config_tether_wimax_regexs", "array", "android"))
.filterNotNull() .filterNotNull()
.map { it.toPattern() } .map { it.toPattern() }
bluetoothRegexes = appRes.getStringArray(sysRes bluetoothRegexs = appRes.getStringArray(sysRes
.getIdentifier("config_tether_bluetooth_regexs", "array", "android")) .getIdentifier("config_tether_bluetooth_regexs", "array", "android"))
.filterNotNull() .filterNotNull()
.map { it.toPattern() } .map { it.toPattern() }
} }
/**
* Based on: https://android.googlesource.com/platform/frameworks/base/+/0e3d092/services/core/java/com/android/server/connectivity/Tethering.java#311
*/
fun ofInterface(iface: String?, p2pDev: String? = null) = when { fun ofInterface(iface: String?, p2pDev: String? = null) = when {
iface == null -> NONE iface == null -> NONE
iface == p2pDev -> WIFI_P2P iface == p2pDev -> WIFI_P2P
usbRegexes.any { it.matcher(iface).matches() } -> USB wifiRegexs.any { it.matcher(iface).matches() } -> WIFI
wifiRegexes.any { it.matcher(iface).matches() } -> WIFI usbRegexs.any { it.matcher(iface).matches() } -> USB
wimaxRegexes.any { it.matcher(iface).matches() } -> WIMAX bluetoothRegexs.any { it.matcher(iface).matches() } -> BLUETOOTH
bluetoothRegexes.any { it.matcher(iface).matches() } -> BLUETOOTH wimaxRegexs.any { it.matcher(iface).matches() } -> WIMAX
else -> NONE else -> NONE
} }
} }