allNetworks replacement
This commit is contained in:
@@ -63,6 +63,7 @@ class BaresipService: Service() {
|
|||||||
private var btAdapter: BluetoothAdapter? = null
|
private var btAdapter: BluetoothAdapter? = null
|
||||||
private var linkAddresses = mutableMapOf<String, String>()
|
private var linkAddresses = mutableMapOf<String, String>()
|
||||||
private var activeNetwork: Network? = null
|
private var activeNetwork: Network? = null
|
||||||
|
private var allNetworks = mutableSetOf<Network>()
|
||||||
private var hotSpotIsEnabled = false
|
private var hotSpotIsEnabled = false
|
||||||
private var hotSpotAddresses = mapOf<String, String>()
|
private var hotSpotAddresses = mapOf<String, String>()
|
||||||
private var mediaPlayer: MediaPlayer? = null
|
private var mediaPlayer: MediaPlayer? = null
|
||||||
@@ -103,6 +104,13 @@ class BaresipService: Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
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()
|
val builder = NetworkRequest.Builder()
|
||||||
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
|
.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)
|
||||||
cm.registerNetworkCallback(
|
cm.registerNetworkCallback(
|
||||||
@@ -112,6 +120,8 @@ 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 (network !in allNetworks)
|
||||||
|
allNetworks.add(network)
|
||||||
// If API >= 26, this will be followed by onCapabilitiesChanged
|
// If API >= 26, this will be followed by onCapabilitiesChanged
|
||||||
if (isServiceRunning && VERSION.SDK_INT < 26)
|
if (isServiceRunning && VERSION.SDK_INT < 26)
|
||||||
updateNetwork()
|
updateNetwork()
|
||||||
@@ -125,6 +135,8 @@ 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 (network in allNetworks)
|
||||||
|
allNetworks.remove(network)
|
||||||
if (isServiceRunning)
|
if (isServiceRunning)
|
||||||
updateNetwork()
|
updateNetwork()
|
||||||
}
|
}
|
||||||
@@ -1240,9 +1252,9 @@ class BaresipService: Service() {
|
|||||||
if (!isServiceRunning)
|
if (!isServiceRunning)
|
||||||
return
|
return
|
||||||
|
|
||||||
/* for (n in cm.allNetworks)
|
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()
|
||||||
|
|
||||||
@@ -1292,15 +1304,15 @@ class BaresipService: Service() {
|
|||||||
|
|
||||||
private fun linkAddresses(): MutableMap<String, String> {
|
private fun linkAddresses(): MutableMap<String, String> {
|
||||||
val lnAddrs = mutableMapOf<String, String>()
|
val lnAddrs = mutableMapOf<String, String>()
|
||||||
for (n in cm.allNetworks) {
|
for (n in allNetworks) {
|
||||||
val caps = cm.getNetworkCapabilities(n) ?: continue
|
val caps = cm.getNetworkCapabilities(n) ?: continue
|
||||||
if (VERSION.SDK_INT < 28 ||
|
if (VERSION.SDK_INT < 28 ||
|
||||||
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND)) {
|
caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND)) {
|
||||||
val props = cm.getLinkProperties(n) ?: continue
|
val props = cm.getLinkProperties(n) ?: continue
|
||||||
for (la in props.linkAddresses)
|
for (la in props.linkAddresses)
|
||||||
if (la.scope == android.system.OsConstants.RT_SCOPE_UNIVERSE &&
|
if (la.scope == android.system.OsConstants.RT_SCOPE_UNIVERSE &&
|
||||||
props.interfaceName != null)
|
props.interfaceName != null && la.address.hostAddress != null)
|
||||||
lnAddrs[la.address.hostAddress] = props.interfaceName!!
|
lnAddrs[la.address.hostAddress!!] = props.interfaceName!!
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (hotSpotIsEnabled) {
|
if (hotSpotIsEnabled) {
|
||||||
@@ -1317,7 +1329,7 @@ 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 cm.allNetworks)
|
for (n in allNetworks)
|
||||||
if (isNetworkActive(n)) {
|
if (isNetworkActive(n)) {
|
||||||
val linkProps = cm.getLinkProperties(n)
|
val linkProps = cm.getLinkProperties(n)
|
||||||
if (linkProps != null) {
|
if (linkProps != null) {
|
||||||
@@ -1326,7 +1338,7 @@ class BaresipService: Service() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Then add DNS servers from the other networks
|
// Then add DNS servers from the other networks
|
||||||
for (n in cm.allNetworks) {
|
for (n in allNetworks) {
|
||||||
if (isNetworkActive(n)) continue
|
if (isNetworkActive(n)) continue
|
||||||
val linkProps = cm.getLinkProperties(n)
|
val linkProps = cm.getLinkProperties(n)
|
||||||
if (linkProps != null)
|
if (linkProps != null)
|
||||||
@@ -1348,7 +1360,7 @@ class BaresipService: Service() {
|
|||||||
return if (VERSION.SDK_INT >= 23)
|
return if (VERSION.SDK_INT >= 23)
|
||||||
cm.activeNetwork
|
cm.activeNetwork
|
||||||
else {
|
else {
|
||||||
for (n in cm.allNetworks)
|
for (n in allNetworks)
|
||||||
if (isNetworkActive(n)) return n
|
if (isNetworkActive(n)) return n
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user