Merge "[Unicast] Won't show "active" when changing active from HS to LEHS" into tm-qpr-dev

This commit is contained in:
SongFerng Wang
2022-12-16 21:58:08 +00:00
committed by Android (Google) Code Review
2 changed files with 59 additions and 1 deletions

View File

@@ -233,7 +233,22 @@ public class BluetoothEventManager {
@Nullable CachedBluetoothDevice activeDevice,
int bluetoothProfile) {
for (CachedBluetoothDevice cachedDevice : mDeviceManager.getCachedDevicesCopy()) {
Set<CachedBluetoothDevice> memberSet = cachedDevice.getMemberDevice();
boolean isActive = Objects.equals(cachedDevice, activeDevice);
if (!isActive && !memberSet.isEmpty()) {
for (CachedBluetoothDevice memberCachedDevice : memberSet) {
isActive = Objects.equals(memberCachedDevice, activeDevice);
if (isActive) {
Log.d(TAG,
"The active device is the member device "
+ activeDevice.getDevice().getAnonymizedAddress()
+ ". change activeDevice as main device "
+ cachedDevice.getDevice().getAnonymizedAddress());
activeDevice = cachedDevice;
break;
}
}
}
cachedDevice.onActiveDeviceChanged(isActive, bluetoothProfile);
}
for (BluetoothCallback callback : mCallbacks) {

View File

@@ -70,10 +70,14 @@ public class BluetoothEventManagerTest {
@Mock
private HearingAidProfile mHearingAidProfile;
@Mock
private LeAudioProfile mLeAudioProfile;
@Mock
private BluetoothDevice mDevice1;
@Mock
private BluetoothDevice mDevice2;
@Mock
private BluetoothDevice mDevice3;
@Mock
private LocalBluetoothProfileManager mLocalProfileManager;
@Mock
private BluetoothUtils.ErrorListener mErrorListener;
@@ -83,6 +87,7 @@ public class BluetoothEventManagerTest {
private BluetoothEventManager mBluetoothEventManager;
private CachedBluetoothDevice mCachedDevice1;
private CachedBluetoothDevice mCachedDevice2;
private CachedBluetoothDevice mCachedDevice3;
@Before
public void setUp() {
@@ -95,9 +100,10 @@ public class BluetoothEventManagerTest {
when(mHfpProfile.isProfileReady()).thenReturn(true);
when(mA2dpProfile.isProfileReady()).thenReturn(true);
when(mHearingAidProfile.isProfileReady()).thenReturn(true);
when(mLeAudioProfile.isProfileReady()).thenReturn(true);
mCachedDevice1 = new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice1);
mCachedDevice2 = new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice2);
mCachedDevice3 = new CachedBluetoothDevice(mContext, mLocalProfileManager, mDevice3);
BluetoothUtils.setErrorListener(mErrorListener);
}
@@ -293,6 +299,43 @@ public class BluetoothEventManagerTest {
assertThat(mCachedDevice2.isActiveDevice(BluetoothProfile.HEADSET)).isFalse();
}
@Test
public void dispatchActiveDeviceChanged_connectedMemberDevices_activeDeviceChanged() {
final List<CachedBluetoothDevice> cachedDevices = new ArrayList<>();
cachedDevices.add(mCachedDevice1);
cachedDevices.add(mCachedDevice2);
int group1 = 1;
when(mDevice3.getAddress()).thenReturn("testAddress3");
mCachedDevice1.setGroupId(group1);
mCachedDevice3.setGroupId(group1);
mCachedDevice1.addMemberDevice(mCachedDevice3);
when(mDevice1.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mDevice2.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mDevice3.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices);
// Connect device1 and device3 for LE and device2 for A2DP and HFP
mCachedDevice1.onProfileStateChanged(mLeAudioProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice3.onProfileStateChanged(mLeAudioProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice2.onProfileStateChanged(mA2dpProfile, BluetoothProfile.STATE_CONNECTED);
mCachedDevice2.onProfileStateChanged(mHfpProfile, BluetoothProfile.STATE_CONNECTED);
// Verify that both devices are connected and none is Active
assertThat(mCachedDevice1.isActiveDevice(BluetoothProfile.LE_AUDIO)).isFalse();
assertThat(mCachedDevice2.isActiveDevice(BluetoothProfile.A2DP)).isFalse();
assertThat(mCachedDevice2.isActiveDevice(BluetoothProfile.HEADSET)).isFalse();
assertThat(mCachedDevice3.isActiveDevice(BluetoothProfile.LE_AUDIO)).isFalse();
// The member device is active.
mBluetoothEventManager.dispatchActiveDeviceChanged(mCachedDevice3,
BluetoothProfile.LE_AUDIO);
// The main device is active since the member is active.
assertThat(mCachedDevice1.isActiveDevice(BluetoothProfile.LE_AUDIO)).isTrue();
}
/**
* Test to verify onActiveDeviceChanged() with A2DP and Hearing Aid.
*/