diff options
| author | 2024-02-09 01:01:26 +0000 | |
|---|---|---|
| committer | 2024-02-09 01:01:26 +0000 | |
| commit | 30e1c74a4a5afbe08c79e7532623e596b340295b (patch) | |
| tree | aea260174d44047565ea90433b350b7d2d3b5432 | |
| parent | 63f3bbd4045ddbed434bc86b1d59dbca886cb180 (diff) | |
| parent | 75c32a19652718986bcfba7bcb265423cf47ed27 (diff) | |
Merge "[API] Provide foreground app components in onShow args" into main
5 files changed, 86 insertions, 9 deletions
diff --git a/core/api/current.txt b/core/api/current.txt index f41982ff75bf..b095501844a1 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -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 diff --git a/core/java/android/service/voice/VoiceInteractionSession.java b/core/java/android/service/voice/VoiceInteractionSession.java index d40b39e18edb..d1368caac4fc 100644 --- a/core/java/android/service/voice/VoiceInteractionSession.java +++ b/core/java/android/service/voice/VoiceInteractionSession.java @@ -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<ComponentName> + * </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 - * 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. + * 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 + * </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}. */ diff --git a/core/java/android/service/voice/flags/flags.aconfig b/core/java/android/service/voice/flags/flags.aconfig index b596666bf607..1c8752b234c1 100644 --- a/core/java/android/service/voice/flags/flags.aconfig +++ b/core/java/android/service/voice/flags/flags.aconfig @@ -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" +} diff --git a/services/core/java/com/android/server/wm/ActivityAssistInfo.java b/services/core/java/com/android/server/wm/ActivityAssistInfo.java index e1e7ee4b3f9e..3b91780431cb 100644 --- a/services/core/java/com/android/server/wm/ActivityAssistInfo.java +++ b/services/core/java/com/android/server/wm/ActivityAssistInfo.java @@ -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; + } } diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java index d722f2f1aa0c..5dd4feb700e8 100644 --- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java +++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java @@ -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(), |