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

Commit 4c8d25bd authored by Yiyi Shen's avatar Yiyi Shen
Browse files

[Audiosharing] Add util to check sink has local source

Test: atest
Flag: com.android.settingslib.flags.enable_le_audio_sharing
Bug: 362714470
Bug: 355222285
Change-Id: Ied7cbba23cf26f16a66f133f860c72d0e002f602
parent 9dc301d3
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
package com.android.settingslib.bluetooth;

import static com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast.UNKNOWN_VALUE_PLACEHOLDER;
import static com.android.settingslib.widget.AdaptiveOutlineDrawable.ICON_TYPE_ADVANCED;

import android.annotation.SuppressLint;
@@ -704,12 +705,50 @@ public class BluetoothUtils {
        return !sourceList.isEmpty() && sourceList.stream().anyMatch(BluetoothUtils::isConnected);
    }

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

    /** Checks the connectivity status based on the provided broadcast receive state. */
    @WorkerThread
    public static boolean isConnected(BluetoothLeBroadcastReceiveState state) {
        return state.getBisSyncState().stream().anyMatch(bitmap -> bitmap != 0);
    }

    /** Checks if the broadcast id is matched based on the provided broadcast receive state. */
    @WorkerThread
    public static boolean isSourceMatched(
            @Nullable BluetoothLeBroadcastReceiveState state, int broadcastId) {
        return state != null && state.getBroadcastId() == broadcastId;
    }

    /**
     * Checks if the Bluetooth device is an available hearing device, which means: 1) currently
     * connected 2) is Hearing Aid 3) connected profile match hearing aid related profiles (e.g.
+1 −1
Original line number Diff line number Diff line
@@ -101,7 +101,7 @@ public class LocalBluetoothLeBroadcast implements LocalBluetoothProfile {
    private static final int DEFAULT_CODE_MIN = 1000;
    // Order of this profile in device profiles list
    private static final int ORDINAL = 1;
    private static final int UNKNOWN_VALUE_PLACEHOLDER = -1;
    static final int UNKNOWN_VALUE_PLACEHOLDER = -1;
    private static final Uri[] SETTINGS_URIS =
            new Uri[] {
                Settings.Secure.getUriFor(Settings.Secure.BLUETOOTH_LE_BROADCAST_NAME),
+30 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
package com.android.settingslib.bluetooth;

import static com.android.settingslib.bluetooth.BluetoothUtils.isAvailableAudioSharingMediaBluetoothDevice;
import static com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast.UNKNOWN_VALUE_PLACEHOLDER;
import static com.android.settingslib.flags.Flags.FLAG_ENABLE_DETERMINING_ADVANCED_DETAILS_HEADER_WITH_METADATA;

import static com.google.common.truth.Truth.assertThat;
@@ -96,6 +97,7 @@ public class BluetoothUtilsTest {
                    + "</HEARABLE_CONTROL_SLICE_WITH_WIDTH>";
    private static final String TEST_EXCLUSIVE_MANAGER_PACKAGE = "com.test.manager";
    private static final String TEST_EXCLUSIVE_MANAGER_COMPONENT = "com.test.manager/.component";
    private static final int TEST_BROADCAST_ID = 25;

    @Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

@@ -670,6 +672,34 @@ public class BluetoothUtilsTest {
                .isFalse();
    }

    @Test
    public void testHasActiveLocalBroadcastSourceForBtDevice_hasActiveLocalSource() {
        when(mBroadcast.getLatestBroadcastId()).thenReturn(TEST_BROADCAST_ID);
        when(mLeBroadcastReceiveState.getBroadcastId()).thenReturn(TEST_BROADCAST_ID);
        List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>();
        sourceList.add(mLeBroadcastReceiveState);
        when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(sourceList);

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

    @Test
    public void testHasActiveLocalBroadcastSourceForBtDevice_noActiveLocalSource() {
        when(mLeBroadcastReceiveState.getBroadcastId()).thenReturn(UNKNOWN_VALUE_PLACEHOLDER);
        List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>();
        sourceList.add(mLeBroadcastReceiveState);
        when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(sourceList);

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


    @Test
    public void isAvailableHearingDevice_isConnectedHearingAid_returnTure() {
        when(mCachedBluetoothDevice.isConnectedHearingAidDevice()).thenReturn(true);