From 7226e5cfe07150f514108deffc3c995316f9d20f Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Sun, 15 Nov 2020 18:28:01 +0200 Subject: [PATCH] Added isNetworkActive utility function --- .../com/tutpro/baresip/BaresipService.kt | 129 +++++++++--------- 1 file changed, 62 insertions(+), 67 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index b7b3af74..f5ddd5d0 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -1029,77 +1029,72 @@ class BaresipService: Service() { } private fun updateNetwork() { - if (Build.VERSION.SDK_INT >= 23) { - for (n in cm.allNetworks) { - val caps = cm.getNetworkCapabilities(n) ?: continue - val props = cm.getLinkProperties(n) ?: continue - Log.i(LOG_TAG, "Network $n ${n == cm.activeNetwork}" + - " is available with caps: $caps, props: $props") - } - // Use VPN network if available - for (n in cm.allNetworks) { - val caps = cm.getNetworkCapabilities(n) ?: continue - val props = cm.getLinkProperties(n) ?: continue - if (n == cm.activeNetwork) - Log.i("Baresip", "Network $n is active") + // Use VPN network if available + for (n in cm.allNetworks) { + val caps = cm.getNetworkCapabilities(n) ?: continue + val props = cm.getLinkProperties(n) ?: continue + if (isNetworkActive(n) && caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) { + Log.i(LOG_TAG, "Active VPN network $n is available with caps: " + + "$caps, props: $props") + activeNetwork = "$n" + if (!isConfigInitialized) + Log.i("Baresip", "Config is NOT initialized") else - Log.i("Baresip", "Network $n is NOT active") - if (caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN) && - (n == cm.activeNetwork)) { - Log.i(LOG_TAG, "Active VPN network $n is available with caps: " + - "$caps, props: $props") - activeNetwork = "$n" - if (!isConfigInitialized) - Log.i("Baresip", "Config is NOT initialized") - else - Log.i("Baresip", "Config is initialized") - if (isConfigInitialized) { - Utils.updateLinkProperties(props) - } else { - for (s in props.dnsServers) - Log.i(LOG_TAG, "DNS Server ${s.hostAddress}") - dnsServers = props.dnsServers - linkAddresses = props.linkAddresses - } - return - } - } - // Otherwise, use active network with Internet access - for (n in cm.allNetworks) { - val caps = cm.getNetworkCapabilities(n) ?: continue - val props = cm.getLinkProperties(n) ?: continue - if ((n == cm.activeNetwork) && - caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { - Log.i(LOG_TAG, "Active Internet network $n is available with caps: " + - "$caps, props: $props") - activeNetwork = "$n" - if (isServiceRunning) { - Utils.updateLinkProperties(props) - } else { - dnsServers = props.dnsServers - linkAddresses = props.linkAddresses - } - return - } - } - // Otherwise, use an active network - for (n in cm.allNetworks) { - val caps = cm.getNetworkCapabilities(n) ?: continue - val props = cm.getLinkProperties(n) ?: continue - if (n == cm.activeNetwork) { - Log.i(LOG_TAG, "Active network $n is available with caps: " + - "$caps, props: $props") - activeNetwork = "$n" - if (isServiceRunning) { - Utils.updateLinkProperties(props) - } else { - dnsServers = props.dnsServers - linkAddresses = props.linkAddresses - } - return + Log.i("Baresip", "Config is initialized") + if (isConfigInitialized) { + Utils.updateLinkProperties(props) + } else { + for (s in props.dnsServers) + Log.i(LOG_TAG, "DNS Server ${s.hostAddress}") + dnsServers = props.dnsServers + linkAddresses = props.linkAddresses } + return } } + // Otherwise, use active network with Internet access + for (n in cm.allNetworks) { + val caps = cm.getNetworkCapabilities(n) ?: continue + val props = cm.getLinkProperties(n) ?: continue + if (isNetworkActive(n) && caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { + Log.i(LOG_TAG, "Active Internet network $n is available with caps: " + + "$caps, props: $props") + activeNetwork = "$n" + if (isServiceRunning) { + Utils.updateLinkProperties(props) + } else { + dnsServers = props.dnsServers + linkAddresses = props.linkAddresses + } + return + } + } + // Otherwise, use an active network + for (n in cm.allNetworks) { + val caps = cm.getNetworkCapabilities(n) ?: continue + val props = cm.getLinkProperties(n) ?: continue + if (isNetworkActive(n)) { + Log.i(LOG_TAG, "Active network $n is available with caps: " + + "$caps, props: $props") + activeNetwork = "$n" + if (isServiceRunning) { + Utils.updateLinkProperties(props) + } else { + dnsServers = props.dnsServers + linkAddresses = props.linkAddresses + } + return + } + } + } + + private fun isNetworkActive(network: Network): Boolean { + if (Build.VERSION.SDK_INT >= 23) + return network == cm.activeNetwork + if (Build.VERSION.SDK_INT < 23) + if ((cm.activeNetworkInfo != null) && (cm.getNetworkInfo(network) != null)) + return cm.activeNetworkInfo!!.toString() == cm.getNetworkInfo(network)!!.toString() + return false } private fun cleanService() {