Network update improvements

This commit is contained in:
Juha Heinanen
2021-11-07 10:10:55 +02:00
parent aacdeb8783
commit 04ea1fcf3c
@@ -120,12 +120,13 @@ class BaresipService: Service() {
override fun onAvailable(network: Network) { override fun onAvailable(network: Network) {
super.onAvailable(network) super.onAvailable(network)
Log.d(TAG, "Network $network is available") Log.d(TAG, "Network $network is available")
// If API >= 26, this will be followed by onCapabilitiesChanged
if (isServiceRunning && VERSION.SDK_INT < 26) {
if (network !in allNetworks) if (network !in allNetworks)
allNetworks.add(network) allNetworks.add(network)
// If API >= 26, this will be followed by onCapabilitiesChanged
if (isServiceRunning && VERSION.SDK_INT < 26)
updateNetwork() updateNetwork()
} }
}
override fun onLosing(network: Network, maxMsToLive: Int) { override fun onLosing(network: Network, maxMsToLive: Int) {
super.onLosing(network, maxMsToLive) super.onLosing(network, maxMsToLive)
@@ -135,25 +136,32 @@ class BaresipService: Service() {
override fun onLost(network: Network) { override fun onLost(network: Network) {
super.onLost(network) super.onLost(network)
Log.d(TAG, "Network $network is lost") Log.d(TAG, "Network $network is lost")
if (isServiceRunning) {
if (network in allNetworks) if (network in allNetworks)
allNetworks.remove(network) allNetworks.remove(network)
if (isServiceRunning)
updateNetwork() updateNetwork()
} }
}
override fun onCapabilitiesChanged(network: Network, caps: NetworkCapabilities) { override fun onCapabilitiesChanged(network: Network, caps: NetworkCapabilities) {
super.onCapabilitiesChanged(network, caps) super.onCapabilitiesChanged(network, caps)
Log.d(TAG, "Network $network capabilities changed: $caps") Log.d(TAG, "Network $network capabilities changed: $caps")
if (isServiceRunning) if (isServiceRunning) {
if (network !in allNetworks)
allNetworks.add(network)
updateNetwork() updateNetwork()
} }
}
override fun onLinkPropertiesChanged(network: Network, props: LinkProperties) { override fun onLinkPropertiesChanged(network: Network, props: LinkProperties) {
super.onLinkPropertiesChanged(network, props) super.onLinkPropertiesChanged(network, props)
Log.d(TAG, "Network $network link properties changed: $props") Log.d(TAG, "Network $network link properties changed: $props")
if (isServiceRunning) if (isServiceRunning) {
if (network !in allNetworks)
allNetworks.add(network)
updateNetwork() updateNetwork()
} }
}
} }
) )
@@ -1249,12 +1257,9 @@ class BaresipService: Service() {
private fun updateNetwork() { private fun updateNetwork() {
if (!isServiceRunning) /* for (n in allNetworks)
return
for (n in allNetworks)
Log.d(TAG, "NETWORK $n with caps ${cm.getNetworkCapabilities(n)} and props " + 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() updateDnsServers()
@@ -1329,13 +1334,11 @@ class BaresipService: Service() {
return return
val servers = mutableListOf<InetAddress>() val servers = mutableListOf<InetAddress>()
// Use DNS servers first from active network (if available) // Use DNS servers first from active network (if available)
for (n in allNetworks) val activeNetwork = activeNetwork()
if (isNetworkActive(n)) { if (activeNetwork != null) {
val linkProps = cm.getLinkProperties(n) val linkProps = cm.getLinkProperties(activeNetwork)
if (linkProps != null) { if (linkProps != null)
servers.addAll(linkProps.dnsServers) servers.addAll(linkProps.dnsServers)
break
}
} }
// Then add DNS servers from the other networks // Then add DNS servers from the other networks
for (n in allNetworks) { for (n in allNetworks) {