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

Commit 5a583a8c authored by Azhara Assanova's avatar Azhara Assanova Committed by Android (Google) Code Review
Browse files

Merge "Add ComponentCaller with getUid(), getPackage() APIs for Activity" into main

parents 50004872 e8157fa9
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -4380,6 +4380,7 @@ package android.app {
    method public android.transition.TransitionManager getContentTransitionManager();
    method @Nullable public android.view.View getCurrentFocus();
    method @Deprecated public android.app.FragmentManager getFragmentManager();
    method @FlaggedApi("android.security.content_uri_permission_apis") @NonNull public android.app.ComponentCaller getInitialCaller();
    method public android.content.Intent getIntent();
    method @Nullable public Object getLastNonConfigurationInstance();
    method @Nullable public String getLaunchedFromPackage();
@@ -5420,6 +5421,12 @@ package android.app {
    field public static final int DELIVERY_GROUP_POLICY_MOST_RECENT = 1; // 0x1
  }
  @FlaggedApi("android.security.content_uri_permission_apis") public final class ComponentCaller {
    ctor public ComponentCaller(@NonNull android.os.IBinder, @Nullable android.os.IBinder);
    method @Nullable public String getPackage();
    method public int getUid();
  }
  public class DatePickerDialog extends android.app.AlertDialog implements android.widget.DatePicker.OnDateChangedListener android.content.DialogInterface.OnClickListener {
    ctor public DatePickerDialog(@NonNull android.content.Context);
    ctor public DatePickerDialog(@NonNull android.content.Context, @StyleRes int);
+32 −0
Original line number Diff line number Diff line
@@ -844,6 +844,7 @@ public class Activity extends ContextThemeWrapper
    private IBinder mToken;
    private IBinder mAssistToken;
    private IBinder mShareableActivityToken;
    private ComponentCaller mInitialCaller;
    @UnsupportedAppUsage
    private int mIdent;
    @UnsupportedAppUsage
@@ -7030,6 +7031,20 @@ public class Activity extends ContextThemeWrapper
        return ActivityClient.getInstance().getLaunchedFromPackage(getActivityToken());
    }

    /**
     * Returns the ComponentCaller instance of the app that initially launched this activity.
     *
     * <p>Note that calls to {@link #onNewIntent} have no effect on the returned value of this
     * method.
     *
     * @return {@link ComponentCaller} instance
     * @see ComponentCaller
     */
    @FlaggedApi(android.security.Flags.FLAG_CONTENT_URI_PERMISSION_APIS)
    public @NonNull ComponentCaller getInitialCaller() {
        return mInitialCaller;
    }

    /**
     * Control whether this activity's main window is visible.  This is intended
     * only for the special case of an activity that is not going to show a
@@ -8647,6 +8662,19 @@ public class Activity extends ContextThemeWrapper
            Configuration config, String referrer, IVoiceInteractor voiceInteractor,
            Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken,
            IBinder shareableActivityToken) {
        attach(context, aThread, instr, token, ident, application, intent, info, title, parent, id,
                lastNonConfigurationInstances, config, referrer, voiceInteractor, window,
                activityConfigCallback, assistToken, shareableActivityToken, null);
    }

    final void attach(Context context, ActivityThread aThread,
            Instrumentation instr, IBinder token, int ident,
            Application application, Intent intent, ActivityInfo info,
            CharSequence title, Activity parent, String id,
            NonConfigurationInstances lastNonConfigurationInstances,
            Configuration config, String referrer, IVoiceInteractor voiceInteractor,
            Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken,
            IBinder shareableActivityToken, IBinder initialCallerInfoAccessToken) {
        if (sandboxActivitySdkBasedContext()) {
            // Sandbox activities extract a token from the intent's extra to identify the related
            // SDK as part of overriding attachBaseContext, then it wraps the passed context in an
@@ -8711,6 +8739,10 @@ public class Activity extends ContextThemeWrapper

        getAutofillClientController().onActivityAttached(application);
        setContentCaptureOptions(application.getContentCaptureOptions());

        if (android.security.Flags.contentUriPermissionApis()) {
            mInitialCaller = new ComponentCaller(getActivityToken(), initialCallerInfoAccessToken);
        }
    }

    /** @hide */
+4 −2
Original line number Diff line number Diff line
@@ -580,6 +580,7 @@ public final class ActivityThread extends ClientTransactionHandler
        public IBinder shareableActivityToken;
        // The token of the TaskFragment that embedded this activity.
        @Nullable public IBinder mTaskFragmentToken;
        public IBinder initialCallerInfoAccessToken;
        int ident;
        @UnsupportedAppUsage
        Intent intent;
@@ -668,7 +669,7 @@ public final class ActivityThread extends ClientTransactionHandler
                List<ReferrerIntent> pendingNewIntents, SceneTransitionInfo sceneTransitionInfo,
                boolean isForward, ProfilerInfo profilerInfo, ClientTransactionHandler client,
                IBinder assistToken, IBinder shareableActivityToken, boolean launchedFromBubble,
                IBinder taskFragmentToken) {
                IBinder taskFragmentToken, IBinder initialCallerInfoAccessToken) {
            this.token = token;
            this.assistToken = assistToken;
            this.shareableActivityToken = shareableActivityToken;
@@ -685,6 +686,7 @@ public final class ActivityThread extends ClientTransactionHandler
            this.profilerInfo = profilerInfo;
            this.overrideConfig = overrideConfig;
            this.packageInfo = client.getPackageInfoNoCheck(activityInfo.applicationInfo);
            this.initialCallerInfoAccessToken = initialCallerInfoAccessToken;
            mSceneTransitionInfo = sceneTransitionInfo;
            mLaunchedFromBubble = launchedFromBubble;
            mTaskFragmentToken = taskFragmentToken;
@@ -3914,7 +3916,7 @@ public final class ActivityThread extends ClientTransactionHandler
                        r.ident, app, r.intent, r.activityInfo, title, r.parent,
                        r.embeddedID, r.lastNonConfigurationInstances, config,
                        r.referrer, r.voiceInteractor, window, r.activityConfigCallback,
                        r.assistToken, r.shareableActivityToken);
                        r.assistToken, r.shareableActivityToken, r.initialCallerInfoAccessToken);

                if (customIntent != null) {
                    activity.mIntent = customIntent;
+137 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.app;

import android.annotation.FlaggedApi;
import android.annotation.Nullable;
import android.os.IBinder;
import android.os.Process;

import androidx.annotation.NonNull;

import java.util.Objects;

/**
 * Represents the app that launched the component. See below for the APIs available on the component
 * caller.
 *
 * <p><b>Note</b>, that in {@link android.os.Build.VERSION_CODES#VANILLA_ICE_CREAM} only
 * {@link Activity} has access to {@link ComponentCaller} instances.
 *
 * @see Activity#getInitialCaller()
 */
@FlaggedApi(android.security.Flags.FLAG_CONTENT_URI_PERMISSION_APIS)
public final class ComponentCaller {
    private final IBinder mActivityToken;
    private final IBinder mCallerToken;

    public ComponentCaller(@NonNull IBinder activityToken, @Nullable IBinder callerToken) {
        mActivityToken = activityToken;
        mCallerToken = callerToken;
    }

    /**
     * Returns the uid of this component caller.
     *
     * <p><b>Note</b>, in {@link android.os.Build.VERSION_CODES#VANILLA_ICE_CREAM} only
     * {@link Activity} has access to {@link ComponentCaller} instances.
     * <p>
     * <h3>Requirements for {@link Activity} callers</h3>
     *
     * <p>In order to receive the calling app's uid, at least one of the following has to be met:
     * <ul>
     *     <li>The calling app must call {@link ActivityOptions#setShareIdentityEnabled(boolean)}
     *     with a value of {@code true} and launch this activity with the resulting
     *     {@code ActivityOptions}.
     *     <li>The launched activity has the same uid as the calling app.
     *     <li>The launched activity is running in a package that is signed with the same key used
     *     to sign the platform (typically only system packages such as Settings will meet this
     *     requirement).
     * </ul>
     * These are the same requirements for {@link #getPackage()}; if any of these are met, then
     * these methods can be used to obtain the uid and package name of the calling app. If none are
     * met, then {@link Process#INVALID_UID} is returned.
     *
     * <p>Note, even if the above conditions are not met, the calling app's identity may still be
     * available from {@link Activity#getCallingPackage()} if this activity was started with
     * {@code Activity#startActivityForResult} to allow validation of the result's recipient.
     *
     * @return the uid of the calling app or {@link Process#INVALID_UID} if the current component
     * cannot access the identity of the calling app or the caller is invalid
     *
     * @see ActivityOptions#setShareIdentityEnabled(boolean)
     * @see Activity#getLaunchedFromUid()
     */
    public int getUid() {
        return ActivityClient.getInstance().getLaunchedFromUid(mActivityToken);
    }

    /**
     * Returns the package name of this component caller.
     *
     * <p><b>Note</b>, in {@link android.os.Build.VERSION_CODES#VANILLA_ICE_CREAM} only
     * {@link Activity} has access to {@link ComponentCaller} instances.
     * <p>
     * <h3>Requirements for {@link Activity} callers</h3>
     *
     * <p>In order to receive the calling app's package name, at least one of the following has to
     * be met:
     * <ul>
     *     <li>The calling app must call {@link ActivityOptions#setShareIdentityEnabled(boolean)}
     *     with a value of {@code true} and launch this activity with the resulting
     *     {@code ActivityOptions}.
     *     <li>The launched activity has the same uid as the calling app.
     *     <li>The launched activity is running in a package that is signed with the same key used
     *     to sign the platform (typically only system packages such as Settings will meet this
     *     meet this requirement).
     * </ul>
     * These are the same requirements for {@link #getUid()}; if any of these are met, then these
     * methods can be used to obtain the uid and package name of the calling app. If none are met,
     * then {@code null} is returned.
     *
     * <p>Note, even if the above conditions are not met, the calling app's identity may still be
     * available from {@link Activity#getCallingPackage()} if this activity was started with
     * {@code Activity#startActivityForResult} to allow validation of the result's recipient.
     *
     * @return the package name of the calling app or null if the current component cannot access
     * the identity of the calling app or the caller is invalid
     *
     * @see ActivityOptions#setShareIdentityEnabled(boolean)
     * @see Activity#getLaunchedFromPackage()
     */
    @Nullable
    public String getPackage() {
        return ActivityClient.getInstance().getLaunchedFromPackage(mActivityToken);
    }

    @Override
    public boolean equals(@Nullable Object obj) {
        if (obj == null || !(obj instanceof ComponentCaller other)) {
            return false;
        }
        return this.mActivityToken == other.mActivityToken
                && this.mCallerToken == other.mCallerToken;
    }

    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + Objects.hashCode(mActivityToken);
        result = 31 * result + Objects.hashCode(mCallerToken);
        return result;
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -1424,7 +1424,7 @@ public class Instrumentation {
                (Activity.NonConfigurationInstances)lastNonConfigurationInstance,
                new Configuration(), null /* referrer */, null /* voiceInteractor */,
                null /* window */, null /* activityCallback */, null /* assistToken */,
                null /*shareableActivityToken*/);
                null /* shareableActivityToken */, null /* initialCallerInfoAccessToken */);
        return activity;
    }

Loading