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

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

Add util function to check headset device

BUG: 343317785
Test: local tested
Flag: EXEMPT add utils function
Change-Id: Ia12759f5715c3ab6672a29dd35936a1ca75a7fe1
parent d6efc540
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -73,6 +73,10 @@ public class BluetoothUtils {
    private static final Set<Integer> SA_PROFILES =
            ImmutableSet.of(
                    BluetoothProfile.A2DP, BluetoothProfile.LE_AUDIO, BluetoothProfile.HEARING_AID);
    private static final List<Integer> BLUETOOTH_DEVICE_CLASS_HEADSET =
            List.of(
                    BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES,
                    BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET);

    private static ErrorListener sErrorListener;

@@ -387,6 +391,19 @@ public class BluetoothUtils {
        return false;
    }

    /** Checks whether the bluetooth device is a headset. */
    public static boolean isHeadset(@NonNull BluetoothDevice bluetoothDevice) {
        String deviceType =
                BluetoothUtils.getStringMetaData(
                        bluetoothDevice, BluetoothDevice.METADATA_DEVICE_TYPE);
        if (!TextUtils.isEmpty(deviceType)) {
            return BluetoothDevice.DEVICE_TYPE_HEADSET.equals(deviceType)
                    || BluetoothDevice.DEVICE_TYPE_UNTETHERED_HEADSET.equals(deviceType);
        }
        BluetoothClass btClass = bluetoothDevice.getBluetoothClass();
        return btClass != null && BLUETOOTH_DEVICE_CLASS_HEADSET.contains(btClass.getDeviceClass());
    }

    /** Create an Icon pointing to a drawable. */
    public static IconCompat createIconWithDrawable(Drawable drawable) {
        Bitmap bitmap;
+35 −1
Original line number Diff line number Diff line
@@ -80,7 +80,9 @@ public class BluetoothUtilsTest {
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private CachedBluetoothDevice mCachedBluetoothDevice;

    @Mock private BluetoothDevice mBluetoothDevice;
    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    private BluetoothDevice mBluetoothDevice;

    @Mock private AudioManager mAudioManager;
    @Mock private PackageManager mPackageManager;
    @Mock private LeAudioProfile mA2dpProfile;
@@ -395,6 +397,38 @@ public class BluetoothUtilsTest {
        assertThat(BluetoothUtils.isAdvancedUntetheredDevice(mBluetoothDevice)).isFalse();
    }

    @Test
    public void isHeadset_metadataMatched_returnTrue() {
        when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_DEVICE_TYPE))
                .thenReturn(BluetoothDevice.DEVICE_TYPE_UNTETHERED_HEADSET.getBytes());

        assertThat(BluetoothUtils.isHeadset(mBluetoothDevice)).isTrue();
    }

    @Test
    public void isHeadset_metadataNotMatched_returnFalse() {
        when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_DEVICE_TYPE))
                .thenReturn(BluetoothDevice.DEVICE_TYPE_CARKIT.getBytes());

        assertThat(BluetoothUtils.isHeadset(mBluetoothDevice)).isFalse();
    }

    @Test
    public void isHeadset_btClassMatched_returnTrue() {
        when(mBluetoothDevice.getBluetoothClass().getDeviceClass())
                .thenReturn(BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES);

        assertThat(BluetoothUtils.isHeadset(mBluetoothDevice)).isTrue();
    }

    @Test
    public void isHeadset_btClassNotMatched_returnFalse() {
        when(mBluetoothDevice.getBluetoothClass().getDeviceClass())
                .thenReturn(BluetoothClass.Device.AUDIO_VIDEO_LOUDSPEAKER);

        assertThat(BluetoothUtils.isHeadset(mBluetoothDevice)).isFalse();
    }

    @Test
    public void isAvailableMediaBluetoothDevice_isConnectedLeAudioDevice_returnTrue() {
        when(mCachedBluetoothDevice.isConnectedLeAudioDevice()).thenReturn(true);