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,9 +215,13 @@ class WebViewAudioManager(
return return
} }
coroutineScope.launch {
proximitySensorMutex.withLock {
if (proximitySensorWakeLock?.isHeld == true) { if (proximitySensorWakeLock?.isHeld == true) {
proximitySensorWakeLock?.release() proximitySensorWakeLock?.release()
} }
}
}
audioManager.mode = AudioManager.MODE_NORMAL audioManager.mode = AudioManager.MODE_NORMAL
@ -397,6 +408,8 @@ class WebViewAudioManager(
expectedNewCommunicationDeviceId = null expectedNewCommunicationDeviceId = null
coroutineScope.launch {
proximitySensorMutex.withLock {
@Suppress("WakeLock", "WakeLockTimeout") @Suppress("WakeLock", "WakeLockTimeout")
if (device?.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE && proximitySensorWakeLock?.isHeld == false) { if (device?.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE && proximitySensorWakeLock?.isHeld == false) {
// If the device is the built-in earpiece, we need to acquire the proximity sensor wake lock // If the device is the built-in earpiece, we need to acquire the proximity sensor wake lock
@ -406,6 +419,8 @@ class WebViewAudioManager(
proximitySensorWakeLock?.release() proximitySensorWakeLock?.release()
} }
} }
}
}
/** /**
* Sets whether the audio is enabled for Element Call in the WebView. * Sets whether the audio is enabled for Element Call in the WebView.