diff options
| author | Etienne Ruffieux <eruffieux@google.com> | 2021-12-16 21:16:58 +0000 |
|---|---|---|
| committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2021-12-16 21:16:58 +0000 |
| commit | 70d456d06cf5f2ea7c2effb4e452a00c7759b787 (patch) | |
| tree | b560bcff2ee16e38303b8daf0c22f1568a5985ce /core/java/android/content | |
| parent | d089108eacfeae4bdaf7ae4cf1454ce465a64756 (diff) | |
| parent | 770d50ed431dd16850b916c7adf7a9b89d443cc3 (diff) | |
Merge "Moved AttributionSource related APIs in AttributionSource"
Diffstat (limited to 'core/java/android/content')
| -rw-r--r-- | core/java/android/content/AttributionSource.java | 43 |
1 files changed, 38 insertions, 5 deletions
diff --git a/core/java/android/content/AttributionSource.java b/core/java/android/content/AttributionSource.java index 6ae2bb5b642a..157e709a67f0 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. + * + * <p>Callers are <em>strongly</em> 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() { |
