diff --git a/core/api/current.txt b/core/api/current.txt index 7c480f9a70b14..896060ba7d655 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 5612e7cf56ce6..9b37457c99076 100644 --- a/core/java/android/bluetooth/BluetoothAdapter.java +++ b/core/java/android/bluetooth/BluetoothAdapter.java @@ -786,7 +786,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 9ff4dc3bb125c..93f0268608562 100644 --- a/core/java/android/bluetooth/BluetoothDevice.java +++ b/core/java/android/bluetooth/BluetoothDevice.java @@ -1177,7 +1177,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 c93de41b5b36a..fef6f225dd3bc 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.AttributionSource; @@ -68,37 +64,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() {