diff --git a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/utils/WebViewAudioManager.kt b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/utils/WebViewAudioManager.kt index 80cc9b535a..c6e8557ff9 100644 --- a/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/utils/WebViewAudioManager.kt +++ b/features/call/impl/src/main/kotlin/io/element/android/features/call/impl/utils/WebViewAudioManager.kt @@ -64,6 +64,11 @@ class WebViewAudioManager( */ private val isWebViewAudioEnabled = AtomicBoolean(true) + /** + * Store the device id requested by EC, and re-set it if something try to switch (only android S+). + */ + private var ecRequestedDeviceId: String? = null + /** * The list of device types that are considered as communication devices, sorted by likelihood of it being used for communication. */ @@ -114,6 +119,12 @@ class WebViewAudioManager( private val commsDeviceChangedListener by lazy { AudioManager.OnCommunicationDeviceChangedListener { device -> Timber.d("Audio device changed, type: ${device?.id}") + val wantedDevice = this.ecRequestedDeviceId + if (wantedDevice != null && this.ecRequestedDeviceId != device?.id?.toString()) { + // We want to ensure that we stick to what EC selected even if it was changed outside + Timber.d("Audio device changed to unwanted device ${device?.id}, enforce using the expected device $wantedDevice") + audioManager.selectAudioDevice(wantedDevice) + } } } @@ -136,12 +147,6 @@ class WebViewAudioManager( setAvailableAudioDevices() } } -// -// -// /** -// * When a new audio device is selected but not yet set as the communication device by the OS, this id is used to check if the device is the expected one. -// */ -// private var expectedNewCommunicationDeviceId: Int? = null /** * Previously selected device, used to restore the selection when the selected device is removed. @@ -228,6 +233,7 @@ class WebViewAudioManager( val webViewAudioDeviceSelectedCallback = AndroidWebViewAudioBridge( onAudioDeviceSelected = { selectedDeviceId -> previousSelectedDevice = listAudioDevices().find { it.id.toString() == selectedDeviceId } + this.ecRequestedDeviceId = selectedDeviceId audioManager.selectAudioDevice(selectedDeviceId) }, onAudioPlaybackStarted = { @@ -295,17 +301,6 @@ class WebViewAudioManager( }) } - /** - * Updates the WebView's UI to reflect the selected audio device. - * - * @param deviceId The id of the selected audio device. - */ - private fun updateSelectedAudioDeviceInWebView(deviceId: String) { - coroutineScope.launch(Dispatchers.Main) { - webView.evaluateJavascript("controls.setOutputDevice('$deviceId');", null) - } - } - /** * Selects the audio device on the OS based on the provided device id. * diff --git a/libraries/audio/impl/src/main/kotlin/io/element/android/libraries/audio/impl/DefaultAudioFocus.kt b/libraries/audio/impl/src/main/kotlin/io/element/android/libraries/audio/impl/DefaultAudioFocus.kt index faab73593e..bc6a42d2f9 100644 --- a/libraries/audio/impl/src/main/kotlin/io/element/android/libraries/audio/impl/DefaultAudioFocus.kt +++ b/libraries/audio/impl/src/main/kotlin/io/element/android/libraries/audio/impl/DefaultAudioFocus.kt @@ -19,6 +19,7 @@ import dev.zacsweers.metro.ContributesBinding import io.element.android.libraries.audio.api.AudioFocus import io.element.android.libraries.audio.api.AudioFocusRequester import io.element.android.libraries.di.annotations.ApplicationContext +import timber.log.Timber @ContributesBinding(AppScope::class) class DefaultAudioFocus( @@ -38,9 +39,11 @@ class DefaultAudioFocus( when (it) { AudioManager.AUDIOFOCUS_GAIN -> { // Do nothing + Timber.d("AudioFocus: AUDIOFOCUS_GAIN") } AudioManager.AUDIOFOCUS_LOSS -> { // Permanent focus loss (e.g., phone call) — always stop/pause. + Timber.d("AudioFocus: AUDIOFOCUS_LOSS") onFocusLost() } AudioManager.AUDIOFOCUS_LOSS_TRANSIENT,