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

Commit 319512b3 authored by Chris Antol's avatar Chris Antol
Browse files

Update ThreadUtils APIs

Expose ExecutorServices to facilitate better async handling

Bug: 306256803
Test: Unit tests. General pass over bluetooth and profiles components
Change-Id: Ia1489b18cd75445bf784768f186517c8652109f0
parent c963d064
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ android_library {
        "androidx.localbroadcastmanager_localbroadcastmanager",
        "androidx.room_room-runtime",
        "zxing-core",
        "guava",

        "WifiTrackerLibRes",
        "iconloader",
+1 −1
Original line number Diff line number Diff line
@@ -281,7 +281,7 @@ public class AppUtils {

        for (int i = 0; i < Math.min(appEntries.size(), number); i++) {
            final ApplicationsState.AppEntry entry = appEntries.get(i);
            ThreadUtils.postOnBackgroundThread(() -> {
            var unused = ThreadUtils.getBackgroundExecutor().submit(() -> {
                getIcon(context, entry);
            });
        }
+1 −1
Original line number Diff line number Diff line
@@ -1679,7 +1679,7 @@ public class ApplicationsState {
            ensureLabel(context);
            // Speed up the cache of the label description if they haven't been created.
            if (this.labelDescription == null) {
                ThreadUtils.postOnBackgroundThread(
                var unused = ThreadUtils.getBackgroundExecutor().submit(
                        () -> this.ensureLabelDescriptionLocked(context));
            }
            UserManager um = UserManager.get(context);
+15 −5
Original line number Diff line number Diff line
@@ -49,6 +49,10 @@ import com.android.settingslib.Utils;
import com.android.settingslib.utils.ThreadUtils;
import com.android.settingslib.widget.AdaptiveOutlineDrawable;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
@@ -708,7 +712,7 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>


    void refresh() {
        ThreadUtils.postOnBackgroundThread(() -> {
        ListenableFuture<Void> future = ThreadUtils.getBackgroundExecutor().submit(() -> {
            if (BluetoothUtils.isAdvancedDetailsHeader(mDevice)) {
                Uri uri = BluetoothUtils.getUriMetaData(getDevice(),
                        BluetoothDevice.METADATA_MAIN_ICON);
@@ -718,11 +722,17 @@ public class CachedBluetoothDevice implements Comparable<CachedBluetoothDevice>
                                    mContext, this).first);
                }
            }

            ThreadUtils.postOnMainThread(() -> {
                dispatchAttributesChanged();
            });
            return null;
        });
        Futures.addCallback(future, new FutureCallback<>() {
            @Override
            public void onSuccess(Void result) {
                dispatchAttributesChanged();
            }

            @Override
            public void onFailure(Throwable t) {}
        }, mContext.getMainExecutor());
    }

    public void setJustDiscovered(boolean justDiscovered) {
+15 −5
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
@@ -29,6 +30,10 @@ import com.android.settingslib.R;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.utils.ThreadUtils;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

/**
 * Preference controller for bluetooth address
 */
@@ -75,9 +80,11 @@ 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(() -> {
            ListenableFuture<String> future = ThreadUtils.getBackgroundExecutor()
                    .submit(() -> bluetooth.isEnabled() ? bluetooth.getAddress() : null);
            Futures.addCallback(future, new FutureCallback<>() {
                @Override
                public void onSuccess(@Nullable String address) {
                    if (!TextUtils.isEmpty(address)) {
                        // Convert the address to lowercase for consistency with the wifi MAC
                        // address.
@@ -85,8 +92,11 @@ public abstract class AbstractBluetoothAddressPreferenceController
                    } else {
                        mBtAddress.setSummary(R.string.status_unavailable);
                    }
                });
            });
                }

                @Override
                public void onFailure(Throwable t) {}
            }, mContext.getMainExecutor());
        }
    }
}
Loading