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

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

Merge "[Audiosharing] Use getBroadcastToUnicastFallbackGroup to get primary" into main

parents 4d4337cf edf4c63b
Loading
Loading
Loading
Loading
+26 −2
Original line number Diff line number Diff line
@@ -1068,18 +1068,42 @@ public class BluetoothUtils {
    /** Get primary device Uri in broadcast. */
    @NonNull
    public static String getPrimaryGroupIdUriForBroadcast() {
        // TODO: once API is stable, deprecate SettingsProvider solution
        return "bluetooth_le_broadcast_fallback_active_group_id";
    }

    /** Get primary device group id in broadcast. */
    /** Get primary device group id in broadcast from SettingsProvider. */
    @WorkerThread
    public static int getPrimaryGroupIdForBroadcast(@NonNull ContentResolver contentResolver) {
        // TODO: once API is stable, deprecate SettingsProvider solution
        return Settings.Secure.getInt(
                contentResolver,
                getPrimaryGroupIdUriForBroadcast(),
                BluetoothCsipSetCoordinator.GROUP_ID_INVALID);
    }

    /**
     * Get primary device group id in broadcast.
     *
     * If Flags.adoptPrimaryGroupManagementApiV2 is enabled, get group id by API,
     * Otherwise, still get value from SettingsProvider.
     */
    @WorkerThread
    public static int getPrimaryGroupIdForBroadcast(@NonNull ContentResolver contentResolver,
            @Nullable LocalBluetoothManager manager) {
        if (Flags.adoptPrimaryGroupManagementApiV2()) {
            LeAudioProfile leaProfile = manager == null ? null :
                    manager.getProfileManager().getLeAudioProfile();
            if (leaProfile == null) {
                Log.d(TAG, "getPrimaryGroupIdForBroadcast: profile is null");
                return BluetoothCsipSetCoordinator.GROUP_ID_INVALID;
            }
            return leaProfile.getBroadcastToUnicastFallbackGroup();
        } else {
            return getPrimaryGroupIdForBroadcast(contentResolver);
        }
    }

    /** Get develop option value for audio sharing preview. */
    @WorkerThread
    public static boolean getAudioSharingPreviewValue(@Nullable ContentResolver contentResolver) {
@@ -1101,7 +1125,7 @@ public class BluetoothUtils {
        LocalBluetoothLeBroadcast broadcast =
                localBtManager.getProfileManager().getLeAudioBroadcastProfile();
        if (broadcast == null || !broadcast.isEnabled(null)) return null;
        int primaryGroupId = getPrimaryGroupIdForBroadcast(contentResolver);
        int primaryGroupId = getPrimaryGroupIdForBroadcast(contentResolver, localBtManager);
        if (primaryGroupId == BluetoothCsipSetCoordinator.GROUP_ID_INVALID) return null;
        LocalBluetoothLeBroadcastAssistant assistant =
                localBtManager.getProfileManager().getLeAudioBroadcastAssistantProfile();
+1 −1
Original line number Diff line number Diff line
@@ -1358,7 +1358,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
            // Gets summary for the buds which are in the audio sharing.
            int groupId = BluetoothUtils.getGroupId(this);
            int primaryGroupId = BluetoothUtils.getPrimaryGroupIdForBroadcast(
                    mContext.getContentResolver());
                    mContext.getContentResolver(), mBluetoothManager);
            if ((primaryGroupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID)
                    ? (groupId == primaryGroupId) : isActiveDevice(BluetoothProfile.LE_AUDIO)) {
                // The buds are primary buds
+49 −3
Original line number Diff line number Diff line
@@ -936,15 +936,60 @@ public class BluetoothUtilsTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void getSecondaryDeviceForBroadcast_adoptAPI_noSecondary_returnNull() {
        when(mBroadcast.isEnabled(any())).thenReturn(true);
        when(mLeAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(1);
        when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice);
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mCachedBluetoothDevice.getGroupId()).thenReturn(1);
        BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class);
        when(mAssistant.getAllSources(mBluetoothDevice)).thenReturn(ImmutableList.of(state));
        when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mBluetoothDevice));

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

    @Test
    @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void getSecondaryDeviceForBroadcast_adoptAPI_returnCorrectDevice() {
        when(mBroadcast.isEnabled(any())).thenReturn(true);
        when(mLeAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(1);
        CachedBluetoothDevice cachedBluetoothDevice = mock(CachedBluetoothDevice.class);
        BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
        when(cachedBluetoothDevice.getDevice()).thenReturn(bluetoothDevice);
        when(cachedBluetoothDevice.getGroupId()).thenReturn(1);
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mCachedBluetoothDevice.getGroupId()).thenReturn(2);
        when(mDeviceManager.findDevice(bluetoothDevice)).thenReturn(cachedBluetoothDevice);
        when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice);
        BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class);
        List<Long> bisSyncState = new ArrayList<>();
        bisSyncState.add(1L);
        when(state.getBisSyncState()).thenReturn(bisSyncState);
        when(mAssistant.getAllSources(any(BluetoothDevice.class)))
                .thenReturn(ImmutableList.of(state));
        when(mAssistant.getAllConnectedDevices())
                .thenReturn(ImmutableList.of(mBluetoothDevice, bluetoothDevice));

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

    @Test
    @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void getSecondaryDeviceForBroadcast_noSecondary_returnNull() {
        Settings.Secure.putInt(
                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);
        when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice);
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mCachedBluetoothDevice.getGroupId()).thenReturn(1);
        BluetoothLeBroadcastReceiveState state = mock(BluetoothLeBroadcastReceiveState.class);
@@ -958,6 +1003,7 @@ public class BluetoothUtilsTest {
    }

    @Test
    @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void getSecondaryDeviceForBroadcast_returnCorrectDevice() {
        Settings.Secure.putInt(
                mContext.getContentResolver(),
+69 −0
Original line number Diff line number Diff line
@@ -42,6 +42,8 @@ import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.hardware.input.InputManager;
import android.media.AudioManager;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;
import android.text.Spannable;
@@ -163,6 +165,7 @@ public class CachedBluetoothDeviceTest {
        when(mHidProfile.getProfileId()).thenReturn(BluetoothProfile.HID_HOST);
        when(mLocalBluetoothManager.getProfileManager()).thenReturn(mProfileManager);
        when(mBroadcast.isEnabled(any())).thenReturn(false);
        when(mProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile);
        when(mProfileManager.getLeAudioBroadcastProfile()).thenReturn(mBroadcast);
        when(mProfileManager.getLeAudioBroadcastAssistantProfile()).thenReturn(mAssistant);
        mCachedDevice = spy(new CachedBluetoothDevice(mContext, mProfileManager, mDevice));
@@ -2004,6 +2007,70 @@ public class CachedBluetoothDeviceTest {
    }

    @Test
    @EnableFlags(com.android.settingslib.flags.Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void getConnectionSummary_adoptAPI_isBroadcastPrimary_fallbackDevice_returnActive() {
        when(mBroadcast.isEnabled(any())).thenReturn(true);
        when(mCachedDevice.getDevice()).thenReturn(mDevice);
        when(mLeAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(1);

        List<Long> bisSyncState = new ArrayList<>();
        bisSyncState.add(1L);
        when(mLeBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState);
        List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>();
        sourceList.add(mLeBroadcastReceiveState);
        when(mAssistant.getAllSources(any())).thenReturn(sourceList);

        when(mCachedDevice.getGroupId()).thenReturn(1);

        assertThat(mCachedDevice.getConnectionSummary(false))
                .isEqualTo(mContext.getString(R.string.bluetooth_active_no_battery_level));
    }

    @Test
    @EnableFlags(com.android.settingslib.flags.Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void getConnectionSummary_adoptAPI_isBroadcastPrimary_activeDevice_returnActive() {
        when(mBroadcast.isEnabled(any())).thenReturn(true);
        when(mCachedDevice.getDevice()).thenReturn(mDevice);
        when(mLeAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(
                BluetoothCsipSetCoordinator.GROUP_ID_INVALID);

        List<Long> bisSyncState = new ArrayList<>();
        bisSyncState.add(1L);
        when(mLeBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState);
        List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>();
        sourceList.add(mLeBroadcastReceiveState);
        when(mAssistant.getAllSources(any())).thenReturn(sourceList);

        when(mCachedDevice.getGroupId()).thenReturn(1);
        when(mCachedDevice.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(true);

        assertThat(mCachedDevice.getConnectionSummary(false))
                .isEqualTo(mContext.getString(R.string.bluetooth_active_no_battery_level));
    }

    @Test
    @EnableFlags(com.android.settingslib.flags.Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void getConnectionSummary_adoptAPI_isBroadcastNotPrimary_returnActiveMedia() {
        when(mBroadcast.isEnabled(any())).thenReturn(true);
        when(mCachedDevice.getDevice()).thenReturn(mDevice);
        when(mLeAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(1);

        List<Long> bisSyncState = new ArrayList<>();
        bisSyncState.add(1L);
        when(mLeBroadcastReceiveState.getBisSyncState()).thenReturn(bisSyncState);
        List<BluetoothLeBroadcastReceiveState> sourceList = new ArrayList<>();
        sourceList.add(mLeBroadcastReceiveState);
        when(mAssistant.getAllSources(any())).thenReturn(sourceList);

        when(mCachedDevice.getGroupId()).thenReturn(BluetoothCsipSetCoordinator.GROUP_ID_INVALID);

        assertThat(mCachedDevice.getConnectionSummary(false))
                .isEqualTo(
                        mContext.getString(R.string.bluetooth_active_media_only_no_battery_level));
    }

    @Test
    @DisableFlags(com.android.settingslib.flags.Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void getConnectionSummary_isBroadcastPrimary_fallbackDevice_returnActive() {
        when(mBroadcast.isEnabled(any())).thenReturn(true);
        when(mCachedDevice.getDevice()).thenReturn(mDevice);
@@ -2026,6 +2093,7 @@ public class CachedBluetoothDeviceTest {
    }

    @Test
    @DisableFlags(com.android.settingslib.flags.Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void getConnectionSummary_isBroadcastPrimary_activeDevice_returnActive() {
        when(mBroadcast.isEnabled(any())).thenReturn(true);
        when(mCachedDevice.getDevice()).thenReturn(mDevice);
@@ -2049,6 +2117,7 @@ public class CachedBluetoothDeviceTest {
    }

    @Test
    @DisableFlags(com.android.settingslib.flags.Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void getConnectionSummary_isBroadcastNotPrimary_returnActiveMedia() {
        when(mBroadcast.isEnabled(any())).thenReturn(true);
        when(mCachedDevice.getDevice()).thenReturn(mDevice);