Merge "Fix exception handling in getState() binder cache" into rvc-dev am: 6e17bc7fae am: 3bfe58bd5e

Change-Id: I68d37b6591f6725a0dfba4ddefafe80a197ecf28
This commit is contained in:
Lee Shombert
2020-04-16 16:26:15 +00:00
committed by Automerger Merge Worker

View File

@@ -980,16 +980,10 @@ public final class BluetoothAdapter {
@Override @Override
protected Integer recompute(Void query) { protected Integer recompute(Void query) {
try { try {
mServiceLock.readLock().lock(); return mService.getState();
if (mService != null) {
return mService.getState();
}
} catch (RemoteException e) { } catch (RemoteException e) {
Log.e(TAG, "", e); throw e.rethrowFromSystemServer();
} finally {
mServiceLock.readLock().unlock();
} }
return BluetoothAdapter.STATE_OFF;
} }
}; };
@@ -1003,6 +997,30 @@ public final class BluetoothAdapter {
PropertyInvalidatedCache.invalidateCache(BLUETOOTH_GET_STATE_CACHE_PROPERTY); PropertyInvalidatedCache.invalidateCache(BLUETOOTH_GET_STATE_CACHE_PROPERTY);
} }
/**
* 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. * Get the current state of the local Bluetooth adapter.
* <p>Possible return values are * <p>Possible return values are
@@ -1016,7 +1034,7 @@ public final class BluetoothAdapter {
@RequiresPermission(Manifest.permission.BLUETOOTH) @RequiresPermission(Manifest.permission.BLUETOOTH)
@AdapterState @AdapterState
public int getState() { public int getState() {
int state = mBluetoothGetStateCache.query(null); int state = getStateInternal();
// Consider all internal states as OFF // Consider all internal states as OFF
if (state == BluetoothAdapter.STATE_BLE_ON || state == BluetoothAdapter.STATE_BLE_TURNING_ON 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 " @UnsupportedAppUsage(publicAlternatives = "Use {@link #getState()} instead to determine "
+ "whether you can use BLE & BT classic.") + "whether you can use BLE & BT classic.")
public int getLeState() { public int getLeState() {
int state = mBluetoothGetStateCache.query(null); int state = getStateInternal();
if (VDBG) { if (VDBG) {
Log.d(TAG, "getLeState() returning " + BluetoothAdapter.nameForState(state)); Log.d(TAG, "getLeState() returning " + BluetoothAdapter.nameForState(state));