Acquire PARTIAL_WAKE lock only during calls and briefly during registrations and network transitions

This commit is contained in:
Juha Heinanen
2026-04-12 18:52:04 +03:00
parent 71119f1c76
commit 97a31b5e31
6 changed files with 63 additions and 39 deletions
@@ -167,8 +167,10 @@ class BaresipService: Service() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
Log.d(TAG, "Network $network is available")
if (network !in allNetworks)
allNetworks.add(network)
synchronized(allNetworks) {
if (network !in allNetworks)
allNetworks.add(network)
}
}
override fun onLosing(network: Network, maxMsToLive: Int) {
@@ -179,16 +181,20 @@ class BaresipService: Service() {
override fun onLost(network: Network) {
super.onLost(network)
Log.d(TAG, "Network $network is lost")
if (network in allNetworks)
allNetworks.remove(network)
synchronized(allNetworks) {
if (network in allNetworks)
allNetworks.remove(network)
}
if (isServiceRunning)
updateNetwork()
}
override fun onCapabilitiesChanged(network: Network, caps: NetworkCapabilities) {
super.onCapabilitiesChanged(network, caps)
if (network !in allNetworks)
allNetworks.add(network)
synchronized(allNetworks) {
if (network !in allNetworks)
allNetworks.add(network)
}
if (isServiceRunning)
updateNetwork()
}
@@ -196,8 +202,10 @@ class BaresipService: Service() {
override fun onLinkPropertiesChanged(network: Network, props: LinkProperties) {
super.onLinkPropertiesChanged(network, props)
Log.d(TAG, "Network $network link properties changed: $props")
if (network !in allNetworks)
allNetworks.add(network)
synchronized(allNetworks) {
if (network !in allNetworks)
allNetworks.add(network)
}
if (isServiceRunning)
updateNetwork()
}
@@ -740,6 +748,7 @@ class BaresipService: Service() {
if (uas.value[accountIndex].account.aor == aor) {
when (ev[0]) {
"registering", "unregistering" -> {
updatePartialWakeLock(brief = true)
ua.updateStatus(circleYellow.getValue(colorblind))
updateStatusNotification()
if (isMainVisible)
@@ -747,6 +756,7 @@ class BaresipService: Service() {
return
}
"registered" -> {
updatePartialWakeLock(brief = true)
ua.updateStatus(
if (Api.account_regint(ua.account.accp) == 0)
R.drawable.circle_white
@@ -759,6 +769,7 @@ class BaresipService: Service() {
return
}
"registering failed" -> {
updatePartialWakeLock(brief = true)
ua.updateStatus(if (Api.account_regint(ua.account.accp) == 0)
R.drawable.circle_white
else
@@ -1408,6 +1419,7 @@ class BaresipService: Service() {
@Keep
fun started() {
Log.d(TAG, "Received 'started' from baresip")
isNativeReady = true
Api.net_debug()
postServiceEvent(ServiceEvent("started", arrayListOf(callActionUri), System.nanoTime()))
callActionUri = ""
@@ -1422,6 +1434,7 @@ class BaresipService: Service() {
@Keep
fun stopped(error: String) {
Log.d(TAG, "Received 'stopped' from baresip with start error '$error'")
isNativeReady = false
quitTimer.cancel()
cleanService()
isServiceRunning = false
@@ -1629,18 +1642,23 @@ class BaresipService: Service() {
}
@SuppressLint("WakelockTimeout")
private fun updatePartialWakeLock() {
val isAnyUaActive = uasStatus.value.values.any { it != R.drawable.circle_white }
val needsToStayAwake = isAnyUaActive || calls.isNotEmpty()
private fun updatePartialWakeLock(brief: Boolean = false) {
// Only hold a permanent wake lock during active calls.
// For registration standby, rely on the foreground service and kernel socket wakes.
// Also hold it briefly for registration events or network transitions.
val needsToStayAwake = calls.isNotEmpty()
try {
if (needsToStayAwake) {
if (!partialWakeLock.isHeld) {
Log.d(TAG, "Acquiring Partial Wake Lock (UA Active or Call in progress)")
Log.d(TAG, "Acquiring Partial Wake Lock (Call in progress)")
partialWakeLock.acquire()
}
} else if (brief) {
Log.d(TAG, "Acquiring Brief Partial Wake Lock (2s)")
partialWakeLock.acquire(2000L)
} else {
if (partialWakeLock.isHeld) {
Log.d(TAG, "Releasing Partial Wake Lock (All UAs idle and no calls)")
Log.d(TAG, "Releasing Partial Wake Lock (No calls in progress)")
partialWakeLock.release()
}
}
@@ -1943,6 +1961,9 @@ class BaresipService: Service() {
}
private fun updateNetwork() {
if (!isNativeReady) return
updatePartialWakeLock(brief = true)
updateDnsServers()
val addresses = linkAddresses()
@@ -1999,15 +2020,18 @@ class BaresipService: Service() {
private fun linkAddresses(): MutableMap<String, String> {
val addresses = mutableMapOf<String, String>()
for (n in allNetworks) {
val caps = cm.getNetworkCapabilities(n) ?: continue
if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND)) {
val props = cm.getLinkProperties(n) ?: continue
for (la in props.linkAddresses)
if (la.scope == OsConstants.RT_SCOPE_UNIVERSE &&
synchronized(allNetworks) {
for (n in allNetworks) {
val caps = cm.getNetworkCapabilities(n) ?: continue
if (caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND)) {
val props = cm.getLinkProperties(n) ?: continue
for (la in props.linkAddresses)
if (la.scope == OsConstants.RT_SCOPE_UNIVERSE &&
props.interfaceName != null && la.address.hostAddress != null &&
afMatch(la.address.hostAddress!!))
addresses[la.address.hostAddress!!] = props.interfaceName!!
afMatch(la.address.hostAddress!!)
)
addresses[la.address.hostAddress!!] = props.interfaceName!!
}
}
}
if (hotSpotIsEnabled) {
@@ -2138,6 +2162,7 @@ class BaresipService: Service() {
var instance: BaresipService? = null
var isServiceRunning = false
var isNativeReady = false
var isStartReceived = false
var isConfigInitialized = false
var libraryLoaded = false