From 0faf38eadd1a124e7d91e46d8c1c5f3df78692ca Mon Sep 17 00:00:00 2001 From: SongFerngWang Date: Fri, 12 May 2023 17:18:37 +0800 Subject: [PATCH] The UI does not remove the preference The LE audio structure have two or more devices, it use CSIP to combine them with the groupId. It breaks the UI structure, since the UI use the map to save relationship between the bluetoothDevice and preference. There are two or more devices using the same UI, it causes UI show the wrong preference when CSIP do switching of device. Remove the unuse device when UI refreshing. Bug: 281697186 Test: Build pass Test: make RunSettingsRoboTests ROBOTEST_FILTER=AvailableMediaDeviceGroupControllerTest Change-Id: I798cf9edb590c4a25273913d2f2faf0ed4364ba9 --- .../bluetooth/BluetoothDeviceUpdater.java | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java b/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java index 17c5f36d5c6..2935c6779dc 100644 --- a/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java +++ b/src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java @@ -35,8 +35,10 @@ import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -125,7 +127,7 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback, update(cachedBluetoothDevice); } } else { - removeAllDevicesFromPreference(); + removeAllDevicesFromPreference(); } } @@ -252,7 +254,7 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback, btPreference.setOnGearClickListener(mDeviceProfilesListener); if (this instanceof Preference.OnPreferenceClickListener) { btPreference.setOnPreferenceClickListener( - (Preference.OnPreferenceClickListener)this); + (Preference.OnPreferenceClickListener) this); } mPreferenceMap.put(device, btPreference); mDevicePreferenceCallback.onDeviceAdded(btPreference); @@ -266,17 +268,20 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback, final BluetoothDevice device = cachedDevice.getDevice(); final CachedBluetoothDevice subCachedDevice = cachedDevice.getSubDevice(); if (mPreferenceMap.containsKey(device)) { - mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(device)); - mPreferenceMap.remove(device); + removePreference(device); } else if (subCachedDevice != null) { // When doing remove, to check if preference maps to sub device. // This would happen when connection state is changed in detail page that there is no // callback from SettingsLib. final BluetoothDevice subDevice = subCachedDevice.getDevice(); - if (mPreferenceMap.containsKey(subDevice)) { - mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(subDevice)); - mPreferenceMap.remove(subDevice); - } + removePreference(subDevice); + } + } + + private void removePreference(BluetoothDevice device) { + if (mPreferenceMap.containsKey(device)) { + mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(device)); + mPreferenceMap.remove(device); } } @@ -324,14 +329,38 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback, * Update the attributes of {@link Preference}. */ public void refreshPreference() { - for (Preference preference : mPreferenceMap.values()) { - ((BluetoothDevicePreference) preference).onPreferenceAttributesChanged(); + List removeList = new ArrayList<>(); + mPreferenceMap.forEach((key, preference) -> { + if (isDeviceOfMapInCachedDevicesList(key)) { + ((BluetoothDevicePreference) preference).onPreferenceAttributesChanged(); + } else { + // If the BluetoothDevice of preference is not in the CachedDevices List, then + // remove this preference. + removeList.add(key); + } + }); + + for (BluetoothDevice bluetoothDevice : removeList) { + Log.d(getLogTag(), "removePreference key: " + bluetoothDevice.getAnonymizedAddress()); + removePreference(bluetoothDevice); } } - protected boolean isDeviceInCachedDevicesList(CachedBluetoothDevice cachedDevice){ + protected boolean isDeviceInCachedDevicesList(CachedBluetoothDevice cachedDevice) { return mLocalManager.getCachedDeviceManager().getCachedDevicesCopy().contains(cachedDevice); } + + private boolean isDeviceOfMapInCachedDevicesList(BluetoothDevice inputBluetoothDevice) { + Collection cachedDevices = + mLocalManager.getCachedDeviceManager().getCachedDevicesCopy(); + if (cachedDevices == null || cachedDevices.isEmpty()) { + return false; + } + return cachedDevices.stream() + .anyMatch(cachedBluetoothDevice -> cachedBluetoothDevice.getDevice() != null + && cachedBluetoothDevice.getDevice().equals(inputBluetoothDevice)); + } + protected String getLogTag() { return TAG; }