- Improved handling of network changes.

This commit is contained in:
Juha Heinanen
2020-02-08 22:41:43 +02:00
parent 86b60fa0f8
commit 57304c32d0
2 changed files with 46 additions and 24 deletions
@@ -51,7 +51,7 @@ class BaresipService: Service() {
internal var audioFocused = false internal var audioFocused = false
internal var origCallVolume = -1 internal var origCallVolume = -1
internal val btAdapter = BluetoothAdapter.getDefaultAdapter() internal val btAdapter = BluetoothAdapter.getDefaultAdapter()
internal var activeNetwork: Network? = null internal var activeNetwork = ""
override fun onCreate() { override fun onCreate() {
@@ -86,29 +86,49 @@ class BaresipService: Service() {
override fun onAvailable(network: Network) { override fun onAvailable(network: Network) {
super.onAvailable(network) super.onAvailable(network)
val linkProperties = cm.getLinkProperties(network) val linkProperties = cm.getLinkProperties(network)
Log.i(LOG_TAG, "Network $network is available: '$linkProperties'") val interfaceName = linkProperties.interfaceName!!
activeNetwork = network if (((Build.VERSION.SDK_INT >= 23) && (network == cm.activeNetwork)) ||
((Build.VERSION.SDK_INT < 23) &&
(cm.activeNetworkInfo.toString() ==
cm.getNetworkInfo(network).toString()))) {
Log.d(LOG_TAG, "Active network $network@$interfaceName " +
" is available: $linkProperties")
activeNetwork = "$network"
if (isServiceRunning) { if (isServiceRunning) {
Utils.updateLinkProperties(linkProperties) Utils.updateLinkProperties(linkProperties)
} else { } else {
dnsServers = linkProperties.dnsServers dnsServers = linkProperties.dnsServers
linkAddresses = linkProperties.linkAddresses linkAddresses = linkProperties.linkAddresses
} }
} else {
Log.d(LOG_TAG, "Non-active network $network@$interfaceName " +
" is available: $linkProperties")
}
} }
override fun onLost(network: Network) { override fun onLost(network: Network) {
super.onLost(network) super.onLost(network)
if (activeNetwork == network) { if (activeNetwork == "$network") {
Log.d(LOG_TAG, "Active network '$network' is lost") Log.d(LOG_TAG, "Currently active network $network is lost")
activeNetwork = null var newNetwork: Network? = null
for (net in cm.allNetworks) for (net in cm.allNetworks)
if (net != network) { if (net != network) {
Log.d(LOG_TAG, "New network $net is available") if (((Build.VERSION.SDK_INT >= 23) && (net == cm.activeNetwork)) ||
activeNetwork = net ((Build.VERSION.SDK_INT < 23) &&
cm.activeNetworkInfo.toString() ==
cm.getNetworkInfo(net).toString())) {
Log.d(LOG_TAG, "New active network $net is available")
newNetwork = net
break break
} }
if (activeNetwork != null) { }
Utils.updateLinkProperties(cm.getLinkProperties(activeNetwork)) if (newNetwork != null) {
val interfaceName = cm.getLinkProperties(newNetwork).interfaceName!!
val linkProperties = cm.getLinkProperties(newNetwork)
Log.d(LOG_TAG, "Updating new active network " +
"$newNetwork@$interfaceName link properties: $linkProperties")
activeNetwork = "$newNetwork"
Utils.updateLinkProperties(cm.getLinkProperties(newNetwork))
} }
} else { } else {
Log.d(LOG_TAG, "Network '$network' is lost") Log.d(LOG_TAG, "Network '$network' is lost")
@@ -117,11 +137,14 @@ class BaresipService: Service() {
override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) { override fun onLinkPropertiesChanged(network: Network, linkProperties: LinkProperties) {
super.onLinkPropertiesChanged(network, linkProperties) super.onLinkPropertiesChanged(network, linkProperties)
if (!isServiceRunning) val interfaceName = cm.getLinkProperties(network).interfaceName!!
return if (((Build.VERSION.SDK_INT >= 23) && (network == cm.activeNetwork)) ||
if (network == activeNetwork) { ((Build.VERSION.SDK_INT < 23) &&
Log.d(LOG_TAG, "Active network $network link properties changed: " + (cm.activeNetworkInfo.toString() ==
"$linkProperties") cm.getNetworkInfo(network).toString()))) {
Log.d(LOG_TAG, "Active network $network@$interfaceName " +
" link properties changed: $linkProperties")
activeNetwork = "$network"
if (isServiceRunning) { if (isServiceRunning) {
Utils.updateLinkProperties(linkProperties) Utils.updateLinkProperties(linkProperties)
} else { } else {
@@ -129,8 +152,8 @@ class BaresipService: Service() {
linkAddresses = linkProperties.linkAddresses linkAddresses = linkProperties.linkAddresses
} }
} else { } else {
Log.e(LOG_TAG, "Network $network link properties changed: " + Log.e(LOG_TAG, "Network $network@$interfaceName " +
"$linkProperties") " link properties changed: $linkProperties")
} }
} }
} }
@@ -281,9 +281,8 @@ object Utils {
} }
if (updated) { if (updated) {
BaresipService.linkAddresses = linkAddresses BaresipService.linkAddresses = linkAddresses
Api.uag_reset_transp(true,true) Api.uag_reset_transp(true, true)
Api.net_debug() Api.net_debug()
} }
} }