enforce selecting the EC preferred device

This commit is contained in:
Valere 2026-04-17 18:00:25 +02:00
parent 09e57e077a
commit 58bde16b81
2 changed files with 15 additions and 17 deletions

View file

@ -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.
*

View file

@ -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,