Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c897cccf authored by Yiyi Shen's avatar Yiyi Shen
Browse files

Quick fix: Move blocking code to background thread

BluetoothAdapter#isEnabled may block with small probability when
PropertyInvalidatedCache miss and recompute. Thus we move it to a
backgroud thread to avoid ANR.

Bug: 294835798
Test: manual
Change-Id: I33a2b4b8c848f66bf213d10088640cfb5f9b744d
parent 009d050e
Loading
Loading
Loading
Loading
+13 −7
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import androidx.preference.PreferenceScreen;

import com.android.settingslib.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.utils.ThreadUtils;

/**
 * Preference controller for bluetooth address
@@ -74,13 +75,18 @@ public abstract class AbstractBluetoothAddressPreferenceController
    protected void updateConnectivity() {
        BluetoothAdapter bluetooth = BluetoothAdapter.getDefaultAdapter();
        if (bluetooth != null && mBtAddress != null) {
            ThreadUtils.postOnBackgroundThread(() -> {
                String address = bluetooth.isEnabled() ? bluetooth.getAddress() : null;
                ThreadUtils.postOnMainThread(() -> {
                    if (!TextUtils.isEmpty(address)) {
                // Convert the address to lowercase for consistency with the wifi MAC address.
                        // Convert the address to lowercase for consistency with the wifi MAC
                        // address.
                        mBtAddress.setSummary(address.toLowerCase());
                    } else {
                        mBtAddress.setSummary(R.string.status_unavailable);
                    }
                });
            });
        }
    }
}