Fix bluetooth settings pairing page stuck

There is heavy work to do when add device to list in the
DeviceListPreferenceFragment, off load these work from main thread
to solve the issue.

Make devicePreferenceMap a ConcurrentHashMap to avoid potential race
condition.

Also no longer use getCachedPreference(key) since we not put anything
into the cache, the fallback flow is always used.

Also in BluetoothDevicePreference.onPreferenceAttributesChanged(), move
more heavy work to background thread.

Using System.currentTimeMillis() to sort devices could cause flaky
because System.currentTimeMillis() could be same for different device,
use AtomicInteger instead.

Fix: 286628533
Test: Following the step in bug
Change-Id: Ia9750adb6b4c1424d084381e9d7c2ca8e7912391
This commit is contained in:
Chaohui Wang
2023-06-13 20:30:26 +08:00
parent 61659799de
commit bb47f32011
6 changed files with 412 additions and 392 deletions

View File

@@ -128,7 +128,7 @@ public abstract class BluetoothDevicePairingDetailBase extends DeviceListPrefere
if (device != null && mSelectedList.contains(device)) {
setResult(RESULT_OK);
finish();
} else if (mDevicePreferenceMap.containsKey(cachedDevice)) {
} else {
onDeviceDeleted(cachedDevice);
}
}
@@ -175,8 +175,6 @@ public abstract class BluetoothDevicePairingDetailBase extends DeviceListPrefere
public void updateContent(int bluetoothState) {
switch (bluetoothState) {
case BluetoothAdapter.STATE_ON:
mDevicePreferenceMap.clear();
clearPreferenceGroupCache();
mBluetoothAdapter.enable();
enableScanning();
break;
@@ -187,14 +185,6 @@ public abstract class BluetoothDevicePairingDetailBase extends DeviceListPrefere
}
}
/**
* Clears all cached preferences in {@code preferenceGroup}.
*/
private void clearPreferenceGroupCache() {
cacheRemoveAllPrefs(mAvailableDevicesCategory);
removeCachedPrefs(mAvailableDevicesCategory);
}
@VisibleForTesting
void showBluetoothTurnedOnToast() {
Toast.makeText(getContext(), R.string.connected_device_bluetooth_turned_on_toast,