diff options
author | Brian Orr <brianorr@google.com> | 2022-05-15 21:53:53 +0000 |
---|---|---|
committer | Brian Orr <brianorr@google.com> | 2022-05-15 21:58:48 +0000 |
commit | 7f249f8a08052d03bda4ebb8f55907fff1b8ee2e (patch) | |
tree | 8fa3abea83f8c1b720ecf8987b268315fde0c334 /framework/java/android/bluetooth/BluetoothAdapter.java | |
parent | 38b36958966ff4a561cc1b6f9e8df87ae79793e0 (diff) | |
parent | 8b91c4ad7fae67132ca52a62ba74b11f8bd49f3d (diff) |
Merge TP1A.220507.001
Change-Id: I0474d4c5321a9bca5e519669c5e7f8eb3aee821d
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothAdapter.java')
-rw-r--r-- | framework/java/android/bluetooth/BluetoothAdapter.java | 218 |
1 files changed, 121 insertions, 97 deletions
diff --git a/framework/java/android/bluetooth/BluetoothAdapter.java b/framework/java/android/bluetooth/BluetoothAdapter.java index 509af74297..4d96ebc518 100644 --- a/framework/java/android/bluetooth/BluetoothAdapter.java +++ b/framework/java/android/bluetooth/BluetoothAdapter.java @@ -1273,36 +1273,27 @@ public final class BluetoothAdapter { IpcDataCache.invalidateCache(IpcDataCache.MODULE_BLUETOOTH, api); } - /** - * The binder cache for getState(). - */ - private static final String GET_STATE_API = "getState"; - - private final IpcDataCache.QueryHandler<Void, Integer> mBluetoothGetStateQuery = + private final IpcDataCache.QueryHandler<IBluetooth, Integer> mBluetoothGetStateQuery = new IpcDataCache.QueryHandler<>() { - @RequiresLegacyBluetoothPermission - @RequiresNoPermission - @Override - public @InternalAdapterState Integer apply(Void query) { - int state = BluetoothAdapter.STATE_OFF; - mServiceLock.readLock().lock(); - try { - if (mService != null) { - final SynchronousResultReceiver<Integer> recv = - new SynchronousResultReceiver(); - mService.getState(recv); - return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(state); + @RequiresLegacyBluetoothPermission + @RequiresNoPermission + @Override + public @InternalAdapterState Integer apply(IBluetooth serviceQuery) { + try { + final SynchronousResultReceiver<Integer> recv = new SynchronousResultReceiver(); + serviceQuery.getState(recv); + return recv.awaitResultNoInterrupt(getSyncTimeout()) + .getValue(BluetoothAdapter.STATE_OFF); + } catch (RemoteException | TimeoutException e) { + throw new RuntimeException(e); } - } catch (RemoteException | TimeoutException e) { - Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); - } finally { - mServiceLock.readLock().unlock(); } - return state; - }}; + }; - private final IpcDataCache<Void, Integer> mBluetoothGetStateCache = - new BluetoothCache<Void, Integer>(GET_STATE_API, mBluetoothGetStateQuery); + private static final String GET_STATE_API = "BluetoothAdapter_getState"; + + private final IpcDataCache<IBluetooth, Integer> mBluetoothGetStateCache = + new BluetoothCache<>(GET_STATE_API, mBluetoothGetStateQuery); /** @hide */ @RequiresNoPermission @@ -1320,7 +1311,21 @@ public final class BluetoothAdapter { * OFF. */ private @InternalAdapterState int getStateInternal() { - return mBluetoothGetStateCache.query(null); + mServiceLock.readLock().lock(); + try { + if (mService != null) { + return mBluetoothGetStateCache.query(mService); + } + } catch (RuntimeException e) { + if (!(e.getCause() instanceof TimeoutException) + && !(e.getCause() instanceof RemoteException)) { + throw e; + } + Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); + } finally { + mServiceLock.readLock().unlock(); + } + return STATE_OFF; } /** @@ -2440,31 +2445,25 @@ public final class BluetoothAdapter { } } - private final IpcDataCache.QueryHandler<Void, Boolean> mBluetoothFilteringQuery = + private final IpcDataCache.QueryHandler<IBluetooth, Boolean> mBluetoothFilteringQuery = new IpcDataCache.QueryHandler<>() { @RequiresLegacyBluetoothPermission @RequiresNoPermission @Override - public Boolean apply(Void query) { - mServiceLock.readLock().lock(); + public Boolean apply(IBluetooth serviceQuery) { try { - if (mService != null) { - final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver(); - mService.isOffloadedFilteringSupported(recv); - return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(false); - } + final SynchronousResultReceiver<Boolean> recv = new SynchronousResultReceiver(); + serviceQuery.isOffloadedFilteringSupported(recv); + return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(false); } catch (RemoteException | TimeoutException e) { - Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); - } finally { - mServiceLock.readLock().unlock(); + throw new RuntimeException(e); } - return false; }}; - private static final String FILTERING_API = "isOffloadedFilteringSupported"; + private static final String FILTERING_API = "BluetoothAdapter_isOffloadedFilteringSupported"; - private final IpcDataCache<Void, Boolean> mBluetoothFilteringCache = - new BluetoothCache<Void, Boolean>(FILTERING_API, mBluetoothFilteringQuery); + private final IpcDataCache<IBluetooth, Boolean> mBluetoothFilteringCache = + new BluetoothCache<>(FILTERING_API, mBluetoothFilteringQuery); /** @hide */ @RequiresNoPermission @@ -2488,7 +2487,19 @@ public final class BluetoothAdapter { if (!getLeAccess()) { return false; } - return mBluetoothFilteringCache.query(null); + mServiceLock.readLock().lock(); + try { + if (mService != null) return mBluetoothFilteringCache.query(mService); + } catch (RuntimeException e) { + if (!(e.getCause() instanceof TimeoutException) + && !(e.getCause() instanceof RemoteException)) { + throw e; + } + Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); + } finally { + mServiceLock.readLock().unlock(); + } + return false; } /** @@ -2998,35 +3009,28 @@ public final class BluetoothAdapter { return supportedProfiles; } - private final IpcDataCache.QueryHandler<Void, Integer> mBluetoothGetAdapterQuery = - new IpcDataCache.QueryHandler<>() { - @RequiresLegacyBluetoothPermission - @RequiresNoPermission - @Override - public Integer apply(Void query) { - mServiceLock.readLock().lock(); - try { - if (mService != null) { - final SynchronousResultReceiver<Integer> recv = - new SynchronousResultReceiver(); - mService.getAdapterConnectionState(recv); - return recv.awaitResultNoInterrupt(getSyncTimeout()) + private final IpcDataCache.QueryHandler<IBluetooth, Integer> + mBluetoothGetAdapterConnectionStateQuery = new IpcDataCache.QueryHandler<>() { + @RequiresLegacyBluetoothPermission + @RequiresNoPermission + @Override + public Integer apply(IBluetooth serviceQuery) { + try { + final SynchronousResultReceiver<Integer> recv = + new SynchronousResultReceiver(); + serviceQuery.getAdapterConnectionState(recv); + return recv.awaitResultNoInterrupt(getSyncTimeout()) .getValue(STATE_DISCONNECTED); + } catch (RemoteException | TimeoutException e) { + throw new RuntimeException(e); + } } - } catch (TimeoutException e) { - Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); - } catch (RemoteException e) { - Log.e(TAG, "failed to getConnectionState, error: ", e); - } finally { - mServiceLock.readLock().unlock(); - } - return BluetoothAdapter.STATE_DISCONNECTED; - }}; + }; + + private static final String GET_CONNECTION_API = "BluetoothAdapter_getConnectionState"; - private static final String GET_CONNECTION_API = "getAdapterConnectionState"; - private final IpcDataCache<Void, Integer> - mBluetoothGetAdapterConnectionStateCache = - new BluetoothCache<Void, Integer>(GET_CONNECTION_API, mBluetoothGetAdapterQuery); + private final IpcDataCache<IBluetooth, Integer> mBluetoothGetAdapterConnectionStateCache = + new BluetoothCache<>(GET_CONNECTION_API, mBluetoothGetAdapterConnectionStateQuery); /** @hide */ @RequiresNoPermission @@ -3056,36 +3060,42 @@ public final class BluetoothAdapter { if (getState() != STATE_ON) { return BluetoothAdapter.STATE_DISCONNECTED; } - return mBluetoothGetAdapterConnectionStateCache.query(null); + mServiceLock.readLock().lock(); + try { + if (mService != null) return mBluetoothGetAdapterConnectionStateCache.query(mService); + } catch (RuntimeException e) { + if (!(e.getCause() instanceof TimeoutException) + && !(e.getCause() instanceof RemoteException)) { + throw e; + } + Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); + } finally { + mServiceLock.readLock().unlock(); + } + return STATE_DISCONNECTED; } - private final IpcDataCache.QueryHandler<Integer, Integer> mBluetoothProfileQuery = - new IpcDataCache.QueryHandler<>() { - @RequiresNoPermission - @Override - public Integer apply(Integer query) { - try { - mServiceLock.readLock().lock(); - if (mService != null) { - final SynchronousResultReceiver<Integer> recv = new SynchronousResultReceiver(); - mService.getProfileConnectionState(query, recv); - return recv.awaitResultNoInterrupt(getSyncTimeout()) - .getValue(BluetoothProfile.STATE_DISCONNECTED); + private final IpcDataCache.QueryHandler<Pair<IBluetooth, Integer>, Integer> + mBluetoothProfileQuery = new IpcDataCache.QueryHandler<>() { + @RequiresNoPermission + @Override + public Integer apply(Pair<IBluetooth, Integer> pairQuery) { + final int defaultValue = STATE_DISCONNECTED; + try { + final SynchronousResultReceiver<Integer> recv = + new SynchronousResultReceiver(); + pairQuery.first.getProfileConnectionState(pairQuery.second, recv); + return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue); + } catch (RemoteException | TimeoutException e) { + throw new RuntimeException(e); + } } - } catch (TimeoutException e) { - Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); - } catch (RemoteException e) { - Log.e(TAG, "failed to getProfileConnectionState, error: ", e); - } finally { - mServiceLock.readLock().unlock(); - } - return BluetoothProfile.STATE_DISCONNECTED; - }}; + }; + + private static final String PROFILE_API = "BluetoothAdapter_getProfileConnectionState"; - private static final String PROFILE_API = "getProfileConnectionState"; - private final IpcDataCache<Integer, Integer> - mGetProfileConnectionStateCache = - new BluetoothCache<Integer, Integer>(PROFILE_API, mBluetoothProfileQuery); + private final IpcDataCache<Pair<IBluetooth, Integer>, Integer> mGetProfileConnectionStateCache = + new BluetoothCache<>(PROFILE_API, mBluetoothProfileQuery); /** * @hide @@ -3117,9 +3127,23 @@ public final class BluetoothAdapter { public @ConnectionState int getProfileConnectionState(int profile) { android.util.SeempLog.record(64); if (getState() != STATE_ON) { - return BluetoothProfile.STATE_DISCONNECTED; + return STATE_DISCONNECTED; + } + mServiceLock.readLock().lock(); + try { + if (mService != null) { + return mGetProfileConnectionStateCache.query(new Pair<>(mService, profile)); + } + } catch (RuntimeException e) { + if (!(e.getCause() instanceof TimeoutException) + && !(e.getCause() instanceof RemoteException)) { + throw e; + } + Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); + } finally { + mServiceLock.readLock().unlock(); } - return mGetProfileConnectionStateCache.query(profile); + return STATE_DISCONNECTED; } /** |