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

Commit f28d1a34 authored by William Escande's avatar William Escande Committed by Automerger Merge Worker
Browse files

Merge "AdapterService, use RemoteCallbackList instead of a set" into main am:...

Merge "AdapterService, use RemoteCallbackList instead of a set" into main am: d9a9502b am: a4edadd8

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/3253517



Change-Id: I7ed6a2b14f9efe4a0a10b5640d5419b2246c4e0d
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents b5744c05 a4edadd8
Loading
Loading
Loading
Loading
+12 −5
Original line number Diff line number Diff line
@@ -186,6 +186,7 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@@ -257,6 +258,8 @@ public class AdapterService extends Service {
            mBluetoothQualityReportReadyCallbacks = new RemoteCallbackList<>();
    private final RemoteCallbackList<IBluetoothCallback> mRemoteCallbacks =
            new RemoteCallbackList<>();
    private final RemoteCallbackList<IBluetoothConnectionCallback> mBluetoothConnectionCallbacks =
            new RemoteCallbackList<>();

    private final EvictingQueue<String> mScanModeChanges = EvictingQueue.create(10);

@@ -283,7 +286,6 @@ public class AdapterService extends Service {

    private boolean mNativeAvailable;
    private boolean mCleaningUp;
    private Set<IBluetoothConnectionCallback> mBluetoothConnectionCallbacks = new HashSet<>();
    private boolean mQuietmode = false;
    private Map<String, CallerInfo> mBondAttemptCallerInfo = new HashMap<>();

@@ -3452,7 +3454,7 @@ public class AdapterService extends Service {
                return;
            }
            service.enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, null);
            service.mBluetoothConnectionCallbacks.add(callback);
            service.mBluetoothConnectionCallbacks.register(callback);
        }

        @Override
@@ -3466,7 +3468,7 @@ public class AdapterService extends Service {
                return;
            }
            service.enforceCallingOrSelfPermission(BLUETOOTH_PRIVILEGED, null);
            service.mBluetoothConnectionCallbacks.remove(callback);
            service.mBluetoothConnectionCallbacks.unregister(callback);
        }

        @Override
@@ -5517,8 +5519,13 @@ public class AdapterService extends Service {
        return mRemoteDevices.getUuids(device);
    }

    public Set<IBluetoothConnectionCallback> getBluetoothConnectionCallbacks() {
        return mBluetoothConnectionCallbacks;
    void aclStateChangeBroadcastCallback(Consumer<IBluetoothConnectionCallback> cb) {
        int n = mBluetoothConnectionCallbacks.beginBroadcast();
        Log.d(TAG, "aclStateChangeBroadcastCallback() - Broadcasting to " + n + " receivers.");
        for (int i = 0; i < n; i++) {
            cb.accept(mBluetoothConnectionCallbacks.getBroadcastItem(i));
        }
        mRemoteCallbacks.finishBroadcast();
    }

    /**
+24 −27
Original line number Diff line number Diff line
@@ -42,7 +42,6 @@ import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelUuid;
import android.os.RemoteException;
import android.os.SystemProperties;
import android.util.Log;

@@ -1177,17 +1176,19 @@ public class RemoteDevices {
            return;
        }

        BluetoothDevice device = getDevice(address);

        if (device == null) {
            warnLog(
                    "aclStateChangeCallback: device is NULL, address="
                            + Utils.getRedactedAddressStringFromByte(address)
                            + ", newState="
                            + newState);
            addDeviceProperties(address);
            device = Objects.requireNonNull(getDevice(address));
        }
        final BluetoothDevice device =
                Objects.requireNonNullElseGet(
                        getDevice(address),
                        () -> {
                            Log.w(
                                    TAG,
                                    "aclStateChangeCallback: device is NULL"
                                            + ("address="
                                                    + Utils.getRedactedAddressStringFromByte(
                                                            address))
                                            + (" newState=" + newState));
                            return addDeviceProperties(address).getDevice();
                        });

        DeviceProperties deviceProperties = getDeviceProperties(device);

@@ -1297,22 +1298,18 @@ public class RemoteDevices {
        mAdapterService.sendBroadcast(
                intent, BLUETOOTH_CONNECT, Utils.getTempBroadcastOptions().toBundle());

        synchronized (mAdapterService.getBluetoothConnectionCallbacks()) {
            Set<IBluetoothConnectionCallback> bluetoothConnectionCallbacks =
                    mAdapterService.getBluetoothConnectionCallbacks();
            for (IBluetoothConnectionCallback callback : bluetoothConnectionCallbacks) {
                try {
        Utils.RemoteExceptionIgnoringConsumer<IBluetoothConnectionCallback>
                connectionChangeConsumer;
        if (connectionState == BluetoothAdapter.STATE_CONNECTED) {
                        callback.onDeviceConnected(device);
            connectionChangeConsumer = cb -> cb.onDeviceConnected(device);
        } else {
                        callback.onDeviceDisconnected(
            connectionChangeConsumer =
                    cb ->
                            cb.onDeviceDisconnected(
                                    device, AdapterService.hciToAndroidDisconnectReason(hciReason));
        }
                } catch (RemoteException ex) {
                    Log.e(TAG, "RemoteException in calling IBluetoothConnectionCallback");
                }
            }
        }

        mAdapterService.aclStateChangeBroadcastCallback(connectionChangeConsumer);
    }

    @NonNull