Fix audio output selection for Element Call (#4602)

* Fix audio output selection.

* Ensure that Element Call audio output uses a new connected device, even during a call.
Also add a few logs.

* Extract functions.

* Add more log and protect from crash.

* Revert formatting change

* Update screenshots

---------

Co-authored-by: ElementBot <android@element.io>
This commit is contained in:
Benoit Marty 2025-04-17 10:36:42 +02:00 committed by GitHub
parent 7ed362b9db
commit be1c9b793b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 62 additions and 17 deletions

View file

@ -8,6 +8,9 @@
package io.element.android.features.call.impl.ui
import android.annotation.SuppressLint
import android.content.Context
import android.media.AudioDeviceCallback
import android.media.AudioDeviceInfo
import android.media.AudioManager
import android.util.Log
import android.view.ViewGroup
@ -22,6 +25,10 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalInspectionMode
@ -151,15 +158,11 @@ private fun CallWebView(
Text("WebView - can't be previewed")
}
} else {
var audioDeviceCallback: AudioDeviceCallback? by remember { mutableStateOf(null) }
AndroidView(
modifier = modifier,
factory = { context ->
// Set 'voice call' mode so volume keys actually control the call volume
val audioManager = context.getSystemService<AudioManager>()
audioManager?.mode = AudioManager.MODE_IN_COMMUNICATION
audioManager?.enableExternalAudioDevice()
audioDeviceCallback = context.setupAudioConfiguration()
WebView(context).apply {
onWebViewCreate(this)
setup(userAgent, onPermissionsRequest)
@ -172,16 +175,40 @@ private fun CallWebView(
},
onRelease = { webView ->
// Reset audio mode
val audioManager = webView.context.getSystemService<AudioManager>()
audioManager?.disableExternalAudioDevice()
audioManager?.mode = AudioManager.MODE_NORMAL
webView.context.releaseAudioConfiguration(audioDeviceCallback)
webView.destroy()
}
)
}
}
private fun Context.setupAudioConfiguration(): AudioDeviceCallback? {
val audioManager = getSystemService<AudioManager>() ?: return null
// Set 'voice call' mode so volume keys actually control the call volume
audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
audioManager.enableExternalAudioDevice()
return object : AudioDeviceCallback() {
override fun onAudioDevicesAdded(addedDevices: Array<out AudioDeviceInfo>?) {
Timber.d("Audio devices added")
audioManager.enableExternalAudioDevice()
}
override fun onAudioDevicesRemoved(removedDevices: Array<out AudioDeviceInfo>?) {
Timber.d("Audio devices removed")
audioManager.enableExternalAudioDevice()
}
}.also {
audioManager.registerAudioDeviceCallback(it, null)
}
}
private fun Context.releaseAudioConfiguration(audioDeviceCallback: AudioDeviceCallback?) {
val audioManager = getSystemService<AudioManager>() ?: return
audioManager.unregisterAudioDeviceCallback(audioDeviceCallback)
audioManager.disableExternalAudioDevice()
audioManager.mode = AudioManager.MODE_NORMAL
}
@SuppressLint("SetJavaScriptEnabled")
private fun WebView.setup(
userAgent: String,