summaryrefslogtreecommitdiff
path: root/core/java/android/bluetooth/BluetoothManager.java
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@android.com>2021-04-22 16:01:29 -0600
committerJeff Sharkey <jsharkey@android.com>2021-04-23 08:51:49 -0600
commitefe1110be8712d497edc39f7559e31d83abc8e6a (patch)
tree65164f0a8de377915ac722b3a74073b1d0b57903 /core/java/android/bluetooth/BluetoothManager.java
parent52f8d4c9c3e4df1acc3db2b85314ae4a1a9ec6b9 (diff)
More AttributionSource plumbing.
To prepare for future work which will plumb AttributionSource values through all remaining AIDLs, we need profiles to interact directly with the specific BluetoothAdapter they were created from. This is how we'll ensure that the relevant AttributionSource can be chained down from the original Context they're obtained from. This change also marks getDefaultAdapter() as deprecated to clearly communicate that BluetoothManager.getAdapter() is the best-practice path to obtaining a correctly scoped BluetoothAdapter instance. Bug: 183626112 Test: atest BluetoothInstrumentationTests Change-Id: I1e15170d7679019bbb6e396279d6e633e3dad4d6
Diffstat (limited to 'core/java/android/bluetooth/BluetoothManager.java')
-rw-r--r--core/java/android/bluetooth/BluetoothManager.java59
1 files changed, 35 insertions, 24 deletions
diff --git a/core/java/android/bluetooth/BluetoothManager.java b/core/java/android/bluetooth/BluetoothManager.java
index 07af8dbd2349..b13ccaf7e570 100644
--- a/core/java/android/bluetooth/BluetoothManager.java
+++ b/core/java/android/bluetooth/BluetoothManager.java
@@ -21,6 +21,7 @@ import android.annotation.RequiresNoPermission;
import android.annotation.RequiresPermission;
import android.annotation.SystemService;
import android.app.ActivityThread;
+import android.app.AppGlobals;
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
import android.content.AttributionSource;
@@ -31,7 +32,6 @@ import android.util.Log;
import java.util.ArrayList;
import java.util.List;
-import java.util.function.Supplier;
/**
* High level manager used to obtain an instance of an {@link BluetoothAdapter}
@@ -66,11 +66,36 @@ public final class BluetoothManager {
* @hide
*/
public BluetoothManager(Context context) {
- mAttributionSource = (context != null) ? context.getAttributionSource()
- : ActivityThread.currentAttributionSource();
+ mAttributionSource = resolveAttributionSource(context);
mAdapter = BluetoothAdapter.createAdapter(mAttributionSource);
}
+ /** {@hide} */
+ public static AttributionSource resolveAttributionSource(Context context) {
+ AttributionSource res = null;
+ if (context != null) {
+ res = context.getAttributionSource();
+ }
+ if (res == null) {
+ res = ActivityThread.currentAttributionSource();
+ }
+ if (res == null) {
+ int uid = android.os.Process.myUid();
+ if (uid == android.os.Process.ROOT_UID) {
+ uid = android.os.Process.SYSTEM_UID;
+ }
+ try {
+ res = new AttributionSource(uid,
+ AppGlobals.getPackageManager().getPackagesForUid(uid)[0], null);
+ } catch (RemoteException ignored) {
+ }
+ }
+ if (res == null) {
+ throw new IllegalStateException("Failed to resolve AttributionSource");
+ }
+ return res;
+ }
+
/**
* Get the BLUETOOTH Adapter for this device.
*
@@ -129,24 +154,9 @@ public final class BluetoothManager {
@RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT)
public List<BluetoothDevice> getConnectedDevices(int profile) {
if (DBG) Log.d(TAG, "getConnectedDevices");
- if (profile != BluetoothProfile.GATT && profile != BluetoothProfile.GATT_SERVER) {
- throw new IllegalArgumentException("Profile not supported: " + profile);
- }
-
- List<BluetoothDevice> connectedDevices = new ArrayList<BluetoothDevice>();
-
- try {
- IBluetoothManager managerService = mAdapter.getBluetoothManager();
- IBluetoothGatt iGatt = managerService.getBluetoothGatt();
- if (iGatt == null) return connectedDevices;
-
- connectedDevices = iGatt.getDevicesMatchingConnectionStates(
- new int[]{BluetoothProfile.STATE_CONNECTED}, mAttributionSource);
- } catch (RemoteException e) {
- Log.e(TAG, "", e);
- }
-
- return connectedDevices;
+ return getDevicesMatchingConnectionStates(profile, new int[] {
+ BluetoothProfile.STATE_CONNECTED
+ });
}
/**
@@ -183,8 +193,9 @@ public final class BluetoothManager {
IBluetoothManager managerService = mAdapter.getBluetoothManager();
IBluetoothGatt iGatt = managerService.getBluetoothGatt();
if (iGatt == null) return devices;
- devices = iGatt.getDevicesMatchingConnectionStates(
- states, mAttributionSource);
+ devices = BluetoothDevice.setAttributionSource(
+ iGatt.getDevicesMatchingConnectionStates(states, mAttributionSource),
+ mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -288,7 +299,7 @@ public final class BluetoothManager {
return null;
}
BluetoothGattServer mGattServer =
- new BluetoothGattServer(iGatt, transport, mAttributionSource);
+ new BluetoothGattServer(iGatt, transport, mAdapter);
Boolean regStatus = mGattServer.registerCallback(callback, eatt_support);
return regStatus ? mGattServer : null;
} catch (RemoteException e) {