From e114bb306ab64502cc6dc3c74ed152d0190a904a Mon Sep 17 00:00:00 2001 From: Mygod Date: Tue, 23 Jun 2020 07:53:42 +0800 Subject: [PATCH] Fix race in ofInterface --- mobile/src/main/java/be/mygod/vpnhotspot/net/TetherType.kt | 5 +++-- .../main/java/be/mygod/vpnhotspot/net/TetheringManager.kt | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherType.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherType.kt index 796b8e6d..05b6c1c5 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherType.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetherType.kt @@ -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 */ - 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 == p2pDev -> WIFI_P2P requiresUpdate -> { Timber.d("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 wifiP2pRegexs.any { it.matcher(iface).matches() } -> WIFI_P2P diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt index 197abcbb..299ad7a5 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/TetheringManager.kt @@ -464,8 +464,8 @@ object TetheringManager { * 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. */ - fun onClientsChanged(clients: Iterable<*>) { - Timber.i("onClientsChanged: ${clients.joinToString()}") + fun onClientsChanged(clients: Collection<*>) { + if (clients.isNotEmpty()) Timber.i("onClientsChanged: ${clients.joinToString()}") } /** @@ -543,7 +543,7 @@ object TetheringManager { } "onClientsChanged" -> { if (noArgs != 1) Timber.w("Unexpected args for $name: $args") - callback?.onClientsChanged(args!![0] as Iterable<*>) + callback?.onClientsChanged(args!![0] as Collection<*>) } "onOffloadStatusChanged" -> { if (noArgs != 1) Timber.w("Unexpected args for $name: $args")