Fix race in ofInterface

This commit is contained in:
Mygod
2020-06-23 07:53:42 +08:00
parent d7f01a22f3
commit e114bb306a
2 changed files with 6 additions and 5 deletions

View File

@@ -90,13 +90,14 @@ 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 * Based on: https://android.googlesource.com/platform/frameworks/base/+/5d36f01/packages/Tethering/src/com/android/networkstack/tethering/Tethering.java#479
*/ */
tailrec fun ofInterface(iface: String?, p2pDev: String? = null): TetherType = when { 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 == null -> NONE
iface == p2pDev -> WIFI_P2P iface == p2pDev -> WIFI_P2P
requiresUpdate -> { requiresUpdate -> {
Timber.d("requiresUpdate") Timber.d("requiresUpdate")
if (Build.VERSION.SDK_INT >= 30) updateRegexs() else error("unexpected requiresUpdate") if (Build.VERSION.SDK_INT >= 30) updateRegexs() else error("unexpected requiresUpdate")
ofInterface(iface, p2pDev) ofInterfaceImpl(iface, p2pDev)
} }
wifiRegexs.any { it.matcher(iface).matches() } -> WIFI wifiRegexs.any { it.matcher(iface).matches() } -> WIFI
wifiP2pRegexs.any { it.matcher(iface).matches() } -> WIFI_P2P wifiP2pRegexs.any { it.matcher(iface).matches() } -> WIFI_P2P

View File

@@ -464,8 +464,8 @@ object TetheringManager {
* Only called if having permission one of NETWORK_SETTINGS, MAINLINE_NETWORK_STACK, NETWORK_STACK. * Only called if having permission one of NETWORK_SETTINGS, MAINLINE_NETWORK_STACK, NETWORK_STACK.
* @param clients The new set of tethered clients; the collection is not ordered. * @param clients The new set of tethered clients; the collection is not ordered.
*/ */
fun onClientsChanged(clients: Iterable<*>) { fun onClientsChanged(clients: Collection<*>) {
Timber.i("onClientsChanged: ${clients.joinToString()}") if (clients.isNotEmpty()) Timber.i("onClientsChanged: ${clients.joinToString()}")
} }
/** /**
@@ -543,7 +543,7 @@ object TetheringManager {
} }
"onClientsChanged" -> { "onClientsChanged" -> {
if (noArgs != 1) Timber.w("Unexpected args for $name: $args") if (noArgs != 1) Timber.w("Unexpected args for $name: $args")
callback?.onClientsChanged(args!![0] as Iterable<*>) callback?.onClientsChanged(args!![0] as Collection<*>)
} }
"onOffloadStatusChanged" -> { "onOffloadStatusChanged" -> {
if (noArgs != 1) Timber.w("Unexpected args for $name: $args") if (noArgs != 1) Timber.w("Unexpected args for $name: $args")