Prevent crash caused by re-release of wakelock in calls (#5077)

This commit is contained in:
Jorge Martin Espinosa 2025-07-25 13:36:23 +02:00 committed by GitHub
parent b57e79f8c6
commit 7958bb4692

View file

@ -21,6 +21,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient import kotlinx.serialization.Transient
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@ -84,6 +86,11 @@ class WebViewAudioManager(
?.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "${webView.context.packageName}:ProximitySensorCallWakeLock") ?.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "${webView.context.packageName}:ProximitySensorCallWakeLock")
} }
/**
* Used to ensure that only one coroutine can access the proximity sensor wake lock at a time, preventing re-acquiring or re-releasing it.
*/
private val proximitySensorMutex = Mutex()
/** /**
* This listener tracks the current communication device and updates the WebView when it changes. * This listener tracks the current communication device and updates the WebView when it changes.
*/ */
@ -208,8 +215,12 @@ class WebViewAudioManager(
return return
} }
if (proximitySensorWakeLock?.isHeld == true) { coroutineScope.launch {
proximitySensorWakeLock?.release() proximitySensorMutex.withLock {
if (proximitySensorWakeLock?.isHeld == true) {
proximitySensorWakeLock?.release()
}
}
} }
audioManager.mode = AudioManager.MODE_NORMAL audioManager.mode = AudioManager.MODE_NORMAL
@ -397,13 +408,17 @@ class WebViewAudioManager(
expectedNewCommunicationDeviceId = null expectedNewCommunicationDeviceId = null
@Suppress("WakeLock", "WakeLockTimeout") coroutineScope.launch {
if (device?.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE && proximitySensorWakeLock?.isHeld == false) { proximitySensorMutex.withLock {
// If the device is the built-in earpiece, we need to acquire the proximity sensor wake lock @Suppress("WakeLock", "WakeLockTimeout")
proximitySensorWakeLock?.acquire() if (device?.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE && proximitySensorWakeLock?.isHeld == false) {
} else if (proximitySensorWakeLock?.isHeld == true) { // If the device is the built-in earpiece, we need to acquire the proximity sensor wake lock
// If the device is no longer the earpiece, we need to release the wake lock proximitySensorWakeLock?.acquire()
proximitySensorWakeLock?.release() } else if (proximitySensorWakeLock?.isHeld == true) {
// If the device is no longer the earpiece, we need to release the wake lock
proximitySensorWakeLock?.release()
}
}
} }
} }