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

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

Merge changes I4fb6ca45,I932be706,I185129bb am: 6c98b547

parents 8ea7236a 6c98b547
Loading
Loading
Loading
Loading
+157 −136
Original line number Diff line number Diff line
@@ -80,13 +80,11 @@ import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.WeakHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
@@ -1060,14 +1058,13 @@ public final class BluetoothAdapter {
     * Use {@link #getDefaultAdapter} to get the BluetoothAdapter instance.
     */
    BluetoothAdapter(IBluetoothManager managerService, AttributionSource attributionSource) {
        mManagerService = Objects.requireNonNull(managerService);
        mAttributionSource = Objects.requireNonNull(attributionSource);
        Lock l = mServiceLock.writeLock();
        l.lock();
        mManagerService = requireNonNull(managerService);
        mAttributionSource = requireNonNull(attributionSource);
        mServiceLock.writeLock().lock();
        try {
            mService = getBluetoothService(mManagerCallback);
        } finally {
            l.unlock();
            mServiceLock.writeLock().unlock();
        }
        mLeScanClients = new HashMap<LeScanCallback, ScanCallback>();
        mToken = new Binder(DESCRIPTOR);
@@ -1660,12 +1657,17 @@ public final class BluetoothAdapter {
    @RequiresBluetoothAdvertisePermission
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_ADVERTISE)
    public int getNameLengthForAdvertise() {
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();
                mService.getNameLengthForAdvertise(mAttributionSource, recv);
                return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(-1);
            }
        } catch (RemoteException | TimeoutException e) {
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        } finally {
            mServiceLock.readLock().unlock();
        }
        return -1;
    }
@@ -1745,15 +1747,17 @@ public final class BluetoothAdapter {
    @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
    public @NonNull List<ParcelUuid> getUuidsList() {
        List<ParcelUuid> defaultValue = new ArrayList<>();
        if (getState() != STATE_ON || mService == null) {
        if (getState() != STATE_ON) {
            return defaultValue;
        }
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                final SynchronousResultReceiver<List<ParcelUuid>> recv =
                        SynchronousResultReceiver.get();
                mService.getUuids(mAttributionSource, recv);
                return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
            }
        } catch (RemoteException | TimeoutException e) {
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        } finally {
@@ -3092,7 +3096,6 @@ public final class BluetoothAdapter {

        mServiceLock.readLock().lock();
        try {
            synchronized (mManagerCallback) {
            if (mService != null) {
                final SynchronousResultReceiver<Long> recv = SynchronousResultReceiver.get();
                mService.getSupportedProfiles(mAttributionSource, recv);
@@ -3110,7 +3113,6 @@ public final class BluetoothAdapter {
                    supportedProfiles.add(BluetoothProfile.HEARING_AID);
                }
            }
            }
        } catch (RemoteException | TimeoutException e) {
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
        } finally {
@@ -3923,14 +3925,17 @@ public final class BluetoothAdapter {

    private final IBluetoothManagerCallback mManagerCallback =
            new IBluetoothManagerCallback.Stub() {
                public void onBluetoothServiceUp(IBluetooth bluetoothService) {
                    Lock l = mServiceLock.writeLock();
                    l.lock();
                public void onBluetoothServiceUp(@NonNull IBluetooth bluetoothService) {
                    requireNonNull(bluetoothService, "bluetoothService cannot be null");
                    mServiceLock.writeLock().lock();
                    try {
                        mService = bluetoothService;
                    } finally {
                        l.unlock();
                        // lock downgrade is possible in ReentrantReadWriteLock
                        mServiceLock.readLock().lock();
                        mServiceLock.writeLock().unlock();
                    }
                    try {
                        synchronized (mMetadataListeners) {
                            mMetadataListeners.forEach((device, pair) -> {
                                try {
@@ -3952,8 +3957,8 @@ public final class BluetoothAdapter {
                                    final SynchronousResultReceiver recv =
                                        SynchronousResultReceiver.get();
                                    mService.registerPreferredAudioProfilesChangedCallback(
                                        mPreferredAudioProfilesChangedCallback, mAttributionSource,
                                        recv);
                                            mPreferredAudioProfilesChangedCallback,
                                            mAttributionSource, recv);
                                    recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(
                                            BluetoothStatusCodes.ERROR_UNKNOWN);
                                } catch (RemoteException | TimeoutException e) {
@@ -3968,8 +3973,8 @@ public final class BluetoothAdapter {
                                    final SynchronousResultReceiver recv =
                                        SynchronousResultReceiver.get();
                                    mService.registerBluetoothQualityReportReadyCallback(
                                        mBluetoothQualityReportReadyCallback, mAttributionSource,
                                        recv);
                                            mBluetoothQualityReportReadyCallback,
                                            mAttributionSource, recv);
                                    recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(
                                            BluetoothStatusCodes.ERROR_UNKNOWN);
                                } catch (RemoteException | TimeoutException e) {
@@ -3978,11 +3983,13 @@ public final class BluetoothAdapter {
                                }
                            }
                        }
                    } finally {
                        mServiceLock.readLock().unlock();
                    }
                }

                public void onBluetoothServiceDown() {
                    Lock l = mServiceLock.writeLock();
                    l.lock();
                    mServiceLock.writeLock().lock();
                    try {
                        mService = null;
                        if (mLeScanClients != null) {
@@ -3995,7 +4002,7 @@ public final class BluetoothAdapter {
                            mBluetoothLeScanner.cleanup();
                        }
                    } finally {
                        l.unlock();
                        mServiceLock.writeLock().unlock();
                    }
                }

@@ -4156,13 +4163,18 @@ public final class BluetoothAdapter {
            Log.w(TAG, "generateLocalOobData(): Adapter isn't enabled!");
            callback.onError(BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED);
        } else {
            mServiceLock.readLock().lock();
            try {
                if (mService != null) {
                    final SynchronousResultReceiver recv = SynchronousResultReceiver.get();
                    mService.generateLocalOobData(transport, new WrappedOobDataCallback(callback,
                            executor), mAttributionSource, recv);
                    recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
                }
            } catch (RemoteException | TimeoutException e) {
                Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            } finally {
                mServiceLock.readLock().unlock();
            }
        }
    }
@@ -4326,7 +4338,7 @@ public final class BluetoothAdapter {
     * /
    @UnsupportedAppUsage
    /*package*/ IBluetooth getBluetoothService(IBluetoothManagerCallback cb) {
        Objects.requireNonNull(cb);
        requireNonNull(cb);
        synchronized (sServiceLock) {
            sProxyServiceStateCallbacks.put(cb, null);
            registerOrUnregisterAdapterLocked();
@@ -4335,7 +4347,7 @@ public final class BluetoothAdapter {
    }

    /*package*/ void removeServiceStateCallback(IBluetoothManagerCallback cb) {
        Objects.requireNonNull(cb);
        requireNonNull(cb);
        synchronized (sServiceLock) {
            sProxyServiceStateCallbacks.remove(cb);
            registerOrUnregisterAdapterLocked();
@@ -4729,11 +4741,6 @@ public final class BluetoothAdapter {
            @NonNull Executor executor, @NonNull OnMetadataChangedListener listener) {
        if (DBG) Log.d(TAG, "addOnMetadataChangedListener()");

        final IBluetooth service = mService;
        if (service == null) {
            Log.e(TAG, "Bluetooth is not enabled. Cannot register metadata listener");
            return false;
        }
        if (listener == null) {
            throw new NullPointerException("listener is null");
        }
@@ -4744,6 +4751,14 @@ public final class BluetoothAdapter {
            throw new NullPointerException("executor is null");
        }

        mServiceLock.readLock().lock();
        try {
            if (mService == null) {
                Log.e(TAG, "Bluetooth is not enabled. Cannot register metadata listener");
                return false;
            }


            synchronized (mMetadataListeners) {
                List<Pair<OnMetadataChangedListener, Executor>> listenerList =
                    mMetadataListeners.get(device);
@@ -4759,13 +4774,14 @@ public final class BluetoothAdapter {
                    }
                }

            Pair<OnMetadataChangedListener, Executor> listenerPair = new Pair(listener, executor);
                Pair<OnMetadataChangedListener, Executor> listenerPair =
                        new Pair(listener, executor);
                listenerList.add(listenerPair);

                boolean ret = false;
                try {
                    final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
                service.registerMetadataListener(mBluetoothMetadataListener, device,
                    mService.registerMetadataListener(mBluetoothMetadataListener, device,
                            mAttributionSource, recv);
                    ret = recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(false);
                } catch (RemoteException | TimeoutException e) {
@@ -4782,6 +4798,9 @@ public final class BluetoothAdapter {
                }
                return ret;
            }
        } finally {
            mServiceLock.readLock().unlock();
        }
    }

    /**
@@ -4826,19 +4845,21 @@ public final class BluetoothAdapter {
                // Unregister to Bluetooth service if all listeners are removed from
                // the registered device
                mMetadataListeners.remove(device);
                final IBluetooth service = mService;
                if (service == null) {
                    // Bluetooth is OFF, do nothing to Bluetooth service.
                    return true;
                }
                mServiceLock.readLock().lock();
                try {
                    final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get();
                    service.unregisterMetadataListener(device, mAttributionSource, recv);
                    if (mService != null) {
                        final SynchronousResultReceiver<Boolean> recv =
                                SynchronousResultReceiver.get();
                        mService.unregisterMetadataListener(device, mAttributionSource, recv);
                        return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(false);
                    }
                } catch (RemoteException | TimeoutException e) {
                    Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
                    return false;
                } finally {
                    mServiceLock.readLock().unlock();
                }

            }
        }
        return true;
@@ -5123,8 +5144,8 @@ public final class BluetoothAdapter {
        if (DBG) {
            Log.d(TAG, "setPreferredAudioProfiles( " + modeToProfileBundle + ", " + device + ")");
        }
        Objects.requireNonNull(modeToProfileBundle, "modeToProfileBundle must not be null");
        Objects.requireNonNull(device, "device must not be null");
        requireNonNull(modeToProfileBundle, "modeToProfileBundle must not be null");
        requireNonNull(device, "device must not be null");
        if (!BluetoothAdapter.checkBluetoothAddress(getAddress())) {
            throw new IllegalArgumentException("device cannot have an invalid address");
        }
@@ -5208,7 +5229,7 @@ public final class BluetoothAdapter {
    @NonNull
    public Bundle getPreferredAudioProfiles(@NonNull BluetoothDevice device) {
        if (DBG) Log.d(TAG, "getPreferredAudioProfiles(" + device + ")");
        Objects.requireNonNull(device, "device cannot be null");
        requireNonNull(device, "device cannot be null");
        if (!BluetoothAdapter.checkBluetoothAddress(device.getAddress())) {
            throw new IllegalArgumentException("device cannot have an invalid address");
        }
@@ -5270,7 +5291,7 @@ public final class BluetoothAdapter {
    @NotifyActiveDeviceChangeAppliedReturnValues
    public int notifyActiveDeviceChangeApplied(@NonNull BluetoothDevice device) {
        if (DBG) Log.d(TAG, "notifyActiveDeviceChangeApplied(" + device + ")");
        Objects.requireNonNull(device, "device cannot be null");
        requireNonNull(device, "device cannot be null");
        if (!BluetoothAdapter.checkBluetoothAddress(device.getAddress())) {
            throw new IllegalArgumentException("device cannot have an invalid address");
        }
@@ -5349,8 +5370,8 @@ public final class BluetoothAdapter {
            @NonNull @CallbackExecutor Executor executor,
            @NonNull PreferredAudioProfilesChangedCallback callback) {
        if (DBG) Log.d(TAG, "registerPreferredAudioProfilesChangedCallback()");
        Objects.requireNonNull(executor, "executor cannot be null");
        Objects.requireNonNull(callback, "callback cannot be null");
        requireNonNull(executor, "executor cannot be null");
        requireNonNull(callback, "callback cannot be null");

        final int defaultValue = BluetoothStatusCodes.ERROR_UNKNOWN;
        int serviceCallStatus = defaultValue;
@@ -5423,7 +5444,7 @@ public final class BluetoothAdapter {
    public int unregisterPreferredAudioProfilesChangedCallback(
            @NonNull PreferredAudioProfilesChangedCallback callback) {
        if (DBG) Log.d(TAG, "unregisterPreferredAudioProfilesChangedCallback()");
        Objects.requireNonNull(callback, "callback cannot be null");
        requireNonNull(callback, "callback cannot be null");

        synchronized (mAudioProfilesChangedCallbackExecutorMap) {
            if (mAudioProfilesChangedCallbackExecutorMap.remove(callback) == null) {
@@ -5547,8 +5568,8 @@ public final class BluetoothAdapter {
            @NonNull @CallbackExecutor Executor executor,
            @NonNull BluetoothQualityReportReadyCallback callback) {
        if (DBG) Log.d(TAG, "registerBluetoothQualityReportReadyCallback()");
        Objects.requireNonNull(executor, "executor cannot be null");
        Objects.requireNonNull(callback, "callback cannot be null");
        requireNonNull(executor, "executor cannot be null");
        requireNonNull(callback, "callback cannot be null");

        final int defaultValue = BluetoothStatusCodes.ERROR_UNKNOWN;
        int serviceCallStatus = defaultValue;
@@ -5621,7 +5642,7 @@ public final class BluetoothAdapter {
    public int unregisterBluetoothQualityReportReadyCallback(
            @NonNull BluetoothQualityReportReadyCallback callback) {
        if (DBG) Log.d(TAG, "unregisterBluetoothQualityReportReadyCallback()");
        Objects.requireNonNull(callback, "callback cannot be null");
        requireNonNull(callback, "callback cannot be null");

        synchronized (mBluetoothQualityReportReadyCallbackExecutorMap) {
            if (mBluetoothQualityReportReadyCallbackExecutorMap.remove(callback) == null) {