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

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

Merge "[Audiosharing] Skip sync source for work profile" into main

parents 3d191ef3 c8fa642d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ public class CachedBluetoothDeviceManager {
        mBtManager = localBtManager;
        mHearingAidDeviceManager = new HearingAidDeviceManager(context, localBtManager,
                mCachedDevices);
        mCsipDeviceManager = new CsipDeviceManager(localBtManager, mCachedDevices);
        mCsipDeviceManager = new CsipDeviceManager(context, localBtManager, mCachedDevices);
    }

    public synchronized Collection<CachedBluetoothDevice> getCachedDevicesCopy() {
+15 −2
Original line number Diff line number Diff line
@@ -21,8 +21,10 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothLeBroadcastMetadata;
import android.bluetooth.BluetoothProfile;
import android.bluetooth.BluetoothUuid;
import android.content.Context;
import android.os.Build;
import android.os.ParcelUuid;
import android.os.UserManager;
import android.util.Log;

import androidx.annotation.ChecksSdkIntAtLeast;
@@ -45,9 +47,11 @@ public class CsipDeviceManager {

    private final LocalBluetoothManager mBtManager;
    private final List<CachedBluetoothDevice> mCachedDevices;
    private final Context mContext;

    CsipDeviceManager(LocalBluetoothManager localBtManager,
    CsipDeviceManager(Context context, LocalBluetoothManager localBtManager,
            List<CachedBluetoothDevice> cachedDevices) {
        mContext = context;
        mBtManager = localBtManager;
        mCachedDevices = cachedDevices;
    }
@@ -379,8 +383,12 @@ public class CsipDeviceManager {
                preferredMainDevice.refresh();
                hasChanged = true;
            }
            if (isWorkProfile()) {
                log("addMemberDevicesIntoMainDevice: skip sync source for work profile");
            } else {
                syncAudioSharingSourceIfNeeded(preferredMainDevice);
            }
        }
        if (hasChanged) {
            log("addMemberDevicesIntoMainDevice: After changed, CachedBluetoothDevice list: "
                    + mCachedDevices);
@@ -388,6 +396,11 @@ public class CsipDeviceManager {
        return hasChanged;
    }

    private boolean isWorkProfile() {
        UserManager userManager = mContext.getSystemService(UserManager.class);
        return userManager != null && userManager.isManagedProfile();
    }

    private void syncAudioSharingSourceIfNeeded(CachedBluetoothDevice mainDevice) {
        boolean isAudioSharingEnabled = BluetoothUtils.isAudioSharingEnabled();
        if (isAudioSharingEnabled) {
+35 −1
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ import android.bluetooth.BluetoothStatusCodes;
import android.content.Context;
import android.os.Looper;
import android.os.Parcel;
import android.os.UserManager;
import android.platform.test.flag.junit.SetFlagsRule;

import com.android.settingslib.flags.Flags;
@@ -104,6 +105,8 @@ public class CsipDeviceManagerTest {
    private LocalBluetoothLeBroadcast mBroadcast;
    @Mock
    private LocalBluetoothLeBroadcastAssistant mAssistant;
    @Mock
    private UserManager mUserManager;

    private ShadowBluetoothAdapter mShadowBluetoothAdapter;
    private CachedBluetoothDevice mCachedDevice1;
@@ -128,7 +131,7 @@ public class CsipDeviceManagerTest {
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        mContext = RuntimeEnvironment.application;
        mContext = spy(RuntimeEnvironment.application);
        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
        mShadowBluetoothAdapter.setEnabled(true);
        mShadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported(
@@ -355,6 +358,37 @@ public class CsipDeviceManagerTest {
                any(BluetoothLeBroadcastMetadata.class), anyBoolean());
    }

    @Test
    public void
            addMemberDevicesIntoMainDevice_preferredDeviceIsMainAndTwoMain_workProfile_doNothing() {
        // Condition: The preferredDevice is main and there is another main device in top list
        // Expected Result: return true and there is the preferredDevice in top list
        CachedBluetoothDevice preferredDevice = mCachedDevice1;
        mCachedDevice1.getMemberDevice().clear();
        mCachedDevices.clear();
        mCachedDevices.add(preferredDevice);
        mCachedDevices.add(mCachedDevice2);
        mCachedDevices.add(mCachedDevice3);
        mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_LE_AUDIO_SHARING);
        when(mBroadcast.isEnabled(null)).thenReturn(true);
        BluetoothLeBroadcastMetadata metadata = Mockito.mock(BluetoothLeBroadcastMetadata.class);
        when(mBroadcast.getLatestBluetoothLeBroadcastMetadata()).thenReturn(metadata);
        BluetoothLeBroadcastReceiveState state = Mockito.mock(
                BluetoothLeBroadcastReceiveState.class);
        when(state.getBisSyncState()).thenReturn(ImmutableList.of(1L));
        when(mAssistant.getAllSources(mDevice2)).thenReturn(ImmutableList.of(state));
        when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
        when(mUserManager.isManagedProfile()).thenReturn(true);

        assertThat(mCsipDeviceManager.addMemberDevicesIntoMainDevice(GROUP1, preferredDevice))
                .isTrue();
        assertThat(mCachedDevices.contains(preferredDevice)).isTrue();
        assertThat(mCachedDevices.contains(mCachedDevice2)).isFalse();
        assertThat(mCachedDevices.contains(mCachedDevice3)).isTrue();
        assertThat(preferredDevice.getMemberDevice()).contains(mCachedDevice2);
        verify(mAssistant, never()).addSource(mDevice1, metadata, /* isGroupOp= */ false);
    }

    @Test
    public void addMemberDevicesIntoMainDevice_preferredDeviceIsMainAndTwoMain_syncSource() {
        // Condition: The preferredDevice is main and there is another main device in top list