summaryrefslogtreecommitdiff
path: root/framework/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
commitf9e176c3dcafb82f251a123751578539e3484deb (patch)
tree7bb1a868a15ea41eb01da5ae0d75752d4a945fb7 /framework/java/android/bluetooth/BluetoothManager.java
parent4dabcb764f1948823dcd74eefb3440afcab07db2 (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 'framework/java/android/bluetooth/BluetoothManager.java')
-rw-r--r--framework/java/android/bluetooth/BluetoothManager.java59
1 files changed, 35 insertions, 24 deletions
diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java
index 07af8dbd23..b13ccaf7e5 100644
--- a/framework/java/android/bluetooth/BluetoothManager.java
+++ b/framework/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) {