From 76dcfdedf2f6c5f35c2032bba72fa421b39ac60f Mon Sep 17 00:00:00 2001 From: hughchen Date: Mon, 10 Feb 2020 19:01:50 +0800 Subject: [PATCH] Fix incorrect UI issue of "previously connected devices" This CL before, the bt device still showing on "previously connected devices" after forgot that bt device. It because the list of getMostRecentlyConnectedDevices() will not include BOND_NONE device. We need to remove the device that not include in list of getMostRecentlyConnectedDevices() . This CL add removePreferenceIfNecessary() to check the device whether is contained in getMostRecentlyConnectedDevices(). If not, remove the preference. Bug: 149193092 Test: make -j42 RunSettingsRoboTests Change-Id: I94bedf222b64d9f05dc6f979b79dbc8794c0f97d --- .../SavedBluetoothDeviceUpdater.java | 20 +++++++++++++- .../SavedBluetoothDeviceUpdaterTest.java | 27 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java index 9a327edb7cf..bbcd13cccd1 100644 --- a/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java +++ b/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdater.java @@ -28,6 +28,9 @@ import com.android.settings.dashboard.DashboardFragment; import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; +import java.util.ArrayList; +import java.util.List; + /** * Maintain and update saved bluetooth devices(bonded but not connected) */ @@ -54,7 +57,10 @@ public class SavedBluetoothDeviceUpdater extends BluetoothDeviceUpdater if (mBluetoothAdapter.isEnabled()) { final CachedBluetoothDeviceManager cachedManager = mLocalManager.getCachedDeviceManager(); - for (BluetoothDevice device : mBluetoothAdapter.getMostRecentlyConnectedDevices()) { + final List bluetoothDevices = + mBluetoothAdapter.getMostRecentlyConnectedDevices(); + removePreferenceIfNecessary(bluetoothDevices, cachedManager); + for (BluetoothDevice device : bluetoothDevices) { final CachedBluetoothDevice cachedDevice = cachedManager.findDevice(device); if (cachedDevice != null) { update(cachedDevice); @@ -65,6 +71,18 @@ public class SavedBluetoothDeviceUpdater extends BluetoothDeviceUpdater } } + private void removePreferenceIfNecessary(List bluetoothDevices, + CachedBluetoothDeviceManager cachedManager) { + for (BluetoothDevice device : new ArrayList<>(mPreferenceMap.keySet())) { + if (!bluetoothDevices.contains(device)) { + final CachedBluetoothDevice cachedDevice = cachedManager.findDevice(device); + if (cachedDevice != null) { + removePreference(cachedDevice); + } + } + } + } + @Override public void update(CachedBluetoothDevice cachedDevice) { if (isFilterMatched(cachedDevice)) { diff --git a/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java index 3993475915c..da117811233 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java @@ -18,6 +18,7 @@ package com.android.settings.bluetooth; import static org.mockito.ArgumentMatchers.any; 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.verify; @@ -208,4 +209,30 @@ public class SavedBluetoothDeviceUpdaterTest { verify(mBluetoothDeviceUpdater).removeAllDevicesFromPreference(); } + + @Test + public void forceUpdate_deviceNotContain_removePreference() { + final List bluetoothDevices = new ArrayList<>(); + bluetoothDevices.add(mBluetoothDevice); + final BluetoothDevice device2 = mock(BluetoothDevice.class); + final CachedBluetoothDevice cachedDevice2 = mock(CachedBluetoothDevice.class); + + mBluetoothDeviceUpdater.mPreferenceMap.put(device2, mPreference); + + when(cachedDevice2.getDevice()).thenReturn(device2); + when(cachedDevice2.getAddress()).thenReturn("04:52:C7:0B:D8:3S"); + when(mDeviceManager.findDevice(device2)).thenReturn(cachedDevice2); + when(mBluetoothAdapter.isEnabled()).thenReturn(true); + when(mBluetoothAdapter.getMostRecentlyConnectedDevices()).thenReturn(bluetoothDevices); + when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); + when(mDeviceManager.findDevice(mBluetoothDevice)).thenReturn(mCachedBluetoothDevice); + when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); + when(mBluetoothDevice.isConnected()).thenReturn(false); + + mBluetoothDeviceUpdater.forceUpdate(); + + verify(mBluetoothDeviceUpdater).removePreference(cachedDevice2); + verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice, + BluetoothDevicePreference.SortType.TYPE_NO_SORT); + } }