Determine Spatial Audio AudioDeviceAttributes by BT profile state
Test: atest SpatialAudioComponentInteractorTest
Flag: com.android.settingslib.flags.enable_determining_spatial_audio_attributes_by_profile
Bug: 341005211
Change-Id: Ia9dc2463c412963b15631e82f1f6dd08dcf7c133
diff --git a/packages/SettingsLib/aconfig/settingslib.aconfig b/packages/SettingsLib/aconfig/settingslib.aconfig
index 32557b9..a158756 100644
--- a/packages/SettingsLib/aconfig/settingslib.aconfig
+++ b/packages/SettingsLib/aconfig/settingslib.aconfig
@@ -79,3 +79,13 @@
purpose: PURPOSE_BUGFIX
}
}
+
+flag {
+ name: "enable_determining_spatial_audio_attributes_by_profile"
+ namespace: "cross_device_experiences"
+ description: "Use bluetooth profile connection policy to determine spatial audio attributes"
+ bug: "341005211"
+ metadata {
+ purpose: PURPOSE_BUGFIX
+ }
+}
diff --git a/packages/SettingsLib/src/com/android/settingslib/volume/data/repository/AudioRepository.kt b/packages/SettingsLib/src/com/android/settingslib/volume/data/repository/AudioRepository.kt
index 20b949f4..8ec5ba1 100644
--- a/packages/SettingsLib/src/com/android/settingslib/volume/data/repository/AudioRepository.kt
+++ b/packages/SettingsLib/src/com/android/settingslib/volume/data/repository/AudioRepository.kt
@@ -20,6 +20,7 @@
import android.database.ContentObserver
import android.media.AudioDeviceInfo
import android.media.AudioManager
+import android.media.AudioManager.AudioDeviceCategory
import android.media.AudioManager.OnCommunicationDeviceChangedListener
import android.provider.Settings
import androidx.concurrent.futures.DirectExecutor
@@ -85,6 +86,10 @@
suspend fun setMuted(audioStream: AudioStream, isMuted: Boolean): Boolean
suspend fun setRingerMode(audioStream: AudioStream, mode: RingerMode)
+
+ /** Gets audio device category. */
+ @AudioDeviceCategory
+ suspend fun getBluetoothAudioDeviceCategory(bluetoothAddress: String): Int
}
class AudioRepositoryImpl(
@@ -211,6 +216,13 @@
withContext(backgroundCoroutineContext) { audioManager.ringerMode = mode.value }
}
+ @AudioDeviceCategory
+ override suspend fun getBluetoothAudioDeviceCategory(bluetoothAddress: String): Int {
+ return withContext(backgroundCoroutineContext) {
+ audioManager.getBluetoothAudioDeviceCategory(bluetoothAddress)
+ }
+ }
+
private fun getMinVolume(stream: AudioStream): Int =
try {
audioManager.getStreamMinVolume(stream.value)
diff --git a/packages/SettingsLib/tests/integ/src/com/android/settingslib/volume/data/repository/AudioRepositoryTest.kt b/packages/SettingsLib/tests/integ/src/com/android/settingslib/volume/data/repository/AudioRepositoryTest.kt
index 683759d..844dc12 100644
--- a/packages/SettingsLib/tests/integ/src/com/android/settingslib/volume/data/repository/AudioRepositoryTest.kt
+++ b/packages/SettingsLib/tests/integ/src/com/android/settingslib/volume/data/repository/AudioRepositoryTest.kt
@@ -247,6 +247,19 @@
}
}
+ @Test
+ fun getBluetoothAudioDeviceCategory() {
+ testScope.runTest {
+ `when`(audioManager.getBluetoothAudioDeviceCategory("12:34:56:78")).thenReturn(
+ AudioManager.AUDIO_DEVICE_CATEGORY_HEADPHONES)
+
+ val category = underTest.getBluetoothAudioDeviceCategory("12:34:56:78")
+ runCurrent()
+
+ assertThat(category).isEqualTo(AudioManager.AUDIO_DEVICE_CATEGORY_HEADPHONES)
+ }
+ }
+
private fun triggerConnectedDeviceChange(communicationDevice: AudioDeviceInfo?) {
verify(audioManager)
.addOnCommunicationDeviceChangedListener(
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/spatial/SpatialAudioComponentKosmos.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/spatial/SpatialAudioComponentKosmos.kt
index 777240c..5826b3f 100644
--- a/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/spatial/SpatialAudioComponentKosmos.kt
+++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/spatial/SpatialAudioComponentKosmos.kt
@@ -17,8 +17,10 @@
package com.android.systemui.volume.panel.component.spatial
import com.android.systemui.kosmos.Kosmos
+import com.android.systemui.kosmos.backgroundCoroutineContext
import com.android.systemui.kosmos.testScope
import com.android.systemui.media.spatializerInteractor
+import com.android.systemui.volume.data.repository.audioRepository
import com.android.systemui.volume.domain.interactor.audioOutputInteractor
import com.android.systemui.volume.panel.component.spatial.domain.interactor.SpatialAudioComponentInteractor
@@ -27,6 +29,8 @@
SpatialAudioComponentInteractor(
audioOutputInteractor,
spatializerInteractor,
+ audioRepository,
+ backgroundCoroutineContext,
testScope.backgroundScope
)
}
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractorTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractorTest.kt
index c6c46fa..555d77c 100644
--- a/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractorTest.kt
+++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractorTest.kt
@@ -16,14 +16,22 @@
package com.android.systemui.volume.panel.component.spatial.domain.interactor
+import android.bluetooth.BluetoothDevice
+import android.bluetooth.BluetoothProfile
import android.media.AudioDeviceAttributes
import android.media.AudioDeviceInfo
import android.media.session.MediaSession
import android.media.session.PlaybackState
+import android.platform.test.annotations.EnableFlags
+import android.platform.test.flag.junit.SetFlagsRule
import android.testing.TestableLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
+import com.android.settingslib.bluetooth.A2dpProfile
import com.android.settingslib.bluetooth.CachedBluetoothDevice
+import com.android.settingslib.bluetooth.HearingAidProfile
+import com.android.settingslib.bluetooth.LeAudioProfile
+import com.android.settingslib.flags.Flags
import com.android.settingslib.media.BluetoothMediaDevice
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
@@ -44,6 +52,7 @@
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
+import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@@ -52,15 +61,29 @@
@RunWith(AndroidJUnit4::class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
class SpatialAudioComponentInteractorTest : SysuiTestCase() {
+ @get:Rule val setFlagsRule = SetFlagsRule()
private val kosmos = testKosmos()
private lateinit var underTest: SpatialAudioComponentInteractor
+ private val a2dpProfile: A2dpProfile = mock {
+ whenever(profileId).thenReturn(BluetoothProfile.A2DP)
+ }
+ private val leAudioProfile: LeAudioProfile = mock {
+ whenever(profileId).thenReturn(BluetoothProfile.LE_AUDIO)
+ }
+ private val hearingAidProfile: HearingAidProfile = mock {
+ whenever(profileId).thenReturn(BluetoothProfile.HEARING_AID)
+ }
+ private val bluetoothDevice: BluetoothDevice = mock {}
@Before
fun setup() {
with(kosmos) {
val cachedBluetoothDevice: CachedBluetoothDevice = mock {
whenever(address).thenReturn("test_address")
+ whenever(device).thenReturn(bluetoothDevice)
+ whenever(profiles)
+ .thenReturn(listOf(a2dpProfile, leAudioProfile, hearingAidProfile))
}
localMediaRepository.updateCurrentConnectedDevice(
mock<BluetoothMediaDevice> {
@@ -83,7 +106,7 @@
fun setEnabled_changesIsEnabled() {
with(kosmos) {
testScope.runTest {
- spatializerRepository.setIsSpatialAudioAvailable(headset, true)
+ spatializerRepository.setIsSpatialAudioAvailable(bleHeadsetAttributes, true)
val values by collectValues(underTest.isEnabled)
underTest.setEnabled(SpatialAudioEnabledModel.Disabled)
@@ -106,10 +129,39 @@
}
@Test
+ @EnableFlags(Flags.FLAG_ENABLE_DETERMINING_SPATIAL_AUDIO_ATTRIBUTES_BY_PROFILE)
+ fun setEnabled_determinedByBluetoothProfile_a2dpProfileEnabled() {
+ with(kosmos) {
+ testScope.runTest {
+ whenever(a2dpProfile.isEnabled(bluetoothDevice)).thenReturn(true)
+ whenever(leAudioProfile.isEnabled(bluetoothDevice)).thenReturn(false)
+ whenever(hearingAidProfile.isEnabled(bluetoothDevice)).thenReturn(false)
+ spatializerRepository.setIsSpatialAudioAvailable(a2dpAttributes, true)
+ val values by collectValues(underTest.isEnabled)
+
+ underTest.setEnabled(SpatialAudioEnabledModel.Disabled)
+ runCurrent()
+ underTest.setEnabled(SpatialAudioEnabledModel.SpatialAudioEnabled)
+ runCurrent()
+
+ assertThat(values)
+ .containsExactly(
+ SpatialAudioEnabledModel.Unknown,
+ SpatialAudioEnabledModel.Disabled,
+ SpatialAudioEnabledModel.SpatialAudioEnabled,
+ )
+ .inOrder()
+ assertThat(spatializerRepository.getSpatialAudioCompatibleDevices())
+ .containsExactly(a2dpAttributes)
+ }
+ }
+ }
+
+ @Test
fun connectedDeviceSupports_isAvailable_SpatialAudio() {
with(kosmos) {
testScope.runTest {
- spatializerRepository.setIsSpatialAudioAvailable(headset, true)
+ spatializerRepository.setIsSpatialAudioAvailable(bleHeadsetAttributes, true)
val isAvailable by collectLastValue(underTest.isAvailable)
@@ -123,8 +175,8 @@
fun connectedDeviceSupportsHeadTracking_isAvailable_HeadTracking() {
with(kosmos) {
testScope.runTest {
- spatializerRepository.setIsSpatialAudioAvailable(headset, true)
- spatializerRepository.setIsHeadTrackingAvailable(headset, true)
+ spatializerRepository.setIsSpatialAudioAvailable(bleHeadsetAttributes, true)
+ spatializerRepository.setIsHeadTrackingAvailable(bleHeadsetAttributes, true)
val isAvailable by collectLastValue(underTest.isAvailable)
@@ -138,7 +190,7 @@
fun connectedDeviceDoesntSupport_isAvailable_Unavailable() {
with(kosmos) {
testScope.runTest {
- spatializerRepository.setIsSpatialAudioAvailable(headset, false)
+ spatializerRepository.setIsSpatialAudioAvailable(bleHeadsetAttributes, false)
val isAvailable by collectLastValue(underTest.isAvailable)
@@ -179,7 +231,13 @@
}
private companion object {
- val headset =
+ val a2dpAttributes =
+ AudioDeviceAttributes(
+ AudioDeviceAttributes.ROLE_OUTPUT,
+ AudioDeviceInfo.TYPE_BLUETOOTH_A2DP,
+ "test_address"
+ )
+ val bleHeadsetAttributes =
AudioDeviceAttributes(
AudioDeviceAttributes.ROLE_OUTPUT,
AudioDeviceInfo.TYPE_BLE_HEADSET,
diff --git a/packages/SystemUI/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractor.kt b/packages/SystemUI/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractor.kt
index 7f1faee..cfcd6b1 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractor.kt
+++ b/packages/SystemUI/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractor.kt
@@ -16,15 +16,23 @@
package com.android.systemui.volume.panel.component.spatial.domain.interactor
+import android.bluetooth.BluetoothProfile
import android.media.AudioDeviceAttributes
import android.media.AudioDeviceInfo
+import android.media.AudioManager
+import com.android.settingslib.bluetooth.CachedBluetoothDevice
+import com.android.settingslib.bluetooth.LocalBluetoothProfile
+import com.android.settingslib.flags.Flags
import com.android.settingslib.media.domain.interactor.SpatializerInteractor
+import com.android.settingslib.volume.data.repository.AudioRepository
+import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.volume.domain.interactor.AudioOutputInteractor
import com.android.systemui.volume.domain.model.AudioOutputDevice
import com.android.systemui.volume.panel.component.spatial.domain.model.SpatialAudioAvailabilityModel
import com.android.systemui.volume.panel.component.spatial.domain.model.SpatialAudioEnabledModel
import com.android.systemui.volume.panel.dagger.scope.VolumePanelScope
import javax.inject.Inject
+import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharingStarted
@@ -33,6 +41,7 @@
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.stateIn
+import kotlinx.coroutines.withContext
/**
* Provides an ability to access and update spatial audio and head tracking state.
@@ -46,6 +55,8 @@
constructor(
audioOutputInteractor: AudioOutputInteractor,
private val spatializerInteractor: SpatializerInteractor,
+ private val audioRepository: AudioRepository,
+ @Background private val backgroundCoroutineContext: CoroutineContext,
@VolumePanelScope private val coroutineScope: CoroutineScope,
) {
@@ -138,42 +149,85 @@
}
private suspend fun AudioOutputDevice.getAudioDeviceAttributes(): AudioDeviceAttributes? {
- when (this) {
- is AudioOutputDevice.BuiltIn -> return builtinSpeaker
+ return when (this) {
+ is AudioOutputDevice.BuiltIn -> builtinSpeaker
is AudioOutputDevice.Bluetooth -> {
- return listOf(
- AudioDeviceAttributes(
- AudioDeviceAttributes.ROLE_OUTPUT,
- AudioDeviceInfo.TYPE_BLE_HEADSET,
- cachedBluetoothDevice.address,
- ),
- AudioDeviceAttributes(
- AudioDeviceAttributes.ROLE_OUTPUT,
- AudioDeviceInfo.TYPE_BLE_SPEAKER,
- cachedBluetoothDevice.address,
- ),
- AudioDeviceAttributes(
- AudioDeviceAttributes.ROLE_OUTPUT,
- AudioDeviceInfo.TYPE_BLE_BROADCAST,
- cachedBluetoothDevice.address,
- ),
- AudioDeviceAttributes(
- AudioDeviceAttributes.ROLE_OUTPUT,
- AudioDeviceInfo.TYPE_BLUETOOTH_A2DP,
- cachedBluetoothDevice.address,
- ),
- AudioDeviceAttributes(
- AudioDeviceAttributes.ROLE_OUTPUT,
- AudioDeviceInfo.TYPE_HEARING_AID,
- cachedBluetoothDevice.address,
+ if (Flags.enableDeterminingSpatialAudioAttributesByProfile()) {
+ getAudioDeviceAttributesByBluetoothProfile(cachedBluetoothDevice)
+ } else {
+ listOf(
+ AudioDeviceAttributes(
+ AudioDeviceAttributes.ROLE_OUTPUT,
+ AudioDeviceInfo.TYPE_BLE_HEADSET,
+ cachedBluetoothDevice.address,
+ ),
+ AudioDeviceAttributes(
+ AudioDeviceAttributes.ROLE_OUTPUT,
+ AudioDeviceInfo.TYPE_BLE_SPEAKER,
+ cachedBluetoothDevice.address,
+ ),
+ AudioDeviceAttributes(
+ AudioDeviceAttributes.ROLE_OUTPUT,
+ AudioDeviceInfo.TYPE_BLE_BROADCAST,
+ cachedBluetoothDevice.address,
+ ),
+ AudioDeviceAttributes(
+ AudioDeviceAttributes.ROLE_OUTPUT,
+ AudioDeviceInfo.TYPE_BLUETOOTH_A2DP,
+ cachedBluetoothDevice.address,
+ ),
+ AudioDeviceAttributes(
+ AudioDeviceAttributes.ROLE_OUTPUT,
+ AudioDeviceInfo.TYPE_HEARING_AID,
+ cachedBluetoothDevice.address,
+ )
)
- )
- .firstOrNull { spatializerInteractor.isSpatialAudioAvailable(it) }
+ .firstOrNull { spatializerInteractor.isSpatialAudioAvailable(it) }
+ }
}
- else -> return null
+ else -> null
}
}
+ private suspend fun getAudioDeviceAttributesByBluetoothProfile(
+ cachedBluetoothDevice: CachedBluetoothDevice
+ ): AudioDeviceAttributes? =
+ withContext(backgroundCoroutineContext) {
+ cachedBluetoothDevice.profiles
+ .firstOrNull {
+ it.profileId in audioProfiles && it.isEnabled(cachedBluetoothDevice.device)
+ }
+ ?.let { profile: LocalBluetoothProfile ->
+ when (profile.profileId) {
+ BluetoothProfile.A2DP -> {
+ AudioDeviceInfo.TYPE_BLUETOOTH_A2DP
+ }
+ BluetoothProfile.LE_AUDIO -> {
+ when (
+ audioRepository.getBluetoothAudioDeviceCategory(
+ cachedBluetoothDevice.address
+ )
+ ) {
+ AudioManager.AUDIO_DEVICE_CATEGORY_SPEAKER ->
+ AudioDeviceInfo.TYPE_BLE_SPEAKER
+ else -> AudioDeviceInfo.TYPE_BLE_HEADSET
+ }
+ }
+ BluetoothProfile.HEARING_AID -> {
+ AudioDeviceInfo.TYPE_HEARING_AID
+ }
+ else -> null
+ }
+ }
+ ?.let {
+ AudioDeviceAttributes(
+ AudioDeviceAttributes.ROLE_OUTPUT,
+ it,
+ cachedBluetoothDevice.address,
+ )
+ }
+ }
+
private companion object {
val builtinSpeaker =
AudioDeviceAttributes(
@@ -181,5 +235,7 @@
AudioDeviceInfo.TYPE_BUILTIN_SPEAKER,
""
)
+ val audioProfiles =
+ setOf(BluetoothProfile.A2DP, BluetoothProfile.LE_AUDIO, BluetoothProfile.HEARING_AID)
}
}
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/FakeAudioRepository.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/FakeAudioRepository.kt
index 21d59f0..fcea9e7b 100644
--- a/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/FakeAudioRepository.kt
+++ b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/data/repository/FakeAudioRepository.kt
@@ -42,6 +42,7 @@
private val models: MutableMap<AudioStream, MutableStateFlow<AudioStreamModel>> = mutableMapOf()
private val lastAudibleVolumes: MutableMap<AudioStream, Int> = mutableMapOf()
+ private val deviceCategories: MutableMap<String, Int> = mutableMapOf()
private fun getAudioStreamModelState(
audioStream: AudioStream
@@ -103,4 +104,12 @@
override suspend fun setRingerMode(audioStream: AudioStream, mode: RingerMode) {
mutableRingerMode.value = mode
}
+
+ fun setBluetoothAudioDeviceCategory(bluetoothAddress: String, category: Int) {
+ deviceCategories[bluetoothAddress] = category
+ }
+
+ override suspend fun getBluetoothAudioDeviceCategory(bluetoothAddress: String): Int {
+ return deviceCategories[bluetoothAddress] ?: AudioManager.AUDIO_DEVICE_CATEGORY_UNKNOWN
+ }
}
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractorKosmos.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractorKosmos.kt
index 95a7b9b..6a46d56 100644
--- a/packages/SystemUI/tests/utils/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractorKosmos.kt
+++ b/packages/SystemUI/tests/utils/src/com/android/systemui/volume/panel/component/spatial/domain/interactor/SpatialAudioComponentInteractorKosmos.kt
@@ -17,8 +17,10 @@
package com.android.systemui.volume.panel.component.spatial.domain.interactor
import com.android.systemui.kosmos.Kosmos
+import com.android.systemui.kosmos.backgroundCoroutineContext
import com.android.systemui.kosmos.testScope
import com.android.systemui.media.spatializerInteractor
+import com.android.systemui.volume.data.repository.audioRepository
import com.android.systemui.volume.domain.interactor.audioOutputInteractor
val Kosmos.spatialAudioComponentInteractor by
@@ -26,6 +28,8 @@
SpatialAudioComponentInteractor(
audioOutputInteractor,
spatializerInteractor,
+ audioRepository,
+ backgroundCoroutineContext,
testScope.backgroundScope,
)
}