Skip to content

Commit 34848aa

Browse files
authored
Prevent crash caused by re-release of wakelock in calls (#5077)
1 parent e14ef24 commit 34848aa

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

features/call/impl/src/main/kotlin/io/element/android/features/call/impl/utils/WebViewAudioManager.kt

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import kotlinx.coroutines.CoroutineScope
2121
import kotlinx.coroutines.Dispatchers
2222
import kotlinx.coroutines.delay
2323
import kotlinx.coroutines.launch
24+
import kotlinx.coroutines.sync.Mutex
25+
import kotlinx.coroutines.sync.withLock
2426
import kotlinx.serialization.Serializable
2527
import kotlinx.serialization.Transient
2628
import kotlinx.serialization.json.Json
@@ -84,6 +86,11 @@ class WebViewAudioManager(
8486
?.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "${webView.context.packageName}:ProximitySensorCallWakeLock")
8587
}
8688

89+
/**
90+
* Used to ensure that only one coroutine can access the proximity sensor wake lock at a time, preventing re-acquiring or re-releasing it.
91+
*/
92+
private val proximitySensorMutex = Mutex()
93+
8794
/**
8895
* This listener tracks the current communication device and updates the WebView when it changes.
8996
*/
@@ -208,8 +215,12 @@ class WebViewAudioManager(
208215
return
209216
}
210217

211-
if (proximitySensorWakeLock?.isHeld == true) {
212-
proximitySensorWakeLock?.release()
218+
coroutineScope.launch {
219+
proximitySensorMutex.withLock {
220+
if (proximitySensorWakeLock?.isHeld == true) {
221+
proximitySensorWakeLock?.release()
222+
}
223+
}
213224
}
214225

215226
audioManager.mode = AudioManager.MODE_NORMAL
@@ -397,13 +408,17 @@ class WebViewAudioManager(
397408

398409
expectedNewCommunicationDeviceId = null
399410

400-
@Suppress("WakeLock", "WakeLockTimeout")
401-
if (device?.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE && proximitySensorWakeLock?.isHeld == false) {
402-
// If the device is the built-in earpiece, we need to acquire the proximity sensor wake lock
403-
proximitySensorWakeLock?.acquire()
404-
} else if (proximitySensorWakeLock?.isHeld == true) {
405-
// If the device is no longer the earpiece, we need to release the wake lock
406-
proximitySensorWakeLock?.release()
411+
coroutineScope.launch {
412+
proximitySensorMutex.withLock {
413+
@Suppress("WakeLock", "WakeLockTimeout")
414+
if (device?.type == AudioDeviceInfo.TYPE_BUILTIN_EARPIECE && proximitySensorWakeLock?.isHeld == false) {
415+
// If the device is the built-in earpiece, we need to acquire the proximity sensor wake lock
416+
proximitySensorWakeLock?.acquire()
417+
} else if (proximitySensorWakeLock?.isHeld == true) {
418+
// If the device is no longer the earpiece, we need to release the wake lock
419+
proximitySensorWakeLock?.release()
420+
}
421+
}
407422
}
408423
}
409424

0 commit comments

Comments
 (0)