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

Commit 6713bf76 authored by Haijie Hong's avatar Haijie Hong Committed by Android (Google) Code Review
Browse files

Merge "Add util function to check headset device" into main

parents 507bb3be ae216160
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 final String TEMP_BOND_TYPE = "TEMP_BOND_TYPE";
    private static final String TEMP_BOND_DEVICE_METADATA_VALUE = "le_audio_sharing";
@@ -390,6 +394,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;
@@ -398,6 +400,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);