List 5 disconnected bt device

- This CL add condition that to list max number is 5
  most recently disconnected device.
- Add test case.

Bug: 152724061
Test: make -j42 RunSettingsLibRoboTests
Change-Id: I14adf136648bccbf3fb206d3c25d15f6ee223a1a
This commit is contained in:
hughchen
2020-03-30 14:11:18 +08:00
parent e7647d94b4
commit 11264e2148
2 changed files with 58 additions and 0 deletions

View File

@@ -45,6 +45,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
public class LocalMediaManager implements BluetoothCallback {
private static final Comparator<MediaDevice> COMPARATOR = Comparator.naturalOrder();
private static final String TAG = "LocalMediaManager";
private static final int MAX_DISCONNECTED_DEVICE_NUM = 5;
@Retention(RetentionPolicy.SOURCE)
@IntDef({MediaDeviceState.STATE_CONNECTED,
@@ -404,13 +405,18 @@ public class LocalMediaManager implements BluetoothCallback {
mLocalBluetoothManager.getCachedDeviceManager();
final List<CachedBluetoothDevice> cachedBluetoothDeviceList = new ArrayList<>();
int deviceCount = 0;
for (BluetoothDevice device : bluetoothDevices) {
final CachedBluetoothDevice cachedDevice =
cachedDeviceManager.findDevice(device);
if (cachedDevice != null) {
if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED
&& !cachedDevice.isConnected()) {
deviceCount++;
cachedBluetoothDeviceList.add(cachedDevice);
if (deviceCount >= MAX_DISCONNECTED_DEVICE_NUM) {
break;
}
}
}
}

View File

@@ -601,4 +601,56 @@ public class LocalMediaManagerTest {
verify(mCallback).onRequestFailed(1);
}
@Test
public void onDeviceListAdded_haveDisconnectedDevice_list5DisconnectedDevice() {
final List<MediaDevice> devices = new ArrayList<>();
final MediaDevice device1 = mock(MediaDevice.class);
final MediaDevice device2 = mock(MediaDevice.class);
final MediaDevice device3 = mock(MediaDevice.class);
mLocalMediaManager.mPhoneDevice = mock(PhoneMediaDevice.class);
devices.add(device1);
devices.add(device2);
mLocalMediaManager.mMediaDevices.add(device3);
mLocalMediaManager.mMediaDevices.add(mLocalMediaManager.mPhoneDevice);
final List<BluetoothDevice> bluetoothDevices = new ArrayList<>();
final BluetoothDevice bluetoothDevice = mock(BluetoothDevice.class);
final BluetoothDevice bluetoothDevice2 = mock(BluetoothDevice.class);
final BluetoothDevice bluetoothDevice3 = mock(BluetoothDevice.class);
final BluetoothDevice bluetoothDevice4 = mock(BluetoothDevice.class);
final BluetoothDevice bluetoothDevice5 = mock(BluetoothDevice.class);
final BluetoothDevice bluetoothDevice6 = mock(BluetoothDevice.class);
final CachedBluetoothDevice cachedDevice = mock(CachedBluetoothDevice.class);
final CachedBluetoothDeviceManager cachedManager = mock(CachedBluetoothDeviceManager.class);
bluetoothDevices.add(bluetoothDevice);
bluetoothDevices.add(bluetoothDevice2);
bluetoothDevices.add(bluetoothDevice3);
bluetoothDevices.add(bluetoothDevice4);
bluetoothDevices.add(bluetoothDevice5);
bluetoothDevices.add(bluetoothDevice6);
mShadowBluetoothAdapter.setMostRecentlyConnectedDevices(bluetoothDevices);
when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(cachedManager);
when(cachedManager.findDevice(bluetoothDevice)).thenReturn(cachedDevice);
when(cachedManager.findDevice(bluetoothDevice2)).thenReturn(cachedDevice);
when(cachedManager.findDevice(bluetoothDevice3)).thenReturn(cachedDevice);
when(cachedManager.findDevice(bluetoothDevice4)).thenReturn(cachedDevice);
when(cachedManager.findDevice(bluetoothDevice5)).thenReturn(cachedDevice);
when(cachedManager.findDevice(bluetoothDevice6)).thenReturn(cachedDevice);
when(cachedDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
when(cachedDevice.isConnected()).thenReturn(false);
when(device1.getId()).thenReturn(TEST_DEVICE_ID_1);
when(device2.getId()).thenReturn(TEST_DEVICE_ID_2);
when(device3.getId()).thenReturn(TEST_DEVICE_ID_3);
when(mLocalMediaManager.mPhoneDevice.getId()).thenReturn("test_phone_id");
assertThat(mLocalMediaManager.mMediaDevices).hasSize(2);
mLocalMediaManager.registerCallback(mCallback);
mLocalMediaManager.mMediaDeviceCallback.onDeviceListAdded(devices);
assertThat(mLocalMediaManager.mMediaDevices).hasSize(7);
verify(mCallback).onDeviceListUpdate(any());
}
}