Move bluetooth operations inAdvancedBluetoothDetailHeader to background thread

Bug: 305636727
Test: atest AdvancedBluetoothDetailsHeaderControllerTest

Change-Id: I2827deb7ab989169eb2c64c8d075e18cd1a307c7
This commit is contained in:
Haijie Hong
2024-01-09 13:38:31 +08:00
parent 5729057880
commit 1f09fa5d3f
2 changed files with 186 additions and 68 deletions

View File

@@ -37,12 +37,16 @@ import androidx.annotation.VisibleForTesting;
import androidx.appcompat.app.AlertDialog;
import com.android.settings.R;
import com.android.settings.flags.Flags;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.BluetoothUtils.ErrorListener;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager.BluetoothManagerCallback;
import com.android.settingslib.utils.ThreadUtils;
import com.google.common.base.Supplier;
import java.util.ArrayList;
import java.util.List;
@@ -272,4 +276,22 @@ public final class Utils {
+ " , deviceList = " + cachedBluetoothDevices);
return cachedBluetoothDevices;
}
/**
* Preloads the values and run the Runnable afterwards.
* @param suppliers the value supplier, should be a memoized supplier
* @param runnable the runnable to be run after value is preloaded
*/
public static void preloadAndRun(List<Supplier<?>> suppliers, Runnable runnable) {
if (!Flags.enableOffloadBluetoothOperationsToBackgroundThread()) {
runnable.run();
return;
}
ThreadUtils.postOnBackgroundThread(() -> {
for (Supplier<?> supplier : suppliers) {
supplier.get();
}
ThreadUtils.postOnMainThread(runnable);
});
}
}