diff options
14 files changed, 680 insertions, 571 deletions
diff --git a/framework/java/android/bluetooth/BluetoothA2dpSink.java b/framework/java/android/bluetooth/BluetoothA2dpSink.java index 611531c4f7..faab000a89 100755 --- a/framework/java/android/bluetooth/BluetoothA2dpSink.java +++ b/framework/java/android/bluetooth/BluetoothA2dpSink.java @@ -125,7 +125,7 @@ public final class BluetoothA2dpSink implements BluetoothProfile { private Context mContext; private ServiceListener mServiceListener; - private IBluetoothA2dpSink mService; + private volatile IBluetoothA2dpSink mService; private BluetoothAdapter mAdapter; private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = @@ -240,15 +240,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile { */ public boolean connect(BluetoothDevice device) { if (DBG) log("connect(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothA2dpSink service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.connect(device); + return service.connect(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -279,15 +280,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile { */ public boolean disconnect(BluetoothDevice device) { if (DBG) log("disconnect(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothA2dpSink service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.disconnect(device); + return service.disconnect(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -297,15 +299,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile { @Override public List<BluetoothDevice> getConnectedDevices() { if (VDBG) log("getConnectedDevices()"); - if (mService != null && isEnabled()) { + final IBluetoothA2dpSink service = mService; + if (service != null && isEnabled()) { try { - return mService.getConnectedDevices(); + return service.getConnectedDevices(); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -315,15 +318,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile { @Override public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { if (VDBG) log("getDevicesMatchingStates()"); - if (mService != null && isEnabled()) { + final IBluetoothA2dpSink service = mService; + if (service != null && isEnabled()) { try { - return mService.getDevicesMatchingConnectionStates(states); + return service.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -333,16 +337,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile { @Override public int getConnectionState(BluetoothDevice device) { if (VDBG) log("getState(" + device + ")"); - if (mService != null && isEnabled() - && isValidDevice(device)) { + final IBluetoothA2dpSink service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getConnectionState(device); + return service.getConnectionState(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return BluetoothProfile.STATE_DISCONNECTED; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return BluetoothProfile.STATE_DISCONNECTED; } @@ -359,16 +363,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile { */ public BluetoothAudioConfig getAudioConfig(BluetoothDevice device) { if (VDBG) log("getAudioConfig(" + device + ")"); - if (mService != null && isEnabled() - && isValidDevice(device)) { + final IBluetoothA2dpSink service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getAudioConfig(device); + return service.getAudioConfig(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return null; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return null; } @@ -389,20 +393,20 @@ public final class BluetoothA2dpSink implements BluetoothProfile { */ public boolean setPriority(BluetoothDevice device, int priority) { if (DBG) log("setPriority(" + device + ", " + priority + ")"); - if (mService != null && isEnabled() - && isValidDevice(device)) { + final IBluetoothA2dpSink service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { if (priority != BluetoothProfile.PRIORITY_OFF && priority != BluetoothProfile.PRIORITY_ON) { return false; } try { - return mService.setPriority(device, priority); + return service.setPriority(device, priority); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -421,16 +425,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile { */ public int getPriority(BluetoothDevice device) { if (VDBG) log("getPriority(" + device + ")"); - if (mService != null && isEnabled() - && isValidDevice(device)) { + final IBluetoothA2dpSink service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getPriority(device); + return service.getPriority(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return BluetoothProfile.PRIORITY_OFF; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return BluetoothProfile.PRIORITY_OFF; } @@ -442,16 +446,16 @@ public final class BluetoothA2dpSink implements BluetoothProfile { * @param device BluetoothDevice device */ public boolean isA2dpPlaying(BluetoothDevice device) { - if (mService != null && isEnabled() - && isValidDevice(device)) { + final IBluetoothA2dpSink service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.isA2dpPlaying(device); + return service.isA2dpPlaying(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -485,7 +489,6 @@ public final class BluetoothA2dpSink implements BluetoothProfile { public void onServiceConnected(ComponentName className, IBinder service) { if (DBG) Log.d(TAG, "Proxy object connected"); mService = IBluetoothA2dpSink.Stub.asInterface(Binder.allowBlocking(service)); - if (mServiceListener != null) { mServiceListener.onServiceConnected(BluetoothProfile.A2DP_SINK, BluetoothA2dpSink.this); @@ -502,15 +505,11 @@ public final class BluetoothA2dpSink 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) { diff --git a/framework/java/android/bluetooth/BluetoothAvrcpController.java b/framework/java/android/bluetooth/BluetoothAvrcpController.java index 7528aa9721..5f0e5d9744 100644 --- a/framework/java/android/bluetooth/BluetoothAvrcpController.java +++ b/framework/java/android/bluetooth/BluetoothAvrcpController.java @@ -81,7 +81,7 @@ public final class BluetoothAvrcpController implements BluetoothProfile { private Context mContext; private ServiceListener mServiceListener; - private IBluetoothAvrcpController mService; + private volatile IBluetoothAvrcpController mService; private BluetoothAdapter mAdapter; private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = @@ -179,15 +179,16 @@ public final class BluetoothAvrcpController implements BluetoothProfile { @Override public List<BluetoothDevice> getConnectedDevices() { if (VDBG) log("getConnectedDevices()"); - if (mService != null && isEnabled()) { + final IBluetoothAvrcpController service = mService; + if (service != null && isEnabled()) { try { - return mService.getConnectedDevices(); + return service.getConnectedDevices(); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -197,15 +198,16 @@ public final class BluetoothAvrcpController implements BluetoothProfile { @Override public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { if (VDBG) log("getDevicesMatchingStates()"); - if (mService != null && isEnabled()) { + final IBluetoothAvrcpController service = mService; + if (service != null && isEnabled()) { try { - return mService.getDevicesMatchingConnectionStates(states); + return service.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -215,16 +217,16 @@ public final class BluetoothAvrcpController implements BluetoothProfile { @Override public int getConnectionState(BluetoothDevice device) { if (VDBG) log("getState(" + device + ")"); - if (mService != null && isEnabled() - && isValidDevice(device)) { + final IBluetoothAvrcpController service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getConnectionState(device); + return service.getConnectionState(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return BluetoothProfile.STATE_DISCONNECTED; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return BluetoothProfile.STATE_DISCONNECTED; } @@ -236,9 +238,10 @@ public final class BluetoothAvrcpController implements BluetoothProfile { public BluetoothAvrcpPlayerSettings getPlayerSettings(BluetoothDevice device) { if (DBG) Log.d(TAG, "getPlayerSettings"); BluetoothAvrcpPlayerSettings settings = null; - if (mService != null && isEnabled()) { + final IBluetoothAvrcpController service = mService; + if (service != null && isEnabled()) { try { - settings = mService.getPlayerSettings(device); + settings = service.getPlayerSettings(device); } catch (RemoteException e) { Log.e(TAG, "Error talking to BT service in getMetadata() " + e); return null; @@ -253,15 +256,16 @@ public final class BluetoothAvrcpController implements BluetoothProfile { */ public boolean setPlayerApplicationSetting(BluetoothAvrcpPlayerSettings plAppSetting) { if (DBG) Log.d(TAG, "setPlayerApplicationSetting"); - if (mService != null && isEnabled()) { + final IBluetoothAvrcpController service = mService; + if (service != null && isEnabled()) { try { - return mService.setPlayerApplicationSetting(plAppSetting); + return service.setPlayerApplicationSetting(plAppSetting); } catch (RemoteException e) { Log.e(TAG, "Error talking to BT service in setPlayerApplicationSetting() " + e); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -272,23 +276,23 @@ public final class BluetoothAvrcpController implements BluetoothProfile { public void sendGroupNavigationCmd(BluetoothDevice device, int keyCode, int keyState) { Log.d(TAG, "sendGroupNavigationCmd dev = " + device + " key " + keyCode + " State = " + keyState); - if (mService != null && isEnabled()) { + final IBluetoothAvrcpController service = mService; + if (service != null && isEnabled()) { try { - mService.sendGroupNavigationCmd(device, keyCode, keyState); + service.sendGroupNavigationCmd(device, keyCode, keyState); return; } catch (RemoteException e) { Log.e(TAG, "Error talking to BT service in sendGroupNavigationCmd()", e); return; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); } private final ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { if (DBG) Log.d(TAG, "Proxy object connected"); mService = IBluetoothAvrcpController.Stub.asInterface(Binder.allowBlocking(service)); - if (mServiceListener != null) { mServiceListener.onServiceConnected(BluetoothProfile.AVRCP_CONTROLLER, BluetoothAvrcpController.this); @@ -305,15 +309,11 @@ public final class BluetoothAvrcpController 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) { diff --git a/framework/java/android/bluetooth/BluetoothDevice.java b/framework/java/android/bluetooth/BluetoothDevice.java index 3ab2c4a8f4..d982bb7ffb 100644 --- a/framework/java/android/bluetooth/BluetoothDevice.java +++ b/framework/java/android/bluetooth/BluetoothDevice.java @@ -712,7 +712,7 @@ public final class BluetoothDevice implements Parcelable { * getService() called. * TODO: Unify implementation of sService amongst BluetoothFoo API's */ - private static IBluetooth sService; + private static volatile IBluetooth sService; private final String mAddress; @@ -839,12 +839,13 @@ public final class BluetoothDevice implements Parcelable { */ @RequiresPermission(Manifest.permission.BLUETOOTH) public String getName() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot get Remote Device name"); return null; } try { - return sService.getRemoteName(this); + return service.getRemoteName(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -859,12 +860,13 @@ public final class BluetoothDevice implements Parcelable { */ @RequiresPermission(Manifest.permission.BLUETOOTH) public int getType() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot get Remote Device type"); return DEVICE_TYPE_UNKNOWN; } try { - return sService.getRemoteType(this); + return service.getRemoteType(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -879,12 +881,13 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public String getAlias() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot get Remote Device Alias"); return null; } try { - return sService.getRemoteAlias(this); + return service.getRemoteAlias(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -902,12 +905,13 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public boolean setAlias(String alias) { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot set Remote Device name"); return false; } try { - return sService.setRemoteAlias(this, alias); + return service.setRemoteAlias(this, alias); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -942,12 +946,13 @@ public final class BluetoothDevice implements Parcelable { */ @RequiresPermission(Manifest.permission.BLUETOOTH) public int getBatteryLevel() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "Bluetooth disabled. Cannot get remote device battery level"); return BATTERY_LEVEL_UNKNOWN; } try { - return sService.getBatteryLevel(this); + return service.getBatteryLevel(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -966,7 +971,8 @@ public final class BluetoothDevice implements Parcelable { */ @RequiresPermission(Manifest.permission.BLUETOOTH_ADMIN) public boolean createBond() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device"); return false; } @@ -974,7 +980,7 @@ public final class BluetoothDevice implements Parcelable { Log.i(TAG, "createBond() for device " + getAddress() + " called by pid: " + Process.myPid() + " tid: " + Process.myTid()); - return sService.createBond(this, TRANSPORT_AUTO); + return service.createBond(this, TRANSPORT_AUTO); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -998,7 +1004,8 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public boolean createBond(int transport) { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot create bond to Remote Device"); return false; } @@ -1009,7 +1016,7 @@ public final class BluetoothDevice implements Parcelable { Log.i(TAG, "createBond() for device " + getAddress() + " called by pid: " + Process.myPid() + " tid: " + Process.myTid()); - return sService.createBond(this, transport); + return service.createBond(this, transport); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1035,8 +1042,13 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public boolean createBondOutOfBand(int transport, OobData oobData) { + final IBluetooth service = sService; + if (service == null) { + Log.w(TAG, "BT not enabled, createBondOutOfBand failed"); + return false; + } try { - return sService.createBondOutOfBand(this, transport, oobData); + return service.createBondOutOfBand(this, transport, oobData); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1045,8 +1057,13 @@ public final class BluetoothDevice implements Parcelable { /** @hide */ public boolean isBondingInitiatedLocally() { + final IBluetooth service = sService; + if (service == null) { + Log.w(TAG, "BT not enabled, isBondingInitiatedLocally failed"); + return false; + } try { - return sService.isBondingInitiatedLocally(this); + return service.isBondingInitiatedLocally(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1082,7 +1099,8 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public boolean cancelBondProcess() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot cancel Remote Device bond"); return false; } @@ -1090,7 +1108,7 @@ public final class BluetoothDevice implements Parcelable { Log.i(TAG, "cancelBondProcess() for device " + getAddress() + " called by pid: " + Process.myPid() + " tid: " + Process.myTid()); - return sService.cancelBondProcess(this); + return service.cancelBondProcess(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1108,7 +1126,8 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public boolean removeBond() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot remove Remote Device bond"); return false; } @@ -1116,7 +1135,7 @@ public final class BluetoothDevice implements Parcelable { Log.i(TAG, "removeBond() for device " + getAddress() + " called by pid: " + Process.myPid() + " tid: " + Process.myTid()); - return sService.removeBond(this); + return service.removeBond(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1134,19 +1153,15 @@ public final class BluetoothDevice implements Parcelable { */ @RequiresPermission(Manifest.permission.BLUETOOTH) public int getBondState() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot get bond state"); return BOND_NONE; } try { - return sService.getBondState(this); + return service.getBondState(this); } catch (RemoteException e) { Log.e(TAG, "", e); - } catch (NullPointerException npe) { - // Handle case where bluetooth service proxy - // is already null. - Log.e(TAG, "NullPointerException for getBondState() of device (" - + getAddress() + ")", npe); } return BOND_NONE; } @@ -1160,12 +1175,13 @@ public final class BluetoothDevice implements Parcelable { */ @SystemApi public boolean isConnected() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { // BT is not enabled, we cannot be connected. return false; } try { - return sService.getConnectionState(this) != CONNECTION_STATE_DISCONNECTED; + return service.getConnectionState(this) != CONNECTION_STATE_DISCONNECTED; } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -1182,12 +1198,13 @@ public final class BluetoothDevice implements Parcelable { */ @SystemApi public boolean isEncrypted() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { // BT is not enabled, we cannot be connected. return false; } try { - return sService.getConnectionState(this) > CONNECTION_STATE_CONNECTED; + return service.getConnectionState(this) > CONNECTION_STATE_CONNECTED; } catch (RemoteException e) { Log.e(TAG, "", e); return false; @@ -1201,12 +1218,13 @@ public final class BluetoothDevice implements Parcelable { */ @RequiresPermission(Manifest.permission.BLUETOOTH) public BluetoothClass getBluetoothClass() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot get Bluetooth Class"); return null; } try { - int classInt = sService.getRemoteClass(this); + int classInt = service.getRemoteClass(this); if (classInt == BluetoothClass.ERROR) return null; return new BluetoothClass(classInt); } catch (RemoteException e) { @@ -1227,12 +1245,13 @@ public final class BluetoothDevice implements Parcelable { */ @RequiresPermission(Manifest.permission.BLUETOOTH) public ParcelUuid[] getUuids() { - if (sService == null || !isBluetoothEnabled()) { + final IBluetooth service = sService; + if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot get remote device Uuids"); return null; } try { - return sService.getRemoteUuids(this); + return service.getRemoteUuids(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1254,7 +1273,7 @@ public final class BluetoothDevice implements Parcelable { */ @RequiresPermission(Manifest.permission.BLUETOOTH) public boolean fetchUuidsWithSdp() { - IBluetooth service = sService; + final IBluetooth service = sService; if (service == null || !isBluetoothEnabled()) { Log.e(TAG, "BT not enabled. Cannot fetchUuidsWithSdp"); return false; @@ -1289,12 +1308,13 @@ public final class BluetoothDevice implements Parcelable { */ /** @hide */ public boolean sdpSearch(ParcelUuid uuid) { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot query remote device sdp records"); return false; } try { - return sService.sdpSearch(this, uuid); + return service.sdpSearch(this, uuid); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1308,12 +1328,13 @@ public final class BluetoothDevice implements Parcelable { * @return true pin has been set false for error */ public boolean setPin(byte[] pin) { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot set Remote Device pin"); return false; } try { - return sService.setPin(this, true, pin.length, pin); + return service.setPin(this, true, pin.length, pin); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1337,12 +1358,13 @@ public final class BluetoothDevice implements Parcelable { */ @RequiresPermission(Manifest.permission.BLUETOOTH_PRIVILEGED) public boolean setPairingConfirmation(boolean confirm) { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot set pairing confirmation"); return false; } try { - return sService.setPairingConfirmation(this, confirm); + return service.setPairingConfirmation(this, confirm); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1361,12 +1383,13 @@ public final class BluetoothDevice implements Parcelable { /** @hide */ public boolean cancelPairingUserInput() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { Log.e(TAG, "BT not enabled. Cannot create pairing user input"); return false; } try { - return sService.cancelBondProcess(this); + return service.cancelBondProcess(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1400,11 +1423,12 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public int getPhonebookAccessPermission() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { return ACCESS_UNKNOWN; } try { - return sService.getPhonebookAccessPermission(this); + return service.getPhonebookAccessPermission(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1421,11 +1445,12 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public boolean setPhonebookAccessPermission(int value) { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { return false; } try { - return sService.setPhonebookAccessPermission(this, value); + return service.setPhonebookAccessPermission(this, value); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1440,11 +1465,12 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public int getMessageAccessPermission() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { return ACCESS_UNKNOWN; } try { - return sService.getMessageAccessPermission(this); + return service.getMessageAccessPermission(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1461,11 +1487,12 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public boolean setMessageAccessPermission(int value) { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { return false; } try { - return sService.setMessageAccessPermission(this, value); + return service.setMessageAccessPermission(this, value); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1480,11 +1507,12 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public int getSimAccessPermission() { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { return ACCESS_UNKNOWN; } try { - return sService.getSimAccessPermission(this); + return service.getSimAccessPermission(this); } catch (RemoteException e) { Log.e(TAG, "", e); } @@ -1501,11 +1529,12 @@ public final class BluetoothDevice implements Parcelable { * @hide */ public boolean setSimAccessPermission(int value) { - if (sService == null) { + final IBluetooth service = sService; + if (service == null) { return false; } try { - return sService.setSimAccessPermission(this, value); + return service.setSimAccessPermission(this, value); } catch (RemoteException e) { Log.e(TAG, "", e); } diff --git a/framework/java/android/bluetooth/BluetoothHeadset.java b/framework/java/android/bluetooth/BluetoothHeadset.java index be1ce63cad..85550c7720 100644 --- a/framework/java/android/bluetooth/BluetoothHeadset.java +++ b/framework/java/android/bluetooth/BluetoothHeadset.java @@ -306,7 +306,7 @@ public final class BluetoothHeadset implements BluetoothProfile { private Context mContext; private ServiceListener mServiceListener; - private IBluetoothHeadset mService; + private volatile IBluetoothHeadset mService; private BluetoothAdapter mAdapter; private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = @@ -418,15 +418,16 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean connect(BluetoothDevice device) { if (DBG) log("connect(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.connect(device); + return service.connect(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -457,15 +458,16 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean disconnect(BluetoothDevice device) { if (DBG) log("disconnect(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.disconnect(device); + return service.disconnect(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -475,15 +477,16 @@ public final class BluetoothHeadset implements BluetoothProfile { @Override public List<BluetoothDevice> getConnectedDevices() { if (VDBG) log("getConnectedDevices()"); - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - return mService.getConnectedDevices(); + return service.getConnectedDevices(); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -493,15 +496,16 @@ public final class BluetoothHeadset implements BluetoothProfile { @Override public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { if (VDBG) log("getDevicesMatchingStates()"); - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - return mService.getDevicesMatchingConnectionStates(states); + return service.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -511,15 +515,16 @@ public final class BluetoothHeadset implements BluetoothProfile { @Override public int getConnectionState(BluetoothDevice device) { if (VDBG) log("getConnectionState(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getConnectionState(device); + return service.getConnectionState(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return BluetoothProfile.STATE_DISCONNECTED; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return BluetoothProfile.STATE_DISCONNECTED; } @@ -540,19 +545,20 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean setPriority(BluetoothDevice device, int priority) { if (DBG) log("setPriority(" + device + ", " + priority + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { if (priority != BluetoothProfile.PRIORITY_OFF && priority != BluetoothProfile.PRIORITY_ON) { return false; } try { - return mService.setPriority(device, priority); + return service.setPriority(device, priority); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -571,15 +577,16 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public int getPriority(BluetoothDevice device) { if (VDBG) log("getPriority(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getPriority(device); + return service.getPriority(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return PRIORITY_OFF; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return PRIORITY_OFF; } @@ -605,14 +612,15 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean startVoiceRecognition(BluetoothDevice device) { if (DBG) log("startVoiceRecognition()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.startVoiceRecognition(device); + return service.startVoiceRecognition(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -627,14 +635,15 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean stopVoiceRecognition(BluetoothDevice device) { if (DBG) log("stopVoiceRecognition()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.stopVoiceRecognition(device); + return service.stopVoiceRecognition(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -648,14 +657,15 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean isAudioConnected(BluetoothDevice device) { if (VDBG) log("isAudioConnected()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.isAudioConnected(device); + return service.isAudioConnected(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -674,14 +684,15 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public int getBatteryUsageHint(BluetoothDevice device) { if (VDBG) log("getBatteryUsageHint()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getBatteryUsageHint(device); + return service.getBatteryUsageHint(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return -1; } @@ -704,9 +715,10 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean acceptIncomingConnect(BluetoothDevice device) { if (DBG) log("acceptIncomingConnect"); - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - return mService.acceptIncomingConnect(device); + return service.acceptIncomingConnect(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -724,9 +736,10 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean rejectIncomingConnect(BluetoothDevice device) { if (DBG) log("rejectIncomingConnect"); - if (mService != null) { + final IBluetoothHeadset service = mService; + if (service != null) { try { - return mService.rejectIncomingConnect(device); + return service.rejectIncomingConnect(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -745,9 +758,10 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public int getAudioState(BluetoothDevice device) { if (VDBG) log("getAudioState"); - if (mService != null && !isDisabled()) { + final IBluetoothHeadset service = mService; + if (service != null && !isDisabled()) { try { - return mService.getAudioState(device); + return service.getAudioState(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -770,9 +784,10 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public void setAudioRouteAllowed(boolean allowed) { if (VDBG) log("setAudioRouteAllowed"); - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - mService.setAudioRouteAllowed(allowed); + service.setAudioRouteAllowed(allowed); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -790,9 +805,10 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean getAudioRouteAllowed() { if (VDBG) log("getAudioRouteAllowed"); - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - return mService.getAudioRouteAllowed(); + return service.getAudioRouteAllowed(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -812,9 +828,10 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public void setForceScoAudio(boolean forced) { if (VDBG) log("setForceScoAudio " + String.valueOf(forced)); - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - mService.setForceScoAudio(forced); + service.setForceScoAudio(forced); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -834,14 +851,15 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean isAudioOn() { if (VDBG) log("isAudioOn()"); - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - return mService.isAudioOn(); + return service.isAudioOn(); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -855,9 +873,10 @@ public final class BluetoothHeadset implements BluetoothProfile { * @hide */ public boolean connectAudio() { - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - return mService.connectAudio(); + return service.connectAudio(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -877,9 +896,10 @@ public final class BluetoothHeadset implements BluetoothProfile { * @hide */ public boolean disconnectAudio() { - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - return mService.disconnectAudio(); + return service.disconnectAudio(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -903,9 +923,10 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean startScoUsingVirtualVoiceCall(BluetoothDevice device) { if (DBG) log("startScoUsingVirtualVoiceCall()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.startScoUsingVirtualVoiceCall(device); + return service.startScoUsingVirtualVoiceCall(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -926,9 +947,10 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public boolean stopScoUsingVirtualVoiceCall(BluetoothDevice device) { if (DBG) log("stopScoUsingVirtualVoiceCall()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.stopScoUsingVirtualVoiceCall(device); + return service.stopScoUsingVirtualVoiceCall(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -949,9 +971,10 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public void phoneStateChanged(int numActive, int numHeld, int callState, String number, int type) { - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - mService.phoneStateChanged(numActive, numHeld, callState, number, type); + service.phoneStateChanged(numActive, numHeld, callState, number, type); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -968,9 +991,10 @@ public final class BluetoothHeadset implements BluetoothProfile { */ public void clccResponse(int index, int direction, int status, int mode, boolean mpty, String number, int type) { - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - mService.clccResponse(index, direction, status, mode, mpty, number, type); + service.clccResponse(index, direction, status, mode, mpty, number, type); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -1006,14 +1030,15 @@ public final class BluetoothHeadset implements BluetoothProfile { if (command == null) { throw new IllegalArgumentException("command is null"); } - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.sendVendorSpecificResultCode(device, command, arg); + return service.sendVendorSpecificResultCode(device, command, arg); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) { + if (service == null) { Log.w(TAG, "Proxy not attached to service"); } return false; @@ -1027,9 +1052,10 @@ public final class BluetoothHeadset implements BluetoothProfile { * @hide */ public boolean enableWBS() { - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - return mService.enableWBS(); + return service.enableWBS(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -1048,9 +1074,10 @@ public final class BluetoothHeadset implements BluetoothProfile { * @hide */ public boolean disableWBS() { - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - return mService.disableWBS(); + return service.disableWBS(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -1083,9 +1110,10 @@ public final class BluetoothHeadset implements BluetoothProfile { * @hide */ public void bindResponse(int indId, boolean indStatus) { - if (mService != null && isEnabled()) { + final IBluetoothHeadset service = mService; + if (service != null && isEnabled()) { try { - mService.bindResponse(indId, indStatus); + service.bindResponse(indId, indStatus); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -1115,20 +1143,15 @@ public final class BluetoothHeadset implements BluetoothProfile { }; private boolean isEnabled() { - if (mAdapter.getState() == BluetoothAdapter.STATE_ON) return true; - return false; + return mAdapter.getState() == BluetoothAdapter.STATE_ON; } private boolean isDisabled() { - if (mAdapter.getState() == BluetoothAdapter.STATE_OFF) return true; - return false; + return mAdapter.getState() == BluetoothAdapter.STATE_OFF; } - 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) { diff --git a/framework/java/android/bluetooth/BluetoothHeadsetClient.java b/framework/java/android/bluetooth/BluetoothHeadsetClient.java index 7ed2d2e98f..031287f5ee 100644 --- a/framework/java/android/bluetooth/BluetoothHeadsetClient.java +++ b/framework/java/android/bluetooth/BluetoothHeadsetClient.java @@ -76,8 +76,8 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { * Intent sent whenever audio state changes. * * <p>It includes two mandatory extras: - * {@link BluetoothProfile.EXTRA_STATE}, - * {@link BluetoothProfile.EXTRA_PREVIOUS_STATE}, + * {@link BluetoothProfile#EXTRA_STATE}, + * {@link BluetoothProfile#EXTRA_PREVIOUS_STATE}, * with possible values: * {@link #STATE_AUDIO_CONNECTING}, * {@link #STATE_AUDIO_CONNECTED}, @@ -367,7 +367,7 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { private Context mContext; private ServiceListener mServiceListener; - private IBluetoothHeadsetClient mService; + private volatile IBluetoothHeadsetClient mService; private BluetoothAdapter mAdapter; private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = @@ -478,15 +478,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean connect(BluetoothDevice device) { if (DBG) log("connect(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.connect(device); + return service.connect(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -499,15 +500,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean disconnect(BluetoothDevice device) { if (DBG) log("disconnect(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.disconnect(device); + return service.disconnect(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -519,15 +521,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { @Override public List<BluetoothDevice> getConnectedDevices() { if (VDBG) log("getConnectedDevices()"); - if (mService != null && isEnabled()) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled()) { try { - return mService.getConnectedDevices(); + return service.getConnectedDevices(); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -541,15 +544,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { @Override public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { if (VDBG) log("getDevicesMatchingStates()"); - if (mService != null && isEnabled()) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled()) { try { - return mService.getDevicesMatchingConnectionStates(states); + return service.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -562,15 +566,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { @Override public int getConnectionState(BluetoothDevice device) { if (VDBG) log("getConnectionState(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getConnectionState(device); + return service.getConnectionState(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return BluetoothProfile.STATE_DISCONNECTED; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return BluetoothProfile.STATE_DISCONNECTED; } @@ -581,19 +586,20 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean setPriority(BluetoothDevice device, int priority) { if (DBG) log("setPriority(" + device + ", " + priority + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { if (priority != BluetoothProfile.PRIORITY_OFF && priority != BluetoothProfile.PRIORITY_ON) { return false; } try { - return mService.setPriority(device, priority); + return service.setPriority(device, priority); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -602,15 +608,16 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public int getPriority(BluetoothDevice device) { if (VDBG) log("getPriority(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getPriority(device); + return service.getPriority(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return PRIORITY_OFF; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return PRIORITY_OFF; } @@ -627,14 +634,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean startVoiceRecognition(BluetoothDevice device) { if (DBG) log("startVoiceRecognition()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.startVoiceRecognition(device); + return service.startVoiceRecognition(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -651,14 +659,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean stopVoiceRecognition(BluetoothDevice device) { if (DBG) log("stopVoiceRecognition()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.stopVoiceRecognition(device); + return service.stopVoiceRecognition(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -670,14 +679,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public List<BluetoothHeadsetClientCall> getCurrentCalls(BluetoothDevice device) { if (DBG) log("getCurrentCalls()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getCurrentCalls(device); + return service.getCurrentCalls(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return null; } @@ -689,14 +699,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public Bundle getCurrentAgEvents(BluetoothDevice device) { if (DBG) log("getCurrentCalls()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getCurrentAgEvents(device); + return service.getCurrentAgEvents(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return null; } @@ -711,14 +722,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean acceptCall(BluetoothDevice device, int flag) { if (DBG) log("acceptCall()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.acceptCall(device, flag); + return service.acceptCall(device, flag); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -731,14 +743,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean holdCall(BluetoothDevice device) { if (DBG) log("holdCall()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.holdCall(device); + return service.holdCall(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -755,14 +768,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean rejectCall(BluetoothDevice device) { if (DBG) log("rejectCall()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.rejectCall(device); + return service.rejectCall(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -784,14 +798,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean terminateCall(BluetoothDevice device, BluetoothHeadsetClientCall call) { if (DBG) log("terminateCall()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.terminateCall(device, call); + return service.terminateCall(device, call); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -811,14 +826,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean enterPrivateMode(BluetoothDevice device, int index) { if (DBG) log("enterPrivateMode()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.enterPrivateMode(device, index); + return service.enterPrivateMode(device, index); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -837,14 +853,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean explicitCallTransfer(BluetoothDevice device) { if (DBG) log("explicitCallTransfer()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.explicitCallTransfer(device); + return service.explicitCallTransfer(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -859,14 +876,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public BluetoothHeadsetClientCall dial(BluetoothDevice device, String number) { if (DBG) log("dial()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.dial(device, number); + return service.dial(device, number); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return null; } @@ -882,14 +900,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean sendDTMF(BluetoothDevice device, byte code) { if (DBG) log("sendDTMF()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.sendDTMF(device, code); + return service.sendDTMF(device, code); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -907,14 +926,15 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean getLastVoiceTagNumber(BluetoothDevice device) { if (DBG) log("getLastVoiceTagNumber()"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getLastVoiceTagNumber(device); + return service.getLastVoiceTagNumber(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -925,9 +945,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public int getAudioState(BluetoothDevice device) { if (VDBG) log("getAudioState"); - if (mService != null && isEnabled()) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled()) { try { - return mService.getAudioState(device); + return service.getAudioState(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -947,9 +968,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public void setAudioRouteAllowed(BluetoothDevice device, boolean allowed) { if (VDBG) log("setAudioRouteAllowed"); - if (mService != null && isEnabled()) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled()) { try { - mService.setAudioRouteAllowed(device, allowed); + service.setAudioRouteAllowed(device, allowed); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -968,9 +990,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { */ public boolean getAudioRouteAllowed(BluetoothDevice device) { if (VDBG) log("getAudioRouteAllowed"); - if (mService != null && isEnabled()) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled()) { try { - return mService.getAudioRouteAllowed(device); + return service.getAudioRouteAllowed(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -991,9 +1014,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent; */ public boolean connectAudio(BluetoothDevice device) { - if (mService != null && isEnabled()) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled()) { try { - return mService.connectAudio(device); + return service.connectAudio(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -1014,9 +1038,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { * otherwise; upon completion HFP sends {@link #ACTION_AUDIO_STATE_CHANGED} intent; */ public boolean disconnectAudio(BluetoothDevice device) { - if (mService != null && isEnabled()) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled()) { try { - return mService.disconnectAudio(device); + return service.disconnectAudio(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -1034,9 +1059,10 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { * @return bundle of AG features; null if no service or AG not connected */ public Bundle getCurrentAgFeatures(BluetoothDevice device) { - if (mService != null && isEnabled()) { + final IBluetoothHeadsetClient service = mService; + if (service != null && isEnabled()) { try { - return mService.getCurrentAgFeatures(device); + return service.getCurrentAgFeatures(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -1048,7 +1074,7 @@ public final class BluetoothHeadsetClient implements BluetoothProfile { } - private ServiceConnection mConnection = new ServiceConnection() { + private final ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { if (DBG) Log.d(TAG, "Proxy object connected"); @@ -1071,15 +1097,11 @@ public final class BluetoothHeadsetClient 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) { diff --git a/framework/java/android/bluetooth/BluetoothHealth.java b/framework/java/android/bluetooth/BluetoothHealth.java index dc5f38135a..57a019755f 100644 --- a/framework/java/android/bluetooth/BluetoothHealth.java +++ b/framework/java/android/bluetooth/BluetoothHealth.java @@ -176,9 +176,10 @@ public final class BluetoothHealth implements BluetoothProfile { BluetoothHealthAppConfiguration config = new BluetoothHealthAppConfiguration(name, dataType, role, channelType); - if (mService != null) { + final IBluetoothHealth service = mService; + if (service != null) { try { - result = mService.registerAppConfiguration(config, wrapper); + result = service.registerAppConfiguration(config, wrapper); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -200,9 +201,10 @@ public final class BluetoothHealth implements BluetoothProfile { */ public boolean unregisterAppConfiguration(BluetoothHealthAppConfiguration config) { boolean result = false; - if (mService != null && isEnabled() && config != null) { + final IBluetoothHealth service = mService; + if (service != null && isEnabled() && config != null) { try { - result = mService.unregisterAppConfiguration(config); + result = service.unregisterAppConfiguration(config); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -228,9 +230,10 @@ public final class BluetoothHealth implements BluetoothProfile { */ public boolean connectChannelToSource(BluetoothDevice device, BluetoothHealthAppConfiguration config) { - if (mService != null && isEnabled() && isValidDevice(device) && config != null) { + final IBluetoothHealth service = mService; + if (service != null && isEnabled() && isValidDevice(device) && config != null) { try { - return mService.connectChannelToSource(device, config); + return service.connectChannelToSource(device, config); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -256,9 +259,10 @@ public final class BluetoothHealth implements BluetoothProfile { */ public boolean connectChannelToSink(BluetoothDevice device, BluetoothHealthAppConfiguration config, int channelType) { - if (mService != null && isEnabled() && isValidDevice(device) && config != null) { + final IBluetoothHealth service = mService; + if (service != null && isEnabled() && isValidDevice(device) && config != null) { try { - return mService.connectChannelToSink(device, config, channelType); + return service.connectChannelToSink(device, config, channelType); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -284,9 +288,10 @@ public final class BluetoothHealth implements BluetoothProfile { */ public boolean disconnectChannel(BluetoothDevice device, BluetoothHealthAppConfiguration config, int channelId) { - if (mService != null && isEnabled() && isValidDevice(device) && config != null) { + final IBluetoothHealth service = mService; + if (service != null && isEnabled() && isValidDevice(device) && config != null) { try { - return mService.disconnectChannel(device, config, channelId); + return service.disconnectChannel(device, config, channelId); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -312,9 +317,10 @@ public final class BluetoothHealth implements BluetoothProfile { */ public ParcelFileDescriptor getMainChannelFd(BluetoothDevice device, BluetoothHealthAppConfiguration config) { - if (mService != null && isEnabled() && isValidDevice(device) && config != null) { + final IBluetoothHealth service = mService; + if (service != null && isEnabled() && isValidDevice(device) && config != null) { try { - return mService.getMainChannelFd(device, config); + return service.getMainChannelFd(device, config); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -341,9 +347,10 @@ public final class BluetoothHealth implements BluetoothProfile { */ @Override public int getConnectionState(BluetoothDevice device) { - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothHealth service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getHealthDeviceConnectionState(device); + return service.getHealthDeviceConnectionState(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -370,15 +377,16 @@ public final class BluetoothHealth implements BluetoothProfile { */ @Override public List<BluetoothDevice> getConnectedDevices() { - if (mService != null && isEnabled()) { + final IBluetoothHealth service = mService; + if (service != null && isEnabled()) { try { - return mService.getConnectedHealthDevices(); + return service.getConnectedHealthDevices(); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -401,15 +409,16 @@ public final class BluetoothHealth implements BluetoothProfile { */ @Override public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { - if (mService != null && isEnabled()) { + final IBluetoothHealth service = mService; + if (service != null && isEnabled()) { try { - return mService.getHealthDevicesMatchingConnectionStates(states); + return service.getHealthDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -455,7 +464,7 @@ public final class BluetoothHealth implements BluetoothProfile { private Context mContext; private ServiceListener mServiceListener; - private IBluetoothHealth mService; + private volatile IBluetoothHealth mService; BluetoothAdapter mAdapter; /** @@ -540,11 +549,8 @@ public final class BluetoothHealth implements BluetoothProfile { return false; } - 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 boolean checkAppParam(String name, int role, int channelType, diff --git a/framework/java/android/bluetooth/BluetoothInputDevice.java b/framework/java/android/bluetooth/BluetoothInputDevice.java index a9a9010c7e..32615761cf 100644 --- a/framework/java/android/bluetooth/BluetoothInputDevice.java +++ b/framework/java/android/bluetooth/BluetoothInputDevice.java @@ -222,7 +222,7 @@ public final class BluetoothInputDevice implements BluetoothProfile { private Context mContext; private ServiceListener mServiceListener; private BluetoothAdapter mAdapter; - private IBluetoothInputDevice mService; + private volatile IBluetoothInputDevice mService; private final IBluetoothStateChangeCallback mBluetoothStateChangeCallback = new IBluetoothStateChangeCallback.Stub() { @@ -331,15 +331,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { */ public boolean connect(BluetoothDevice device) { if (DBG) log("connect(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.connect(device); + return service.connect(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -370,15 +371,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { */ public boolean disconnect(BluetoothDevice device) { if (DBG) log("disconnect(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.disconnect(device); + return service.disconnect(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -388,15 +390,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { @Override public List<BluetoothDevice> getConnectedDevices() { if (VDBG) log("getConnectedDevices()"); - if (mService != null && isEnabled()) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled()) { try { - return mService.getConnectedDevices(); + return service.getConnectedDevices(); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -406,15 +409,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { @Override public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { if (VDBG) log("getDevicesMatchingStates()"); - if (mService != null && isEnabled()) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled()) { try { - return mService.getDevicesMatchingConnectionStates(states); + return service.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -424,15 +428,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { @Override public int getConnectionState(BluetoothDevice device) { if (VDBG) log("getState(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getConnectionState(device); + return service.getConnectionState(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return BluetoothProfile.STATE_DISCONNECTED; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return BluetoothProfile.STATE_DISCONNECTED; } @@ -453,19 +458,20 @@ public final class BluetoothInputDevice implements BluetoothProfile { */ public boolean setPriority(BluetoothDevice device, int priority) { if (DBG) log("setPriority(" + device + ", " + priority + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { if (priority != BluetoothProfile.PRIORITY_OFF && priority != BluetoothProfile.PRIORITY_ON) { return false; } try { - return mService.setPriority(device, priority); + return service.setPriority(device, priority); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -484,15 +490,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { */ public int getPriority(BluetoothDevice device) { if (VDBG) log("getPriority(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getPriority(device); + return service.getPriority(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return BluetoothProfile.PRIORITY_OFF; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return BluetoothProfile.PRIORITY_OFF; } @@ -517,18 +524,13 @@ public final class BluetoothInputDevice 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()); } - /** * Initiate virtual unplug for a HID input device. * @@ -540,16 +542,17 @@ public final class BluetoothInputDevice implements BluetoothProfile { */ public boolean virtualUnplug(BluetoothDevice device) { if (DBG) log("virtualUnplug(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.virtualUnplug(device); + return service.virtualUnplug(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -565,15 +568,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { */ public boolean getProtocolMode(BluetoothDevice device) { if (VDBG) log("getProtocolMode(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getProtocolMode(device); + return service.getProtocolMode(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -588,15 +592,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { */ public boolean setProtocolMode(BluetoothDevice device, int protocolMode) { if (DBG) log("setProtocolMode(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.setProtocolMode(device, protocolMode); + return service.setProtocolMode(device, protocolMode); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -615,19 +620,19 @@ public final class BluetoothInputDevice implements BluetoothProfile { public boolean getReport(BluetoothDevice device, byte reportType, byte reportId, int bufferSize) { if (VDBG) { - log( - "getReport(" + device + "), reportType=" + reportType + " reportId=" + reportId - + "bufferSize=" + bufferSize); + log("getReport(" + device + "), reportType=" + reportType + " reportId=" + reportId + + "bufferSize=" + bufferSize); } - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getReport(device, reportType, reportId, bufferSize); + return service.getReport(device, reportType, reportId, bufferSize); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -644,15 +649,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { */ public boolean setReport(BluetoothDevice device, byte reportType, String report) { if (VDBG) log("setReport(" + device + "), reportType=" + reportType + " report=" + report); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.setReport(device, reportType, report); + return service.setReport(device, reportType, report); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -668,15 +674,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { */ public boolean sendData(BluetoothDevice device, String report) { if (DBG) log("sendData(" + device + "), report=" + report); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.sendData(device, report); + return service.sendData(device, report); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -691,15 +698,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { */ public boolean getIdleTime(BluetoothDevice device) { if (DBG) log("getIdletime(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getIdleTime(device); + return service.getIdleTime(device); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -715,15 +723,16 @@ public final class BluetoothInputDevice implements BluetoothProfile { */ public boolean setIdleTime(BluetoothDevice device, byte idleTime) { if (DBG) log("setIdletime(" + device + "), idleTime=" + idleTime); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothInputDevice service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.setIdleTime(device, idleTime); + return service.setIdleTime(device, idleTime); } catch (RemoteException e) { Log.e(TAG, "Stack:" + Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } diff --git a/framework/java/android/bluetooth/BluetoothInputHost.java b/framework/java/android/bluetooth/BluetoothInputHost.java index 15303dc794..37f04278d4 100644 --- a/framework/java/android/bluetooth/BluetoothInputHost.java +++ b/framework/java/android/bluetooth/BluetoothInputHost.java @@ -113,7 +113,7 @@ public final class BluetoothInputHost implements BluetoothProfile { private ServiceListener mServiceListener; - private IBluetoothInputHost mService; + private volatile IBluetoothInputHost mService; private BluetoothAdapter mAdapter; @@ -202,24 +202,18 @@ public final class BluetoothInputHost implements BluetoothProfile { } }; - private ServiceConnection mConnection = new ServiceConnection() { - + private final ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { Log.d(TAG, "onServiceConnected()"); - mService = IBluetoothInputHost.Stub.asInterface(service); - if (mServiceListener != null) { mServiceListener.onServiceConnected(BluetoothProfile.INPUT_HOST, BluetoothInputHost.this); } } - public void onServiceDisconnected(ComponentName className) { Log.d(TAG, "onServiceDisconnected()"); - mService = null; - if (mServiceListener != null) { mServiceListener.onServiceDisconnected(BluetoothProfile.INPUT_HOST); } @@ -291,9 +285,10 @@ public final class BluetoothInputHost implements BluetoothProfile { public List<BluetoothDevice> getConnectedDevices() { Log.v(TAG, "getConnectedDevices()"); - if (mService != null) { + final IBluetoothInputHost service = mService; + if (service != null) { try { - return mService.getConnectedDevices(); + return service.getConnectedDevices(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -311,9 +306,10 @@ public final class BluetoothInputHost implements BluetoothProfile { public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { Log.v(TAG, "getDevicesMatchingConnectionStates(): states=" + Arrays.toString(states)); - if (mService != null) { + final IBluetoothInputHost service = mService; + if (service != null) { try { - return mService.getDevicesMatchingConnectionStates(states); + return service.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -331,9 +327,10 @@ public final class BluetoothInputHost implements BluetoothProfile { public int getConnectionState(BluetoothDevice device) { Log.v(TAG, "getConnectionState(): device=" + device); - if (mService != null) { + final IBluetoothInputHost service = mService; + if (service != null) { try { - return mService.getConnectionState(device); + return service.getConnectionState(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -370,13 +367,14 @@ public final class BluetoothInputHost implements BluetoothProfile { return false; } - if (mService != null) { + final IBluetoothInputHost service = mService; + if (service != null) { try { BluetoothHidDeviceAppConfiguration config = new BluetoothHidDeviceAppConfiguration(); BluetoothHidDeviceCallbackWrapper cbw = new BluetoothHidDeviceCallbackWrapper(callback); - result = mService.registerApp(config, sdp, inQos, outQos, cbw); + result = service.registerApp(config, sdp, inQos, outQos, cbw); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -403,9 +401,10 @@ public final class BluetoothInputHost implements BluetoothProfile { boolean result = false; - if (mService != null) { + final IBluetoothInputHost service = mService; + if (service != null) { try { - result = mService.unregisterApp(config); + result = service.unregisterApp(config); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -427,9 +426,10 @@ public final class BluetoothInputHost implements BluetoothProfile { public boolean sendReport(BluetoothDevice device, int id, byte[] data) { boolean result = false; - if (mService != null) { + final IBluetoothInputHost service = mService; + if (service != null) { try { - result = mService.sendReport(device, id, data); + result = service.sendReport(device, id, data); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -454,9 +454,10 @@ public final class BluetoothInputHost implements BluetoothProfile { boolean result = false; - if (mService != null) { + final IBluetoothInputHost service = mService; + if (service != null) { try { - result = mService.replyReport(device, type, id, data); + result = service.replyReport(device, type, id, data); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -479,9 +480,10 @@ public final class BluetoothInputHost implements BluetoothProfile { boolean result = false; - if (mService != null) { + final IBluetoothInputHost service = mService; + if (service != null) { try { - result = mService.reportError(device, error); + result = service.reportError(device, error); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -502,9 +504,10 @@ public final class BluetoothInputHost implements BluetoothProfile { boolean result = false; - if (mService != null) { + final IBluetoothInputHost service = mService; + if (service != null) { try { - result = mService.unplug(device); + result = service.unplug(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -526,9 +529,10 @@ public final class BluetoothInputHost implements BluetoothProfile { boolean result = false; - if (mService != null) { + final IBluetoothInputHost service = mService; + if (service != null) { try { - result = mService.connect(device); + result = service.connect(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -549,9 +553,10 @@ public final class BluetoothInputHost implements BluetoothProfile { boolean result = false; - if (mService != null) { + final IBluetoothInputHost service = mService; + if (service != null) { try { - result = mService.disconnect(device); + result = service.disconnect(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } diff --git a/framework/java/android/bluetooth/BluetoothMap.java b/framework/java/android/bluetooth/BluetoothMap.java index 26a9106f49..5b55b23680 100644 --- a/framework/java/android/bluetooth/BluetoothMap.java +++ b/framework/java/android/bluetooth/BluetoothMap.java @@ -43,7 +43,7 @@ public final class BluetoothMap implements BluetoothProfile { public static final String ACTION_CONNECTION_STATE_CHANGED = "android.bluetooth.map.profile.action.CONNECTION_STATE_CHANGED"; - private IBluetoothMap mService; + private volatile IBluetoothMap mService; private final Context mContext; private ServiceListener mServiceListener; private BluetoothAdapter mAdapter; @@ -161,9 +161,10 @@ public final class BluetoothMap implements BluetoothProfile { */ public int getState() { if (VDBG) log("getState()"); - if (mService != null) { + final IBluetoothMap service = mService; + if (service != null) { try { - return mService.getState(); + return service.getState(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -182,9 +183,10 @@ public final class BluetoothMap implements BluetoothProfile { */ public BluetoothDevice getClient() { if (VDBG) log("getClient()"); - if (mService != null) { + final IBluetoothMap service = mService; + if (service != null) { try { - return mService.getClient(); + return service.getClient(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -202,9 +204,10 @@ public final class BluetoothMap implements BluetoothProfile { */ public boolean isConnected(BluetoothDevice device) { if (VDBG) log("isConnected(" + device + ")"); - if (mService != null) { + final IBluetoothMap service = mService; + if (service != null) { try { - return mService.isConnected(device); + return service.isConnected(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -232,15 +235,16 @@ public final class BluetoothMap implements BluetoothProfile { */ public boolean disconnect(BluetoothDevice device) { if (DBG) log("disconnect(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothMap service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.disconnect(device); + return service.disconnect(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -272,15 +276,16 @@ public final class BluetoothMap implements BluetoothProfile { */ public List<BluetoothDevice> getConnectedDevices() { if (DBG) log("getConnectedDevices()"); - if (mService != null && isEnabled()) { + final IBluetoothMap service = mService; + if (service != null && isEnabled()) { try { - return mService.getConnectedDevices(); + return service.getConnectedDevices(); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -291,15 +296,16 @@ public final class BluetoothMap implements BluetoothProfile { */ public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { if (DBG) log("getDevicesMatchingStates()"); - if (mService != null && isEnabled()) { + final IBluetoothMap service = mService; + if (service != null && isEnabled()) { try { - return mService.getDevicesMatchingConnectionStates(states); + return service.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -310,15 +316,16 @@ public final class BluetoothMap implements BluetoothProfile { */ public int getConnectionState(BluetoothDevice device) { if (DBG) log("getConnectionState(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothMap service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getConnectionState(device); + return service.getConnectionState(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return BluetoothProfile.STATE_DISCONNECTED; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return BluetoothProfile.STATE_DISCONNECTED; } @@ -335,19 +342,20 @@ public final class BluetoothMap implements BluetoothProfile { */ public boolean setPriority(BluetoothDevice device, int priority) { if (DBG) log("setPriority(" + device + ", " + priority + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothMap service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { if (priority != BluetoothProfile.PRIORITY_OFF && priority != BluetoothProfile.PRIORITY_ON) { return false; } try { - return mService.setPriority(device, priority); + return service.setPriority(device, priority); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -363,15 +371,16 @@ public final class BluetoothMap implements BluetoothProfile { */ public int getPriority(BluetoothDevice device) { if (VDBG) log("getPriority(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothMap service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getPriority(device); + return service.getPriority(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return PRIORITY_OFF; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return PRIORITY_OFF; } @@ -403,13 +412,8 @@ public final class BluetoothMap implements BluetoothProfile { log("Bluetooth is Not enabled"); return false; } - - 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()); } - } diff --git a/framework/java/android/bluetooth/BluetoothMapClient.java b/framework/java/android/bluetooth/BluetoothMapClient.java index 3e0c36548c..af3b662d6a 100644 --- a/framework/java/android/bluetooth/BluetoothMapClient.java +++ b/framework/java/android/bluetooth/BluetoothMapClient.java @@ -59,7 +59,7 @@ public final class BluetoothMapClient implements BluetoothProfile { public static final String EXTRA_SENDER_CONTACT_NAME = "android.bluetooth.mapmce.profile.extra.SENDER_CONTACT_NAME"; - private IBluetoothMapClient mService; + private volatile IBluetoothMapClient mService; private final Context mContext; private ServiceListener mServiceListener; private BluetoothAdapter mAdapter; @@ -176,9 +176,10 @@ public final class BluetoothMapClient implements BluetoothProfile { */ public boolean isConnected(BluetoothDevice device) { if (VDBG) Log.d(TAG, "isConnected(" + device + ")"); - if (mService != null) { + final IBluetoothMapClient service = mService; + if (service != null) { try { - return mService.isConnected(device); + return service.isConnected(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -195,9 +196,10 @@ public final class BluetoothMapClient implements BluetoothProfile { */ public boolean connect(BluetoothDevice device) { if (DBG) Log.d(TAG, "connect(" + device + ")" + "for MAPS MCE"); - if (mService != null) { + final IBluetoothMapClient service = mService; + if (service != null) { try { - return mService.connect(device); + return service.connect(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -216,14 +218,15 @@ public final class BluetoothMapClient implements BluetoothProfile { */ public boolean disconnect(BluetoothDevice device) { if (DBG) Log.d(TAG, "disconnect(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothMapClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.disconnect(device); + return service.disconnect(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -235,15 +238,16 @@ public final class BluetoothMapClient implements BluetoothProfile { @Override public List<BluetoothDevice> getConnectedDevices() { if (DBG) Log.d(TAG, "getConnectedDevices()"); - if (mService != null && isEnabled()) { + final IBluetoothMapClient service = mService; + if (service != null && isEnabled()) { try { - return mService.getConnectedDevices(); + return service.getConnectedDevices(); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<>(); } @@ -255,15 +259,16 @@ public final class BluetoothMapClient implements BluetoothProfile { @Override public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { if (DBG) Log.d(TAG, "getDevicesMatchingStates()"); - if (mService != null && isEnabled()) { + final IBluetoothMapClient service = mService; + if (service != null && isEnabled()) { try { - return mService.getDevicesMatchingConnectionStates(states); + return service.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<>(); } @@ -275,15 +280,16 @@ public final class BluetoothMapClient implements BluetoothProfile { @Override public int getConnectionState(BluetoothDevice device) { if (DBG) Log.d(TAG, "getConnectionState(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothMapClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getConnectionState(device); + return service.getConnectionState(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return BluetoothProfile.STATE_DISCONNECTED; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return BluetoothProfile.STATE_DISCONNECTED; } @@ -298,19 +304,20 @@ public final class BluetoothMapClient implements BluetoothProfile { */ public boolean setPriority(BluetoothDevice device, int priority) { if (DBG) Log.d(TAG, "setPriority(" + device + ", " + priority + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothMapClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { if (priority != BluetoothProfile.PRIORITY_OFF && priority != BluetoothProfile.PRIORITY_ON) { return false; } try { - return mService.setPriority(device, priority); + return service.setPriority(device, priority); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -326,15 +333,16 @@ public final class BluetoothMapClient implements BluetoothProfile { */ public int getPriority(BluetoothDevice device) { if (VDBG) Log.d(TAG, "getPriority(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothMapClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getPriority(device); + return service.getPriority(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return PRIORITY_OFF; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return PRIORITY_OFF; } @@ -353,9 +361,10 @@ public final class BluetoothMapClient implements BluetoothProfile { public boolean sendMessage(BluetoothDevice device, Uri[] contacts, String message, PendingIntent sentIntent, PendingIntent deliveredIntent) { if (DBG) Log.d(TAG, "sendMessage(" + device + ", " + contacts + ", " + message); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothMapClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.sendMessage(device, contacts, message, sentIntent, deliveredIntent); + return service.sendMessage(device, contacts, message, sentIntent, deliveredIntent); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; @@ -372,9 +381,10 @@ public final class BluetoothMapClient implements BluetoothProfile { */ public boolean getUnreadMessages(BluetoothDevice device) { if (DBG) Log.d(TAG, "getUnreadMessages(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothMapClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getUnreadMessages(device); + return service.getUnreadMessages(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; @@ -409,12 +419,8 @@ public final class BluetoothMapClient implements BluetoothProfile { return false; } - 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()); } - } 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) { diff --git a/framework/java/android/bluetooth/BluetoothPbap.java b/framework/java/android/bluetooth/BluetoothPbap.java index 78b7c7b7a7..19f5198ca7 100644 --- a/framework/java/android/bluetooth/BluetoothPbap.java +++ b/framework/java/android/bluetooth/BluetoothPbap.java @@ -68,7 +68,7 @@ public class BluetoothPbap { public static final String PBAP_STATE_CHANGED_ACTION = "android.bluetooth.pbap.intent.action.PBAP_STATE_CHANGED"; - private IBluetoothPbap mService; + private volatile IBluetoothPbap mService; private final Context mContext; private ServiceListener mServiceListener; private BluetoothAdapter mAdapter; @@ -214,9 +214,10 @@ public class BluetoothPbap { */ public int getState() { if (VDBG) log("getState()"); - if (mService != null) { + final IBluetoothPbap service = mService; + if (service != null) { try { - return mService.getState(); + return service.getState(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -235,9 +236,10 @@ public class BluetoothPbap { */ public BluetoothDevice getClient() { if (VDBG) log("getClient()"); - if (mService != null) { + final IBluetoothPbap service = mService; + if (service != null) { try { - return mService.getClient(); + return service.getClient(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -255,9 +257,10 @@ public class BluetoothPbap { */ public boolean isConnected(BluetoothDevice device) { if (VDBG) log("isConnected(" + device + ")"); - if (mService != null) { + final IBluetoothPbap service = mService; + if (service != null) { try { - return mService.isConnected(device); + return service.isConnected(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -275,9 +278,10 @@ public class BluetoothPbap { */ public boolean disconnect() { if (DBG) log("disconnect()"); - if (mService != null) { + final IBluetoothPbap service = mService; + if (service != null) { try { - mService.disconnect(); + service.disconnect(); return true; } catch (RemoteException e) { Log.e(TAG, e.toString()); diff --git a/framework/java/android/bluetooth/BluetoothPbapClient.java b/framework/java/android/bluetooth/BluetoothPbapClient.java index b9b372c848..00a15f3f70 100644 --- a/framework/java/android/bluetooth/BluetoothPbapClient.java +++ b/framework/java/android/bluetooth/BluetoothPbapClient.java @@ -42,7 +42,7 @@ public final class BluetoothPbapClient implements BluetoothProfile { public static final String ACTION_CONNECTION_STATE_CHANGED = "android.bluetooth.pbap.profile.action.CONNECTION_STATE_CHANGED"; - private IBluetoothPbapClient mService; + private volatile IBluetoothPbapClient mService; private final Context mContext; private ServiceListener mServiceListener; private BluetoothAdapter mAdapter; @@ -173,15 +173,16 @@ public final class BluetoothPbapClient implements BluetoothProfile { if (DBG) { log("connect(" + device + ") for PBAP Client."); } - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothPbapClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.connect(device); + return service.connect(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) { + if (service == null) { Log.w(TAG, "Proxy not attached to service"); } return false; @@ -197,16 +198,17 @@ public final class BluetoothPbapClient implements BluetoothProfile { if (DBG) { log("disconnect(" + device + ")" + new Exception()); } - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothPbapClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - mService.disconnect(device); + service.disconnect(device); return true; } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) { + if (service == null) { Log.w(TAG, "Proxy not attached to service"); } return false; @@ -223,15 +225,16 @@ public final class BluetoothPbapClient implements BluetoothProfile { if (DBG) { log("getConnectedDevices()"); } - if (mService != null && isEnabled()) { + final IBluetoothPbapClient service = mService; + if (service != null && isEnabled()) { try { - return mService.getConnectedDevices(); + return service.getConnectedDevices(); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) { + if (service == null) { Log.w(TAG, "Proxy not attached to service"); } return new ArrayList<BluetoothDevice>(); @@ -247,15 +250,16 @@ public final class BluetoothPbapClient implements BluetoothProfile { if (DBG) { log("getDevicesMatchingStates()"); } - if (mService != null && isEnabled()) { + final IBluetoothPbapClient service = mService; + if (service != null && isEnabled()) { try { - return mService.getDevicesMatchingConnectionStates(states); + return service.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) { + if (service == null) { Log.w(TAG, "Proxy not attached to service"); } return new ArrayList<BluetoothDevice>(); @@ -271,15 +275,16 @@ public final class BluetoothPbapClient implements BluetoothProfile { if (DBG) { log("getConnectionState(" + device + ")"); } - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothPbapClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getConnectionState(device); + return service.getConnectionState(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return BluetoothProfile.STATE_DISCONNECTED; } } - if (mService == null) { + if (service == null) { Log.w(TAG, "Proxy not attached to service"); } return BluetoothProfile.STATE_DISCONNECTED; @@ -321,14 +326,8 @@ public final class BluetoothPbapClient implements BluetoothProfile { return false; } - 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()); } /** @@ -339,26 +338,27 @@ public final class BluetoothPbapClient implements BluetoothProfile { * {@link #PRIORITY_OFF}, * * @param device Paired bluetooth device - * @param priority + * @param priority Priority of this profile * @return true if priority is set, false on error */ public boolean setPriority(BluetoothDevice device, int priority) { if (DBG) { log("setPriority(" + device + ", " + priority + ")"); } - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothPbapClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { if (priority != BluetoothProfile.PRIORITY_OFF && priority != BluetoothProfile.PRIORITY_ON) { return false; } try { - return mService.setPriority(device, priority); + return service.setPriority(device, priority); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) { + if (service == null) { Log.w(TAG, "Proxy not attached to service"); } return false; @@ -378,15 +378,16 @@ public final class BluetoothPbapClient implements BluetoothProfile { if (VDBG) { log("getPriority(" + device + ")"); } - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothPbapClient service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getPriority(device); + return service.getPriority(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return PRIORITY_OFF; } } - if (mService == null) { + if (service == null) { Log.w(TAG, "Proxy not attached to service"); } return PRIORITY_OFF; diff --git a/framework/java/android/bluetooth/BluetoothSap.java b/framework/java/android/bluetooth/BluetoothSap.java index bcdf4938fe..48481620c9 100644 --- a/framework/java/android/bluetooth/BluetoothSap.java +++ b/framework/java/android/bluetooth/BluetoothSap.java @@ -68,7 +68,7 @@ public final class BluetoothSap implements BluetoothProfile { public static final String ACTION_CONNECTION_STATE_CHANGED = "android.bluetooth.sap.profile.action.CONNECTION_STATE_CHANGED"; - private IBluetoothSap mService; + private volatile IBluetoothSap mService; private final Context mContext; private ServiceListener mServiceListener; private BluetoothAdapter mAdapter; @@ -202,9 +202,10 @@ public final class BluetoothSap implements BluetoothProfile { */ public int getState() { if (VDBG) log("getState()"); - if (mService != null) { + final IBluetoothSap service = mService; + if (service != null) { try { - return mService.getState(); + return service.getState(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -224,9 +225,10 @@ public final class BluetoothSap implements BluetoothProfile { */ public BluetoothDevice getClient() { if (VDBG) log("getClient()"); - if (mService != null) { + final IBluetoothSap service = mService; + if (service != null) { try { - return mService.getClient(); + return service.getClient(); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -246,9 +248,10 @@ public final class BluetoothSap implements BluetoothProfile { */ public boolean isConnected(BluetoothDevice device) { if (VDBG) log("isConnected(" + device + ")"); - if (mService != null) { + final IBluetoothSap service = mService; + if (service != null) { try { - return mService.isConnected(device); + return service.isConnected(device); } catch (RemoteException e) { Log.e(TAG, e.toString()); } @@ -279,15 +282,16 @@ public final class BluetoothSap implements BluetoothProfile { */ public boolean disconnect(BluetoothDevice device) { if (DBG) log("disconnect(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothSap service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.disconnect(device); + return service.disconnect(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -299,15 +303,16 @@ public final class BluetoothSap implements BluetoothProfile { */ public List<BluetoothDevice> getConnectedDevices() { if (DBG) log("getConnectedDevices()"); - if (mService != null && isEnabled()) { + final IBluetoothSap service = mService; + if (service != null && isEnabled()) { try { - return mService.getConnectedDevices(); + return service.getConnectedDevices(); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -319,15 +324,16 @@ public final class BluetoothSap implements BluetoothProfile { */ public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { if (DBG) log("getDevicesMatchingStates()"); - if (mService != null && isEnabled()) { + final IBluetoothSap service = mService; + if (service != null && isEnabled()) { try { - return mService.getDevicesMatchingConnectionStates(states); + return service.getDevicesMatchingConnectionStates(states); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return new ArrayList<BluetoothDevice>(); } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return new ArrayList<BluetoothDevice>(); } @@ -339,15 +345,16 @@ public final class BluetoothSap implements BluetoothProfile { */ public int getConnectionState(BluetoothDevice device) { if (DBG) log("getConnectionState(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothSap service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getConnectionState(device); + return service.getConnectionState(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return BluetoothProfile.STATE_DISCONNECTED; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return BluetoothProfile.STATE_DISCONNECTED; } @@ -363,19 +370,20 @@ public final class BluetoothSap implements BluetoothProfile { */ public boolean setPriority(BluetoothDevice device, int priority) { if (DBG) log("setPriority(" + device + ", " + priority + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothSap service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { if (priority != BluetoothProfile.PRIORITY_OFF && priority != BluetoothProfile.PRIORITY_ON) { return false; } try { - return mService.setPriority(device, priority); + return service.setPriority(device, priority); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return false; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return false; } @@ -388,19 +396,20 @@ public final class BluetoothSap implements BluetoothProfile { */ public int getPriority(BluetoothDevice device) { if (VDBG) log("getPriority(" + device + ")"); - if (mService != null && isEnabled() && isValidDevice(device)) { + final IBluetoothSap service = mService; + if (service != null && isEnabled() && isValidDevice(device)) { try { - return mService.getPriority(device); + return service.getPriority(device); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(new Throwable())); return PRIORITY_OFF; } } - if (mService == null) Log.w(TAG, "Proxy not attached to service"); + if (service == null) Log.w(TAG, "Proxy not attached to service"); return PRIORITY_OFF; } - private ServiceConnection mConnection = new ServiceConnection() { + private final ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { if (DBG) log("Proxy object connected"); mService = IBluetoothSap.Stub.asInterface(Binder.allowBlocking(service)); @@ -432,15 +441,8 @@ public final class BluetoothSap implements BluetoothProfile { return false; } - 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()); } } |