diff options
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothDevice.java')
-rw-r--r-- | framework/java/android/bluetooth/BluetoothDevice.java | 121 |
1 files changed, 74 insertions, 47 deletions
diff --git a/framework/java/android/bluetooth/BluetoothDevice.java b/framework/java/android/bluetooth/BluetoothDevice.java index 9caed4ccac..f25af1d9a6 100644 --- a/framework/java/android/bluetooth/BluetoothDevice.java +++ b/framework/java/android/bluetooth/BluetoothDevice.java @@ -60,7 +60,7 @@ import android.annotation.RequiresPermission; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; import android.annotation.SuppressLint; -import android.annotation.SystemApi; //import android.app.PropertyInvalidatedCache; +import android.annotation.SystemApi; import android.bluetooth.annotations.RequiresBluetoothConnectPermission; import android.bluetooth.annotations.RequiresBluetoothLocationPermission; import android.bluetooth.annotations.RequiresBluetoothScanPermission; @@ -72,6 +72,7 @@ import android.content.AttributionSource; import android.content.Context; import android.os.Build; import android.os.Handler; +import android.os.IpcDataCache; import android.os.Parcel; import android.os.ParcelUuid; import android.os.Parcelable; @@ -310,7 +311,8 @@ public final class BluetoothDevice implements Parcelable, Attributable { * Used as an Integer extra field in {@link #ACTION_BATTERY_LEVEL_CHANGED} * intent. It contains the most recently retrieved battery level information * ranging from 0% to 100% for a remote device, {@link #BATTERY_LEVEL_UNKNOWN} - * when the valid is unknown or there is an error + * when the valid is unknown or there is an error, {@link #BATTERY_LEVEL_BLUETOOTH_OFF} when the + * bluetooth is off * * @hide */ @@ -559,7 +561,10 @@ public final class BluetoothDevice implements Parcelable, Attributable { /** * Used as an int extra field in {@link #ACTION_PAIRING_REQUEST} - * intents as the value of passkey. + * intents as the location of initiator. Possible value are: + * {@link #EXTRA_PAIRING_INITIATOR_FOREGROUND}, + * {@link #EXTRA_PAIRING_INITIATOR_BACKGROUND}, + * * @hide */ @SystemApi @@ -1532,6 +1537,17 @@ public final class BluetoothDevice implements Parcelable, Attributable { } /** + * Returns the address type of this BluetoothDevice. + * + * @return Bluetooth address type + * @hide + */ + public int getAddressType() { + if (DBG) Log.d(TAG, "mAddressType: " + mAddressType); + return mAddressType; + } + + /** * Returns the anonymized hardware address of this BluetoothDevice. The first three octets * will be suppressed for anonymization. * <p> For example, "XX:XX:XX:AA:BB:CC". @@ -1993,42 +2009,71 @@ public final class BluetoothDevice implements Parcelable, Attributable { return defaultValue; } - /* - private static final String BLUETOOTH_BONDING_CACHE_PROPERTY = - "cache_key.bluetooth.get_bond_state"; - private final PropertyInvalidatedCache<BluetoothDevice, Integer> mBluetoothBondCache = - new PropertyInvalidatedCache<BluetoothDevice, Integer>( - 8, BLUETOOTH_BONDING_CACHE_PROPERTY) { + /** + * 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); + }}; + + /** + * 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<BluetoothDevice, Integer> mBluetoothBondQuery = + new IpcDataCache.QueryHandler<>() { + @RequiresLegacyBluetoothPermission + @RequiresBluetoothConnectPermission + @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) @Override - @SuppressLint("AndroidFrameworkRequiresPermission") - public Integer recompute(BluetoothDevice query) { - final int defaultValue = BluetoothDevice.BOND_NONE; - try { - final SynchronousResultReceiver<Integer> recv = - new SynchronousResultReceiver(); - sService.getBondState(query, mAttributionSource, recv); - return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue); - } catch (TimeoutException e) { - Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); - } catch (RemoteException e) { - throw e.rethrowAsRuntimeException(); + 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(); + } } return defaultValue; } }; - */ + + private static final String GET_BOND_STATE_API = "getBondState"; + + private final BluetoothCache<BluetoothDevice, Integer> mBluetoothBondCache = + new BluetoothCache<BluetoothDevice, Integer>(GET_BOND_STATE_API, mBluetoothBondQuery); /** @hide */ - /* public void disableBluetoothGetBondStateCache() { - mBluetoothBondCache.disableLocal(); - } */ + public void disableBluetoothGetBondStateCache() { + mBluetoothBondCache.disableForCurrentProcess(); + } /** @hide */ - /* public static void invalidateBluetoothGetBondStateCache() { - PropertyInvalidatedCache.invalidateCache(BLUETOOTH_BONDING_CACHE_PROPERTY); + invalidateCache(GET_BOND_STATE_API); } - */ /** * Get the bond state of the remote device. @@ -2045,25 +2090,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { @SuppressLint("AndroidFrameworkRequiresPermission") public int getBondState() { if (DBG) log("getBondState()"); - 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(); - //return mBluetoothBondCache.query(this); - service.getBondState(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(); - } - } - return defaultValue; + return mBluetoothBondCache.query(null); } /** |