summaryrefslogtreecommitdiff
path: root/framework/java/android/bluetooth/BluetoothPan.java
diff options
context:
space:
mode:
authorJack He <siyuanh@google.com>2017-09-06 00:53:03 +0000
committerandroid-build-merger <android-build-merger@google.com>2017-09-06 00:53:03 +0000
commit979ad80543b27c464a4395989bfc347041828f8c (patch)
treedffcb5f35d1055eab767caf0822d9cc4e90c53df /framework/java/android/bluetooth/BluetoothPan.java
parent8a537758694f68d49dffb0f1b3265e2de64b6f11 (diff)
parent52bd129ba515fd794d98b24420627e7bb43dcbb4 (diff)
Merge "Bluetooth: Thread-safe binder invocation" am: 2c9db622d2 am: 52bd129ba5
am: 9a68d78b44 Change-Id: I08808aaec456fbb53126fcf0130c02afac82f29b
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothPan.java')
-rw-r--r--framework/java/android/bluetooth/BluetoothPan.java61
1 files changed, 30 insertions, 31 deletions
diff --git a/framework/java/android/bluetooth/BluetoothPan.java b/framework/java/android/bluetooth/BluetoothPan.java
index 63e83d2217..866b063083 100644
--- a/framework/java/android/bluetooth/BluetoothPan.java
+++ b/framework/java/android/bluetooth/BluetoothPan.java
@@ -123,7 +123,7 @@ public final class BluetoothPan implements BluetoothProfile {
private Context mContext;
private ServiceListener mServiceListener;
private BluetoothAdapter mAdapter;
- private IBluetoothPan mPanService;
+ private volatile IBluetoothPan mPanService;
/**
* Create a BluetoothPan proxy object for interacting with the local
@@ -238,15 +238,16 @@ public final class BluetoothPan implements BluetoothProfile {
*/
public boolean connect(BluetoothDevice device) {
if (DBG) log("connect(" + device + ")");
- if (mPanService != null && isEnabled() && isValidDevice(device)) {
+ final IBluetoothPan service = mPanService;
+ if (service != null && isEnabled() && isValidDevice(device)) {
try {
- return mPanService.connect(device);
+ return service.connect(device);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false;
}
}
- if (mPanService == null) Log.w(TAG, "Proxy not attached to service");
+ if (service == null) Log.w(TAG, "Proxy not attached to service");
return false;
}
@@ -277,15 +278,16 @@ public final class BluetoothPan implements BluetoothProfile {
*/
public boolean disconnect(BluetoothDevice device) {
if (DBG) log("disconnect(" + device + ")");
- if (mPanService != null && isEnabled() && isValidDevice(device)) {
+ final IBluetoothPan service = mPanService;
+ if (service != null && isEnabled() && isValidDevice(device)) {
try {
- return mPanService.disconnect(device);
+ return service.disconnect(device);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return false;
}
}
- if (mPanService == null) Log.w(TAG, "Proxy not attached to service");
+ if (service == null) Log.w(TAG, "Proxy not attached to service");
return false;
}
@@ -295,15 +297,16 @@ public final class BluetoothPan implements BluetoothProfile {
@Override
public List<BluetoothDevice> getConnectedDevices() {
if (VDBG) log("getConnectedDevices()");
- if (mPanService != null && isEnabled()) {
+ final IBluetoothPan service = mPanService;
+ if (service != null && isEnabled()) {
try {
- return mPanService.getConnectedDevices();
+ return service.getConnectedDevices();
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>();
}
}
- if (mPanService == null) Log.w(TAG, "Proxy not attached to service");
+ if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>();
}
@@ -313,15 +316,16 @@ public final class BluetoothPan implements BluetoothProfile {
@Override
public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) {
if (VDBG) log("getDevicesMatchingStates()");
- if (mPanService != null && isEnabled()) {
+ final IBluetoothPan service = mPanService;
+ if (service != null && isEnabled()) {
try {
- return mPanService.getDevicesMatchingConnectionStates(states);
+ return service.getDevicesMatchingConnectionStates(states);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return new ArrayList<BluetoothDevice>();
}
}
- if (mPanService == null) Log.w(TAG, "Proxy not attached to service");
+ if (service == null) Log.w(TAG, "Proxy not attached to service");
return new ArrayList<BluetoothDevice>();
}
@@ -331,25 +335,25 @@ public final class BluetoothPan implements BluetoothProfile {
@Override
public int getConnectionState(BluetoothDevice device) {
if (VDBG) log("getState(" + device + ")");
- if (mPanService != null && isEnabled()
- && isValidDevice(device)) {
+ final IBluetoothPan service = mPanService;
+ if (service != null && isEnabled() && isValidDevice(device)) {
try {
- return mPanService.getConnectionState(device);
+ return service.getConnectionState(device);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
return BluetoothProfile.STATE_DISCONNECTED;
}
}
- if (mPanService == null) Log.w(TAG, "Proxy not attached to service");
+ if (service == null) Log.w(TAG, "Proxy not attached to service");
return BluetoothProfile.STATE_DISCONNECTED;
}
public void setBluetoothTethering(boolean value) {
if (DBG) log("setBluetoothTethering(" + value + ")");
-
- if (mPanService != null && isEnabled()) {
+ final IBluetoothPan service = mPanService;
+ if (service != null && isEnabled()) {
try {
- mPanService.setBluetoothTethering(value);
+ service.setBluetoothTethering(value);
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
}
@@ -358,10 +362,10 @@ public final class BluetoothPan implements BluetoothProfile {
public boolean isTetheringOn() {
if (VDBG) log("isTetheringOn()");
-
- if (mPanService != null && isEnabled()) {
+ final IBluetoothPan service = mPanService;
+ if (service != null && isEnabled()) {
try {
- return mPanService.isTetheringOn();
+ return service.isTetheringOn();
} catch (RemoteException e) {
Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable()));
}
@@ -373,7 +377,6 @@ public final class BluetoothPan implements BluetoothProfile {
public void onServiceConnected(ComponentName className, IBinder service) {
if (DBG) Log.d(TAG, "BluetoothPAN Proxy object connected");
mPanService = IBluetoothPan.Stub.asInterface(Binder.allowBlocking(service));
-
if (mServiceListener != null) {
mServiceListener.onServiceConnected(BluetoothProfile.PAN,
BluetoothPan.this);
@@ -390,15 +393,11 @@ public final class BluetoothPan implements BluetoothProfile {
};
private boolean isEnabled() {
- if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true;
- return false;
+ return mAdapter.getState() == BluetoothAdapter.STATE_ON;
}
- private boolean isValidDevice(BluetoothDevice device) {
- if (device == null) return false;
-
- if (BluetoothAdapter.checkBluetoothAddress(device.getAddress())) return true;
- return false;
+ private static boolean isValidDevice(BluetoothDevice device) {
+ return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress());
}
private static void log(String msg) {