From 770d50ed431dd16850b916c7adf7a9b89d443cc3 Mon Sep 17 00:00:00 2001 From: Etienne Ruffieux Date: Tue, 14 Dec 2021 18:39:55 +0000 Subject: [PATCH] Moved AttributionSource related APIs in AttributionSource Modified myAttributionSource() to check for global AS for process in ActivityThread and fallback to building new AS with PackageManager#getPackageForUid(myUid()) if null. Tag: #feature Bug: 210467846 Bug: 210468546 Test: build Change-Id: I7aa75395469bf0bb806100420faaf98c52057355 CTS-Coverage-Bug: 210906055 --- core/api/current.txt | 1 + .../android/bluetooth/BluetoothAdapter.java | 2 +- .../android/bluetooth/BluetoothDevice.java | 2 +- .../android/bluetooth/BluetoothManager.java | 34 +-------------- .../android/content/AttributionSource.java | 43 ++++++++++++++++--- 5 files changed, 43 insertions(+), 39 deletions(-) diff --git a/core/api/current.txt b/core/api/current.txt index d9753ab5e4a0f..16f66a9062107 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -10091,6 +10091,7 @@ package android.content { method @Nullable public String getPackageName(); method public int getUid(); method public boolean isTrusted(@NonNull android.content.Context); + method @NonNull public static android.content.AttributionSource myAttributionSource(); method public void writeToParcel(@NonNull android.os.Parcel, int); field @NonNull public static final android.os.Parcelable.Creator CREATOR; } diff --git a/core/java/android/bluetooth/BluetoothAdapter.java b/core/java/android/bluetooth/BluetoothAdapter.java index 14be9215c769a..9160edea873a6 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -788,7 +788,7 @@ public final class BluetoothAdapter { @RequiresNoPermission public static synchronized BluetoothAdapter getDefaultAdapter() { if (sAdapter == null) { - sAdapter = createAdapter(BluetoothManager.resolveAttributionSource(null)); + sAdapter = createAdapter(AttributionSource.myAttributionSource()); } return sAdapter; } diff --git a/core/java/android/bluetooth/BluetoothDevice.java b/core/java/android/bluetooth/BluetoothDevice.java index 6e918bd6243d6..c889d100a49da 100644 --- a/core/java/android/bluetooth/BluetoothDevice.java +++ b/core/java/android/bluetooth/BluetoothDevice.java @@ -1178,7 +1178,7 @@ public final class BluetoothDevice implements Parcelable, Attributable { mAddress = address; mAddressType = ADDRESS_TYPE_PUBLIC; - mAttributionSource = BluetoothManager.resolveAttributionSource(null); + mAttributionSource = AttributionSource.myAttributionSource(); } /** {@hide} */ diff --git a/core/java/android/bluetooth/BluetoothManager.java b/core/java/android/bluetooth/BluetoothManager.java index 20152f3d2471f..2d8625098cf88 100644 --- a/core/java/android/bluetooth/BluetoothManager.java +++ b/core/java/android/bluetooth/BluetoothManager.java @@ -16,14 +16,10 @@ package android.bluetooth; -import android.annotation.NonNull; -import android.annotation.Nullable; 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.Attributable; @@ -69,37 +65,11 @@ public final class BluetoothManager { * @hide */ public BluetoothManager(Context context) { - mAttributionSource = resolveAttributionSource(context); + mAttributionSource = (context != null) ? context.getAttributionSource() : + AttributionSource.myAttributionSource(); mAdapter = BluetoothAdapter.createAdapter(mAttributionSource); } - /** {@hide} */ - public static @NonNull AttributionSource resolveAttributionSource(@Nullable 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.Builder(uid) - .setPackageName(AppGlobals.getPackageManager().getPackagesForUid(uid)[0]) - .build(); - } catch (RemoteException ignored) { - } - } - if (res == null) { - throw new IllegalStateException("Failed to resolve AttributionSource"); - } - return res; - } - /** * Get the BLUETOOTH Adapter for this device. * diff --git a/core/java/android/content/AttributionSource.java b/core/java/android/content/AttributionSource.java index 6ae2bb5b642a7..157e709a67f03 100644 --- a/core/java/android/content/AttributionSource.java +++ b/core/java/android/content/AttributionSource.java @@ -22,6 +22,7 @@ import android.annotation.RequiresPermission; import android.annotation.SystemApi; import android.annotation.TestApi; import android.app.ActivityThread; +import android.app.AppGlobals; import android.os.Binder; import android.os.Build; import android.os.IBinder; @@ -191,10 +192,42 @@ public final class AttributionSource implements Parcelable { return new ScopedParcelState(this); } - /** @hide */ - public static AttributionSource myAttributionSource() { - return new AttributionSource(Process.myUid(), ActivityThread.currentOpPackageName(), - /*attributionTag*/ null, (String[]) /*renouncedPermissions*/ null, /*next*/ null); + /** + * Returns a generic {@link AttributionSource} that represents the entire + * calling process. + * + *

Callers are strongly encouraged to use a more specific + * attribution source whenever possible, such as from + * {@link Context#getAttributionSource()}, since that enables developers to + * have more detailed and scoped control over attribution within + * sub-components of their app. + * + * @see Context#createAttributionContext(String) + * @see Context#getAttributionTag() + * @return a generic {@link AttributionSource} representing the entire + * calling process + * @throws IllegalStateException when no accurate {@link AttributionSource} + * can be determined + */ + public static @NonNull AttributionSource myAttributionSource() { + + final AttributionSource globalSource = ActivityThread.currentAttributionSource(); + if (globalSource != null) { + return globalSource; + } + + int uid = Process.myUid(); + if (uid == Process.ROOT_UID) { + uid = Process.SYSTEM_UID; + } + try { + return new AttributionSource.Builder(uid) + .setPackageName(AppGlobals.getPackageManager().getPackagesForUid(uid)[0]) + .build(); + } catch (Exception ignored) { + } + + throw new IllegalStateException("Failed to resolve AttributionSource"); } /** @@ -247,7 +280,7 @@ public final class AttributionSource implements Parcelable { * whether the attribution source is one for the calling app to prevent the caller * to pass you a source from another app without including themselves in the * attribution chain. - *f + * * @return if the attribution source cannot be trusted to be from the caller. */ public boolean checkCallingUid() {