Acquire WiFi lock while baresip is using WiFi network
This commit is contained in:
@ -44,10 +44,11 @@ class BaresipService: Service() {
|
||||
private lateinit var snb: NotificationCompat.Builder
|
||||
private lateinit var cm: ConnectivityManager
|
||||
private lateinit var pm: PowerManager
|
||||
private lateinit var wm: WifiManager
|
||||
private lateinit var tm: TelephonyManager
|
||||
private lateinit var partialWakeLock: PowerManager.WakeLock
|
||||
private lateinit var proximityWakeLock: PowerManager.WakeLock
|
||||
private lateinit var fl: WifiManager.WifiLock
|
||||
private lateinit var wifiLock: WifiManager.WifiLock
|
||||
private lateinit var br: BroadcastReceiver
|
||||
|
||||
internal var rtTimer: Timer? = null
|
||||
@ -116,6 +117,8 @@ class BaresipService: Service() {
|
||||
|
||||
pm = getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||
|
||||
wm = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
||||
|
||||
tm = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
|
||||
|
||||
// This is needed to keep service running also in Doze Mode
|
||||
@ -131,6 +134,9 @@ class BaresipService: Service() {
|
||||
proximityWakeLock = pm.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK,
|
||||
"com.tutpro.baresip:proximity_wakelog")
|
||||
|
||||
wifiLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "Baresip")
|
||||
wifiLock.setReferenceCounted(false)
|
||||
|
||||
br = object : BroadcastReceiver() {
|
||||
override fun onReceive(ctx: Context, intent: Intent) {
|
||||
when (intent.action) {
|
||||
@ -227,9 +233,6 @@ class BaresipService: Service() {
|
||||
when (action) {
|
||||
|
||||
"Start" -> {
|
||||
val wm = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
|
||||
fl = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "Baresip")
|
||||
|
||||
val assets = arrayOf("accounts", "config", "contacts", "busy.wav", "callwaiting.wav",
|
||||
"error.wav", "notfound.wav", "ring.wav", "ringback.wav")
|
||||
var file = File(filesPath)
|
||||
@ -1105,7 +1108,7 @@ class BaresipService: Service() {
|
||||
"$caps, props: $props")
|
||||
activeNetwork = "$n"
|
||||
if (isConfigInitialized) {
|
||||
Utils.updateLinkProperties(props)
|
||||
updateLinkProperties(caps, props)
|
||||
} else {
|
||||
for (s in props.dnsServers)
|
||||
Log.i(TAG, "DNS Server ${s.hostAddress}")
|
||||
@ -1124,7 +1127,7 @@ class BaresipService: Service() {
|
||||
"$caps, props: $props")
|
||||
activeNetwork = "$n"
|
||||
if (isServiceRunning) {
|
||||
Utils.updateLinkProperties(props)
|
||||
updateLinkProperties(caps, props)
|
||||
} else {
|
||||
dnsServers = props.dnsServers
|
||||
linkAddresses = props.linkAddresses
|
||||
@ -1141,7 +1144,7 @@ class BaresipService: Service() {
|
||||
"$caps, props: $props")
|
||||
activeNetwork = "$n"
|
||||
if (isServiceRunning) {
|
||||
Utils.updateLinkProperties(props)
|
||||
updateLinkProperties(caps, props)
|
||||
} else {
|
||||
dnsServers = props.dnsServers
|
||||
linkAddresses = props.linkAddresses
|
||||
@ -1151,6 +1154,59 @@ class BaresipService: Service() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateLinkProperties(caps: NetworkCapabilities, props: LinkProperties) {
|
||||
if (dynDns && (dnsServers != props.dnsServers)) {
|
||||
if (isServiceRunning)
|
||||
if (Config.updateDnsServers(props.dnsServers) != 0)
|
||||
Log.w(TAG, "Failed to update DNS servers '${props.dnsServers}'")
|
||||
else
|
||||
dnsServers = props.dnsServers
|
||||
else
|
||||
dnsServers = props.dnsServers
|
||||
}
|
||||
updateLinkAddresses(props.linkAddresses)
|
||||
if (caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
|
||||
Log.d(TAG, "Acquiring WiFi Lock")
|
||||
wifiLock.acquire()
|
||||
} else {
|
||||
Log.d(TAG, "Releasing WiFi Lock")
|
||||
wifiLock.release()
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateLinkAddresses(linkAddresses: List<LinkAddress>) {
|
||||
var updated = false
|
||||
val ipV6Addr = Utils.findIpV6Address(linkAddresses)
|
||||
if (ipV6Addr != Utils.findIpV6Address(BaresipService.linkAddresses)) {
|
||||
Log.d(TAG, "Updating IPv6 address to '$ipV6Addr'")
|
||||
if (ipV6Addr != "") {
|
||||
if (Api.net_set_address(ipV6Addr) != 0)
|
||||
Log.w(TAG, "Failed to update net address '$ipV6Addr")
|
||||
} else {
|
||||
Api.net_unset_address(Api.AF_INET6)
|
||||
}
|
||||
updated = true
|
||||
}
|
||||
val ipV4Addr = Utils.findIpV4Address(linkAddresses)
|
||||
if (ipV4Addr != Utils.findIpV4Address(BaresipService.linkAddresses)) {
|
||||
Log.d(TAG, "Updating IPv4 address to '$ipV4Addr'")
|
||||
if (ipV4Addr != "") {
|
||||
if (Api.net_set_address(ipV4Addr) != 0)
|
||||
Log.w(TAG, "Failed to update net address '$ipV4Addr'")
|
||||
} else {
|
||||
Api.net_unset_address(Api.AF_INET)
|
||||
}
|
||||
updated = true
|
||||
}
|
||||
if (updated) {
|
||||
BaresipService.linkAddresses = linkAddresses
|
||||
Api.uag_reset_transp(register = true, reinvite = true)
|
||||
Api.net_debug()
|
||||
} else {
|
||||
UserAgent.register()
|
||||
}
|
||||
}
|
||||
|
||||
private fun isNetworkActive(network: Network): Boolean {
|
||||
if (VERSION.SDK_INT >= 23)
|
||||
return network == cm.activeNetwork
|
||||
@ -1173,8 +1229,8 @@ class BaresipService: Service() {
|
||||
partialWakeLock.release()
|
||||
if (this::proximityWakeLock.isInitialized && proximityWakeLock.isHeld)
|
||||
proximityWakeLock.release()
|
||||
if (this::fl.isInitialized && fl.isHeld)
|
||||
fl.release()
|
||||
if (this::wifiLock.isInitialized)
|
||||
wifiLock.release()
|
||||
isServiceClean = true
|
||||
}
|
||||
|
||||
|
||||
@ -311,52 +311,6 @@ object Utils {
|
||||
return servers
|
||||
}
|
||||
|
||||
private fun updateLinkAddresses(linkAddresses: List<LinkAddress>) {
|
||||
var updated = false
|
||||
val ipV6Addr = findIpV6Address(linkAddresses)
|
||||
if (ipV6Addr != findIpV6Address(BaresipService.linkAddresses)) {
|
||||
Log.d(TAG, "Updating IPv6 address to '$ipV6Addr'")
|
||||
if (ipV6Addr != "") {
|
||||
if (Api.net_set_address(ipV6Addr) != 0)
|
||||
Log.w(TAG, "Failed to update net address '$ipV6Addr")
|
||||
} else {
|
||||
Api.net_unset_address(Api.AF_INET6)
|
||||
}
|
||||
updated = true
|
||||
}
|
||||
val ipV4Addr = findIpV4Address(linkAddresses)
|
||||
if (ipV4Addr != findIpV4Address(BaresipService.linkAddresses)) {
|
||||
Log.d(TAG, "Updating IPv4 address to '$ipV4Addr'")
|
||||
if (ipV4Addr != "") {
|
||||
if (Api.net_set_address(ipV4Addr) != 0)
|
||||
Log.w(TAG, "Failed to update net address '$ipV4Addr'")
|
||||
} else {
|
||||
Api.net_unset_address(Api.AF_INET)
|
||||
}
|
||||
updated = true
|
||||
}
|
||||
if (updated) {
|
||||
BaresipService.linkAddresses = linkAddresses
|
||||
Api.uag_reset_transp(register = true, reinvite = true)
|
||||
Api.net_debug()
|
||||
} else {
|
||||
UserAgent.register()
|
||||
}
|
||||
}
|
||||
|
||||
fun updateLinkProperties(props: LinkProperties) {
|
||||
if (BaresipService.dynDns && (BaresipService.dnsServers != props.dnsServers)) {
|
||||
if (BaresipService.isServiceRunning)
|
||||
if (Config.updateDnsServers(props.dnsServers) != 0)
|
||||
Log.w(TAG, "Failed to update DNS servers '${props.dnsServers}'")
|
||||
else
|
||||
BaresipService.dnsServers = props.dnsServers
|
||||
else
|
||||
BaresipService.dnsServers = props.dnsServers
|
||||
}
|
||||
updateLinkAddresses(props.linkAddresses)
|
||||
}
|
||||
|
||||
fun implode(list: List<String>, sep: String): String {
|
||||
var res = ""
|
||||
for (s in list) {
|
||||
|
||||
Reference in New Issue
Block a user