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

Commit 1c2f61ce authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "[RFCOMM] Lock & check service before accessing it"

parents 0472b987 a7faca2a
Loading
Loading
Loading
Loading
+32 −15
Original line number Diff line number Diff line
@@ -3245,16 +3245,22 @@ public final class BluetoothAdapter {
        if (!pendingIntent.isImmutable()) {
            throw new IllegalArgumentException("The provided PendingIntent is not immutable");
        }
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();
                mService.startRfcommListener(
                        name, new ParcelUuid(uuid), pendingIntent, mAttributionSource, recv);
                return recv.awaitResultNoInterrupt(getSyncTimeout())
                    .getValue(BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND);
            }
        } catch (RemoteException | TimeoutException e) {
            Log.e(TAG, "Failed to transact RFCOMM listener start request", e);
            return BluetoothStatusCodes.ERROR_TIMEOUT;
        } finally {
            mServiceLock.readLock().unlock();
        }
        return BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND;
    }

    /**
@@ -3274,15 +3280,21 @@ public final class BluetoothAdapter {
    })
    @RfcommListenerResult
    public int stopRfcommServer(@NonNull UUID uuid) {
        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get();
                mService.stopRfcommListener(new ParcelUuid(uuid), mAttributionSource, recv);
                return recv.awaitResultNoInterrupt(getSyncTimeout())
                    .getValue(BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND);
            }
        } catch (RemoteException | TimeoutException e) {
            Log.e(TAG, "Failed to transact RFCOMM listener stop request", e);
            return BluetoothStatusCodes.ERROR_TIMEOUT;
        } finally {
            mServiceLock.readLock().unlock();
        }
        return BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND;
    }

    /**
@@ -3307,17 +3319,22 @@ public final class BluetoothAdapter {
            android.Manifest.permission.BLUETOOTH_PRIVILEGED,
    })
    public @NonNull BluetoothSocket retrieveConnectedRfcommSocket(@NonNull UUID uuid) {
        IncomingRfcommSocketInfo socketInfo;
        IncomingRfcommSocketInfo socketInfo = null;

        mServiceLock.readLock().lock();
        try {
            if (mService != null) {
                final SynchronousResultReceiver<IncomingRfcommSocketInfo> recv =
                        SynchronousResultReceiver.get();
                mService.retrievePendingSocketForServiceRecord(new ParcelUuid(uuid),
                        mAttributionSource, recv);
                socketInfo = recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null);
            }
        } catch (RemoteException | TimeoutException e) {
            Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable()));
            return null;
        } finally {
            mServiceLock.readLock().unlock();
        }
        if (socketInfo == null) {
            return null;