summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
authorLee Shombert <shombert@google.com>2020-04-09 09:00:46 -0700
committerLee Shombert <shombert@google.com>2020-04-15 13:58:55 -0700
commit6d1fa6f65a1c7c0617bc79b754e8f19fbff8e838 (patch)
treeb952878db7cbaa92d2e8e3d4338e7df52011f57b /core/java/android
parentd3f12e835310b0f5b0710fbf139ad24dc8e7187d (diff)
Fix exception handling in getState() binder cache
Bug: 153103051 A binder cache recompute() function cannot compute a result based on any data that is not known to the binder server. If the code depends on local data that can change, then invalidation will not work properly. The original getState() method returned OFF if the bluetooth service was unavailable. This computation now occurs in the getStateInternal() method, outside of the binder cache query() method. The recompute() method converts RemoteExceptions to RuntimeExceptions. Then, the conversion is reversed in getStateInternal(). This double conversion is needed because the cache recompute() method has no throw spec. Test: Create a debug image that enables binder cache VERIFY. Run the following tests: * atest BluetoothInstrumentationTests * atest PtsChreTestCases * atest UserLifecycleTests * manual testing connecting to bluetooth devices and toggling airplane mode. No cache inconsistencies found. No test failures seen. Change-Id: I93b9742587c4eb695d9a11fc6ab145f6a40a0ece
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/bluetooth/BluetoothAdapter.java38
1 files changed, 28 insertions, 10 deletions
diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java
index f216db6fc717..29a98faf5cd1 100644
--- a/core/java/android/bluetooth/BluetoothAdapter.java
+++ b/core/java/android/bluetooth/BluetoothAdapter.java
@@ -980,16 +980,10 @@ public final class BluetoothAdapter {
@Override
protected Integer recompute(Void query) {
try {
- mServiceLock.readLock().lock();
- if (mService != null) {
- return mService.getState();
- }
+ return mService.getState();
} catch (RemoteException e) {
- Log.e(TAG, "", e);
- } finally {
- mServiceLock.readLock().unlock();
+ throw e.rethrowFromSystemServer();
}
- return BluetoothAdapter.STATE_OFF;
}
};
@@ -1004,6 +998,30 @@ public final class BluetoothAdapter {
}
/**
+ * Fetch the current bluetooth state. If the service is down, return
+ * OFF.
+ */
+ @AdapterState
+ private int getStateInternal() {
+ int state = BluetoothAdapter.STATE_OFF;
+ try {
+ mServiceLock.readLock().lock();
+ if (mService != null) {
+ state = mBluetoothGetStateCache.query(null);
+ }
+ } catch (RuntimeException e) {
+ if (e.getCause() instanceof RemoteException) {
+ Log.e(TAG, "", e.getCause());
+ } else {
+ throw e;
+ }
+ } finally {
+ mServiceLock.readLock().unlock();
+ }
+ return state;
+ }
+
+ /**
* Get the current state of the local Bluetooth adapter.
* <p>Possible return values are
* {@link #STATE_OFF},
@@ -1016,7 +1034,7 @@ public final class BluetoothAdapter {
@RequiresPermission(Manifest.permission.BLUETOOTH)
@AdapterState
public int getState() {
- int state = mBluetoothGetStateCache.query(null);
+ int state = getStateInternal();
// Consider all internal states as OFF
if (state == BluetoothAdapter.STATE_BLE_ON || state == BluetoothAdapter.STATE_BLE_TURNING_ON
@@ -1054,7 +1072,7 @@ public final class BluetoothAdapter {
@UnsupportedAppUsage(publicAlternatives = "Use {@link #getState()} instead to determine "
+ "whether you can use BLE & BT classic.")
public int getLeState() {
- int state = mBluetoothGetStateCache.query(null);
+ int state = getStateInternal();
if (VDBG) {
Log.d(TAG, "getLeState() returning " + BluetoothAdapter.nameForState(state));