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

Commit ab6c7a8d authored by Yiyi Shen's avatar Yiyi Shen
Browse files

[Audiosharing] Use getBroadcastToUnicastFallbackGroup to get primary

Flag: com.android.settingslib.flags.adopt_primary_group_management_api_v2
Test: atest
Bug: 397568136

Change-Id: I50487a8e5948fec6d8e71e2a0cf499cc7f0cf59d
parent c8d9ab2c
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -207,7 +207,7 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
                                    (AudioSharingDeviceItem item) -> {
                                        int currentCallAudioGroupId =
                                                BluetoothUtils.getPrimaryGroupIdForBroadcast(
                                                        mContext.getContentResolver());
                                                        mContext.getContentResolver(), mBtManager);
                                        int clickedGroupId = item.getGroupId();
                                        if (clickedGroupId == currentCallAudioGroupId) {
                                            Log.d(TAG, "Skip set call audio device: unchanged");
@@ -414,7 +414,8 @@ public class AudioSharingCallAudioPreferenceController extends AudioSharingBaseP
    private Pair<Integer, AudioSharingDeviceItem> getActiveItemWithIndex() {
        List<AudioSharingDeviceItem> deviceItems = new ArrayList<>(mDeviceItemsInSharingSession);
        int fallbackActiveGroupId =
                BluetoothUtils.getPrimaryGroupIdForBroadcast(mContext.getContentResolver());
                BluetoothUtils.getPrimaryGroupIdForBroadcast(mContext.getContentResolver(),
                        mBtManager);
        if (fallbackActiveGroupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID) {
            for (AudioSharingDeviceItem item : deviceItems) {
                if (item.getGroupId() == fallbackActiveGroupId) {
+2 −1
Original line number Diff line number Diff line
@@ -418,7 +418,8 @@ public class AudioSharingDeviceVolumeGroupController extends AudioSharingBasePre
        int groupId = BluetoothUtils.getGroupId(cachedDevice);
        // The fallback device rank first among the audio sharing device list.
        return (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID
                && groupId == BluetoothUtils.getPrimaryGroupIdForBroadcast(mContentResolver))
                && groupId == BluetoothUtils.getPrimaryGroupIdForBroadcast(mContentResolver,
                mBtManager))
                ? 0
                : 1;
    }
+5 −4
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ public class AudioSharingDeviceVolumePreference extends SeekBarPreference {

    private final Context mContext;
    private final CachedBluetoothDevice mCachedDevice;
    @Nullable private final LocalBluetoothManager mBtManager;
    @Nullable protected SeekBar mSeekBar;
    private Boolean mTrackingTouch = false;
    private MetricsFeatureProvider mMetricsFeatureProvider =
@@ -57,6 +58,7 @@ public class AudioSharingDeviceVolumePreference extends SeekBarPreference {
        setLayoutResource(R.layout.preference_volume_slider);
        mContext = context;
        mCachedDevice = device;
        mBtManager = Utils.getLocalBtManager(mContext);
    }

    @NonNull
@@ -110,7 +112,7 @@ public class AudioSharingDeviceVolumePreference extends SeekBarPreference {
                            if (groupId != BluetoothCsipSetCoordinator.GROUP_ID_INVALID
                                    && groupId
                                            == BluetoothUtils.getPrimaryGroupIdForBroadcast(
                                                    mContext.getContentResolver())) {
                                                    mContext.getContentResolver(), mBtManager)) {
                                // Set media stream volume for primary buds, audio manager will
                                // update all buds volume in the audio sharing.
                                setAudioManagerStreamVolume(progress);
@@ -126,9 +128,8 @@ public class AudioSharingDeviceVolumePreference extends SeekBarPreference {
            Log.d(TAG, "Skip set device volume, device is null");
            return;
        }
        LocalBluetoothManager btManager = Utils.getLocalBtManager(mContext);
        VolumeControlProfile vc =
                btManager == null ? null : btManager.getProfileManager().getVolumeControlProfile();
        VolumeControlProfile vc = mBtManager == null ? null
                : mBtManager.getProfileManager().getVolumeControlProfile();
        if (vc != null) {
            vc.setDeviceVolume(device, progress, /* isGroupOp= */ true);
            mMetricsFeatureProvider.action(
+266 −28

File changed.

Preview size limit exceeded, changes collapsed.

+57 −12
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
@@ -42,6 +43,8 @@ import android.content.Context;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.os.Looper;
import android.platform.test.annotations.DisableFlags;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;
import android.provider.Settings;

@@ -60,6 +63,7 @@ import com.android.settings.testutils.shadow.ShadowThreadUtils;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LeAudioProfile;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcast;
import com.android.settingslib.bluetooth.LocalBluetoothLeBroadcastAssistant;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -196,8 +200,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStart_flagOff_doNothing() {
        mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.onStart(mLifecycleOwner);
        verify(mAssistant, never())
                .registerServiceCallBack(
@@ -214,8 +218,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStart_flagOn_registerCallbacks() {
        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.onStart(mLifecycleOwner);
        verify(mAssistant)
                .registerServiceCallBack(
@@ -229,8 +233,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onAudioSharingProfilesConnected_flagOn_registerCallbacks() {
        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.onAudioSharingProfilesConnected();
        verify(mAssistant)
                .registerServiceCallBack(
@@ -247,8 +251,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStop_flagOff_doNothing() {
        mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.onStop(mLifecycleOwner);
        verify(mAssistant, never())
                .unregisterServiceCallBack(any(BluetoothLeBroadcastAssistant.Callback.class));
@@ -259,8 +263,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStop_flagOn_callbacksNotRegistered_doNothing() {
        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.setCallbacksRegistered(false);
        mController.onStop(mLifecycleOwner);
        verify(mAssistant, never())
@@ -272,8 +276,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void onStop_flagOn_callbacksRegistered_unregisterCallbacks() {
        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.setCallbacksRegistered(true);
        mController.onStop(mLifecycleOwner);
        verify(mAssistant)
@@ -284,16 +288,16 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void displayPreference_flagOff_doNothing() {
        mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.displayPreference(mScreen);
        assertThat(mPreferenceGroup.isVisible()).isFalse();
        verify(mDeviceUpdater, never()).forceUpdate();
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void displayPreference_flagOn_updateDeviceList() {
        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.displayPreference(mScreen);
        assertThat(mPreferenceGroup.isVisible()).isFalse();
        verify(mDeviceUpdater).forceUpdate();
@@ -316,6 +320,24 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void onDeviceAdded_adoptApi_rankFallbackDeviceOnTop() {
        LeAudioProfile leAudioProfile = mock(LeAudioProfile.class);
        when(leAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID2);
        when(mProfileManager.getLeAudioProfile()).thenReturn(leAudioProfile);
        when(mPreference1.getProgress()).thenReturn(TEST_VOLUME_VALUE);
        when(mPreference2.getProgress()).thenReturn(TEST_VOLUME_VALUE);
        mController.setPreferenceGroup(mPreferenceGroup);
        mController.onDeviceAdded(mPreference1);
        mController.onDeviceAdded(mPreference2);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mPreference1).setOrder(1);
        verify(mPreference2).setOrder(0);
    }

    @Test
    @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void onDeviceAdded_rankFallbackDeviceOnTop() {
        Settings.Secure.putInt(
                mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(),
@@ -374,8 +396,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void updateVisibility_emptyPreferenceGroup_doNothing() {
        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.setCallbacksRegistered(true);
        mController.updateVisibility();
        shadowOf(Looper.getMainLooper()).idle();
@@ -384,8 +406,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @DisableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void updateVisibility_flagOff_setVisibleToFalse() {
        mSetFlagsRule.disableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.setCallbacksRegistered(true);
        mPreferenceGroup.addPreference(mPreference1);
        when(mBroadcast.isEnabled(null)).thenReturn(true);
@@ -399,8 +421,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void updateVisibility_notEmptyPreferenceGroup_noSharing_setVisibleToFalse() {
        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.setCallbacksRegistered(true);
        mPreferenceGroup.addPreference(mPreference1);
        when(mBroadcast.isEnabled(null)).thenReturn(false);
@@ -414,8 +436,8 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING)
    public void updateVisibility_notEmptyPreferenceGroup_isSharing_setVisibleToTrue() {
        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        mController.setCallbacksRegistered(true);
        mPreferenceGroup.addPreference(mPreference1);
        when(mBroadcast.isEnabled(null)).thenReturn(true);
@@ -429,6 +451,29 @@ public class AudioSharingDeviceVolumeGroupControllerTest {
    }

    @Test
    @EnableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void settingsObserverOnChange_adoptApi_updatePreferenceOrder() {
        LeAudioProfile leAudioProfile = mock(LeAudioProfile.class);
        when(leAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID2);
        when(mProfileManager.getLeAudioProfile()).thenReturn(leAudioProfile);
        when(mPreference1.getProgress()).thenReturn(TEST_VOLUME_VALUE);
        when(mPreference2.getProgress()).thenReturn(TEST_VOLUME_VALUE);
        mController.setPreferenceGroup(mPreferenceGroup);
        mController.onDeviceAdded(mPreference1);
        mController.onDeviceAdded(mPreference2);
        shadowOf(Looper.getMainLooper()).idle();


        when(leAudioProfile.getBroadcastToUnicastFallbackGroup()).thenReturn(TEST_DEVICE_GROUP_ID1);
        mContentObserver.onChange(true);
        shadowOf(Looper.getMainLooper()).idle();

        verify(mPreference1).setOrder(0);
        verify(mPreference2).setOrder(1);
    }

    @Test
    @DisableFlags(Flags.FLAG_ADOPT_PRIMARY_GROUP_MANAGEMENT_API_V2)
    public void settingsObserverOnChange_updatePreferenceOrder() {
        Settings.Secure.putInt(
                mContentResolver, BluetoothUtils.getPrimaryGroupIdUriForBroadcast(),
Loading