Skip to content

Commit f224fe5

Browse files
authored
Fix crash when using Element Call on API <= 30 (#4847)
1 parent 20270cd commit f224fe5

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import android.os.Build
1515
import android.os.PowerManager
1616
import android.webkit.JavascriptInterface
1717
import android.webkit.WebView
18+
import androidx.annotation.RequiresApi
1819
import androidx.core.content.getSystemService
1920
import kotlinx.coroutines.CoroutineScope
2021
import kotlinx.coroutines.Dispatchers
@@ -66,23 +67,26 @@ class WebViewAudioManager(
6667
/**
6768
* This listener tracks the current communication device and updates the WebView when it changes.
6869
*/
69-
private val commsDeviceChangedListener = AudioManager.OnCommunicationDeviceChangedListener { device ->
70-
if (device != null && device.id == expectedNewCommunicationDeviceId) {
71-
expectedNewCommunicationDeviceId = null
72-
Timber.d("Audio device changed, type: ${device.type}")
73-
updateSelectedAudioDeviceInWebView(device.id.toString())
74-
} else if (device != null && device.id != expectedNewCommunicationDeviceId) {
75-
// We were expecting a device change but it didn't happen, so we should retry
76-
val expectedDeviceId = expectedNewCommunicationDeviceId
77-
if (expectedDeviceId != null) {
78-
// Remove the expected id so we only retry once
70+
@get:RequiresApi(Build.VERSION_CODES.S)
71+
private val commsDeviceChangedListener by lazy {
72+
AudioManager.OnCommunicationDeviceChangedListener { device ->
73+
if (device != null && device.id == expectedNewCommunicationDeviceId) {
7974
expectedNewCommunicationDeviceId = null
80-
audioManager.selectAudioDevice(expectedDeviceId.toString())
75+
Timber.d("Audio device changed, type: ${device.type}")
76+
updateSelectedAudioDeviceInWebView(device.id.toString())
77+
} else if (device != null && device.id != expectedNewCommunicationDeviceId) {
78+
// We were expecting a device change but it didn't happen, so we should retry
79+
val expectedDeviceId = expectedNewCommunicationDeviceId
80+
if (expectedDeviceId != null) {
81+
// Remove the expected id so we only retry once
82+
expectedNewCommunicationDeviceId = null
83+
audioManager.selectAudioDevice(expectedDeviceId.toString())
84+
}
85+
} else {
86+
Timber.d("Audio device cleared")
87+
expectedNewCommunicationDeviceId = null
88+
audioManager.selectAudioDevice(null)
8189
}
82-
} else {
83-
Timber.d("Audio device cleared")
84-
expectedNewCommunicationDeviceId = null
85-
audioManager.selectAudioDevice(null)
8690
}
8791
}
8892

0 commit comments

Comments
 (0)