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

Commit d9a9502b authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

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

parents 56677bcd d4306c71
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
@@ -39,7 +39,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;

@@ -1174,17 +1173,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);

@@ -1294,22 +1295,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