Fix crash on connected devices settings

If disconnected bluetooth devices didn't exist in
recent devices, the index will return -1. Our condition
didn't check this situation, the IndexOutOfBoundsException
will throw when this case happens.

This CL adds the condition to check whether index is over
than or equal 0 to fix IndexOutOfBoundsException.

Bug: 173059077
Test: make -j42 RunSettingsRoboTests
Change-Id: Id93f4cb6bded83540045c5e8c21c6f6122a39fed
This commit is contained in:
Hugh Chen
2020-11-12 16:56:15 +08:00
parent b6bba3e9a2
commit 8f29850cca
2 changed files with 19 additions and 1 deletions

View File

@@ -144,7 +144,7 @@ public class PreviouslyConnectedDevicePreferenceController extends BasePreferenc
private void addPreference(int index, Preference preference) { private void addPreference(int index, Preference preference) {
if (preference instanceof BluetoothDevicePreference) { if (preference instanceof BluetoothDevicePreference) {
if (mDevicesList.size() >= index) { if (index >= 0 && mDevicesList.size() >= index) {
mDevicesList.add(index, preference); mDevicesList.add(index, preference);
} else { } else {
mDevicesList.add(preference); mDevicesList.add(preference);

View File

@@ -66,6 +66,7 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
private static final String FAKE_ADDRESS_2 = "AA:AA:AA:AA:AA:02"; private static final String FAKE_ADDRESS_2 = "AA:AA:AA:AA:AA:02";
private static final String FAKE_ADDRESS_3 = "AA:AA:AA:AA:AA:03"; private static final String FAKE_ADDRESS_3 = "AA:AA:AA:AA:AA:03";
private static final String FAKE_ADDRESS_4 = "AA:AA:AA:AA:AA:04"; private static final String FAKE_ADDRESS_4 = "AA:AA:AA:AA:AA:04";
private static final String FAKE_ADDRESS_5 = "AA:AA:AA:AA:AA:05";
@Mock @Mock
private DashboardFragment mDashboardFragment; private DashboardFragment mDashboardFragment;
@@ -88,6 +89,8 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
@Mock @Mock
private CachedBluetoothDevice mCachedDevice4; private CachedBluetoothDevice mCachedDevice4;
@Mock @Mock
private CachedBluetoothDevice mCachedDevice5;
@Mock
private BluetoothDevice mBluetoothDevice1; private BluetoothDevice mBluetoothDevice1;
@Mock @Mock
private BluetoothDevice mBluetoothDevice2; private BluetoothDevice mBluetoothDevice2;
@@ -95,6 +98,8 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
private BluetoothDevice mBluetoothDevice3; private BluetoothDevice mBluetoothDevice3;
@Mock @Mock
private BluetoothDevice mBluetoothDevice4; private BluetoothDevice mBluetoothDevice4;
@Mock
private BluetoothDevice mBluetoothDevice5;
private Context mContext; private Context mContext;
private PreviouslyConnectedDevicePreferenceController mPreConnectedDeviceController; private PreviouslyConnectedDevicePreferenceController mPreConnectedDeviceController;
@@ -121,6 +126,8 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
when(mCachedDevice3.getAddress()).thenReturn(FAKE_ADDRESS_3); when(mCachedDevice3.getAddress()).thenReturn(FAKE_ADDRESS_3);
when(mCachedDevice4.getDevice()).thenReturn(mBluetoothDevice4); when(mCachedDevice4.getDevice()).thenReturn(mBluetoothDevice4);
when(mCachedDevice4.getAddress()).thenReturn(FAKE_ADDRESS_4); when(mCachedDevice4.getAddress()).thenReturn(FAKE_ADDRESS_4);
when(mCachedDevice5.getDevice()).thenReturn(mBluetoothDevice5);
when(mCachedDevice5.getAddress()).thenReturn(FAKE_ADDRESS_5);
final List<BluetoothDevice> mMostRecentlyConnectedDevices = new ArrayList<>(); final List<BluetoothDevice> mMostRecentlyConnectedDevices = new ArrayList<>();
mMostRecentlyConnectedDevices.add(mBluetoothDevice1); mMostRecentlyConnectedDevices.add(mBluetoothDevice1);
@@ -221,6 +228,17 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(4); assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(4);
} }
@Test
public void onDeviceAdded_addPreferenceNotExistInRecentlyDevices_noCrash() {
final BluetoothDevicePreference preference = new BluetoothDevicePreference(
mContext, mCachedDevice5, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT);
mPreConnectedDeviceController.onDeviceAdded(preference);
// 1 BluetoothDevicePreference and 1 see all preference
assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(2);
}
@Test @Test
public void onDeviceRemoved_removeLastDevice_showSeeAllPreference() { public void onDeviceRemoved_removeLastDevice_showSeeAllPreference() {
final BluetoothDevicePreference preference1 = new BluetoothDevicePreference( final BluetoothDevicePreference preference1 = new BluetoothDevicePreference(