Loading src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java +40 −11 Original line number Original line Diff line number Diff line Loading @@ -35,8 +35,10 @@ import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import java.util.ArrayList; import java.util.Collection; import java.util.Collection; import java.util.HashMap; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map; /** /** Loading Loading @@ -266,18 +268,21 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback, final BluetoothDevice device = cachedDevice.getDevice(); final BluetoothDevice device = cachedDevice.getDevice(); final CachedBluetoothDevice subCachedDevice = cachedDevice.getSubDevice(); final CachedBluetoothDevice subCachedDevice = cachedDevice.getSubDevice(); if (mPreferenceMap.containsKey(device)) { if (mPreferenceMap.containsKey(device)) { mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(device)); removePreference(device); mPreferenceMap.remove(device); } else if (subCachedDevice != null) { } else if (subCachedDevice != null) { // When doing remove, to check if preference maps to sub device. // 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 // This would happen when connection state is changed in detail page that there is no // callback from SettingsLib. // callback from SettingsLib. final BluetoothDevice subDevice = subCachedDevice.getDevice(); final BluetoothDevice subDevice = subCachedDevice.getDevice(); if (mPreferenceMap.containsKey(subDevice)) { removePreference(subDevice); mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(subDevice)); mPreferenceMap.remove(subDevice); } } } } private void removePreference(BluetoothDevice device) { if (mPreferenceMap.containsKey(device)) { mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(device)); mPreferenceMap.remove(device); } } } /** /** Loading Loading @@ -324,14 +329,38 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback, * Update the attributes of {@link Preference}. * Update the attributes of {@link Preference}. */ */ public void refreshPreference() { public void refreshPreference() { for (Preference preference : mPreferenceMap.values()) { List<BluetoothDevice> removeList = new ArrayList<>(); mPreferenceMap.forEach((key, preference) -> { if (isDeviceOfMapInCachedDevicesList(key)) { ((BluetoothDevicePreference) preference).onPreferenceAttributesChanged(); ((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); return mLocalManager.getCachedDeviceManager().getCachedDevicesCopy().contains(cachedDevice); } } private boolean isDeviceOfMapInCachedDevicesList(BluetoothDevice inputBluetoothDevice) { Collection<CachedBluetoothDevice> 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() { protected String getLogTag() { return TAG; return TAG; } } Loading Loading
src/com/android/settings/bluetooth/BluetoothDeviceUpdater.java +40 −11 Original line number Original line Diff line number Diff line Loading @@ -35,8 +35,10 @@ import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import java.util.ArrayList; import java.util.Collection; import java.util.Collection; import java.util.HashMap; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map; /** /** Loading Loading @@ -266,18 +268,21 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback, final BluetoothDevice device = cachedDevice.getDevice(); final BluetoothDevice device = cachedDevice.getDevice(); final CachedBluetoothDevice subCachedDevice = cachedDevice.getSubDevice(); final CachedBluetoothDevice subCachedDevice = cachedDevice.getSubDevice(); if (mPreferenceMap.containsKey(device)) { if (mPreferenceMap.containsKey(device)) { mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(device)); removePreference(device); mPreferenceMap.remove(device); } else if (subCachedDevice != null) { } else if (subCachedDevice != null) { // When doing remove, to check if preference maps to sub device. // 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 // This would happen when connection state is changed in detail page that there is no // callback from SettingsLib. // callback from SettingsLib. final BluetoothDevice subDevice = subCachedDevice.getDevice(); final BluetoothDevice subDevice = subCachedDevice.getDevice(); if (mPreferenceMap.containsKey(subDevice)) { removePreference(subDevice); mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(subDevice)); mPreferenceMap.remove(subDevice); } } } } private void removePreference(BluetoothDevice device) { if (mPreferenceMap.containsKey(device)) { mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(device)); mPreferenceMap.remove(device); } } } /** /** Loading Loading @@ -324,14 +329,38 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback, * Update the attributes of {@link Preference}. * Update the attributes of {@link Preference}. */ */ public void refreshPreference() { public void refreshPreference() { for (Preference preference : mPreferenceMap.values()) { List<BluetoothDevice> removeList = new ArrayList<>(); mPreferenceMap.forEach((key, preference) -> { if (isDeviceOfMapInCachedDevicesList(key)) { ((BluetoothDevicePreference) preference).onPreferenceAttributesChanged(); ((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); return mLocalManager.getCachedDeviceManager().getCachedDevicesCopy().contains(cachedDevice); } } private boolean isDeviceOfMapInCachedDevicesList(BluetoothDevice inputBluetoothDevice) { Collection<CachedBluetoothDevice> 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() { protected String getLogTag() { return TAG; return TAG; } } Loading