From 049a781031dda11ce0e9aebc2ad723b3010ec5ef Mon Sep 17 00:00:00 2001 From: Mygod Date: Wed, 14 Mar 2018 22:04:29 -0700 Subject: [PATCH] Fix IllegalStateException in VpnMonitor --- .../java/be/mygod/vpnhotspot/net/VpnMonitor.kt | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/mobile/src/main/java/be/mygod/vpnhotspot/net/VpnMonitor.kt b/mobile/src/main/java/be/mygod/vpnhotspot/net/VpnMonitor.kt index ff599d1e..8e9a8724 100644 --- a/mobile/src/main/java/be/mygod/vpnhotspot/net/VpnMonitor.kt +++ b/mobile/src/main/java/be/mygod/vpnhotspot/net/VpnMonitor.kt @@ -51,13 +51,19 @@ object VpnMonitor : ConnectivityManager.NetworkCallback() { val ifname = available.remove(network) ?: return debugLog(TAG, "onLost: $ifname") if (currentNetwork != network) return - val next = available.entries.firstOrNull() - currentNetwork = next?.key callbacks.forEach { it.onLost(ifname) } - if (next == null) return - debugLog(TAG, "Switching to ${next.value} as VPN interface") - val properties = manager.getLinkProperties(next.key) - callbacks.forEach { it.onAvailable(next.value, properties.dnsServers) } + while (available.isNotEmpty()) { + val next = available.entries.first() + currentNetwork = next.key + val properties = manager.getLinkProperties(next.key) + if (properties != null) { + debugLog(TAG, "Switching to ${next.value} as VPN interface") + callbacks.forEach { it.onAvailable(next.value, properties.dnsServers) } + return + } + available.remove(next.key) + } + currentNetwork = null } fun registerCallback(callback: Callback, failfast: (() -> Unit)? = null) {