From 60ce7bebcf8f127cae5f6f12a1752491d2ae241e Mon Sep 17 00:00:00 2001 From: Hugh Chen Date: Wed, 18 Nov 2020 17:37:56 +0800 Subject: [PATCH] Fix anr issue on connected devices settings Before this CL, connected devices settings will use ContentResolver to get advanced device bitmaps on the UI thread. It will have a little chance to cause ANR. This CL uses background thread to get a bitmap to fix ANR issue. Bug: 173283590 Test: Test: make -j42 RunSettingsRoboTests Change-Id: I987759a775d507003fd4ef54f5376bd24e660b3b --- .../bluetooth/BluetoothDevicePreference.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java index d927121e931..f8950325904 100644 --- a/src/com/android/settings/bluetooth/BluetoothDevicePreference.java +++ b/src/com/android/settings/bluetooth/BluetoothDevicePreference.java @@ -44,6 +44,7 @@ import com.android.settings.widget.GearPreference; import com.android.settingslib.bluetooth.BluetoothUtils; import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; +import com.android.settingslib.utils.ThreadUtils; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -175,6 +176,18 @@ public final class BluetoothDevicePreference extends GearPreference { } private void onPreferenceAttributesChanged() { + ThreadUtils.postOnBackgroundThread(() -> { + final Pair pair = + BluetoothUtils.getBtRainbowDrawableWithDescription(getContext(), mCachedDevice); + + ThreadUtils.postOnMainThread(() -> { + if (pair.first != null) { + setIcon(pair.first); + contentDescription = pair.second; + } + }); + }); + /* * The preference framework takes care of making sure the value has * changed before proceeding. It will also call notifyChanged() if @@ -184,13 +197,6 @@ public final class BluetoothDevicePreference extends GearPreference { // Null check is done at the framework setSummary(mCachedDevice.getConnectionSummary()); - final Pair pair = - BluetoothUtils.getBtRainbowDrawableWithDescription(getContext(), mCachedDevice); - if (pair.first != null) { - setIcon(pair.first); - contentDescription = pair.second; - } - // Used to gray out the item setEnabled(!mCachedDevice.isBusy());