summaryrefslogtreecommitdiff
path: root/framework/java/android/bluetooth/BluetoothManager.java
diff options
context:
space:
mode:
authorJeff Sharkey <jsharkey@google.com>2021-04-23 19:38:54 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2021-04-23 19:38:54 +0000
commit995557650f13f2ee98a4bdeaa8096c78658ef1b1 (patch)
tree0bca20468489fa6064bd73e4a720cc013c4fa12a /framework/java/android/bluetooth/BluetoothManager.java
parent26a3a23d4c105c5ccd1b98ce0f83b21ee3b1ffe4 (diff)
parentf9e176c3dcafb82f251a123751578539e3484deb (diff)
Merge changes from topic "btapiattribution" into sc-dev
* changes: More AttributionSource plumbing. Pass attribution source to BT APIs.
Diffstat (limited to 'framework/java/android/bluetooth/BluetoothManager.java')
-rw-r--r--framework/java/android/bluetooth/BluetoothManager.java60
1 files changed, 38 insertions, 22 deletions
diff --git a/framework/java/android/bluetooth/BluetoothManager.java b/framework/java/android/bluetooth/BluetoothManager.java
index 69f9a79c73..b13ccaf7e5 100644
--- a/framework/java/android/bluetooth/BluetoothManager.java
+++ b/framework/java/android/bluetooth/BluetoothManager.java
@@ -20,8 +20,11 @@ import android.annotation.RequiresFeature;
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;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.RemoteException;
@@ -56,16 +59,41 @@ public final class BluetoothManager {
private static final String TAG = "BluetoothManager";
private static final boolean DBG = false;
+ private final AttributionSource mAttributionSource;
private final BluetoothAdapter mAdapter;
/**
* @hide
*/
public BluetoothManager(Context context) {
- mAdapter = BluetoothAdapter.createAdapter();
+ mAttributionSource = resolveAttributionSource(context);
+ mAdapter = BluetoothAdapter.createAdapter(mAttributionSource);
+ }
+
+ /** {@hide} */
+ public static AttributionSource resolveAttributionSource(Context context) {
+ AttributionSource res = null;
if (context != null) {
- mAdapter.setAttributionSource(context.getAttributionSource());
+ 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;
}
/**
@@ -126,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});
- } catch (RemoteException e) {
- Log.e(TAG, "", e);
- }
-
- return connectedDevices;
+ return getDevicesMatchingConnectionStates(profile, new int[] {
+ BluetoothProfile.STATE_CONNECTED
+ });
}
/**
@@ -180,7 +193,9 @@ public final class BluetoothManager {
IBluetoothManager managerService = mAdapter.getBluetoothManager();
IBluetoothGatt iGatt = managerService.getBluetoothGatt();
if (iGatt == null) return devices;
- devices = iGatt.getDevicesMatchingConnectionStates(states);
+ devices = BluetoothDevice.setAttributionSource(
+ iGatt.getDevicesMatchingConnectionStates(states, mAttributionSource),
+ mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -283,7 +298,8 @@ public final class BluetoothManager {
Log.e(TAG, "Fail to get GATT Server connection");
return null;
}
- BluetoothGattServer mGattServer = new BluetoothGattServer(iGatt, transport);
+ BluetoothGattServer mGattServer =
+ new BluetoothGattServer(iGatt, transport, mAdapter);
Boolean regStatus = mGattServer.registerCallback(callback, eatt_support);
return regStatus ? mGattServer : null;
} catch (RemoteException e) {