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

Commit 9a36f80b authored by hughchen's avatar hughchen
Browse files

Extend the MediaDeviceType to support more media device

Bug: 152633051
Test: make -j42 RunSettingsLibRoboTests
Change-Id: I87b233b25a4ca3758a26c712629b38532e54267a
parent 0237b4a5
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ public class BluetoothMediaDevice extends MediaDevice {

    BluetoothMediaDevice(Context context, CachedBluetoothDevice device,
            MediaRouter2Manager routerManager, MediaRoute2Info info, String packageName) {
        super(context, MediaDeviceType.TYPE_BLUETOOTH_DEVICE, routerManager, info, packageName);
        super(context, routerManager, info, packageName);
        mCachedDevice = device;
        initDeviceRecord();
    }
+1 −1
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ public class InfoMediaDevice extends MediaDevice {

    InfoMediaDevice(Context context, MediaRouter2Manager routerManager, MediaRoute2Info info,
            String packageName) {
        super(context, MediaDeviceType.TYPE_CAST_DEVICE, routerManager, info, packageName);
        super(context, routerManager, info, packageName);
        initDeviceRecord();
    }

+55 −7
Original line number Diff line number Diff line
@@ -15,6 +15,16 @@
 */
package com.android.settingslib.media;

import static android.media.MediaRoute2Info.TYPE_BLUETOOTH_A2DP;
import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER;
import static android.media.MediaRoute2Info.TYPE_GROUP;
import static android.media.MediaRoute2Info.TYPE_HEARING_AID;
import static android.media.MediaRoute2Info.TYPE_REMOTE_SPEAKER;
import static android.media.MediaRoute2Info.TYPE_REMOTE_TV;
import static android.media.MediaRoute2Info.TYPE_UNKNOWN;
import static android.media.MediaRoute2Info.TYPE_WIRED_HEADPHONES;
import static android.media.MediaRoute2Info.TYPE_WIRED_HEADSET;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
@@ -38,13 +48,21 @@ public abstract class MediaDevice implements Comparable<MediaDevice> {
    private static final String TAG = "MediaDevice";

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({MediaDeviceType.TYPE_CAST_DEVICE,
    @IntDef({MediaDeviceType.TYPE_USB_C_AUDIO_DEVICE,
            MediaDeviceType.TYPE_3POINT5_MM_AUDIO_DEVICE,
            MediaDeviceType.TYPE_FAST_PAIR_BLUETOOTH_DEVICE,
            MediaDeviceType.TYPE_BLUETOOTH_DEVICE,
            MediaDeviceType.TYPE_CAST_DEVICE,
            MediaDeviceType.TYPE_CAST_GROUP_DEVICE,
            MediaDeviceType.TYPE_PHONE_DEVICE})
    public @interface MediaDeviceType {
        int TYPE_PHONE_DEVICE = 1;
        int TYPE_CAST_DEVICE = 2;
        int TYPE_BLUETOOTH_DEVICE = 3;
        int TYPE_USB_C_AUDIO_DEVICE = 1;
        int TYPE_3POINT5_MM_AUDIO_DEVICE = 2;
        int TYPE_FAST_PAIR_BLUETOOTH_DEVICE = 3;
        int TYPE_BLUETOOTH_DEVICE = 4;
        int TYPE_CAST_DEVICE = 5;
        int TYPE_CAST_GROUP_DEVICE = 6;
        int TYPE_PHONE_DEVICE = 7;
    }

    @VisibleForTesting
@@ -58,13 +76,43 @@ public abstract class MediaDevice implements Comparable<MediaDevice> {
    protected final MediaRouter2Manager mRouterManager;
    protected final String mPackageName;

    MediaDevice(Context context, @MediaDeviceType int type, MediaRouter2Manager routerManager,
            MediaRoute2Info info, String packageName) {
        mType = type;
    MediaDevice(Context context, MediaRouter2Manager routerManager, MediaRoute2Info info,
            String packageName) {
        mContext = context;
        mRouteInfo = info;
        mRouterManager = routerManager;
        mPackageName = packageName;
        setType(info);
    }

    private void setType(MediaRoute2Info info) {
        if (info == null) {
            mType = MediaDeviceType.TYPE_BLUETOOTH_DEVICE;
            return;
        }

        switch (info.getType()) {
            case TYPE_GROUP:
                mType = MediaDeviceType.TYPE_CAST_GROUP_DEVICE;
                break;
            case TYPE_BUILTIN_SPEAKER:
                mType = MediaDeviceType.TYPE_PHONE_DEVICE;
                break;
            case TYPE_WIRED_HEADSET:
            case TYPE_WIRED_HEADPHONES:
                mType = MediaDeviceType.TYPE_3POINT5_MM_AUDIO_DEVICE;
                break;
            case TYPE_HEARING_AID:
            case TYPE_BLUETOOTH_A2DP:
                mType = MediaDeviceType.TYPE_BLUETOOTH_DEVICE;
                break;
            case TYPE_UNKNOWN:
            case TYPE_REMOTE_TV:
            case TYPE_REMOTE_SPEAKER:
            default:
                mType = MediaDeviceType.TYPE_CAST_DEVICE;
                break;
        }
    }

    void initDeviceRecord() {
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ public class PhoneMediaDevice extends MediaDevice {

    PhoneMediaDevice(Context context, MediaRouter2Manager routerManager, MediaRoute2Info info,
            String packageName) {
        super(context, MediaDeviceType.TYPE_PHONE_DEVICE, routerManager, info, packageName);
        super(context, routerManager, info, packageName);

        initDeviceRecord();
    }
+22 −11
Original line number Diff line number Diff line
@@ -15,6 +15,10 @@
 */
package com.android.settingslib.media;

import static android.media.MediaRoute2Info.TYPE_BLUETOOTH_A2DP;
import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER;
import static android.media.MediaRoute2Info.TYPE_REMOTE_SPEAKER;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.verify;
@@ -144,12 +148,19 @@ public class MediaDeviceTest {
        when(mCachedDevice2.isConnected()).thenReturn(true);
        when(mCachedDevice3.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
        when(mCachedDevice3.isConnected()).thenReturn(true);
        when(mBluetoothRouteInfo1.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
        when(mBluetoothRouteInfo2.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
        when(mBluetoothRouteInfo3.getType()).thenReturn(TYPE_BLUETOOTH_A2DP);
        when(mRouteInfo1.getId()).thenReturn(ROUTER_ID_1);
        when(mRouteInfo2.getId()).thenReturn(ROUTER_ID_2);
        when(mRouteInfo3.getId()).thenReturn(ROUTER_ID_3);
        when(mRouteInfo1.getName()).thenReturn(DEVICE_NAME_1);
        when(mRouteInfo2.getName()).thenReturn(DEVICE_NAME_2);
        when(mRouteInfo3.getName()).thenReturn(DEVICE_NAME_3);
        when(mRouteInfo1.getType()).thenReturn(TYPE_REMOTE_SPEAKER);
        when(mRouteInfo2.getType()).thenReturn(TYPE_REMOTE_SPEAKER);
        when(mRouteInfo3.getType()).thenReturn(TYPE_REMOTE_SPEAKER);
        when(mPhoneRouteInfo.getType()).thenReturn(TYPE_BUILTIN_SPEAKER);
        when(mLocalBluetoothManager.getProfileManager()).thenReturn(mProfileManager);
        when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
        when(mProfileManager.getHearingAidProfile()).thenReturn(mHapProfile);
@@ -271,12 +282,12 @@ public class MediaDeviceTest {

    @Test
    public void compareTo_info_bluetooth_infoFirst() {
        mMediaDevices.add(mBluetoothMediaDevice1);
        mMediaDevices.add(mInfoMediaDevice1);
        mMediaDevices.add(mBluetoothMediaDevice1);

        assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
        Collections.sort(mMediaDevices, COMPARATOR);
        assertThat(mMediaDevices.get(0)).isEqualTo(mInfoMediaDevice1);
        Collections.sort(mMediaDevices, COMPARATOR);
        assertThat(mMediaDevices.get(0)).isEqualTo(mBluetoothMediaDevice1);
    }

    @Test
@@ -327,7 +338,7 @@ public class MediaDeviceTest {
    // 5.mBluetoothMediaDevice2: * 2 times usage
    // 6.mBluetoothMediaDevice3: * 1 time usage
    // 7.mPhoneMediaDevice:      * 0 time usage
    // Order: 7 -> 2 -> 1 -> 3 -> 5 -> 4 -> 6
    // Order: 7 -> 2 -> 1 -> 5 -> 3 -> 6 -> 4
    @Test
    public void compareTo_mixedDevices_carKitFirst() {
        when(mDevice1.getBluetoothClass()).thenReturn(mCarkitClass);
@@ -352,10 +363,10 @@ public class MediaDeviceTest {
        assertThat(mMediaDevices.get(0)).isEqualTo(mPhoneMediaDevice);
        assertThat(mMediaDevices.get(1)).isEqualTo(mBluetoothMediaDevice1);
        assertThat(mMediaDevices.get(2)).isEqualTo(mInfoMediaDevice1);
        assertThat(mMediaDevices.get(3)).isEqualTo(mInfoMediaDevice2);
        assertThat(mMediaDevices.get(4)).isEqualTo(mBluetoothMediaDevice2);
        assertThat(mMediaDevices.get(5)).isEqualTo(mInfoMediaDevice3);
        assertThat(mMediaDevices.get(6)).isEqualTo(mBluetoothMediaDevice3);
        assertThat(mMediaDevices.get(3)).isEqualTo(mBluetoothMediaDevice2);
        assertThat(mMediaDevices.get(4)).isEqualTo(mInfoMediaDevice2);
        assertThat(mMediaDevices.get(5)).isEqualTo(mBluetoothMediaDevice3);
        assertThat(mMediaDevices.get(6)).isEqualTo(mInfoMediaDevice3);
    }

    // 1.mInfoMediaDevice1:      Last Selected device
@@ -365,7 +376,7 @@ public class MediaDeviceTest {
    // 5.mBluetoothMediaDevice2: * 4 times usage not connected
    // 6.mBluetoothMediaDevice3: * 1 time usage
    // 7.mPhoneMediaDevice:      * 0 time usage
    // Order: 7 -> 1 -> 3 -> 4 -> 6 -> 2 -> 5
    // Order: 7 -> 1 -> 3 -> 6 -> 4  -> 2 -> 5
    @Test
    public void compareTo_mixedDevices_connectDeviceFirst() {
        when(mDevice1.getBluetoothClass()).thenReturn(mCarkitClass);
@@ -394,8 +405,8 @@ public class MediaDeviceTest {
        assertThat(mMediaDevices.get(0)).isEqualTo(mPhoneMediaDevice);
        assertThat(mMediaDevices.get(1)).isEqualTo(mInfoMediaDevice1);
        assertThat(mMediaDevices.get(2)).isEqualTo(mInfoMediaDevice2);
        assertThat(mMediaDevices.get(3)).isEqualTo(mInfoMediaDevice3);
        assertThat(mMediaDevices.get(4)).isEqualTo(mBluetoothMediaDevice3);
        assertThat(mMediaDevices.get(3)).isEqualTo(mBluetoothMediaDevice3);
        assertThat(mMediaDevices.get(4)).isEqualTo(mInfoMediaDevice3);
        assertThat(mMediaDevices.get(5)).isEqualTo(mBluetoothMediaDevice1);
        assertThat(mMediaDevices.get(6)).isEqualTo(mBluetoothMediaDevice2);
    }