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