summaryrefslogtreecommitdiff
path: root/framework/java/android/bluetooth/BluetoothSap.java
diff options
context:
space:
mode:
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothSap.java')
-rw-r--r--framework/java/android/bluetooth/BluetoothSap.java73
1 files changed, 66 insertions, 7 deletions
diff --git a/framework/java/android/bluetooth/BluetoothSap.java b/framework/java/android/bluetooth/BluetoothSap.java
index 50c0edf442..100f1d8605 100644
--- a/framework/java/android/bluetooth/BluetoothSap.java
+++ b/framework/java/android/bluetooth/BluetoothSap.java
@@ -33,9 +33,11 @@ import android.content.AttributionSource;
import android.content.Context;
import android.os.Build;
import android.os.IBinder;
+import android.os.IpcDataCache;
import android.os.RemoteException;
import android.util.CloseGuard;
import android.util.Log;
+import android.util.Pair;
import com.android.modules.utils.SynchronousResultReceiver;
@@ -351,6 +353,62 @@ public final class BluetoothSap implements BluetoothProfile, AutoCloseable {
}
/**
+ * There are several instances of IpcDataCache used in this class.
+ * BluetoothCache wraps up the common code. All caches are created with a maximum of
+ * eight entries, and the key is in the bluetooth module. The name is set to the api.
+ */
+ private static class BluetoothCache<Q, R> extends IpcDataCache<Q, R> {
+ BluetoothCache(String api, IpcDataCache.QueryHandler query) {
+ super(8, IpcDataCache.MODULE_BLUETOOTH, api, api, query);
+ }};
+
+ /** @hide */
+ public void disableBluetoothGetConnectionStateCache() {
+ mBluetoothConnectionCache.disableForCurrentProcess();
+ }
+
+ /** @hide */
+ public static void invalidateBluetoothGetConnectionStateCache() {
+ invalidateCache(GET_CONNECTION_STATE_API);
+ }
+
+ /**
+ * Invalidate a bluetooth cache. This method is just a short-hand wrapper that
+ * enforces the bluetooth module.
+ */
+ private static void invalidateCache(@NonNull String api) {
+ IpcDataCache.invalidateCache(IpcDataCache.MODULE_BLUETOOTH, api);
+ }
+
+ private final IpcDataCache.QueryHandler<Pair<IBluetoothSap, BluetoothDevice>, Integer>
+ mBluetoothConnectionQuery = new IpcDataCache.QueryHandler<>() {
+ @RequiresBluetoothConnectPermission
+ @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
+ @Override
+ public Integer apply(Pair<IBluetoothSap, BluetoothDevice> pairQuery) {
+ if (DBG) {
+ log("getConnectionState(" + pairQuery.second.getAnonymizedAddress()
+ + ") uncached");
+ }
+ final SynchronousResultReceiver<Integer> recv = new SynchronousResultReceiver();
+ try {
+ pairQuery.first
+ .getConnectionState(pairQuery.second, mAttributionSource, recv);
+ return recv.awaitResultNoInterrupt(getSyncTimeout())
+ .getValue(BluetoothProfile.STATE_DISCONNECTED);
+ } catch (RemoteException | TimeoutException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ };
+
+ private static final String GET_CONNECTION_STATE_API = "BluetoothSap_getConnectionState";
+
+ private final BluetoothCache<Pair<IBluetoothSap, BluetoothDevice>, Integer>
+ mBluetoothConnectionCache = new BluetoothCache<>(GET_CONNECTION_STATE_API,
+ mBluetoothConnectionQuery);
+
+ /**
* Get connection state of device
*
* @return device connection state
@@ -361,20 +419,21 @@ public final class BluetoothSap implements BluetoothProfile, AutoCloseable {
public int getConnectionState(BluetoothDevice device) {
if (DBG) log("getConnectionState(" + device + ")");
final IBluetoothSap service = getService();
- final int defaultValue = BluetoothProfile.STATE_DISCONNECTED;
if (service == null) {
- Log.w(TAG, "Proxy not attached to service");
+ Log.w(TAG, "BT not enabled. Cannot get connection state");
if (DBG) log(Log.getStackTraceString(new Throwable()));
} else if (isEnabled() && isValidDevice(device)) {
try {
- final SynchronousResultReceiver<Integer> recv = new SynchronousResultReceiver();
- service.getConnectionState(device, mAttributionSource, recv);
- return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue);
- } catch (RemoteException | TimeoutException e) {
+ return mBluetoothConnectionCache.query(new Pair<>(service, device));
+ } 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 defaultValue;
+ return BluetoothProfile.STATE_DISCONNECTED;
}
/**