OSDN Git Service

Bluetooth: thread-safe callback invocation
authorJakub Pawlowski <jpawlowski@google.com>
Wed, 13 Sep 2017 16:33:34 +0000 (09:33 -0700)
committerJakub Pawlowski <jpawlowski@google.com>
Thu, 14 Sep 2017 02:37:45 +0000 (02:37 +0000)
Bug: 65596701
Test: manual
Change-Id: I92a436328a3070ea842e8e652891e485406c2ed7
Merged-In: I92a436328a3070ea842e8e652891e485406c2ed7
(cherry picked from commit 3eb569fc72781857f1f2722bb645a6090a9b53ce)

core/java/android/bluetooth/BluetoothGatt.java

index 2c3aa32..b596dd6 100644 (file)
@@ -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);
                         }
                     }