@@ -15,6 +15,7 @@ import android.os.Build
1515import android.os.PowerManager
1616import android.webkit.JavascriptInterface
1717import android.webkit.WebView
18+ import androidx.annotation.RequiresApi
1819import androidx.core.content.getSystemService
1920import kotlinx.coroutines.CoroutineScope
2021import 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