Fix IllegalStateException in VpnMonitor

This commit is contained in:
Mygod
2018-03-14 22:04:29 -07:00
parent d0d78520da
commit 049a781031

View File

@@ -51,13 +51,19 @@ object VpnMonitor : ConnectivityManager.NetworkCallback() {
val ifname = available.remove(network) ?: return val ifname = available.remove(network) ?: return
debugLog(TAG, "onLost: $ifname") debugLog(TAG, "onLost: $ifname")
if (currentNetwork != network) return if (currentNetwork != network) return
val next = available.entries.firstOrNull()
currentNetwork = next?.key
callbacks.forEach { it.onLost(ifname) } callbacks.forEach { it.onLost(ifname) }
if (next == null) return while (available.isNotEmpty()) {
debugLog(TAG, "Switching to ${next.value} as VPN interface") val next = available.entries.first()
val properties = manager.getLinkProperties(next.key) currentNetwork = next.key
callbacks.forEach { it.onAvailable(next.value, properties.dnsServers) } 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) { fun registerCallback(callback: Callback, failfast: (() -> Unit)? = null) {