From b79d10770cfd9bfb6697ff9fd8557cd31f5b0466 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Tue, 9 Nov 2021 09:32:09 +0200 Subject: [PATCH] Improved detecting networks at baresip start --- .../com/tutpro/baresip/BaresipService.kt | 40 ++++++++----------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 4e0c889c..368f70e3 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -104,13 +104,6 @@ class BaresipService: Service() { } cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - if (VERSION.SDK_INT < 31) - @Suppress("DEPRECATION") - allNetworks = cm.allNetworks.toMutableSet() - else - if (cm.activeNetwork != null) - allNetworks.add(cm.activeNetwork!!) - val builder = NetworkRequest.Builder() .removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) cm.registerNetworkCallback( @@ -120,12 +113,11 @@ class BaresipService: Service() { override fun onAvailable(network: Network) { super.onAvailable(network) Log.d(TAG, "Network $network is available") + if (network !in allNetworks) + allNetworks.add(network) // If API >= 26, this will be followed by onCapabilitiesChanged - if (isServiceRunning && VERSION.SDK_INT < 26) { - if (network !in allNetworks) - allNetworks.add(network) + if (isServiceRunning && VERSION.SDK_INT < 26) updateNetwork() - } } override fun onLosing(network: Network, maxMsToLive: Int) { @@ -136,31 +128,28 @@ class BaresipService: Service() { override fun onLost(network: Network) { super.onLost(network) Log.d(TAG, "Network $network is lost") - if (isServiceRunning) { - if (network in allNetworks) - allNetworks.remove(network) + if (network in allNetworks) + allNetworks.remove(network) + if (isServiceRunning) updateNetwork() - } } override fun onCapabilitiesChanged(network: Network, caps: NetworkCapabilities) { super.onCapabilitiesChanged(network, caps) Log.d(TAG, "Network $network capabilities changed: $caps") - if (isServiceRunning) { - if (network !in allNetworks) - allNetworks.add(network) + if (network !in allNetworks) + allNetworks.add(network) + if (isServiceRunning) updateNetwork() - } } override fun onLinkPropertiesChanged(network: Network, props: LinkProperties) { super.onLinkPropertiesChanged(network, props) Log.d(TAG, "Network $network link properties changed: $props") - if (isServiceRunning) { - if (network !in allNetworks) - allNetworks.add(network) + if (network !in allNetworks) + allNetworks.add(network) + if (isServiceRunning) updateNetwork() - } } } @@ -325,6 +314,11 @@ class BaresipService: Service() { "Start" -> { + if (VERSION.SDK_INT < 31) { + @Suppress("DEPRECATION") + allNetworks = cm.allNetworks.toMutableSet() + } + updateDnsServers() val assets = arrayOf("accounts", "config", "contacts")