From aacdeb87835953c18c93fd6dc0ad982ba289f31f Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Sat, 6 Nov 2021 20:43:45 +0200 Subject: [PATCH] allNetworks replacement --- .../com/tutpro/baresip/BaresipService.kt | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index f448c815..d7dfd738 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -63,6 +63,7 @@ class BaresipService: Service() { private var btAdapter: BluetoothAdapter? = null private var linkAddresses = mutableMapOf() private var activeNetwork: Network? = null + private var allNetworks = mutableSetOf() private var hotSpotIsEnabled = false private var hotSpotAddresses = mapOf() private var mediaPlayer: MediaPlayer? = null @@ -103,6 +104,13 @@ 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( @@ -112,6 +120,8 @@ 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) updateNetwork() @@ -125,6 +135,8 @@ class BaresipService: Service() { override fun onLost(network: Network) { super.onLost(network) Log.d(TAG, "Network $network is lost") + if (network in allNetworks) + allNetworks.remove(network) if (isServiceRunning) updateNetwork() } @@ -1240,9 +1252,9 @@ class BaresipService: Service() { if (!isServiceRunning) return - /* for (n in cm.allNetworks) + for (n in allNetworks) Log.d(TAG, "NETWORK $n with caps ${cm.getNetworkCapabilities(n)} and props " + - "${cm.getLinkProperties(n)} is active ${isNetworkActive(n)}") */ + "${cm.getLinkProperties(n)} is active ${isNetworkActive(n)}") updateDnsServers() @@ -1292,15 +1304,15 @@ class BaresipService: Service() { private fun linkAddresses(): MutableMap { val lnAddrs = mutableMapOf() - for (n in cm.allNetworks) { + for (n in allNetworks) { val caps = cm.getNetworkCapabilities(n) ?: continue if (VERSION.SDK_INT < 28 || caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND)) { val props = cm.getLinkProperties(n) ?: continue for (la in props.linkAddresses) if (la.scope == android.system.OsConstants.RT_SCOPE_UNIVERSE && - props.interfaceName != null) - lnAddrs[la.address.hostAddress] = props.interfaceName!! + props.interfaceName != null && la.address.hostAddress != null) + lnAddrs[la.address.hostAddress!!] = props.interfaceName!! } } if (hotSpotIsEnabled) { @@ -1317,7 +1329,7 @@ class BaresipService: Service() { return val servers = mutableListOf() // Use DNS servers first from active network (if available) - for (n in cm.allNetworks) + for (n in allNetworks) if (isNetworkActive(n)) { val linkProps = cm.getLinkProperties(n) if (linkProps != null) { @@ -1326,7 +1338,7 @@ class BaresipService: Service() { } } // Then add DNS servers from the other networks - for (n in cm.allNetworks) { + for (n in allNetworks) { if (isNetworkActive(n)) continue val linkProps = cm.getLinkProperties(n) if (linkProps != null) @@ -1348,7 +1360,7 @@ class BaresipService: Service() { return if (VERSION.SDK_INT >= 23) cm.activeNetwork else { - for (n in cm.allNetworks) + for (n in allNetworks) if (isNetworkActive(n)) return n return null }