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

Commit cf708e26 authored by Yiyi Shen's avatar Yiyi Shen Committed by Android (Google) Code Review
Browse files

Merge "[Audiosharing] Add util to check if BluetoothDevice has connected source" into main

parents 664ed482 785863f1
Loading
Loading
Loading
Loading
+32 −20
Original line number Diff line number Diff line
@@ -578,7 +578,7 @@ public class BluetoothUtils {
    }

    /**
     * Check if {@link CachedBluetoothDevice} has connected to a broadcast source.
     * Check if {@link CachedBluetoothDevice} (lead or member) has connected to a broadcast source.
     *
     * @param cachedDevice The cached bluetooth device to check.
     * @param localBtManager The BT manager to provide BT functions.
@@ -586,20 +586,10 @@ public class BluetoothUtils {
     */
    @WorkerThread
    public static boolean hasConnectedBroadcastSource(
            CachedBluetoothDevice cachedDevice, LocalBluetoothManager localBtManager) {
        if (localBtManager == null) {
            Log.d(TAG, "Skip check hasConnectedBroadcastSource due to bt manager is null");
            return false;
        }
        LocalBluetoothLeBroadcastAssistant assistant =
                localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
        if (assistant == null) {
            Log.d(TAG, "Skip check hasConnectedBroadcastSource due to assistant profile is null");
            return false;
        }
        List<BluetoothLeBroadcastReceiveState> sourceList =
                assistant.getAllSources(cachedDevice.getDevice());
        if (!sourceList.isEmpty() && sourceList.stream().anyMatch(BluetoothUtils::isConnected)) {
            @Nullable CachedBluetoothDevice cachedDevice,
            @Nullable LocalBluetoothManager localBtManager) {
        if (cachedDevice == null) return false;
        if (hasConnectedBroadcastSourceForBtDevice(cachedDevice.getDevice(), localBtManager)) {
            Log.d(
                    TAG,
                    "Lead device has connected broadcast source, device = "
@@ -608,9 +598,7 @@ public class BluetoothUtils {
        }
        // Return true if member device is in broadcast.
        for (CachedBluetoothDevice device : cachedDevice.getMemberDevice()) {
            List<BluetoothLeBroadcastReceiveState> list =
                    assistant.getAllSources(device.getDevice());
            if (!list.isEmpty() && list.stream().anyMatch(BluetoothUtils::isConnected)) {
            if (hasConnectedBroadcastSourceForBtDevice(device.getDevice(), localBtManager)) {
                Log.d(
                        TAG,
                        "Member device has connected broadcast source, device = "
@@ -621,6 +609,28 @@ public class BluetoothUtils {
        return false;
    }

    /**
     * Check if {@link BluetoothDevice} has connected to a broadcast source.
     *
     * @param device The bluetooth device to check.
     * @param localBtManager The BT manager to provide BT functions.
     * @return Whether the device has connected to a broadcast source.
     */
    @WorkerThread
    public static boolean hasConnectedBroadcastSourceForBtDevice(
            @Nullable BluetoothDevice device, @Nullable LocalBluetoothManager localBtManager) {
        LocalBluetoothLeBroadcastAssistant assistant =
                localBtManager == null
                        ? null
                        : localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
        if (device == null || assistant == null) {
            Log.d(TAG, "Skip check hasConnectedBroadcastSourceForBtDevice due to arg is null");
            return false;
        }
        List<BluetoothLeBroadcastReceiveState> sourceList = assistant.getAllSources(device);
        return !sourceList.isEmpty() && sourceList.stream().anyMatch(BluetoothUtils::isConnected);
    }

    /** Checks the connectivity status based on the provided broadcast receive state. */
    @WorkerThread
    public static boolean isConnected(BluetoothLeBroadcastReceiveState state) {
@@ -805,8 +815,10 @@ public class BluetoothUtils {

        ComponentName exclusiveManagerComponent =
                ComponentName.unflattenFromString(exclusiveManagerName);
        String exclusiveManagerPackage = exclusiveManagerComponent != null
                ? exclusiveManagerComponent.getPackageName() : exclusiveManagerName;
        String exclusiveManagerPackage =
                exclusiveManagerComponent != null
                        ? exclusiveManagerComponent.getPackageName()
                        : exclusiveManagerName;

        if (!isPackageInstalledAndEnabled(context, exclusiveManagerPackage)) {
            return false;
+106 −9
Original line number Diff line number Diff line
@@ -56,7 +56,10 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RunWith(RobolectricTestRunner.class)
public class BluetoothUtilsTest {
@@ -557,8 +560,14 @@ public class BluetoothUtilsTest {
    }

    @Test
    public void testHasConnectedBroadcastSource_deviceConnectedToBroadcastSource() {
    public void testHasConnectedBroadcastSource_leadDeviceConnectedToBroadcastSource() {
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        CachedBluetoothDevice memberCachedDevice = mock(CachedBluetoothDevice.class);
        BluetoothDevice memberDevice = mock(BluetoothDevice.class);
        when(memberCachedDevice.getDevice()).thenReturn(memberDevice);
        Set<CachedBluetoothDevice> memberCachedDevices = new HashSet<>();
        memberCachedDevices.add(memberCachedDevice);
        when(mCachedBluetoothDevice.getMemberDevice()).thenReturn(memberCachedDevices);

        List<Long> bisSyncState = new ArrayList<>();
        bisSyncState.add(1L);
@@ -566,7 +575,8 @@ public class BluetoothUtilsTest {

        List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>();
        sourceList.add(mLeBroadcastReceiveState);
        when(mAssistant.getAllSources(any())).thenReturn(sourceList);
        when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(sourceList);
        when(mAssistant.getAllSources(memberDevice)).thenReturn(Collections.emptyList());

        assertThat(
                        BluetoothUtils.hasConnectedBroadcastSource(
@@ -574,6 +584,79 @@ public class BluetoothUtilsTest {
                .isTrue();
    }

    @Test
    public void testHasConnectedBroadcastSource_memberDeviceConnectedToBroadcastSource() {
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        CachedBluetoothDevice memberCachedDevice = mock(CachedBluetoothDevice.class);
        BluetoothDevice memberDevice = mock(BluetoothDevice.class);
        when(memberCachedDevice.getDevice()).thenReturn(memberDevice);
        Set<CachedBluetoothDevice> memberCachedDevices = new HashSet<>();
        memberCachedDevices.add(memberCachedDevice);
        when(mCachedBluetoothDevice.getMemberDevice()).thenReturn(memberCachedDevices);

        List<Long> bisSyncState = new ArrayList<>();
        bisSyncState.add(1L);
        when(mLeBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState);

        List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>();
        sourceList.add(mLeBroadcastReceiveState);
        when(mAssistant.getAllSources(memberDevice)).thenReturn(sourceList);
        when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(Collections.emptyList());

        assertThat(
                        BluetoothUtils.hasConnectedBroadcastSource(
                                mCachedBluetoothDevice, mLocalBluetoothManager))
                .isTrue();
    }

    @Test
    public void testHasConnectedBroadcastSource_deviceNotConnectedToBroadcastSource() {
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);

        List<Long> bisSyncState = new ArrayList<>();
        when(mLeBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState);

        List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>();
        sourceList.add(mLeBroadcastReceiveState);
        when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(sourceList);

        assertThat(
                        BluetoothUtils.hasConnectedBroadcastSource(
                                mCachedBluetoothDevice, mLocalBluetoothManager))
                .isFalse();
    }

    @Test
    public void testHasConnectedBroadcastSourceForBtDevice_deviceConnectedToBroadcastSource() {
        List<Long> bisSyncState = new ArrayList<>();
        bisSyncState.add(1L);
        when(mLeBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState);

        List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>();
        sourceList.add(mLeBroadcastReceiveState);
        when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(sourceList);

        assertThat(
                        BluetoothUtils.hasConnectedBroadcastSourceForBtDevice(
                                mBluetoothDevice, mLocalBluetoothManager))
                .isTrue();
    }

    @Test
    public void testHasConnectedBroadcastSourceForBtDevice_deviceNotConnectedToBroadcastSource() {
        List<Long> bisSyncState = new ArrayList<>();
        when(mLeBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState);

        List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>();
        sourceList.add(mLeBroadcastReceiveState);
        when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(sourceList);

        assertThat(
                        BluetoothUtils.hasConnectedBroadcastSourceForBtDevice(
                                mBluetoothDevice, mLocalBluetoothManager))
                .isFalse();
    }

    @Test
    public void isAvailableHearingDevice_isConnectedHearingAid_returnTure() {
        when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true);
@@ -604,9 +687,19 @@ public class BluetoothUtilsTest {
    }

    @Test
    public void getSecondaryDeviceForBroadcast_errorState_returnNull() {
        assertThat(BluetoothUtils.getSecondaryDeviceForBroadcast(mContext.getContentResolver(),
                mLocalBluetoothManager))
    public void getSecondaryDeviceForBroadcast_noBroadcast_returnNull() {
        assertThat(
                        BluetoothUtils.getSecondaryDeviceForBroadcast(
                                mContext.getContentResolver(), mLocalBluetoothManager))
                .isNull();
    }

    @Test
    public void getSecondaryDeviceForBroadcast_nullProfile_returnNull() {
        when(mProfileManager.getLeAudioBroadcastProfile()).thenReturn(null);
        assertThat(
                        BluetoothUtils.getSecondaryDeviceForBroadcast(
                                mContext.getContentResolver(), mLocalBluetoothManager))
                .isNull();
    }

@@ -616,6 +709,7 @@ public class BluetoothUtilsTest {
                mContext.getContentResolver(),
                BluetoothUtils.getPrimaryGroupIdUriForBroadcast(),
                1);
        when(mBroadcast.isEnabled(any())).thenReturn(true);
        CachedBluetoothDeviceManager deviceManager = mock(CachedBluetoothDeviceManager.class);
        when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(deviceManager);
        when(deviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice);
@@ -625,8 +719,9 @@ public class BluetoothUtilsTest {
        when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(ImmutableList.of(state));
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mBluetoothDevice));

        assertThat(BluetoothUtils.getSecondaryDeviceForBroadcast(mContext.getContentResolver(),
                mLocalBluetoothManager))
        assertThat(
                        BluetoothUtils.getSecondaryDeviceForBroadcast(
                                mContext.getContentResolver(), mLocalBluetoothManager))
                .isNull();
    }

@@ -636,6 +731,7 @@ public class BluetoothUtilsTest {
                mContext.getContentResolver(),
                BluetoothUtils.getPrimaryGroupIdUriForBroadcast(),
                1);
        when(mBroadcast.isEnabled(any())).thenReturn(true);
        CachedBluetoothDeviceManager deviceManager = mock(CachedBluetoothDeviceManager.class);
        when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(deviceManager);
        CachedBluetoothDevice cachedBluetoothDevice = mock(CachedBluetoothDevice.class);
@@ -655,8 +751,9 @@ public class BluetoothUtilsTest {
        when(mAssistant.getAllConnectedDevices())
                .thenReturn(ImmutableList.of(mBluetoothDevice, bluetoothDevice));

        assertThat(BluetoothUtils.getSecondaryDeviceForBroadcast(mContext.getContentResolver(),
                mLocalBluetoothManager))
        assertThat(
                        BluetoothUtils.getSecondaryDeviceForBroadcast(
                                mContext.getContentResolver(), mLocalBluetoothManager))
                .isEqualTo(mCachedBluetoothDevice);
    }
}