Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d578da12 authored by Haijie Hong's avatar Haijie Hong
Browse files

Use primary device when device is in audio sharing

Test: local tested
Flag: com.android.settingslib.flags.enable_le_audio_sharing
Bug: 419415048
Change-Id: Ia792c4c497b30cbe3ea0fb364928f29cee7a298e
parent 15a07814
Loading
Loading
Loading
Loading
+25 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import com.android.systemui.kosmos.testScope
import com.android.systemui.testKosmos
import com.android.systemui.volume.data.repository.TestAudioDevicesFactory
import com.android.systemui.volume.data.repository.audioRepository
import com.android.systemui.volume.data.repository.audioSharingRepository
import com.android.systemui.volume.domain.model.AudioOutputDevice
import com.android.systemui.volume.localMediaController
import com.android.systemui.volume.localMediaRepository
@@ -69,6 +70,7 @@ class AudioOutputInteractorTest : SysuiTestCase() {
                addOverride(R.drawable.ic_smartphone, testIcon)
                addOverride(R.drawable.ic_media_speaker_device, testIcon)
                addOverride(R.drawable.ic_media_tablet, testIcon)
                addOverride(com.android.internal.R.drawable.ic_settings_bluetooth, testIcon)

                addOverride(com.android.internal.R.drawable.ic_bt_hearing_aid, testIcon)

@@ -215,6 +217,29 @@ class AudioOutputInteractorTest : SysuiTestCase() {
        }
    }

    @Test
    fun inAudioSharing_returnsPrimaryDevice() {
        with(kosmos) {
            testScope.runTest {
                val cachedBluetoothDevice: CachedBluetoothDevice = mock {
                    on { name }.thenReturn("audio_sharing_device_name")
                }
                with(audioSharingRepository) {
                    setInAudioSharing(true)
                    setPrimaryDevice(cachedBluetoothDevice)
                }

                val device by collectLastValue(underTest.currentAudioDevice)

                runCurrent()

                assertThat(device).isInstanceOf(AudioOutputDevice.Bluetooth::class.java)
                assertThat(device!!.icon).isEqualTo(testIcon)
                assertThat(device!!.name).isEqualTo("audio_sharing_device_name")
            }
        }
    }

    private companion object {

        val testIcon = TestStubDrawable()
+43 −9
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.media.flags.Flags
import com.android.settingslib.R
import com.android.settingslib.bluetooth.CachedBluetoothDevice
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testScope
@@ -47,6 +48,7 @@ import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever

private const val builtInDeviceName = "This phone"
@@ -71,6 +73,7 @@ class MediaOutputComponentInteractorTest : SysuiTestCase() {
            with(context.orCreateTestableResources) {
                addOverride(R.drawable.ic_smartphone, testIcon)
                addOverride(R.drawable.ic_media_tablet, testIcon)
                addOverride(com.android.internal.R.drawable.ic_settings_bluetooth, testIcon)

                addOverride(R.string.media_transfer_this_device_name_tv, builtInDeviceName)
                addOverride(R.string.media_transfer_this_device_name_tablet, builtInDeviceName)
@@ -161,10 +164,13 @@ class MediaOutputComponentInteractorTest : SysuiTestCase() {
    fun hasSession_stateIs_MediaSession_canOpenAudioSwitcherDuringAudioSharing() =
        with(kosmos) {
            testScope.runTest {
                audioSharingRepository.setInAudioSharing(true)
                localMediaRepository.updateCurrentConnectedDevice(
                    TestMediaDevicesFactory.builtInMediaDevice()
                )
                val cachedBluetoothDevice: CachedBluetoothDevice = mock {
                    on { name }.thenReturn("audio_sharing_device_name")
                }
                with(audioSharingRepository) {
                    setInAudioSharing(true)
                    setPrimaryDevice(cachedBluetoothDevice)
                }
                mediaControllerRepository.setActiveSessions(listOf(localMediaController))

                val model by collectLastValue(underTest.mediaOutputModel.filterData())
@@ -175,7 +181,13 @@ class MediaOutputComponentInteractorTest : SysuiTestCase() {
                    assertThat(session.packageName).isEqualTo("local.test.pkg")
                    assertThat(session.canAdjustVolume).isTrue()
                    assertThat(device)
                        .isEqualTo(AudioOutputDevice.BuiltIn("built_in_media", testIcon))
                        .isEqualTo(
                            AudioOutputDevice.Bluetooth(
                                "audio_sharing_device_name",
                                testIcon,
                                cachedBluetoothDevice,
                            )
                        )
                    assertThat(isInAudioSharing).isTrue()
                    assertThat(canOpenAudioSwitcher).isTrue()
                }
@@ -187,7 +199,13 @@ class MediaOutputComponentInteractorTest : SysuiTestCase() {
    fun noMediaOrCall_stateIs_Idle() =
        with(kosmos) {
            testScope.runTest {
                audioSharingRepository.setInAudioSharing(true)
                val cachedBluetoothDevice: CachedBluetoothDevice = mock {
                    on { name }.thenReturn("audio_sharing_device_name")
                }
                with(audioSharingRepository) {
                    setInAudioSharing(true)
                    setPrimaryDevice(cachedBluetoothDevice)
                }

                val model by collectLastValue(underTest.mediaOutputModel.filterData())
                runCurrent()
@@ -195,7 +213,12 @@ class MediaOutputComponentInteractorTest : SysuiTestCase() {
                assertThat(model)
                    .isEqualTo(
                        MediaOutputComponentModel.Idle(
                            device = AudioOutputDevice.BuiltIn("built_in_media", testIcon),
                            device =
                                AudioOutputDevice.Bluetooth(
                                    "audio_sharing_device_name",
                                    testIcon,
                                    cachedBluetoothDevice,
                                ),
                            isInAudioSharing = true,
                            canOpenAudioSwitcher = false,
                        )
@@ -208,7 +231,13 @@ class MediaOutputComponentInteractorTest : SysuiTestCase() {
    fun noMediaOrCall_stateIs_Idle_canOpenAudioSwitcherDuringAudioSharing() =
        with(kosmos) {
            testScope.runTest {
                audioSharingRepository.setInAudioSharing(true)
                val cachedBluetoothDevice: CachedBluetoothDevice = mock {
                    on { name }.thenReturn("audio_sharing_device_name")
                }
                with(audioSharingRepository) {
                    setInAudioSharing(true)
                    setPrimaryDevice(cachedBluetoothDevice)
                }

                val model by collectLastValue(underTest.mediaOutputModel.filterData())
                runCurrent()
@@ -216,7 +245,12 @@ class MediaOutputComponentInteractorTest : SysuiTestCase() {
                assertThat(model)
                    .isEqualTo(
                        MediaOutputComponentModel.Idle(
                            device = AudioOutputDevice.BuiltIn("built_in_media", testIcon),
                            device =
                                AudioOutputDevice.Bluetooth(
                                    "audio_sharing_device_name",
                                    testIcon,
                                    cachedBluetoothDevice,
                                ),
                            isInAudioSharing = true,
                            canOpenAudioSwitcher = true,
                        )
+19 −15
Original line number Diff line number Diff line
@@ -56,6 +56,7 @@ constructor(
    private val bluetoothAdapter: BluetoothAdapter?,
    private val deviceIconInteractor: DeviceIconInteractor,
    private val mediaOutputInteractor: MediaOutputInteractor,
    private val audioSharingInteractor: AudioSharingInteractor,
) {

    val currentAudioDevice: StateFlow<AudioOutputDevice> =
@@ -65,12 +66,18 @@ constructor(
                    audioRepository.communicationDevice.map { communicationDevice ->
                        communicationDevice?.toAudioOutputDevice()
                    }
                } else {
                    audioSharingInteractor.isInAudioSharing.flatMapLatest { inAudioSharing ->
                        if (inAudioSharing) {
                            audioSharingInteractor.primaryDevice.map { it?.toAudioOutputDevice() }
                        } else {
                            mediaOutputInteractor.currentConnectedDevice.map { mediaDevice ->
                                mediaDevice?.toAudioOutputDevice()
                            }
                        }
                    }
                }
            }
            .map { it ?: AudioOutputDevice.Unknown }
            .flowOn(backgroundCoroutineContext)
            .stateIn(scope, SharingStarted.Eagerly, AudioOutputDevice.Unavailable)
@@ -104,6 +111,13 @@ constructor(
        )
    }

    private fun CachedBluetoothDevice.toAudioOutputDevice(): AudioOutputDevice =
        AudioOutputDevice.Bluetooth(
            name = name,
            icon = deviceIconInteractor.loadIcon(this),
            cachedBluetoothDevice = this,
        )

    private fun MediaDevice.toAudioOutputDevice(): AudioOutputDevice {
        return when {
            this is BluetoothMediaDevice ->
@@ -114,20 +128,10 @@ constructor(
                )
            deviceType == MediaDeviceType.TYPE_3POINT5_MM_AUDIO_DEVICE ||
                deviceType == MediaDeviceType.TYPE_USB_C_AUDIO_DEVICE ->
                AudioOutputDevice.Wired(
                    name = name,
                    icon = icon,
                )
                AudioOutputDevice.Wired(name = name, icon = icon)
            deviceType == MediaDeviceType.TYPE_CAST_DEVICE ->
                AudioOutputDevice.Remote(
                    name = name,
                    icon = icon,
                )
            else ->
                AudioOutputDevice.BuiltIn(
                    name = name,
                    icon = icon,
                )
                AudioOutputDevice.Remote(name = name, icon = icon)
            else -> AudioOutputDevice.BuiltIn(name = name, icon = icon)
        }
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -36,5 +36,6 @@ val Kosmos.audioOutputInteractor by
            bluetoothAdapter,
            deviceIconInteractor,
            mediaOutputInteractor,
            audioSharingInteractor,
        )
    }