Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f2e0678f authored by Nate Myren's avatar Nate Myren
Browse files

Add shouldRegisterAttributionSource to ContextParams

This will allow apps to register Attribution Sources for themselves, if
they so desire

Bug: 283135898
Test: presubmit
Change-Id: Icfd58b373b0cdfa2dcfa5286671bf33419cdc37f
parent 19d7389f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -10560,6 +10560,7 @@ package android.content {
  public final class ContextParams {
    method @Nullable public String getAttributionTag();
    method @Nullable public android.content.AttributionSource getNextAttributionSource();
    method @NonNull public boolean shouldRegisterAttributionSource();
  }
  public static final class ContextParams.Builder {
@@ -10568,6 +10569,7 @@ package android.content {
    method @NonNull public android.content.ContextParams build();
    method @NonNull public android.content.ContextParams.Builder setAttributionTag(@Nullable String);
    method @NonNull public android.content.ContextParams.Builder setNextAttributionSource(@Nullable android.content.AttributionSource);
    method @NonNull public android.content.ContextParams.Builder setShouldRegisterAttributionSource(boolean);
  }
  public class ContextWrapper extends android.content.Context {
+3 −3
Original line number Diff line number Diff line
@@ -3455,20 +3455,20 @@ class ContextImpl extends Context {
        mOpPackageName = overrideOpPackageName != null ? overrideOpPackageName : opPackageName;
        mParams = Objects.requireNonNull(params);
        mAttributionSource = createAttributionSource(attributionTag, nextAttributionSource,
                params.getRenouncedPermissions());
                params.getRenouncedPermissions(), params.shouldRegisterAttributionSource());
        mContentResolver = new ApplicationContentResolver(this, mainThread);
    }

    private @NonNull AttributionSource createAttributionSource(@Nullable String attributionTag,
            @Nullable AttributionSource nextAttributionSource,
            @Nullable Set<String> renouncedPermissions) {
            @Nullable Set<String> renouncedPermissions, boolean shouldRegister) {
        AttributionSource attributionSource = new AttributionSource(Process.myUid(),
                Process.myPid(), mOpPackageName, attributionTag,
                (renouncedPermissions != null) ? renouncedPermissions.toArray(new String[0]) : null,
                getDeviceId(), nextAttributionSource);
        // If we want to access protected data on behalf of another app we need to
        // tell the OS that we opt in to participate in the attribution chain.
        if (nextAttributionSource != null) {
        if (nextAttributionSource != null || shouldRegister) {
            attributionSource = getSystemService(PermissionManager.class)
                    .registerAttributionSource(attributionSource);
        }
+28 −2
Original line number Diff line number Diff line
@@ -50,17 +50,20 @@ public final class ContextParams {
    private final @Nullable String mAttributionTag;
    private final @Nullable AttributionSource mNext;
    private final @NonNull Set<String> mRenouncedPermissions;
    private final boolean mShouldRegisterAttributionSource;

    /** {@hide} */
    public static final ContextParams EMPTY = new ContextParams.Builder().build();

    private ContextParams(@Nullable String attributionTag,
            @Nullable AttributionSource next,
            @Nullable Set<String> renouncedPermissions) {
            @Nullable Set<String> renouncedPermissions,
            boolean shouldRegister) {
        mAttributionTag = attributionTag;
        mNext = next;
        mRenouncedPermissions = (renouncedPermissions != null)
                ? renouncedPermissions : Collections.emptySet();
        mShouldRegisterAttributionSource = shouldRegister;
    }

    /**
@@ -94,6 +97,15 @@ public final class ContextParams {
        return mNext;
    }

    /**
     * @return Whether the attribution source associated with the Context being created should be
     * registered.
     */
    @NonNull
    public boolean shouldRegisterAttributionSource() {
        return mShouldRegisterAttributionSource;
    }

    /**
     * Builder for creating a {@link ContextParams}.
     */
@@ -101,6 +113,7 @@ public final class ContextParams {
        private @Nullable String mAttributionTag;
        private @NonNull Set<String> mRenouncedPermissions = Collections.emptySet();
        private @Nullable AttributionSource mNext;
        private boolean mShouldRegisterAttributionSource;

        /**
         * Create a new builder.
@@ -158,6 +171,19 @@ public final class ContextParams {
            return this;
        }

        /**
         * Sets whether the attribution source associated with the context created from these params
         * should be registered.
         *
         * @param shouldRegister Whether the attribution source associated with the Context being
         *                       created should be registered.
         */
        @NonNull
        public Builder setShouldRegisterAttributionSource(boolean shouldRegister) {
            mShouldRegisterAttributionSource = shouldRegister;
            return this;
        }

        /**
         * Sets permissions which have been voluntarily "renounced" by the
         * calling app.
@@ -205,7 +231,7 @@ public final class ContextParams {
        @NonNull
        public ContextParams build() {
            return new ContextParams(mAttributionTag, mNext,
                    mRenouncedPermissions);
                    mRenouncedPermissions, mShouldRegisterAttributionSource);
        }
    }
}