Pass AttributionSource to AdapterService methods.

This adds attribution source to AdapterService bluetooth method
calls. This is now required to allow the app ops for the new
bluetooth permissions (BLUETOOTH_CONNECT, BLUETOOTH_ADVERTISE,
and BLUETOOTH_SCAN) to be noted.

Bug: 183626112
Test: atest AdapterServiceTest
Test: atest CtsPermissionTestCases:android.permission.cts.NearbyDevicesPermissionTest
Change-Id: I8d1fe41ca9945a3baab584f248a17b3a1eb255f7
This commit is contained in:
Oli Lan
2021-04-20 09:56:45 +01:00
parent a2be0990e2
commit 141edf9ef6
3 changed files with 69 additions and 52 deletions

View File

@@ -31,12 +31,12 @@ import android.app.ActivityThread;
import android.app.PropertyInvalidatedCache;
import android.bluetooth.BluetoothDevice.Transport;
import android.bluetooth.BluetoothProfile.ConnectionPolicy;
import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
import android.bluetooth.annotations.RequiresBluetoothAdvertisePermission;
import android.bluetooth.annotations.RequiresBluetoothConnectPermission;
import android.bluetooth.annotations.RequiresBluetoothLocationPermission;
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
import android.bluetooth.annotations.RequiresBluetoothScanPermission;
import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
import android.bluetooth.le.BluetoothLeAdvertiser;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.PeriodicAdvertisingManager;
@@ -797,7 +797,7 @@ public final class BluetoothAdapter {
*/
@RequiresNoPermission
public BluetoothDevice getRemoteDevice(String address) {
return new BluetoothDevice(address);
return new BluetoothDevice(address, getAttributionSource());
}
/**
@@ -818,7 +818,7 @@ public final class BluetoothAdapter {
}
return new BluetoothDevice(
String.format(Locale.US, "%02X:%02X:%02X:%02X:%02X:%02X", address[0], address[1],
address[2], address[3], address[4], address[5]));
address[2], address[3], address[4], address[5]), getAttributionSource());
}
/**
@@ -1320,7 +1320,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.getUuids();
return mService.getUuids(getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
@@ -1354,7 +1354,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.setName(name);
return mService.setName(name, getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
@@ -1382,7 +1382,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.getBluetoothClass();
return mService.getBluetoothClass(getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
@@ -1438,7 +1438,7 @@ public final class BluetoothAdapter {
if (getState() != STATE_ON) return BluetoothAdapter.IO_CAPABILITY_UNKNOWN;
try {
mServiceLock.readLock().lock();
if (mService != null) return mService.getIoCapability();
if (mService != null) return mService.getIoCapability(getAttributionSource());
} catch (RemoteException e) {
Log.e(TAG, e.getMessage(), e);
} finally {
@@ -1491,7 +1491,7 @@ public final class BluetoothAdapter {
if (getState() != STATE_ON) return BluetoothAdapter.IO_CAPABILITY_UNKNOWN;
try {
mServiceLock.readLock().lock();
if (mService != null) return mService.getLeIoCapability();
if (mService != null) return mService.getLeIoCapability(getAttributionSource());
} catch (RemoteException e) {
Log.e(TAG, e.getMessage(), e);
} finally {
@@ -1553,7 +1553,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.getScanMode();
return mService.getScanMode(getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
@@ -1602,7 +1602,7 @@ public final class BluetoothAdapter {
mServiceLock.readLock().lock();
if (mService != null) {
int durationSeconds = Math.toIntExact(durationMillis / 1000);
return mService.setScanMode(mode, durationSeconds);
return mService.setScanMode(mode, durationSeconds, getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
@@ -1652,7 +1652,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.setScanMode(mode, getDiscoverableTimeout());
return mService.setScanMode(mode, getDiscoverableTimeout(), getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
@@ -1673,7 +1673,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.getDiscoverableTimeout();
return mService.getDiscoverableTimeout(getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
@@ -1694,7 +1694,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
mService.setDiscoverableTimeout(timeout);
mService.setDiscoverableTimeout(timeout, getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
@@ -1745,7 +1745,7 @@ public final class BluetoothAdapter {
return ActivityThread.currentOpPackageName();
}
private AttributionSource getAttributionSource() {
AttributionSource getAttributionSource() {
if (mContext != null) {
return mContext.getAttributionSource();
}
@@ -1828,7 +1828,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.cancelDiscovery();
return mService.cancelDiscovery(getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
@@ -1866,7 +1866,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.isDiscovering();
return mService.isDiscovering(getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
@@ -2303,7 +2303,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.getMaxConnectedAudioDevices();
return mService.getMaxConnectedAudioDevices(getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "failed to get getMaxConnectedAudioDevices, error: ", e);
@@ -2415,7 +2415,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return mService.getMostRecentlyConnectedDevices();
return mService.getMostRecentlyConnectedDevices(getAttributionSource());
}
} catch (RemoteException e) {
Log.e(TAG, "", e);
@@ -2445,7 +2445,7 @@ public final class BluetoothAdapter {
try {
mServiceLock.readLock().lock();
if (mService != null) {
return toDeviceSet(mService.getBondedDevices());
return toDeviceSet(mService.getBondedDevices(getAttributionSource()));
}
return toDeviceSet(new BluetoothDevice[0]);
} catch (RemoteException e) {

View File

@@ -32,6 +32,7 @@ import android.bluetooth.annotations.RequiresLegacyBluetoothAdminPermission;
import android.bluetooth.annotations.RequiresLegacyBluetoothPermission;
import android.companion.AssociationRequest;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.AttributionSource;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
@@ -1087,6 +1088,8 @@ public final class BluetoothDevice implements Parcelable {
*/
private static volatile IBluetooth sService;
private final AttributionSource mAttributionSource;
private final String mAddress;
@AddressType private final int mAddressType;
@@ -1135,12 +1138,12 @@ public final class BluetoothDevice implements Parcelable {
* and is validated in this constructor.
*
* @param address valid Bluetooth MAC address
* @param attributionSource attribution for permission-protected calls
* @throws RuntimeException Bluetooth is not available on this platform
* @throws IllegalArgumentException address is invalid
* @hide
*/
@UnsupportedAppUsage
/*package*/ BluetoothDevice(String address) {
public BluetoothDevice(String address, AttributionSource attributionSource) {
getService(); // ensures sService is initialized
if (!BluetoothAdapter.checkBluetoothAddress(address)) {
throw new IllegalArgumentException(address + " is not a valid Bluetooth address");
@@ -1148,6 +1151,12 @@ public final class BluetoothDevice implements Parcelable {
mAddress = address;
mAddressType = ADDRESS_TYPE_PUBLIC;
mAttributionSource = attributionSource;
}
@UnsupportedAppUsage
/*package*/ BluetoothDevice(String address) {
this(address, BluetoothAdapter.getDefaultAdapter().getAttributionSource());
}
@Override
@@ -1185,7 +1194,8 @@ public final class BluetoothDevice implements Parcelable {
public static final @android.annotation.NonNull Parcelable.Creator<BluetoothDevice> CREATOR =
new Parcelable.Creator<BluetoothDevice>() {
public BluetoothDevice createFromParcel(Parcel in) {
return new BluetoothDevice(in.readString());
return new BluetoothDevice(
in.readString(), in.readParcelable(getClass().getClassLoader()));
}
public BluetoothDevice[] newArray(int size) {
@@ -1196,6 +1206,7 @@ public final class BluetoothDevice implements Parcelable {
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(mAddress);
out.writeParcelable(mAttributionSource, 0);
}
/**
@@ -1240,7 +1251,7 @@ public final class BluetoothDevice implements Parcelable {
return null;
}
try {
String name = service.getRemoteName(this);
String name = service.getRemoteName(this, mAttributionSource);
if (name != null) {
// remove whitespace characters from the name
return name
@@ -1271,7 +1282,7 @@ public final class BluetoothDevice implements Parcelable {
return DEVICE_TYPE_UNKNOWN;
}
try {
return service.getRemoteType(this);
return service.getRemoteType(this, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1295,7 +1306,7 @@ public final class BluetoothDevice implements Parcelable {
return null;
}
try {
String alias = service.getRemoteAlias(this);
String alias = service.getRemoteAliasWithAttribution(this, mAttributionSource);
if (alias == null) {
return getName();
}
@@ -1337,7 +1348,8 @@ public final class BluetoothDevice implements Parcelable {
}
try {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
return service.setRemoteAlias(this, alias, adapter.getOpPackageName());
return service.setRemoteAlias(
this, alias, adapter.getOpPackageName(), mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1363,7 +1375,7 @@ public final class BluetoothDevice implements Parcelable {
return BATTERY_LEVEL_BLUETOOTH_OFF;
}
try {
return service.getBatteryLevel(this);
return service.getBatteryLevel(this, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1453,8 +1465,8 @@ public final class BluetoothDevice implements Parcelable {
return false;
}
try {
return service.createBond(this, transport, remoteP192Data, remoteP256Data,
BluetoothAdapter.getDefaultAdapter().getOpPackageName());
return service.createBond(
this, transport, remoteP192Data, remoteP256Data, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1479,7 +1491,7 @@ public final class BluetoothDevice implements Parcelable {
return false;
}
try {
return service.isBondingInitiatedLocally(this);
return service.isBondingInitiatedLocally(this, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1504,7 +1516,7 @@ public final class BluetoothDevice implements Parcelable {
Log.i(TAG, "cancelBondProcess() for device " + getAddress()
+ " called by pid: " + Process.myPid()
+ " tid: " + Process.myTid());
return service.cancelBondProcess(this);
return service.cancelBondProcess(this, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1532,7 +1544,7 @@ public final class BluetoothDevice implements Parcelable {
Log.i(TAG, "removeBond() for device " + getAddress()
+ " called by pid: " + Process.myPid()
+ " tid: " + Process.myTid());
return service.removeBond(this);
return service.removeBond(this, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1548,7 +1560,7 @@ public final class BluetoothDevice implements Parcelable {
@SuppressLint("AndroidFrameworkRequiresPermission")
protected Integer recompute(BluetoothDevice query) {
try {
return sService.getBondState(query);
return sService.getBondState(query, mAttributionSource);
} catch (RemoteException e) {
throw e.rethrowAsRuntimeException();
}
@@ -1638,7 +1650,8 @@ public final class BluetoothDevice implements Parcelable {
return false;
}
try {
return service.getConnectionState(this) != CONNECTION_STATE_DISCONNECTED;
return service.getConnectionStateWithAttribution(this, mAttributionSource)
!= CONNECTION_STATE_DISCONNECTED;
} catch (RemoteException e) {
Log.e(TAG, "", e);
return false;
@@ -1663,7 +1676,8 @@ public final class BluetoothDevice implements Parcelable {
return false;
}
try {
return service.getConnectionState(this) > CONNECTION_STATE_CONNECTED;
return service.getConnectionStateWithAttribution(this, mAttributionSource)
> CONNECTION_STATE_CONNECTED;
} catch (RemoteException e) {
Log.e(TAG, "", e);
return false;
@@ -1685,7 +1699,7 @@ public final class BluetoothDevice implements Parcelable {
return null;
}
try {
int classInt = service.getRemoteClass(this);
int classInt = service.getRemoteClass(this, mAttributionSource);
if (classInt == BluetoothClass.ERROR) return null;
return new BluetoothClass(classInt);
} catch (RemoteException e) {
@@ -1714,7 +1728,7 @@ public final class BluetoothDevice implements Parcelable {
return null;
}
try {
return service.getRemoteUuids(this);
return service.getRemoteUuids(this, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1744,7 +1758,7 @@ public final class BluetoothDevice implements Parcelable {
return false;
}
try {
return service.fetchRemoteUuids(this);
return service.fetchRemoteUuidsWithAttribution(this, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1781,7 +1795,7 @@ public final class BluetoothDevice implements Parcelable {
return false;
}
try {
return service.sdpSearch(this, uuid);
return service.sdpSearch(this, uuid, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1803,7 +1817,7 @@ public final class BluetoothDevice implements Parcelable {
return false;
}
try {
return service.setPin(this, true, pin.length, pin);
return service.setPin(this, true, pin.length, pin, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1866,7 +1880,7 @@ public final class BluetoothDevice implements Parcelable {
return false;
}
try {
return service.cancelBondProcess(this);
return service.cancelBondProcess(this, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -1899,7 +1913,7 @@ public final class BluetoothDevice implements Parcelable {
return ACCESS_UNKNOWN;
}
try {
return service.getPhonebookAccessPermission(this);
return service.getPhonebookAccessPermission(this, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -2005,7 +2019,7 @@ public final class BluetoothDevice implements Parcelable {
return ACCESS_UNKNOWN;
}
try {
return service.getMessageAccessPermission(this);
return service.getMessageAccessPermission(this, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}
@@ -2056,7 +2070,7 @@ public final class BluetoothDevice implements Parcelable {
return ACCESS_UNKNOWN;
}
try {
return service.getSimAccessPermission(this);
return service.getSimAccessPermission(this, mAttributionSource);
} catch (RemoteException e) {
Log.e(TAG, "", e);
}

View File

@@ -347,7 +347,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
addActiveLog(
BluetoothProtoEnums.ENABLE_DISABLE_REASON_FACTORY_RESET,
mContext.getPackageName(), false);
mBluetooth.disable();
mBluetooth.disable(mContext.getAttributionSource());
return true;
}
} catch (RemoteException e) {
@@ -1714,7 +1714,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
try {
mBluetoothLock.readLock().lock();
if (mBluetooth != null) {
return mBluetooth.getAddress();
return mBluetooth.getAddressWithAttribution(mContext.getAttributionSource());
}
} catch (RemoteException e) {
Slog.e(TAG,
@@ -1743,7 +1743,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
try {
mBluetoothLock.readLock().lock();
if (mBluetooth != null) {
return mBluetooth.getName();
return mBluetooth.getName(mContext.getAttributionSource());
}
} catch (RemoteException e) {
Slog.e(TAG, "getName(): Unable to retrieve name remotely. Returning cached name", e);
@@ -1830,7 +1830,10 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
}
} else if (mBluetooth != null) {
try {
storeNameAndAddress(mBluetooth.getName(), mBluetooth.getAddress());
storeNameAndAddress(
mBluetooth.getName(mContext.getAttributionSource()),
mBluetooth.getAddressWithAttribution(
mContext.getAttributionSource()));
} catch (RemoteException re) {
Slog.e(TAG, "Unable to grab names", re);
}
@@ -2096,7 +2099,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
//Do enable request
try {
if (!mBluetooth.enable(mQuietEnable)) {
if (!mBluetooth.enable(mQuietEnable, mContext.getAttributionSource())) {
Slog.e(TAG, "IBluetooth.enable() returned false");
}
} catch (RemoteException e) {
@@ -2417,7 +2420,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
} else if (mBluetooth != null) {
//Enable bluetooth
try {
if (!mBluetooth.enable(mQuietEnable)) {
if (!mBluetooth.enable(mQuietEnable, mContext.getAttributionSource())) {
Slog.e(TAG, "IBluetooth.enable() returned false");
}
} catch (RemoteException e) {
@@ -2447,7 +2450,7 @@ class BluetoothManagerService extends IBluetoothManager.Stub {
if (DBG) {
Slog.d(TAG, "Sending off request.");
}
if (!mBluetooth.disable()) {
if (!mBluetooth.disable(mContext.getAttributionSource())) {
Slog.e(TAG, "IBluetooth.disable() returned false");
}
}