@@ -15,6 +15,7 @@ import android.os.Build
15
15
import android.os.PowerManager
16
16
import android.webkit.JavascriptInterface
17
17
import android.webkit.WebView
18
+ import androidx.annotation.RequiresApi
18
19
import androidx.core.content.getSystemService
19
20
import kotlinx.coroutines.CoroutineScope
20
21
import kotlinx.coroutines.Dispatchers
@@ -66,23 +67,26 @@ class WebViewAudioManager(
66
67
/* *
67
68
* This listener tracks the current communication device and updates the WebView when it changes.
68
69
*/
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) {
79
74
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 )
81
89
}
82
- } else {
83
- Timber .d(" Audio device cleared" )
84
- expectedNewCommunicationDeviceId = null
85
- audioManager.selectAudioDevice(null )
86
90
}
87
91
}
88
92
0 commit comments