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

Commit 30e1c74a authored by Vipul Mittal's avatar Vipul Mittal Committed by Android (Google) Code Review
Browse files

Merge "[API] Provide foreground app components in onShow args" into main

parents 63f3bbd4 75c32a19
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -41234,6 +41234,7 @@ package android.service.voice {
    method public void startAssistantActivity(@NonNull android.content.Intent, @NonNull android.os.Bundle);
    method public void startVoiceActivity(android.content.Intent);
    method public final void unregisterVisibleActivityCallback(@NonNull android.service.voice.VoiceInteractionSession.VisibleActivityCallback);
    field @FlaggedApi("android.service.voice.flags.allow_foreground_activities_in_on_show") public static final String KEY_FOREGROUND_ACTIVITIES = "android.service.voice.FOREGROUND_ACTIVITIES";
    field public static final String KEY_SHOW_SESSION_ID = "android.service.voice.SHOW_SESSION_ID";
    field public static final int SHOW_SOURCE_ACTIVITY = 16; // 0x10
    field public static final int SHOW_SOURCE_APPLICATION = 8; // 0x8
+46 −8
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

import android.annotation.CallbackExecutor;
import android.annotation.FlaggedApi;
import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;
@@ -53,6 +54,7 @@ import android.os.Message;
import android.os.RemoteCallback;
import android.os.RemoteException;
import android.os.UserHandle;
import android.service.voice.flags.Flags;
import android.util.ArrayMap;
import android.util.DebugUtils;
import android.util.Log;
@@ -184,6 +186,17 @@ public class VoiceInteractionSession implements KeyEvent.Callback, ComponentCall
     */
    public static final String KEY_SHOW_SESSION_ID = "android.service.voice.SHOW_SESSION_ID";

    /**
     * Bundle key used to specify foreground activity app components.
     * <p>
     * Type: ArrayList&ltComponentName&gt
     * </p>
     * @see #onShow(Bundle, int)
     */
    @FlaggedApi(Flags.FLAG_ALLOW_FOREGROUND_ACTIVITIES_IN_ON_SHOW)
    public static final String KEY_FOREGROUND_ACTIVITIES =
            "android.service.voice.FOREGROUND_ACTIVITIES";

    final Context mContext;
    final HandlerCaller mHandlerCaller;

@@ -1803,14 +1816,39 @@ public class VoiceInteractionSession implements KeyEvent.Callback, ComponentCall
     *
     * @param args The arguments that were supplied to
     * {@link VoiceInteractionService#showSession VoiceInteractionService.showSession}.
     * Some example keys include : "invocation_type", "invocation_phone_state",
     * {@link #KEY_SHOW_SESSION_ID}, "invocation_time_ms",
     * Intent.EXTRA_TIME ("android.intent.extra.TIME") indicating timing
     * in milliseconds of the KeyEvent that triggered Assistant and
     * Some example keys include :
     * <ul>
     *     <li>
     *         invocation_type
     *     </li>
     *     <li>
     *         invocation_phone_state
     *     </li>
     *     <li>
     *         {@link #KEY_SHOW_SESSION_ID}
     *     </li>
     *     <li>
     *         invocation_time_ms
     *     </li>
     *     <li>
     *         Intent.EXTRA_TIME ("android.intent.extra.TIME") indicating timing in milliseconds of
     *         the KeyEvent that triggered Assistant
     *     </li>
     *     <li>
     *         Intent.EXTRA_ASSIST_INPUT_DEVICE_ID (android.intent.extra.ASSIST_INPUT_DEVICE_ID)
     *  referring to the device that sent the request. Starting from Android 14, the system will
     * add {@link #KEY_SHOW_SESSION_ID}, the Bundle is not null. But the
     * application should handle null case before Android 14.
     *         referring to the device that sent the request
     *     </li>
     *     <li>
     *         {@link #KEY_FOREGROUND_ACTIVITIES} provides foreground activities of up coming
     *         onHandleAssist/onHandleScreenshot calls earlier. This is only populated if session
     *         was requested with {@link VoiceInteractionSession.SHOW_WITH_ASSIST} show flag.
     *     </li>
     *     <li>
     *         Starting from Android 14, the system will add {@link #KEY_SHOW_SESSION_ID}, the
     *         Bundle is not null. But the application should handle null case before Android 14.
     *     </li>
     * </ul>
     *
     * @param showFlags The show flags originally provided to
     * {@link VoiceInteractionService#showSession VoiceInteractionService.showSession}.
     */
+7 −0
Original line number Diff line number Diff line
@@ -13,3 +13,10 @@ flag {
    description: "This flag allows hotword detection service to egress reason code for hotword bump."
    bug: "290951024"
}

flag {
    name: "allow_foreground_activities_in_on_show"
    namespace: "voice_interaction_session"
    description: "This flag allows providing foreground app component along with onShow args."
    bug: "319409708"
}
+8 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.server.wm;

import android.content.ComponentName;
import android.os.IBinder;

/**
@@ -28,11 +29,13 @@ public class ActivityAssistInfo {
    private final IBinder mActivityToken;
    private final IBinder mAssistToken;
    private final int mTaskId;
    private final ComponentName mComponentName;

    public ActivityAssistInfo(ActivityRecord activityRecord) {
        this.mActivityToken = activityRecord.token;
        this.mAssistToken = activityRecord.assistToken;
        this.mTaskId = activityRecord.getTask().mTaskId;
        this.mComponentName = activityRecord.mActivityComponent;
    }

    /** @hide */
@@ -49,4 +52,9 @@ public class ActivityAssistInfo {
    public int getTaskId() {
        return mTaskId;
    }

    /** @hide */
    public ComponentName getComponentName() {
        return mComponentName;
    }
}
+24 −1
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.app.AppOpsManager.OP_ASSIST_SCREENSHOT;
import static android.app.AppOpsManager.OP_ASSIST_STRUCTURE;
import static android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION;
import static android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
import static android.service.voice.VoiceInteractionSession.KEY_FOREGROUND_ACTIVITIES;
import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.WindowManager.LayoutParams.TYPE_VOICE_INTERACTION;

@@ -35,6 +36,7 @@ import android.app.ActivityManager;
import android.app.ActivityTaskManager;
import android.app.AppOpsManager;
import android.app.IActivityManager;
import android.app.IActivityTaskManager;
import android.app.UriGrantsManager;
import android.app.assist.AssistContent;
import android.app.assist.AssistStructure;
@@ -112,6 +114,7 @@ final class VoiceInteractionSessionConnection implements ServiceConnection,
    final Callback mCallback;
    final int mCallingUid;
    final Handler mHandler;
    final IActivityTaskManager mActivityTaskManager;
    final IActivityManager mAm;
    final UriGrantsManagerInternal mUgmInternal;
    final IWindowManager mIWindowManager;
@@ -224,6 +227,7 @@ final class VoiceInteractionSessionConnection implements ServiceConnection,
        mCallback = callback;
        mCallingUid = callingUid;
        mHandler = handler;
        mActivityTaskManager = ActivityTaskManager.getService();
        mAm = ActivityManager.getService();
        mUgmInternal = LocalServices.getService(UriGrantsManagerInternal.class);
        mIWindowManager = IWindowManager.Stub.asInterface(
@@ -300,11 +304,30 @@ final class VoiceInteractionSessionConnection implements ServiceConnection,
                for (int i = 0; i < topActivitiesCount; i++) {
                    topActivitiesToken.add(topActivities.get(i).getActivityToken());
                }
                boolean fetchDataAllowed =
                        (disabledContext & VoiceInteractionSession.SHOW_WITH_ASSIST) == 0;

                // Ensure that the current activity supports assist data
                boolean isAssistDataAllowed = false;
                try {
                    isAssistDataAllowed = mActivityTaskManager.isAssistDataAllowed();
                } catch (RemoteException e) {
                    // Should never happen
                }

                // TODO: Refactor to have all assist data allowed checks in one place.
                if (fetchDataAllowed && isAssistDataAllowed) {
                    ArrayList<ComponentName> topComponents = new ArrayList<>(topActivitiesCount);
                    for (int i = 0; i < topActivitiesCount; i++) {
                        topComponents.add(topActivities.get(i).getComponentName());
                    }
                    mShowArgs.putParcelableArrayList(KEY_FOREGROUND_ACTIVITIES, topComponents);
                }

                mAssistDataRequester.requestAssistData(topActivitiesToken,
                        fetchData,
                        fetchScreenshot,
                        (disabledContext & VoiceInteractionSession.SHOW_WITH_ASSIST) == 0,
                        fetchDataAllowed,
                        (disabledContext & VoiceInteractionSession.SHOW_WITH_SCREENSHOT) == 0,
                        mCallingUid,
                        mSessionComponentName.getPackageName(),