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

Commit e41ad67f authored by Svetoslav Ganov's avatar Svetoslav Ganov Committed by Android (Google) Code Review
Browse files

Merge "Add APIs for creating an attribution context" into sc-dev

parents 65de59e1 232636b3
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -10325,6 +10325,7 @@ package android.content {
    method @Deprecated public abstract void clearWallpaper() throws java.io.IOException;
    method @NonNull public android.content.Context createAttributionContext(@Nullable String);
    method public abstract android.content.Context createConfigurationContext(@NonNull android.content.res.Configuration);
    method @NonNull public android.content.Context createContext(@NonNull android.content.ContextParams);
    method public abstract android.content.Context createContextForSplit(String) throws android.content.pm.PackageManager.NameNotFoundException;
    method public abstract android.content.Context createDeviceProtectedStorageContext();
    method public abstract android.content.Context createDisplayContext(@NonNull android.view.Display);
@@ -10546,6 +10547,19 @@ package android.content {
    field public static final String WINDOW_SERVICE = "window";
  }
  public final class ContextParams {
    method @Nullable public String getAttributionTag();
    method @Nullable public String getReceiverAttributionTag();
    method @Nullable public String getReceiverPackage();
  }
  public static final class ContextParams.Builder {
    ctor public ContextParams.Builder();
    method @NonNull public android.content.ContextParams build();
    method @NonNull public android.content.ContextParams.Builder setAttributionTag(@NonNull String);
    method @NonNull public android.content.ContextParams.Builder setReceiverPackage(@NonNull String, @Nullable String);
  }
  public class ContextWrapper extends android.content.Context {
    ctor public ContextWrapper(android.content.Context);
    method protected void attachBaseContext(android.content.Context);
+7 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.ContextParams;
import android.content.AutofillOptions;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
@@ -2606,6 +2607,12 @@ class ContextImpl extends Context {
                compatInfo, mClassLoader, loaders);
    }

    @NonNull
    @Override
    public Context createContext(@NonNull ContextParams contextParams) {
        return this;
    }

    @Override
    public @NonNull Context createAttributionContext(@Nullable String attributionTag) {
        return new ContextImpl(this, mMainThread, mPackageInfo, attributionTag, mSplitName,
+13 −0
Original line number Diff line number Diff line
@@ -6376,6 +6376,19 @@ public abstract class Context {
        throw new RuntimeException("Not implemented. Must override in a subclass.");
    }

    /**
     * Creates a context with specific properties and behaviors.
     *
     * @param contextParams Parameters for how the new context should behave.
     * @return A context with the specified behaviors.
     *
     * @see ContextParams
     */
    @NonNull
    public Context createContext(@NonNull ContextParams contextParams) {
        throw new RuntimeException("Not implemented. Must override in a subclass.");
    }

    /**
     * Return a new Context object for the current Context but attribute to a different tag.
     * In complex apps attribution tagging can be used to distinguish between separate logical
+121 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.content;

import android.annotation.NonNull;
import android.annotation.Nullable;

/**
 * This class represents rules around how a context being created via
 * {@link Context#createContext} should behave.
 *
 * <p>One of the dimensions to customize is how permissions should behave.
 * For example, you can specify how permission accesses from a context should
 * be attributed in the platform's permission tracking system.
 *
 * <p>The two main types of attribution are: against an attribution tag which
 * is an arbitrary string your app specifies for the purposes of tracking permission
 * accesses from a given portion of your app; against another package and optionally
 * its attribution tag if you are accessing the data on behalf of another app and
 * you will be passing that data to this app. Both attributions are not mutually
 * exclusive.
 *
 * <p>For example if you have a feature "foo" in your app which accesses
 * permissions on behalf of app "foo.bar.baz" with feature "bar" you need to
 * create a context like this:
 *
 * <pre class="prettyprint">
 * context.createContext(new ContextParams.Builder()
 *     .setAttributionTag("foo")
 *     .setReceiverPackage("foo.bar.baz", "bar")
 *     .build())
 * </pre>
 *
 * @see Context#createContext(ContextParams)
 */
public final class ContextParams {

    private ContextParams() {
        /* hide ctor */
    }

    /**
     * @return The attribution tag.
     */
    @Nullable
    public String getAttributionTag() {
        return null;
    }

    /**
     * @return The receiving package.
     */
    @Nullable
    public String getReceiverPackage() {
        return null;
    }

    /**
     * @return The receiving package's attribution tag.
     */
    @Nullable
    public String getReceiverAttributionTag() {
        return null;
    }

    /**
     * Builder for creating a {@link ContextParams}.
     */
    public static final class Builder {

        /**
         * Sets an attribution tag against which to track permission accesses.
         *
         * @param attributionTag The attribution tag.
         * @return This builder.
         */
        @NonNull
        public Builder setAttributionTag(@NonNull String attributionTag) {
            return this;
        }

        /**
         * Sets the package and its optional attribution tag that would be receiving
         * the permission protected data.
         *
         * @param packageName The package name receiving the permission protected data.
         * @param attributionTag An attribution tag of the receiving package.
         * @return This builder.
         */
        @NonNull
        public Builder setReceiverPackage(@NonNull String packageName,
                @Nullable String attributionTag) {
            return this;
        }

        /**
         * Creates a new instance. You need to either specify an attribution tag
         * or a receiver package or both.
         *
         * @return The new instance.
         */
        @NonNull
        public ContextParams build() {
            return new ContextParams();
        }
    }
}