Handle failure to resolve service gracefully

This commit is contained in:
Mygod
2021-10-25 00:59:13 -04:00
parent c3f5625eec
commit 1dc1b59c32

View File

@@ -104,13 +104,20 @@ enum class TetherType(@DrawableRes val icon: Int) {
*
* Based on: https://android.googlesource.com/platform/frameworks/base/+/5d36f01/packages/Tethering/src/com/android/networkstack/tethering/Tethering.java#479
*/
fun ofInterface(iface: String?, p2pDev: String? = null) = synchronized(this) { ofInterfaceImpl(iface, p2pDev) }
private tailrec fun ofInterfaceImpl(iface: String?, p2pDev: String?): TetherType = when {
iface == null -> NONE
iface == p2pDev -> WIFI_P2P
fun ofInterface(iface: String?, p2pDev: String? = null) = when (iface) {
null -> NONE
p2pDev -> WIFI_P2P
else -> try {
synchronized(this) { ofInterfaceImpl(iface) }
} catch (e: RuntimeException) {
Timber.w(e)
NONE
}
}
private tailrec fun ofInterfaceImpl(iface: String): TetherType = when {
requiresUpdate -> {
if (Build.VERSION.SDK_INT >= 30) updateRegexs() else error("unexpected requiresUpdate")
ofInterfaceImpl(iface, p2pDev)
ofInterfaceImpl(iface)
}
wifiRegexs.any { it.matcher(iface).matches() } -> WIFI
wigigRegexs.any { it.matcher(iface).matches() } -> WIGIG