diff options
author | William Escande <wescande@google.com> | 2022-04-22 15:49:39 -0700 |
---|---|---|
committer | William Escande <wescande@google.com> | 2022-04-29 22:39:43 +0000 |
commit | 25fc56ed6a3506d3c83cd01803a3b34e5ffff4ca (patch) | |
tree | 3664a30b0cee81565ed8ab3044e7650597f499b4 /framework/java/android/bluetooth/BluetoothDevice.java | |
parent | 89f4da04fe1059d734c11247e0167f4685194c82 (diff) |
Add caching for {Map,Sap}.getConnectionState
BluetoothMap and BluetoothSap are heavily using their getConnectionState
method. By caching the value I intend to reduce drastically the number
of binder calls generated.
While performing a discovery, the caching is working on 80% of binder
calls. This value should be bigger when we kepp asking the value for the
same device. (currently everytime you ask the connection state of a new
device, you refresh the cache, there is no caching for multiples
devices).
I don't have any Map/Sap setup to test it, asking to test team if they
hit some issues
Fixing cache on BluetoothDevice by passing the device as parameter
Doing a refactoring of all cached method in bluetooth to keep code
consistency: AKA we try to do the most of check outside of the cache
query
Bug: 217366135
Test: Put some log to see the value being cached sometimes - need more testing
Ignore-AOSP-First: No caching api on AOSP
Change-Id: Iababf9f9068a181e277b400e786a4a67d4447dc8
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothDevice.java')
-rw-r--r-- | framework/java/android/bluetooth/BluetoothDevice.java | 65 |
1 files changed, 35 insertions, 30 deletions
diff --git a/framework/java/android/bluetooth/BluetoothDevice.java b/framework/java/android/bluetooth/BluetoothDevice.java index d1e33c4454..e20165f522 100644 --- a/framework/java/android/bluetooth/BluetoothDevice.java +++ b/framework/java/android/bluetooth/BluetoothDevice.java @@ -45,6 +45,7 @@ import android.os.Parcelable; import android.os.Process; import android.os.RemoteException; import android.util.Log; +import android.util.Pair; import com.android.modules.utils.SynchronousResultReceiver; @@ -1903,43 +1904,32 @@ public final class BluetoothDevice implements Parcelable, Attributable { IpcDataCache.invalidateCache(IpcDataCache.MODULE_BLUETOOTH, api); } - private final - IpcDataCache.QueryHandler<BluetoothDevice, Integer> mBluetoothBondQuery = - new IpcDataCache.QueryHandler<>() { + private final IpcDataCache.QueryHandler<Pair<IBluetooth, BluetoothDevice>, Integer> + mBluetoothBondQuery = new IpcDataCache.QueryHandler<>() { @RequiresLegacyBluetoothPermission @RequiresBluetoothConnectPermission @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) @Override - public Integer apply(BluetoothDevice query) { - if (DBG) log("getBondState() uncached"); - final IBluetooth service = sService; - final int defaultValue = BOND_NONE; - if (service == null) { - Log.e(TAG, "BT not enabled. Cannot get bond state"); - if (DBG) log(Log.getStackTraceString(new Throwable())); - } else { - try { - final SynchronousResultReceiver<Integer> recv = - new SynchronousResultReceiver(); - service.getBondState(BluetoothDevice.this, mAttributionSource, recv); - return recv.awaitResultNoInterrupt(getSyncTimeout()) - .getValue(defaultValue); - } catch (TimeoutException e) { - Log.e(TAG, e.toString() + "\n" - + Log.getStackTraceString(new Throwable())); - } catch (RemoteException e) { - Log.e(TAG, "failed to ", e); - e.rethrowFromSystemServer(); - } + public Integer apply(Pair<IBluetooth, BluetoothDevice> pairQuery) { + if (DBG) { + log("getConnectionState(" + pairQuery.second.getAnonymizedAddress() + + ") uncached"); + } + try { + final SynchronousResultReceiver<Integer> recv = + new SynchronousResultReceiver(); + pairQuery.first.getBondState(pairQuery.second, mAttributionSource, recv); + return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(BOND_NONE); + } catch (RemoteException | TimeoutException e) { + throw new RuntimeException(e); } - return defaultValue; } }; - private static final String GET_BOND_STATE_API = "getBondState"; + private static final String GET_BOND_STATE_API = "BluetoothDevice_getBondState"; - private final BluetoothCache<BluetoothDevice, Integer> mBluetoothBondCache = - new BluetoothCache<BluetoothDevice, Integer>(GET_BOND_STATE_API, mBluetoothBondQuery); + private final BluetoothCache<Pair<IBluetooth, BluetoothDevice>, Integer> mBluetoothBondCache = + new BluetoothCache<>(GET_BOND_STATE_API, mBluetoothBondQuery); /** @hide */ public void disableBluetoothGetBondStateCache() { @@ -1965,8 +1955,23 @@ public final class BluetoothDevice implements Parcelable, Attributable { @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) @SuppressLint("AndroidFrameworkRequiresPermission") public int getBondState() { - if (DBG) log("getBondState()"); - return mBluetoothBondCache.query(null); + if (DBG) log("getBondState(" + getAnonymizedAddress() + ")"); + final IBluetooth service = sService; + if (service == null) { + Log.e(TAG, "BT not enabled. Cannot get bond state"); + if (DBG) log(Log.getStackTraceString(new Throwable())); + } else { + try { + return mBluetoothBondCache.query(new Pair<>(service, BluetoothDevice.this)); + } catch (RuntimeException e) { + if (!(e.getCause() instanceof TimeoutException) + && !(e.getCause() instanceof RemoteException)) { + throw e; + } + Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); + } + } + return BOND_NONE; } /** |