Fix a missing synchronized

This commit is contained in:
Mygod
2019-07-16 09:40:05 +08:00
parent 2adf3221b8
commit 983e80596b

View File

@@ -23,48 +23,52 @@ object DefaultNetworkMonitor : UpstreamMonitor() {
override fun onAvailable(network: Network) { override fun onAvailable(network: Network) {
val properties = app.connectivity.getLinkProperties(network) val properties = app.connectivity.getLinkProperties(network)
val ifname = properties?.interfaceName ?: return val ifname = properties?.interfaceName ?: return
when (currentNetwork) { synchronized(this@DefaultNetworkMonitor) {
null -> { } when (currentNetwork) {
network -> { null -> { }
val oldProperties = currentLinkProperties!! network -> {
check(ifname == oldProperties.interfaceName) val oldProperties = currentLinkProperties!!
if (properties.dnsServers == oldProperties.dnsServers) return check(ifname == oldProperties.interfaceName)
if (properties.dnsServers == oldProperties.dnsServers) return
}
else -> callbacks.forEach { it.onLost() } // we are using the other default network now
} }
else -> callbacks.forEach { it.onLost() } // we are using the other default network now currentNetwork = network
currentLinkProperties = properties
callbacks.forEach { it.onAvailable(ifname, properties.dnsServers) }
} }
currentNetwork = network
currentLinkProperties = properties
callbacks.forEach { it.onAvailable(ifname, properties.dnsServers) }
} }
override fun onLinkPropertiesChanged(network: Network, properties: LinkProperties) { override fun onLinkPropertiesChanged(network: Network, properties: LinkProperties) {
if (currentNetwork == null) { synchronized(this@DefaultNetworkMonitor) {
onAvailable(network) if (currentNetwork == null) {
return onAvailable(network)
} return
if (currentNetwork != network) return
val oldProperties = currentLinkProperties!!
currentLinkProperties = properties
val ifname = properties.interfaceName
when {
ifname == null -> {
Timber.w("interfaceName became null: $oldProperties -> $properties")
onLost(network)
} }
ifname != oldProperties.interfaceName -> { if (currentNetwork != network) return
Timber.w(RuntimeException("interfaceName changed: $oldProperties -> $properties")) val oldProperties = currentLinkProperties!!
callbacks.forEach { currentLinkProperties = properties
it.onLost() val ifname = properties.interfaceName
when {
ifname == null -> {
Timber.w("interfaceName became null: $oldProperties -> $properties")
onLost(network)
}
ifname != oldProperties.interfaceName -> {
Timber.w(RuntimeException("interfaceName changed: $oldProperties -> $properties"))
callbacks.forEach {
it.onLost()
it.onAvailable(ifname, properties.dnsServers)
}
}
properties.dnsServers != oldProperties.dnsServers -> callbacks.forEach {
it.onAvailable(ifname, properties.dnsServers) it.onAvailable(ifname, properties.dnsServers)
} }
} }
properties.dnsServers != oldProperties.dnsServers -> callbacks.forEach {
it.onAvailable(ifname, properties.dnsServers)
}
} }
} }
override fun onLost(network: Network) { override fun onLost(network: Network) = synchronized(this@DefaultNetworkMonitor) {
if (currentNetwork != network) return if (currentNetwork != network) return
callbacks.forEach { it.onLost() } callbacks.forEach { it.onLost() }
currentNetwork = null currentNetwork = null