Improved active network selection
This commit is contained in:
@ -33,7 +33,6 @@ import java.util.*
|
||||
import kotlin.concurrent.schedule
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
|
||||
class BaresipService: Service() {
|
||||
|
||||
internal lateinit var intent: Intent
|
||||
@ -56,7 +55,7 @@ class BaresipService: Service() {
|
||||
private var audioFocusUsage = -1
|
||||
private var origVolume = -1
|
||||
private val btAdapter = BluetoothAdapter.getDefaultAdapter()
|
||||
internal var activeNetwork = ""
|
||||
internal var activeNetwork: Network? = null
|
||||
private var linkAddresses = listOf<LinkAddress>()
|
||||
|
||||
@SuppressLint("WakelockTimeout")
|
||||
@ -110,14 +109,14 @@ class BaresipService: Service() {
|
||||
override fun onLost(network: Network) {
|
||||
super.onLost(network)
|
||||
Log.i(TAG, "Network $network is lost")
|
||||
if (activeNetwork == "$network")
|
||||
if (activeNetwork == network)
|
||||
updateNetwork()
|
||||
}
|
||||
|
||||
override fun onLinkPropertiesChanged(network: Network, props: LinkProperties) {
|
||||
super.onLinkPropertiesChanged(network, props)
|
||||
Log.i(TAG, "Network $network link properties changed")
|
||||
if (activeNetwork == "$network")
|
||||
if (activeNetwork == network)
|
||||
updateNetwork()
|
||||
}
|
||||
|
||||
@ -1105,57 +1104,40 @@ class BaresipService: Service() {
|
||||
}
|
||||
|
||||
private fun updateNetwork() {
|
||||
|
||||
var selectedNetwork: Network? = null
|
||||
var caps: NetworkCapabilities? = null
|
||||
var props: LinkProperties? = null
|
||||
|
||||
// Use VPN network if available
|
||||
for (n in cm.allNetworks) {
|
||||
val caps = cm.getNetworkCapabilities(n) ?: continue
|
||||
val props = cm.getLinkProperties(n) ?: continue
|
||||
caps = cm.getNetworkCapabilities(n) ?: continue
|
||||
if (isNetworkActive(n) && caps.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
|
||||
Log.i(TAG, "Active VPN network $n is available with caps: " +
|
||||
"$caps, props: $props")
|
||||
activeNetwork = "$n"
|
||||
if (isConfigInitialized) {
|
||||
updateLinkProperties(caps, props)
|
||||
} else {
|
||||
for (s in props.dnsServers)
|
||||
Log.i(TAG, "DNS Server ${s.hostAddress}")
|
||||
dnsServers = props.dnsServers
|
||||
linkAddresses = props.linkAddresses
|
||||
}
|
||||
return
|
||||
props = cm.getLinkProperties(n) ?: continue
|
||||
Log.i(TAG, "Active VPN network $n is available with caps: $caps, props: $props")
|
||||
selectedNetwork = n
|
||||
break
|
||||
}
|
||||
}
|
||||
// Otherwise, use active network with Internet access
|
||||
|
||||
// Otherwise, use currently active default data network
|
||||
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(TAG, "Active Internet network $n is available with caps: " +
|
||||
"$caps, props: $props")
|
||||
activeNetwork = "$n"
|
||||
if (isServiceRunning) {
|
||||
updateLinkProperties(caps, 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(TAG, "Active network $n is available with caps: " +
|
||||
"$caps, props: $props")
|
||||
activeNetwork = "$n"
|
||||
if (isServiceRunning) {
|
||||
updateLinkProperties(caps, props)
|
||||
} else {
|
||||
dnsServers = props.dnsServers
|
||||
linkAddresses = props.linkAddresses
|
||||
}
|
||||
return
|
||||
caps = cm.getNetworkCapabilities(n) ?: continue
|
||||
props = cm.getLinkProperties(n) ?: continue
|
||||
Log.i(TAG, "Active network $n is available with caps: $caps, props: $props")
|
||||
selectedNetwork = n
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (selectedNetwork != null) {
|
||||
activeNetwork = selectedNetwork
|
||||
if (isServiceRunning) {
|
||||
updateLinkProperties(caps!!, props!!)
|
||||
} else {
|
||||
dnsServers = props!!.dnsServers
|
||||
linkAddresses = props.linkAddresses
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user