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

Commit 1e0f5c9b authored by Jakub Pawlowski's avatar Jakub Pawlowski
Browse files

Bluetooth: thread-safe callback invocation

Bug: 65596701
Test: manual
Change-Id: I92a436328a3070ea842e8e652891e485406c2ed7
Merged-In: I92a436328a3070ea842e8e652891e485406c2ed7
(cherry picked from commit 3213042e)
parent b94b075e
Loading
Loading
Loading
Loading
+43 −29
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ public final class BluetoothGatt implements BluetoothProfile {
    private static final boolean VDBG = false;

    private IBluetoothGatt mService;
    private BluetoothGattCallback mCallback;
    private volatile BluetoothGattCallback mCallback;
    private Handler mHandler;
    private int mClientIf;
    private BluetoothDevice mDevice;
@@ -159,8 +159,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                    runOrQueueCallback(new Runnable() {
                        @Override
                        public void run() {
                            if (mCallback != null) {
                                mCallback.onConnectionStateChange(BluetoothGatt.this, GATT_FAILURE,
                            final BluetoothGattCallback callback = mCallback;
                            if (callback != null) {
                                callback.onConnectionStateChange(BluetoothGatt.this, GATT_FAILURE,
                                                  BluetoothProfile.STATE_DISCONNECTED);
                            }
                        }
@@ -194,8 +195,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.onPhyUpdate(BluetoothGatt.this, txPhy, rxPhy, status);
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            callback.onPhyUpdate(BluetoothGatt.this, txPhy, rxPhy, status);
                        }
                    }
                });
@@ -216,8 +218,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.onPhyRead(BluetoothGatt.this, txPhy, rxPhy, status);
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            callback.onPhyRead(BluetoothGatt.this, txPhy, rxPhy, status);
                        }
                    }
                });
@@ -241,8 +244,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.onConnectionStateChange(BluetoothGatt.this, status,
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            callback.onConnectionStateChange(BluetoothGatt.this, status,
                                                              profileState);
                        }
                    }
@@ -303,8 +307,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.onServicesDiscovered(BluetoothGatt.this, status);
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            callback.onServicesDiscovered(BluetoothGatt.this, status);
                        }
                    }
                });
@@ -353,9 +358,10 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            if (status == 0) characteristic.setValue(value);
                            mCallback.onCharacteristicRead(BluetoothGatt.this, characteristic,
                            callback.onCharacteristicRead(BluetoothGatt.this, characteristic,
                                                           status);
                        }
                    }
@@ -403,8 +409,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.onCharacteristicWrite(BluetoothGatt.this, characteristic,
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            callback.onCharacteristicWrite(BluetoothGatt.this, characteristic,
                                                            status);
                        }
                    }
@@ -430,9 +437,10 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            characteristic.setValue(value);
                            mCallback.onCharacteristicChanged(BluetoothGatt.this, characteristic);
                            callback.onCharacteristicChanged(BluetoothGatt.this, characteristic);
                        }
                    }
                });
@@ -477,9 +485,10 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            if (status == 0) descriptor.setValue(value);
                            mCallback.onDescriptorRead(BluetoothGatt.this, descriptor, status);
                            callback.onDescriptorRead(BluetoothGatt.this, descriptor, status);
                        }
                    }
                });
@@ -524,8 +533,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.onDescriptorWrite(BluetoothGatt.this, descriptor, status);
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            callback.onDescriptorWrite(BluetoothGatt.this, descriptor, status);
                        }
                    }
                });
@@ -550,8 +560,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                           mCallback.onReliableWriteCompleted(BluetoothGatt.this, status);
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            callback.onReliableWriteCompleted(BluetoothGatt.this, status);
                        }
                    }
                });
@@ -571,8 +582,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.onReadRemoteRssi(BluetoothGatt.this, rssi, status);
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            callback.onReadRemoteRssi(BluetoothGatt.this, rssi, status);
                        }
                    }
                });
@@ -593,8 +605,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.onMtuChanged(BluetoothGatt.this, mtu, status);
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            callback.onMtuChanged(BluetoothGatt.this, mtu, status);
                        }
                    }
                });
@@ -617,8 +630,9 @@ public final class BluetoothGatt implements BluetoothProfile {
                runOrQueueCallback(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.onConnectionUpdated(BluetoothGatt.this, interval, latency,
                        final BluetoothGattCallback callback = mCallback;
                        if (callback != null) {
                            callback.onConnectionUpdated(BluetoothGatt.this, interval, latency,
                                                          timeout, status);
                        }
                    }