[DO NOT MERGE] Adding API for defining and invoking DirectActions

DirectActions are abstract actions defined by an Activtiy. The
actual definition of these actions will be available through
the support lib.

This API provides a secure channel for system or assistant to
interact with a running app using these Actions.

Test: atest CtsVoiceInteractionTestCases
Test: added android.voiceinteraction.cts.DirectActionsTest

Bug: 129705716

Change-Id: I0ce568e0d8f41e0fe46306052016a74c7b394efa
diff --git a/api/current.txt b/api/current.txt
index b7a951c..f62c689 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -3763,6 +3763,7 @@
     method public void onDetachedFromWindow();
     method public void onEnterAnimationComplete();
     method public boolean onGenericMotionEvent(android.view.MotionEvent);
+    method @NonNull public java.util.List<android.app.DirectAction> onGetDirectActions();
     method public boolean onKeyDown(int, android.view.KeyEvent);
     method public boolean onKeyLongPress(int, android.view.KeyEvent);
     method public boolean onKeyMultiple(int, int, android.view.KeyEvent);
@@ -3782,6 +3783,7 @@
     method public void onOptionsMenuClosed(android.view.Menu);
     method public void onPanelClosed(int, @NonNull android.view.Menu);
     method @CallSuper protected void onPause();
+    method public void onPerformDirectAction(@NonNull String, @Nullable android.os.Bundle, @Nullable android.os.CancellationSignal, @NonNull java.util.function.Consumer<android.os.Bundle>);
     method public void onPictureInPictureModeChanged(boolean, android.content.res.Configuration);
     method @Deprecated public void onPictureInPictureModeChanged(boolean);
     method @CallSuper protected void onPostCreate(@Nullable android.os.Bundle);
@@ -4603,6 +4605,22 @@
     field @Deprecated public static final int STYLE_NO_TITLE = 1; // 0x1
   }
 
+  public final class DirectAction implements android.os.Parcelable {
+    method public int describeContents();
+    method @Nullable public android.os.Bundle getExtras();
+    method @NonNull public String getId();
+    method @Nullable public android.content.LocusId getLocusId();
+    method public void writeToParcel(android.os.Parcel, int);
+    field @NonNull public static final android.os.Parcelable.Creator<android.app.DirectAction> CREATOR;
+  }
+
+  public static final class DirectAction.Builder {
+    ctor public DirectAction.Builder(@NonNull String);
+    method @NonNull public android.app.DirectAction build();
+    method @NonNull public android.app.DirectAction.Builder setExtras(@Nullable android.os.Bundle);
+    method @NonNull public android.app.DirectAction.Builder setLocusId(@Nullable android.content.LocusId);
+  }
+
   public class DownloadManager {
     method public long addCompletedDownload(String, String, boolean, String, String, long, boolean);
     method public long addCompletedDownload(String, String, boolean, String, String, long, boolean, android.net.Uri, android.net.Uri);
@@ -6338,9 +6356,13 @@
   public final class VoiceInteractor {
     method public android.app.VoiceInteractor.Request getActiveRequest(String);
     method public android.app.VoiceInteractor.Request[] getActiveRequests();
+    method public boolean isDestroyed();
+    method public void notifyDirectActionsChanged();
+    method public boolean registerOnDestroyedCallback(@NonNull java.util.concurrent.Executor, @NonNull Runnable);
     method public boolean submitRequest(android.app.VoiceInteractor.Request);
     method public boolean submitRequest(android.app.VoiceInteractor.Request, String);
     method public boolean[] supportsCommands(String[]);
+    method public boolean unregisterOnDestroyedCallback(@NonNull Runnable);
   }
 
   public static class VoiceInteractor.AbortVoiceRequest extends android.app.VoiceInteractor.Request {
@@ -41790,9 +41812,11 @@
     method public void onCreate();
     method public android.view.View onCreateContentView();
     method public void onDestroy();
+    method public void onDirectActionsInvalidated(@NonNull android.service.voice.VoiceInteractionSession.ActivityId);
     method public boolean[] onGetSupportedCommands(String[]);
-    method public void onHandleAssist(@Nullable android.os.Bundle, @Nullable android.app.assist.AssistStructure, @Nullable android.app.assist.AssistContent);
-    method public void onHandleAssistSecondary(@Nullable android.os.Bundle, @Nullable android.app.assist.AssistStructure, @Nullable android.app.assist.AssistContent, int, int);
+    method @Deprecated public void onHandleAssist(@Nullable android.os.Bundle, @Nullable android.app.assist.AssistStructure, @Nullable android.app.assist.AssistContent);
+    method public void onHandleAssist(@NonNull android.service.voice.VoiceInteractionSession.AssistState);
+    method @Deprecated public void onHandleAssistSecondary(@Nullable android.os.Bundle, @Nullable android.app.assist.AssistStructure, @Nullable android.app.assist.AssistContent, int, int);
     method public void onHandleScreenshot(@Nullable android.graphics.Bitmap);
     method public void onHide();
     method public boolean onKeyDown(int, android.view.KeyEvent);
@@ -41811,6 +41835,8 @@
     method public void onTaskFinished(android.content.Intent, int);
     method public void onTaskStarted(android.content.Intent, int);
     method public void onTrimMemory(int);
+    method public final void performDirectAction(@NonNull android.app.DirectAction, @Nullable android.os.Bundle, @Nullable android.os.CancellationSignal, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.os.Bundle>);
+    method public final void requestDirectActions(@NonNull android.service.voice.VoiceInteractionSession.ActivityId, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<java.util.List<android.app.DirectAction>>);
     method public void setContentView(android.view.View);
     method public void setDisabledShowContext(int);
     method public void setKeepAwake(boolean);
@@ -41835,6 +41861,19 @@
     method public void sendAbortResult(android.os.Bundle);
   }
 
+  public static class VoiceInteractionSession.ActivityId {
+  }
+
+  public static final class VoiceInteractionSession.AssistState {
+    method @NonNull public android.service.voice.VoiceInteractionSession.ActivityId getActivityId();
+    method @Nullable public android.app.assist.AssistContent getAssistContent();
+    method @Nullable public android.os.Bundle getAssistData();
+    method @Nullable public android.app.assist.AssistStructure getAssistStructure();
+    method @IntRange(from=0) public int getCount();
+    method @IntRange(from=0xffffffff) public int getIndex();
+    method public boolean isFocused();
+  }
+
   public static final class VoiceInteractionSession.CommandRequest extends android.service.voice.VoiceInteractionSession.Request {
     method public String getCommand();
     method public void sendIntermediateResult(android.os.Bundle);
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index c8bd275..9ae0aa0 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -65,6 +65,7 @@
 import android.os.BadParcelableException;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.CancellationSignal;
 import android.os.GraphicsEnvironment;
 import android.os.Handler;
 import android.os.IBinder;
@@ -148,8 +149,11 @@
 import java.lang.annotation.RetentionPolicy;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
+import java.util.concurrent.Executor;
+import java.util.function.Consumer;
 
 
 /**
@@ -788,6 +792,7 @@
     private Instrumentation mInstrumentation;
     @UnsupportedAppUsage
     private IBinder mToken;
+    private IBinder mAssistToken;
     @UnsupportedAppUsage
     private int mIdent;
     @UnsupportedAppUsage
@@ -866,7 +871,7 @@
     private boolean mEnableDefaultActionBarUp;
 
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
-    private VoiceInteractor mVoiceInteractor;
+    VoiceInteractor mVoiceInteractor;
 
     @UnsupportedAppUsage
     private CharSequence mTitle;
@@ -1858,9 +1863,12 @@
 
     void setVoiceInteractor(IVoiceInteractor voiceInteractor) {
         if (mVoiceInteractor != null) {
-            for (Request activeRequest: mVoiceInteractor.getActiveRequests()) {
-                activeRequest.cancel();
-                activeRequest.clear();
+            final Request[] requests = mVoiceInteractor.getActiveRequests();
+            if (requests != null) {
+                for (Request activeRequest : mVoiceInteractor.getActiveRequests()) {
+                    activeRequest.cancel();
+                    activeRequest.clear();
+                }
             }
         }
         if (voiceInteractor == null) {
@@ -2316,6 +2324,47 @@
     }
 
     /**
+     * Returns the list of direct actions supported by the app.
+     *
+     * <p>You should return the list of actions that could be executed in the
+     * current context, which is in the current state of the app. If the actions
+     * that could be executed by the app changes you should report that via
+     * calling {@link VoiceInteractor#notifyDirectActionsChanged()}.
+     *
+     * <p>To get the voice interactor you need to call {@link #getVoiceInteractor()}
+     * which would return non <code>null<c/ode> only if there is an ongoing voice
+     * interaction session. You an also detect when the voice interactor is no
+     * longer valid because the voice interaction session that is backing is finished
+     * by calling {@link VoiceInteractor#registerOnDestroyedCallback(Executor, Runnable)}.
+     *
+     * <p>This method will be called only after {@link #onStart()} is being called and
+     * before {@link #onStop()} is being called.
+     *
+     * @return The currently supported direct actions which cannot be <code>null</code>
+     * or contain <code>null</null> elements.
+     */
+    @NonNull
+    public List<DirectAction> onGetDirectActions() {
+        return Collections.emptyList();
+    }
+
+    /**
+     * This is called to perform an action previously defined by the app.
+     * Apps also have access to {@link #getVoiceInteractor()} to follow up on the action.
+     *
+     * @param actionId The ID for the action
+     * @param arguments Any additional arguments provided by the caller
+     * @param cancellationSignal A signal to cancel the operation in progress, or {@code null}
+     *                          if none.
+     * @param resultListener The callback to provide the result back to the caller
+     *
+     * @see #onGetDirectActions()
+     */
+    public void onPerformDirectAction(@NonNull String actionId,
+            @Nullable Bundle arguments, @Nullable CancellationSignal cancellationSignal,
+            @NonNull Consumer<Bundle> resultListener) { }
+
+    /**
      * Request the Keyboard Shortcuts screen to show up. This will trigger
      * {@link #onProvideKeyboardShortcuts} to retrieve the shortcuts for the foreground activity.
      */
@@ -7626,7 +7675,7 @@
             CharSequence title, Activity parent, String id,
             NonConfigurationInstances lastNonConfigurationInstances,
             Configuration config, String referrer, IVoiceInteractor voiceInteractor,
-            Window window, ActivityConfigCallback activityConfigCallback) {
+            Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken) {
         attachBaseContext(context);
 
         mFragments.attachHost(null /*parent*/);
@@ -7647,6 +7696,7 @@
         mMainThread = aThread;
         mInstrumentation = instr;
         mToken = token;
+        mAssistToken = assistToken;
         mIdent = ident;
         mApplication = application;
         mIntent = intent;
@@ -7698,6 +7748,11 @@
     }
 
     /** @hide */
+    public final IBinder getAssistToken() {
+        return mParent != null ? mParent.getAssistToken() : mAssistToken;
+    }
+
+    /** @hide */
     @VisibleForTesting
     public final ActivityThread getActivityThread() {
         return mMainThread;
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index 9cd42a5..6552a77 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -88,6 +88,7 @@
 import android.os.Binder;
 import android.os.Build;
 import android.os.Bundle;
+import android.os.CancellationSignal;
 import android.os.Debug;
 import android.os.Environment;
 import android.os.FileUtils;
@@ -95,6 +96,7 @@
 import android.os.Handler;
 import android.os.HandlerExecutor;
 import android.os.IBinder;
+import android.os.ICancellationSignal;
 import android.os.LocaleList;
 import android.os.Looper;
 import android.os.Message;
@@ -121,6 +123,7 @@
 import android.renderscript.RenderScriptCacheDir;
 import android.security.NetworkSecurityPolicy;
 import android.security.net.config.NetworkSecurityConfigProvider;
+import android.service.voice.VoiceInteractionSession;
 import android.system.ErrnoException;
 import android.system.OsConstants;
 import android.system.StructStat;
@@ -160,6 +163,7 @@
 import com.android.internal.os.SomeArgs;
 import com.android.internal.util.ArrayUtils;
 import com.android.internal.util.FastPrintWriter;
+import com.android.internal.util.Preconditions;
 import com.android.internal.util.function.pooled.PooledLambda;
 import com.android.org.conscrypt.OpenSSLSocketImpl;
 import com.android.org.conscrypt.TrustedCertificateStore;
@@ -450,6 +454,7 @@
     public static final class ActivityClientRecord {
         @UnsupportedAppUsage
         public IBinder token;
+        public IBinder assistToken;
         int ident;
         @UnsupportedAppUsage
         Intent intent;
@@ -526,8 +531,10 @@
                 String referrer, IVoiceInteractor voiceInteractor, Bundle state,
                 PersistableBundle persistentState, List<ResultInfo> pendingResults,
                 List<ReferrerIntent> pendingNewIntents, boolean isForward,
-                ProfilerInfo profilerInfo, ClientTransactionHandler client) {
+                ProfilerInfo profilerInfo, ClientTransactionHandler client,
+                IBinder assistToken) {
             this.token = token;
+            this.assistToken = assistToken;
             this.ident = ident;
             this.intent = intent;
             this.referrer = referrer;
@@ -1645,6 +1652,33 @@
         public void scheduleTransaction(ClientTransaction transaction) throws RemoteException {
             ActivityThread.this.scheduleTransaction(transaction);
         }
+
+        @Override
+        public void requestDirectActions(@NonNull IBinder activityToken,
+                @NonNull IVoiceInteractor interactor, @NonNull RemoteCallback callback) {
+            mH.sendMessage(PooledLambda.obtainMessage(ActivityThread::handleRequestDirectActions,
+                    ActivityThread.this, activityToken, interactor, callback));
+        }
+
+        @Override
+        public void performDirectAction(IBinder activityToken, String actionId, Bundle arguments,
+                RemoteCallback cancellationCallback, RemoteCallback resultCallback) {
+            final CancellationSignal cancellationSignal;
+            if (cancellationCallback != null) {
+                final ICancellationSignal transport = CancellationSignal.createTransport();
+                cancellationSignal = CancellationSignal.fromTransport(transport);
+                final Bundle cancellationResult = new Bundle();
+                cancellationResult.putBinder(VoiceInteractor.KEY_CANCELLATION_SIGNAL,
+                        transport.asBinder());
+                cancellationCallback.sendResult(cancellationResult);
+            } else {
+                cancellationSignal = new CancellationSignal();
+            }
+
+            mH.sendMessage(PooledLambda.obtainMessage(ActivityThread::handlePerformDirectAction,
+                    ActivityThread.this, activityToken, actionId, arguments,
+                    cancellationSignal, resultCallback));
+        }
     }
 
     class H extends Handler {
@@ -2877,9 +2911,10 @@
     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
     public final Activity startActivityNow(Activity parent, String id,
         Intent intent, ActivityInfo activityInfo, IBinder token, Bundle state,
-        Activity.NonConfigurationInstances lastNonConfigurationInstances) {
+        Activity.NonConfigurationInstances lastNonConfigurationInstances, IBinder assistToken) {
         ActivityClientRecord r = new ActivityClientRecord();
             r.token = token;
+            r.assistToken = assistToken;
             r.ident = 0;
             r.intent = intent;
             r.state = state;
@@ -3120,7 +3155,8 @@
                 activity.attach(appContext, this, getInstrumentation(), r.token,
                         r.ident, app, r.intent, r.activityInfo, title, r.parent,
                         r.embeddedID, r.lastNonConfigurationInstances, config,
-                        r.referrer, r.voiceInteractor, window, r.configCallback);
+                        r.referrer, r.voiceInteractor, window, r.configCallback,
+                        r.assistToken);
 
                 if (customIntent != null) {
                     activity.mIntent = customIntent;
@@ -3352,7 +3388,6 @@
         } catch (RemoteException ex) {
             throw ex.rethrowFromSystemServer();
         }
-
     }
 
     private void deliverNewIntents(ActivityClientRecord r, List<ReferrerIntent> intents) {
@@ -3432,6 +3467,7 @@
                     r.activity.onProvideAssistContent(content);
                 }
             }
+
         }
         if (structure == null) {
             structure = new AssistStructure();
@@ -3451,6 +3487,68 @@
         }
     }
 
+    /** Fetches the user actions for the corresponding activity */
+    private void handleRequestDirectActions(@NonNull IBinder activityToken,
+            @NonNull IVoiceInteractor interactor, @NonNull RemoteCallback callback) {
+        final ActivityClientRecord r = mActivities.get(activityToken);
+        if (r != null) {
+            final int lifecycleState = r.getLifecycleState();
+            if (lifecycleState < ON_START || lifecycleState >= ON_STOP) {
+                callback.sendResult(null);
+                return;
+            }
+            if (r.activity.mVoiceInteractor == null
+                    || r.activity.mVoiceInteractor.mInteractor.asBinder()
+                    != interactor.asBinder()) {
+                if (r.activity.mVoiceInteractor != null) {
+                    r.activity.mVoiceInteractor.destroy();
+                }
+                r.activity.mVoiceInteractor = new VoiceInteractor(interactor, r.activity,
+                        r.activity, Looper.myLooper());
+            }
+            final List<DirectAction> actions = r.activity.onGetDirectActions();
+            Preconditions.checkNotNull(actions);
+            Preconditions.checkCollectionElementsNotNull(actions, "actions");
+            if (actions != null && !actions.isEmpty()) {
+                final int actionCount = actions.size();
+                for (int i = 0; i < actionCount; i++) {
+                    final DirectAction action = actions.get(i);
+                    action.setSource(r.activity.getTaskId(), r.activity.getAssistToken());
+                }
+                final Bundle result = new Bundle();
+                result.putParcelable(DirectAction.KEY_ACTIONS_LIST,
+                        new ParceledListSlice<>(actions));
+                callback.sendResult(result);
+            }
+        }
+        callback.sendResult(null);
+    }
+
+    /** Performs an actions in the corresponding activity */
+    private void handlePerformDirectAction(@NonNull IBinder activityToken,
+            @NonNull String actionId, @Nullable Bundle arguments,
+            @NonNull CancellationSignal cancellationSignal,
+            @NonNull RemoteCallback resultCallback) {
+        final ActivityClientRecord r = mActivities.get(activityToken);
+        if (r != null) {
+            final int lifecycleState = r.getLifecycleState();
+            if (lifecycleState < ON_START || lifecycleState >= ON_STOP) {
+                resultCallback.sendResult(null);
+                return;
+            }
+            final Bundle nonNullArguments = (arguments != null) ? arguments : Bundle.EMPTY;
+            final WeakReference<RemoteCallback> weakCallback = new WeakReference<>(resultCallback);
+            r.activity.onPerformDirectAction(actionId, nonNullArguments, cancellationSignal,
+                    (b) -> {
+                final RemoteCallback strongCallback = weakCallback.get();
+                if (strongCallback != null) {
+                    strongCallback.sendResult(b);
+                }
+            });
+        }
+        resultCallback.sendResult(null);
+    }
+
     public void handleTranslucentConversionComplete(IBinder token, boolean drawComplete) {
         ActivityClientRecord r = mActivities.get(token);
         if (r != null) {
diff --git a/core/java/android/app/DirectAction.java b/core/java/android/app/DirectAction.java
new file mode 100644
index 0000000..d191f4b
--- /dev/null
+++ b/core/java/android/app/DirectAction.java
@@ -0,0 +1,188 @@
+/*
+ * Copyright (C) 2019 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.NonNull;
+import android.annotation.Nullable;
+import android.content.LocusId;
+import android.os.Bundle;
+import android.os.IBinder;
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import com.android.internal.util.Preconditions;
+
+/**
+ * Represents a abstract action that can be perform on this app. This are requested from
+ * outside the app's UI (eg by SystemUI or assistant).
+ */
+public final class DirectAction implements Parcelable {
+
+    /**
+     * @hide
+     */
+    public static final String KEY_ACTIONS_LIST = "actions_list";
+
+    private int mTaskId;
+    private IBinder mActivityId;
+
+    @NonNull
+    private final String mID;
+    @Nullable
+    private final Bundle mExtras;
+    @Nullable
+    private final LocusId mLocusId;
+
+    /** @hide */
+    public DirectAction(@NonNull String id, @Nullable Bundle extras,
+            @Nullable LocusId locusId) {
+        mID = Preconditions.checkStringNotEmpty(id);
+        mExtras = extras;
+        mLocusId = locusId;
+    }
+
+    /** @hide */
+    public void setSource(int taskId, IBinder activityId) {
+        mTaskId = taskId;
+        mActivityId = activityId;
+    }
+
+    /**
+     * @hide
+     */
+    public DirectAction(@NonNull DirectAction original) {
+        mTaskId = original.mTaskId;
+        mActivityId = original.mActivityId;
+        mID = original.mID;
+        mExtras = original.mExtras;
+        mLocusId = original.mLocusId;
+    }
+
+    private DirectAction(Parcel in) {
+        mTaskId = in.readInt();
+        mActivityId = in.readStrongBinder();
+        mID = in.readString();
+        mExtras = in.readBundle();
+        final String idString = in.readString();
+        mLocusId = (idString != null) ? new LocusId(idString) : null;
+    }
+
+    /** @hide */
+    public int getTaskId() {
+        return mTaskId;
+    }
+
+    /** @hide */
+    public IBinder getActivityId() {
+        return mActivityId;
+    }
+
+    /**
+     * Returns the ID for this action.
+     */
+    @NonNull
+    public String getId() {
+        return mID;
+    }
+
+    /**
+     * Returns any extras associated with this action.
+     */
+    @Nullable
+    public Bundle getExtras() {
+        return mExtras;
+    }
+
+    /**
+     * Returns the LocusId for the current state for the app
+     */
+    @Nullable
+    public LocusId getLocusId() {
+        return mLocusId;
+    }
+
+    @Override
+    public int describeContents() {
+        return 0;
+    }
+
+    @Override
+    public void writeToParcel(Parcel dest, int flags) {
+        dest.writeInt(mTaskId);
+        dest.writeStrongBinder(mActivityId);
+        dest.writeString(mID);
+        dest.writeBundle(mExtras);
+        dest.writeString(mLocusId.getId());
+    }
+
+    /**
+     * Builder for construction of DirectAction.
+     */
+    public static final class Builder {
+        private @NonNull String mId;
+        private @Nullable Bundle mExtras;
+        private @Nullable LocusId mLocusId;
+
+        /**
+         * Creates a new instance.
+         *
+         * @param id The mandatory action id.
+         */
+        public Builder(@NonNull String id) {
+            Preconditions.checkNotNull(id);
+            mId = id;
+        }
+
+        /**
+         * Sets the optional action extras.
+         *
+         * @param extras The extras.
+         * @return This builder.
+         */
+        public @NonNull Builder setExtras(@Nullable Bundle extras) {
+            mExtras = extras;
+            return this;
+        }
+
+        /**
+         * Sets the optional locus id.
+         *
+         * @param locusId The locus id.
+         * @return This builder.
+         */
+        public @NonNull Builder setLocusId(@Nullable LocusId locusId) {
+            mLocusId = locusId;
+            return this;
+        }
+
+        /**
+         * @return A newly constructed instance.
+         */
+        public @NonNull DirectAction build() {
+            return new DirectAction(mId, mExtras, mLocusId);
+        }
+    }
+
+    public static final @NonNull Parcelable.Creator<DirectAction> CREATOR =
+            new Parcelable.Creator<DirectAction>() {
+                public DirectAction createFromParcel(Parcel in) {
+                    return new DirectAction(in);
+                }
+                public DirectAction[] newArray(int size) {
+                    return new DirectAction[size];
+                }
+            };
+}
diff --git a/core/java/android/app/IApplicationThread.aidl b/core/java/android/app/IApplicationThread.aidl
index 3a09c4c..ac55c53 100644
--- a/core/java/android/app/IApplicationThread.aidl
+++ b/core/java/android/app/IApplicationThread.aidl
@@ -140,4 +140,9 @@
     void scheduleApplicationInfoChanged(in ApplicationInfo ai);
     void setNetworkBlockSeq(long procStateSeq);
     void scheduleTransaction(in ClientTransaction transaction);
+    void requestDirectActions(IBinder activityToken, IVoiceInteractor intractor,
+        in RemoteCallback callback);
+    void performDirectAction(IBinder activityToken, String actionId,
+            in Bundle arguments, in RemoteCallback cancellationCallback,
+            in RemoteCallback resultCallback);
 }
diff --git a/core/java/android/app/Instrumentation.java b/core/java/android/app/Instrumentation.java
index 4f94209..41733b3 100644
--- a/core/java/android/app/Instrumentation.java
+++ b/core/java/android/app/Instrumentation.java
@@ -1206,7 +1206,7 @@
     public Activity newActivity(Class<?> clazz, Context context, 
             IBinder token, Application application, Intent intent, ActivityInfo info, 
             CharSequence title, Activity parent, String id,
-            Object lastNonConfigurationInstance) throws InstantiationException, 
+            Object lastNonConfigurationInstance) throws InstantiationException,
             IllegalAccessException {
         Activity activity = (Activity)clazz.newInstance();
         ActivityThread aThread = null;
@@ -1218,7 +1218,7 @@
                 info, title, parent, id,
                 (Activity.NonConfigurationInstances)lastNonConfigurationInstance,
                 new Configuration(), null /* referrer */, null /* voiceInteractor */,
-                null /* window */, null /* activityConfigCallback */);
+                null /* window */, null /* activityConfigCallback */, null /*assistToken*/);
         return activity;
     }
 
diff --git a/core/java/android/app/LocalActivityManager.java b/core/java/android/app/LocalActivityManager.java
index 94b1d77..19575b2 100644
--- a/core/java/android/app/LocalActivityManager.java
+++ b/core/java/android/app/LocalActivityManager.java
@@ -144,7 +144,7 @@
                 r.activityInfo = mActivityThread.resolveActivityInfo(r.intent);
             }
             r.activity = mActivityThread.startActivityNow(
-                    mParent, r.id, r.intent, r.activityInfo, r, r.instanceState, instance);
+                    mParent, r.id, r.intent, r.activityInfo, r, r.instanceState, instance, r);
             if (r.activity == null) {
                 return;
             }
diff --git a/core/java/android/app/VoiceInteractor.java b/core/java/android/app/VoiceInteractor.java
index 36ba78b..7828573 100644
--- a/core/java/android/app/VoiceInteractor.java
+++ b/core/java/android/app/VoiceInteractor.java
@@ -16,11 +16,13 @@
 
 package android.app;
 
+import android.annotation.CallbackExecutor;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.content.Context;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.os.ICancellationSignal;
 import android.os.Looper;
 import android.os.Message;
 import android.os.Parcel;
@@ -29,15 +31,20 @@
 import android.util.ArrayMap;
 import android.util.DebugUtils;
 import android.util.Log;
+
 import com.android.internal.app.IVoiceInteractor;
 import com.android.internal.app.IVoiceInteractorCallback;
 import com.android.internal.app.IVoiceInteractorRequest;
 import com.android.internal.os.HandlerCaller;
 import com.android.internal.os.SomeArgs;
+import com.android.internal.util.Preconditions;
+import com.android.internal.util.function.pooled.PooledLambda;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
+import java.lang.ref.WeakReference;
 import java.util.ArrayList;
+import java.util.concurrent.Executor;
 
 /**
  * Interface for an {@link Activity} to interact with the user through voice.  Use
@@ -67,7 +74,12 @@
 
     static final Request[] NO_REQUESTS = new Request[0];
 
-    final IVoiceInteractor mInteractor;
+    /** @hide */
+    public static final String KEY_CANCELLATION_SIGNAL = "key_cancellation_signal";
+    /** @hide */
+    public static final String KEY_KILL_SIGNAL = "key_kill_signal";
+
+    IVoiceInteractor mInteractor;
 
     Context mContext;
     Activity mActivity;
@@ -189,13 +201,20 @@
         }
 
         @Override
-        public void deliverCancel(IVoiceInteractorRequest request) throws RemoteException {
+        public void deliverCancel(IVoiceInteractorRequest request) {
             mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageOO(
                     MSG_CANCEL_RESULT, request, null));
         }
+
+        @Override
+        public void destroy() {
+            mHandlerCaller.getHandler().sendMessage(PooledLambda.obtainMessage(
+                    VoiceInteractor::destroy, VoiceInteractor.this));
+        }
     };
 
     final ArrayMap<IBinder, Request> mActiveRequests = new ArrayMap<>();
+    final ArrayMap<Runnable, Executor> mOnDestroyCallbacks = new ArrayMap<>();
 
     static final int MSG_CONFIRMATION_RESULT = 1;
     static final int MSG_PICK_OPTION_RESULT = 2;
@@ -887,6 +906,11 @@
         mContext = context;
         mActivity = activity;
         mHandlerCaller = new HandlerCaller(context, looper, mHandlerCallerCallback, true);
+        try {
+            mInteractor.setKillCallback(new KillCallback(this));
+        } catch (RemoteException e) {
+            /* ignore */
+        }
     }
 
     Request pullRequest(IVoiceInteractorRequest request, boolean complete) {
@@ -957,6 +981,27 @@
         mActivity = null;
     }
 
+    void destroy() {
+        final int requestCount = mActiveRequests.size();
+        for (int i = requestCount - 1; i >= 0; i--) {
+            final Request request = mActiveRequests.valueAt(i);
+            mActiveRequests.removeAt(i);
+            request.cancel();
+        }
+
+        final int callbackCount = mOnDestroyCallbacks.size();
+        for (int i = callbackCount - 1; i >= 0; i--) {
+            final Runnable callback = mOnDestroyCallbacks.keyAt(i);
+            final Executor executor = mOnDestroyCallbacks.valueAt(i);
+            executor.execute(callback);
+            mOnDestroyCallbacks.removeAt(i);
+        }
+
+        // destroyed now
+        mInteractor = null;
+        mActivity.setVoiceInteractor(null);
+    }
+
     public boolean submitRequest(Request request) {
         return submitRequest(request, null);
     }
@@ -973,6 +1018,10 @@
      * @return Returns true of the request was successfully submitted, else false.
      */
     public boolean submitRequest(Request request, String name) {
+        if (isDestroyed()) {
+            Log.w(TAG, "Cannot interact with a destroyed voice interactor");
+            return false;
+        }
         try {
             if (request.mRequestInterface != null) {
                 throw new IllegalStateException("Given " + request + " is already active");
@@ -997,6 +1046,10 @@
      * Return all currently active requests.
      */
     public Request[] getActiveRequests() {
+        if (isDestroyed()) {
+            Log.w(TAG, "Cannot interact with a destroyed voice interactor");
+            return null;
+        }
         synchronized (mActiveRequests) {
             final int N = mActiveRequests.size();
             if (N <= 0) {
@@ -1018,6 +1071,10 @@
      * @return Returns the active request with that name, or null if there was none.
      */
     public Request getActiveRequest(String name) {
+        if (isDestroyed()) {
+            Log.w(TAG, "Cannot interact with a destroyed voice interactor");
+            return null;
+        }
         synchronized (mActiveRequests) {
             final int N = mActiveRequests.size();
             for (int i=0; i<N; i++) {
@@ -1040,6 +1097,10 @@
      * @return Array of booleans indicating whether each command is supported or not.
      */
     public boolean[] supportsCommands(String[] commands) {
+        if (isDestroyed()) {
+            Log.w(TAG, "Cannot interact with a destroyed voice interactor");
+            return new boolean[commands.length];
+        }
         try {
             boolean[] res = mInteractor.supportsCommands(mContext.getOpPackageName(), commands);
             if (DEBUG) Log.d(TAG, "supportsCommands: cmds=" + commands + " res=" + res);
@@ -1049,6 +1110,64 @@
         }
     }
 
+    /**
+     * @return whether the voice interactor is destroyed. You should not interact
+     * with a destroyed voice interactor.
+     */
+    public boolean isDestroyed() {
+        return mInteractor == null;
+    }
+
+    /**
+     * Registers a callback to be called when the VoiceInteractor is destroyed.
+     *
+     * @param executor Executor on which to run the callback.
+     * @param callback The callback to run.
+     * @return whether the callback was registered.
+     */
+    public boolean registerOnDestroyedCallback(@NonNull @CallbackExecutor Executor executor,
+            @NonNull Runnable callback) {
+        Preconditions.checkNotNull(executor);
+        Preconditions.checkNotNull(callback);
+        if (isDestroyed()) {
+            Log.w(TAG, "Cannot interact with a destroyed voice interactor");
+            return false;
+        }
+        mOnDestroyCallbacks.put(callback, executor);
+        return true;
+    }
+
+    /**
+     * Unregisters a previously registered onDestroy callback
+     *
+     * @param callback The callback to remove.
+     * @return whether the callback was unregistered.
+     */
+    public boolean unregisterOnDestroyedCallback(@NonNull Runnable callback) {
+        Preconditions.checkNotNull(callback);
+        if (isDestroyed()) {
+            Log.w(TAG, "Cannot interact with a destroyed voice interactor");
+            return false;
+        }
+        return mOnDestroyCallbacks.remove(callback) != null;
+    }
+
+    /**
+     * Notifies the assist framework that the direct actions supported by the app changed.
+     */
+    public void notifyDirectActionsChanged() {
+        if (isDestroyed()) {
+            Log.w(TAG, "Cannot interact with a destroyed voice interactor");
+            return;
+        }
+        try {
+            mInteractor.notifyDirectActionsChanged(mActivity.getTaskId(),
+                    mActivity.getAssistToken());
+        } catch (RemoteException e) {
+            Log.w(TAG, "Voice interactor has died", e);
+        }
+    }
+
     void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
         String innerPrefix = prefix + "    ";
         if (mActiveRequests.size() > 0) {
@@ -1066,4 +1185,21 @@
         writer.println(mInteractor.asBinder());
         writer.print(prefix); writer.print("  mActivity="); writer.println(mActivity);
     }
+
+    private static final class KillCallback extends ICancellationSignal.Stub {
+        private final WeakReference<VoiceInteractor> mInteractor;
+
+        KillCallback(VoiceInteractor interactor) {
+            mInteractor= new WeakReference<>(interactor);
+        }
+
+        @Override
+        public void cancel() {
+            final VoiceInteractor voiceInteractor = mInteractor.get();
+            if (voiceInteractor != null) {
+                voiceInteractor.mHandlerCaller.getHandler().sendMessage(PooledLambda
+                        .obtainMessage(VoiceInteractor::destroy, voiceInteractor));
+            }
+        }
+    }
 }
diff --git a/core/java/android/app/assist/AssistStructure.java b/core/java/android/app/assist/AssistStructure.java
index ed3a296..0d5a763 100644
--- a/core/java/android/app/assist/AssistStructure.java
+++ b/core/java/android/app/assist/AssistStructure.java
@@ -2300,6 +2300,7 @@
 
     /**
      * @return The task id for the associated activity.
+     *
      * @hide
      */
     public int getTaskId() {
diff --git a/core/java/android/app/servertransaction/LaunchActivityItem.java b/core/java/android/app/servertransaction/LaunchActivityItem.java
index cdf5d49..1236e0a 100644
--- a/core/java/android/app/servertransaction/LaunchActivityItem.java
+++ b/core/java/android/app/servertransaction/LaunchActivityItem.java
@@ -63,6 +63,7 @@
     private List<ReferrerIntent> mPendingNewIntents;
     private boolean mIsForward;
     private ProfilerInfo mProfilerInfo;
+    private IBinder mAssistToken;
 
     @Override
     public void preExecute(ClientTransactionHandler client, IBinder token) {
@@ -78,7 +79,7 @@
         ActivityClientRecord r = new ActivityClientRecord(token, mIntent, mIdent, mInfo,
                 mOverrideConfig, mCompatInfo, mReferrer, mVoiceInteractor, mState, mPersistentState,
                 mPendingResults, mPendingNewIntents, mIsForward,
-                mProfilerInfo, client);
+                mProfilerInfo, client, mAssistToken);
         client.handleLaunchActivity(r, pendingActions, null /* customIntent */);
         Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
     }
@@ -99,14 +100,15 @@
             Configuration curConfig, Configuration overrideConfig, CompatibilityInfo compatInfo,
             String referrer, IVoiceInteractor voiceInteractor, int procState, Bundle state,
             PersistableBundle persistentState, List<ResultInfo> pendingResults,
-            List<ReferrerIntent> pendingNewIntents, boolean isForward, ProfilerInfo profilerInfo) {
+            List<ReferrerIntent> pendingNewIntents, boolean isForward, ProfilerInfo profilerInfo,
+            IBinder assistToken) {
         LaunchActivityItem instance = ObjectPool.obtain(LaunchActivityItem.class);
         if (instance == null) {
             instance = new LaunchActivityItem();
         }
         setValues(instance, intent, ident, info, curConfig, overrideConfig, compatInfo, referrer,
                 voiceInteractor, procState, state, persistentState, pendingResults,
-                pendingNewIntents, isForward, profilerInfo);
+                pendingNewIntents, isForward, profilerInfo, assistToken);
 
         return instance;
     }
@@ -114,7 +116,7 @@
     @Override
     public void recycle() {
         setValues(this, null, 0, null, null, null, null, null, null, 0, null, null, null, null,
-                false, null);
+                false, null, null);
         ObjectPool.recycle(this);
     }
 
@@ -139,6 +141,7 @@
         dest.writeTypedList(mPendingNewIntents, flags);
         dest.writeBoolean(mIsForward);
         dest.writeTypedObject(mProfilerInfo, flags);
+        dest.writeStrongBinder(mAssistToken);
     }
 
     /** Read from Parcel. */
@@ -152,7 +155,8 @@
                 in.readPersistableBundle(getClass().getClassLoader()),
                 in.createTypedArrayList(ResultInfo.CREATOR),
                 in.createTypedArrayList(ReferrerIntent.CREATOR), in.readBoolean(),
-                in.readTypedObject(ProfilerInfo.CREATOR));
+                in.readTypedObject(ProfilerInfo.CREATOR),
+                in.readStrongBinder());
     }
 
     public static final @android.annotation.NonNull Creator<LaunchActivityItem> CREATOR =
@@ -187,7 +191,8 @@
                 && Objects.equals(mPendingResults, other.mPendingResults)
                 && Objects.equals(mPendingNewIntents, other.mPendingNewIntents)
                 && mIsForward == other.mIsForward
-                && Objects.equals(mProfilerInfo, other.mProfilerInfo);
+                && Objects.equals(mProfilerInfo, other.mProfilerInfo)
+                && Objects.equals(mAssistToken, other.mAssistToken);
     }
 
     @Override
@@ -206,6 +211,7 @@
         result = 31 * result + Objects.hashCode(mPendingNewIntents);
         result = 31 * result + (mIsForward ? 1 : 0);
         result = 31 * result + Objects.hashCode(mProfilerInfo);
+        result = 31 * result + Objects.hashCode(mAssistToken);
         return result;
     }
 
@@ -247,6 +253,7 @@
                 + ",referrer=" + mReferrer + ",procState=" + mProcState + ",state=" + mState
                 + ",persistentState=" + mPersistentState + ",pendingResults=" + mPendingResults
                 + ",pendingNewIntents=" + mPendingNewIntents + ",profilerInfo=" + mProfilerInfo
+                + " assistToken=" + mAssistToken
                 + "}";
     }
 
@@ -256,7 +263,7 @@
             CompatibilityInfo compatInfo, String referrer, IVoiceInteractor voiceInteractor,
             int procState, Bundle state, PersistableBundle persistentState,
             List<ResultInfo> pendingResults, List<ReferrerIntent> pendingNewIntents,
-            boolean isForward, ProfilerInfo profilerInfo) {
+            boolean isForward, ProfilerInfo profilerInfo, IBinder assistToken) {
         instance.mIntent = intent;
         instance.mIdent = ident;
         instance.mInfo = info;
@@ -272,5 +279,6 @@
         instance.mPendingNewIntents = pendingNewIntents;
         instance.mIsForward = isForward;
         instance.mProfilerInfo = profilerInfo;
+        instance.mAssistToken = assistToken;
     }
 }
diff --git a/core/java/android/service/voice/IVoiceInteractionSession.aidl b/core/java/android/service/voice/IVoiceInteractionSession.aidl
index 78e6bc3..c142a53 100644
--- a/core/java/android/service/voice/IVoiceInteractionSession.aidl
+++ b/core/java/android/service/voice/IVoiceInteractionSession.aidl
@@ -21,6 +21,7 @@
 import android.content.Intent;
 import android.graphics.Bitmap;
 import android.os.Bundle;
+import android.os.IBinder;
 
 import com.android.internal.app.IVoiceInteractionSessionShowCallback;
 
@@ -30,8 +31,8 @@
 oneway interface IVoiceInteractionSession {
     void show(in Bundle sessionArgs, int flags, IVoiceInteractionSessionShowCallback showCallback);
     void hide();
-    void handleAssist(in Bundle assistData, in AssistStructure structure, in AssistContent content,
-                      int index, int count);
+    void handleAssist(int taskId, in IBinder activityId, in Bundle assistData,
+            in AssistStructure structure, in AssistContent content, int index, int count);
     void handleScreenshot(in Bitmap screenshot);
     void taskStarted(in Intent intent, int taskId);
     void taskFinished(in Intent intent, int taskId);
diff --git a/core/java/android/service/voice/VoiceInteractionSession.java b/core/java/android/service/voice/VoiceInteractionSession.java
index 5b5f3b8..81b84e1 100644
--- a/core/java/android/service/voice/VoiceInteractionSession.java
+++ b/core/java/android/service/voice/VoiceInteractionSession.java
@@ -18,9 +18,13 @@
 
 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
 
+import android.annotation.CallbackExecutor;
+import android.annotation.IntRange;
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.Activity;
 import android.app.Dialog;
+import android.app.DirectAction;
 import android.app.Instrumentation;
 import android.app.VoiceInteractor;
 import android.app.assist.AssistContent;
@@ -28,6 +32,7 @@
 import android.content.ComponentCallbacks2;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.ParceledListSlice;
 import android.content.res.Configuration;
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
@@ -36,9 +41,12 @@
 import android.inputmethodservice.SoftInputWindow;
 import android.os.Binder;
 import android.os.Bundle;
+import android.os.CancellationSignal;
 import android.os.Handler;
 import android.os.IBinder;
+import android.os.ICancellationSignal;
 import android.os.Message;
+import android.os.RemoteCallback;
 import android.os.RemoteException;
 import android.os.UserHandle;
 import android.util.ArrayMap;
@@ -52,6 +60,7 @@
 import android.view.WindowManager;
 import android.widget.FrameLayout;
 
+import com.android.internal.annotations.Immutable;
 import com.android.internal.app.IVoiceInteractionManagerService;
 import com.android.internal.app.IVoiceInteractionSessionShowCallback;
 import com.android.internal.app.IVoiceInteractor;
@@ -59,10 +68,16 @@
 import com.android.internal.app.IVoiceInteractorRequest;
 import com.android.internal.os.HandlerCaller;
 import com.android.internal.os.SomeArgs;
+import com.android.internal.util.Preconditions;
+import com.android.internal.util.function.pooled.PooledLambda;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
 import java.lang.ref.WeakReference;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.Executor;
+import java.util.function.Consumer;
 
 /**
  * An active voice interaction session, providing a facility for the implementation
@@ -156,6 +171,8 @@
     final WeakReference<VoiceInteractionSession> mWeakRef
             = new WeakReference<VoiceInteractionSession>(this);
 
+    ICancellationSignal mKillCallback;
+
     final IVoiceInteractor mInteractor = new IVoiceInteractor.Stub() {
         @Override
         public IVoiceInteractorRequest startConfirmation(String callingPackage,
@@ -230,6 +247,19 @@
             }
             return new boolean[commands.length];
         }
+
+        @Override
+        public void notifyDirectActionsChanged(int taskId, IBinder assistToken) {
+            mHandlerCaller.getHandler().sendMessage(PooledLambda.obtainMessage(
+                    VoiceInteractionSession::onDirectActionsInvalidated,
+                    VoiceInteractionSession.this, new ActivityId(taskId, assistToken))
+            );
+        }
+
+        @Override
+        public void setKillCallback(ICancellationSignal callback) {
+            mKillCallback = callback;
+        }
     };
 
     final IVoiceInteractionSession mSession = new IVoiceInteractionSession.Stub() {
@@ -248,8 +278,9 @@
         }
 
         @Override
-        public void handleAssist(final Bundle data, final AssistStructure structure,
-                final AssistContent content, final int index, final int count) {
+        public void handleAssist(final int taskId, final IBinder assistToken, final Bundle data,
+                final AssistStructure structure, final AssistContent content, final int index,
+                final int count) {
             // We want to pre-warm the AssistStructure before handing it off to the main
             // thread.  We also want to do this on a separate thread, so that if the app
             // is for some reason slow (due to slow filling in of async children in the
@@ -267,9 +298,19 @@
                             failure = e;
                         }
                     }
-                    mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageOOOOII(MSG_HANDLE_ASSIST,
-                            data, failure == null ? structure : null, failure, content,
-                            index, count));
+
+                    SomeArgs args = SomeArgs.obtain();
+                    args.argi1 = taskId;
+                    args.arg1 = data;
+                    args.arg2 = (failure == null) ? structure : null;
+                    args.arg3 = failure;
+                    args.arg4 = content;
+                    args.arg5 = assistToken;
+                    args.argi5 = index;
+                    args.argi6 = count;
+
+                    mHandlerCaller.sendMessage(mHandlerCaller.obtainMessageO(
+                            MSG_HANDLE_ASSIST, args));
                 }
             };
             retriever.start();
@@ -855,17 +896,13 @@
                     break;
                 case MSG_HANDLE_ASSIST:
                     args = (SomeArgs)msg.obj;
-                    if (DEBUG) Log.d(TAG, "onHandleAssist: data=" + args.arg1
+                    if (DEBUG) Log.d(TAG, "onHandleAssist: taskId=" + args.argi1
+                            + "assistToken=" + args.arg5 + " data=" + args.arg1
                             + " structure=" + args.arg2 + " content=" + args.arg3
                             + " activityIndex=" + args.argi5 + " activityCount=" + args.argi6);
-                    if (args.argi5 == 0) {
-                        doOnHandleAssist((Bundle) args.arg1, (AssistStructure) args.arg2,
-                                (Throwable) args.arg3, (AssistContent) args.arg4);
-                    } else {
-                        doOnHandleAssistSecondary((Bundle) args.arg1, (AssistStructure) args.arg2,
-                                (Throwable) args.arg3, (AssistContent) args.arg4,
-                                args.argi5, args.argi6);
-                    }
+                    doOnHandleAssist(args.argi1, (IBinder) args.arg5, (Bundle) args.arg1,
+                            (AssistStructure) args.arg2, (Throwable) args.arg3,
+                            (AssistContent) args.arg4, args.argi5, args.argi6);
                     break;
                 case MSG_HANDLE_SCREENSHOT:
                     if (DEBUG) Log.d(TAG, "onHandleScreenshot: " + msg.obj);
@@ -1062,6 +1099,13 @@
 
     void doDestroy() {
         onDestroy();
+        if (mKillCallback != null) {
+            try {
+                mKillCallback.cancel();
+            } catch (RemoteException e) {
+                /* ignore */
+            }
+        }
         if (mInitialized) {
             mRootView.getViewTreeObserver().removeOnComputeInternalInsetsListener(
                     mInsetsComputer);
@@ -1301,6 +1345,94 @@
     }
 
     /**
+     * Requests a list of supported actions from an app.
+     *
+     * @param activityId Ths activity id of the app to get the actions from.
+     * @param resultExecutor The handler to receive the callback
+     * @param callback The callback to receive the response
+     */
+    public final void requestDirectActions(@NonNull ActivityId activityId,
+            @NonNull @CallbackExecutor Executor resultExecutor,
+            @NonNull Consumer<List<DirectAction>> callback) {
+        if (mToken == null) {
+            throw new IllegalStateException("Can't call before onCreate()");
+        }
+        try {
+            mSystemService.requestDirectActions(mToken, activityId.getTaskId(),
+                    activityId.getAssistToken(), new RemoteCallback(new DirectActionsReceiver(
+                            Preconditions.checkNotNull(resultExecutor),
+                            Preconditions.checkNotNull(callback))));
+        } catch (RemoteException e) {
+            e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
+     * Called when the direct actions are invalidated.
+     */
+    public void onDirectActionsInvalidated(@NonNull ActivityId activityId) {
+
+    }
+
+    /**
+     * Asks that an action be performed by the app. This will send a request to the app which
+     * provided this action.
+     *
+     * <p> An action could take time to execute and the result is provided asynchronously
+     * via a callback. If the action is taking longer and you want to cancel its execution
+     * you can pass in a cancellation signal through which to notify the app to abort the
+     * action.
+     *
+     * @param action The action to be performed.
+     * @param extras Any optional extras sent to the app as part of the request
+     * @param cancellationSignal A signal to cancel the operation in progress,
+     *                          or {@code null} if none.
+     * @param resultExecutor The handler to receive the callback.
+     * @param resultListener The callback to receive the response.
+     *
+     * @see #requestDirectActions(ActivityId, Executor, Consumer)
+     * @see Activity#onGetDirectActions()
+     */
+    public final void performDirectAction(@NonNull DirectAction action, @Nullable Bundle extras,
+            @Nullable CancellationSignal cancellationSignal,
+            @NonNull @CallbackExecutor Executor resultExecutor,
+            @NonNull Consumer<Bundle> resultListener) {
+        if (mToken == null) {
+            throw new IllegalStateException("Can't call before onCreate()");
+        }
+        Preconditions.checkNotNull(resultExecutor);
+        Preconditions.checkNotNull(resultListener);
+
+        if (cancellationSignal != null) {
+            cancellationSignal.throwIfCanceled();
+        }
+
+        final RemoteCallback remoteCallback = new RemoteCallback(b -> {
+            if (b != null) {
+                final IBinder cancellation = b.getBinder(VoiceInteractor.KEY_CANCELLATION_SIGNAL);
+                if (cancellation != null) {
+                    if (cancellationSignal != null) {
+                        cancellationSignal.setRemote(ICancellationSignal.Stub.asInterface(
+                                cancellation));
+                    }
+                } else {
+                    resultExecutor.execute(() -> resultListener.accept(b));
+                }
+            } else {
+                resultExecutor.execute(() -> resultListener.accept(Bundle.EMPTY));
+            }
+        });
+
+        try {
+            mSystemService.performDirectAction(mToken, action.getId(), extras,
+                    action.getTaskId(), action.getActivityId(), remoteCallback,
+                    remoteCallback);
+        } catch (RemoteException e) {
+            e.rethrowFromSystemServer();
+        }
+    }
+
+    /**
      * Set whether this session will keep the device awake while it is running a voice
      * activity.  By default, the system holds a wake lock for it while in this state,
      * so that it can work even if the screen is off.  Setting this to false removes that
@@ -1434,20 +1566,14 @@
         mContentFrame.requestApplyInsets();
     }
 
-    void doOnHandleAssist(Bundle data, AssistStructure structure, Throwable failure,
-            AssistContent content) {
+    void doOnHandleAssist(int taskId, IBinder assistToken, Bundle data, AssistStructure structure,
+            Throwable failure, AssistContent content, int index, int count) {
         if (failure != null) {
             onAssistStructureFailure(failure);
         }
-        onHandleAssist(data, structure, content);
-    }
-
-    void doOnHandleAssistSecondary(Bundle data, AssistStructure structure, Throwable failure,
-            AssistContent content, int index, int count) {
-        if (failure != null) {
-            onAssistStructureFailure(failure);
-        }
-        onHandleAssistSecondary(data, structure, content, index, count);
+        AssistState assistState = new AssistState(new ActivityId(taskId, assistToken),
+                data, structure, content, index, count);
+        onHandleAssist(assistState);
     }
 
     /**
@@ -1480,12 +1606,41 @@
      * May be null if assist data has been disabled by the user or device policy; will
      * not be automatically filled in with data from the app if the app has marked its
      * window as secure.
+     *
+     * @deprecated use {@link #onHandleAssist(AssistState)}
      */
+    @Deprecated
     public void onHandleAssist(@Nullable Bundle data, @Nullable AssistStructure structure,
             @Nullable AssistContent content) {
     }
 
     /**
+     * Called to receive data from the application that the user was currently viewing when
+     * an assist session is started.  If the original show request did not specify
+     * {@link #SHOW_WITH_ASSIST}, this method will not be called.
+     *
+     * <p>This method is called for all activities along with an index and count that indicates
+     * which activity the data is for. {@code index} will be between 0 and {@code count}-1 and
+     * this method is called once for each activity in no particular order. The {@code count}
+     * indicates how many activities to expect assist data for, including the top focused one.
+     * The focused activity can be determined by calling {@link AssistState#isFocused()}.
+     *
+     * <p>To be responsive to assist requests, process assist data as soon as it is received,
+     * without waiting for all queued activities to return assist data.
+     *
+     * @param state The state object capturing the state of an activity.
+     */
+    public void onHandleAssist(@NonNull AssistState state) {
+        if (state.getIndex() == 0) {
+            onHandleAssist(state.getAssistData(), state.getAssistStructure(),
+                    state.getAssistContent());
+        } else {
+            onHandleAssistSecondary(state.getAssistData(), state.getAssistStructure(),
+                    state.getAssistContent(), state.getIndex(), state.getCount());
+        }
+    }
+
+    /**
      * Called to receive data from other applications that the user was or is interacting with,
      * that are currently on the screen in a multi-window display environment, not including the
      * currently focused activity. This could be
@@ -1519,7 +1674,10 @@
      * @param count the total number of additional activities for which the assist data is being
      *        returned, including the focused activity that is returned via
      *        {@link #onHandleAssist}.
+     *
+     * @deprecated use {@link #onHandleAssist(AssistState)}
      */
+    @Deprecated
     public void onHandleAssistSecondary(@Nullable Bundle data, @Nullable AssistStructure structure,
             @Nullable AssistContent content, int index, int count) {
     }
@@ -1742,4 +1900,166 @@
             }
         }
     }
+
+    private static class DirectActionsReceiver implements RemoteCallback.OnResultListener {
+
+        @NonNull
+        private final Executor mResultExecutor;
+        private final Consumer<List<DirectAction>> mCallback;
+
+        DirectActionsReceiver(Executor resultExecutor, Consumer<List<DirectAction>> callback) {
+            mResultExecutor = resultExecutor;
+            mCallback = callback;
+        }
+
+        @Override
+        public void onResult(Bundle result) {
+            final List<DirectAction> list;
+            if (result == null) {
+                list = Collections.emptyList();
+            } else {
+                final ParceledListSlice<DirectAction> pls = result.getParcelable(
+                        DirectAction.KEY_ACTIONS_LIST);
+                if (pls != null) {
+                    final List<DirectAction> receivedList = pls.getList();
+                    list = (receivedList != null) ? receivedList : Collections.emptyList();
+                } else {
+                    list = Collections.emptyList();
+                }
+            }
+            mResultExecutor.execute(() -> mCallback.accept(list));
+        }
+    }
+
+    /**
+     * Represents assist state captured when this session was started.
+     * It contains the various assist data objects and a reference to
+     * the source activity.
+     */
+    @Immutable
+    public static final class AssistState {
+        private final @NonNull ActivityId mActivityId;
+        private final int mIndex;
+        private final int mCount;
+        private final @Nullable Bundle mData;
+        private final @Nullable AssistStructure mStructure;
+        private final @Nullable AssistContent mContent;
+
+        AssistState(@NonNull ActivityId activityId, @Nullable Bundle data,
+                @Nullable AssistStructure structure, @Nullable AssistContent content,
+                int index, int count) {
+            mActivityId = activityId;
+            mIndex = index;
+            mCount = count;
+            mData = data;
+            mStructure = structure;
+            mContent = content;
+        }
+
+        /**
+         * @return whether the source activity is focused.
+         */
+        public boolean isFocused() {
+            return mIndex == 0;
+        }
+
+        /**
+         * @return the index of the activity that this state is for.
+         */
+        public @IntRange(from = -1) int getIndex() {
+            return mIndex;
+        }
+
+        /**s
+         * @return the total number of activities for which the assist data is
+         * being returned.
+         */
+        public @IntRange(from = 0) int getCount() {
+            return mCount;
+        }
+
+        /**
+         * @return the id of the source activity
+         */
+        public @NonNull ActivityId getActivityId() {
+            return mActivityId;
+        }
+
+        /**
+         * @return Arbitrary data supplied by the app through
+         * {@link android.app.Activity#onProvideAssistData Activity.onProvideAssistData}.
+         * May be null if assist data has been disabled by the user or device policy.
+         */
+        public @Nullable Bundle getAssistData() {
+            return mData;
+        }
+
+        /**
+         * @return If available, the structure definition of all windows currently
+         * displayed by the app. May be null if assist data has been disabled by the user
+         * or device policy; will be an empty stub if the application has disabled assist
+         * by marking its window as secure.
+         */
+        public @Nullable AssistStructure getAssistStructure() {
+            return mStructure;
+        }
+
+        /**
+         * @return Additional content data supplied by the app through
+         * {@link android.app.Activity#onProvideAssistContent Activity.onProvideAssistContent}.
+         * May be null if assist data has been disabled by the user or device policy; will
+         * not be automatically filled in with data from the app if the app has marked its
+         * window as secure.
+         */
+        public @Nullable AssistContent getAssistContent() {
+            return mContent;
+        }
+    }
+
+    /**
+     * Represents the id of an assist source activity.
+     */
+    public static class ActivityId {
+        private final int mTaskId;
+        private final IBinder mAssistToken;
+
+        ActivityId(int taskId, IBinder assistToken) {
+            mTaskId = taskId;
+            mAssistToken = assistToken;
+        }
+
+        int getTaskId() {
+            return mTaskId;
+        }
+
+        IBinder getAssistToken() {
+            return mAssistToken;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+
+            ActivityId that = (ActivityId) o;
+
+            if (mTaskId != that.mTaskId) {
+                return false;
+            }
+            return mAssistToken != null
+                    ? mAssistToken.equals(that.mAssistToken)
+                    : that.mAssistToken == null;
+        }
+
+        @Override
+        public int hashCode() {
+            int result = mTaskId;
+            result = 31 * result + (mAssistToken != null ? mAssistToken.hashCode() : 0);
+            return result;
+        }
+    }
 }
diff --git a/core/java/com/android/internal/app/IVoiceInteractionManagerService.aidl b/core/java/com/android/internal/app/IVoiceInteractionManagerService.aidl
index 420749e..fb5e006 100644
--- a/core/java/com/android/internal/app/IVoiceInteractionManagerService.aidl
+++ b/core/java/com/android/internal/app/IVoiceInteractionManagerService.aidl
@@ -19,6 +19,7 @@
 import android.content.ComponentName;
 import android.content.Intent;
 import android.os.Bundle;
+import android.os.RemoteCallback;
 
 import com.android.internal.app.IVoiceActionCheckCallback;
 import com.android.internal.app.IVoiceInteractionSessionShowCallback;
@@ -157,4 +158,17 @@
      * Provide hints for showing UI.
      */
     void setUiHints(in IVoiceInteractionService service, in Bundle hints);
+
+    /**
+     * Requests a list of supported actions from a specific activity.
+     */
+    void requestDirectActions(in IBinder token, int taskId, IBinder assistToken,
+            in RemoteCallback callback);
+
+    /**
+     * Requests performing an action from a specific activity.
+     */
+    void performDirectAction(in IBinder token, String actionId, in Bundle arguments, int taskId,
+            IBinder assistToken, in RemoteCallback cancellationCallback,
+            in RemoteCallback resultCallback);
 }
diff --git a/core/java/com/android/internal/app/IVoiceInteractor.aidl b/core/java/com/android/internal/app/IVoiceInteractor.aidl
index 44feafb..d50dc0b 100644
--- a/core/java/com/android/internal/app/IVoiceInteractor.aidl
+++ b/core/java/com/android/internal/app/IVoiceInteractor.aidl
@@ -18,6 +18,7 @@
 
 import android.app.VoiceInteractor;
 import android.os.Bundle;
+import android.os.ICancellationSignal;
 
 import com.android.internal.app.IVoiceInteractorCallback;
 import com.android.internal.app.IVoiceInteractorRequest;
@@ -38,4 +39,6 @@
     IVoiceInteractorRequest startCommand(String callingPackage,
             IVoiceInteractorCallback callback, String command, in Bundle extras);
     boolean[] supportsCommands(String callingPackage, in String[] commands);
+    void notifyDirectActionsChanged(int taskId, IBinder assistToken);
+    void setKillCallback(in ICancellationSignal callback);
 }
diff --git a/core/java/com/android/internal/app/IVoiceInteractorCallback.aidl b/core/java/com/android/internal/app/IVoiceInteractorCallback.aidl
index 1331e74..2d13b03 100644
--- a/core/java/com/android/internal/app/IVoiceInteractorCallback.aidl
+++ b/core/java/com/android/internal/app/IVoiceInteractorCallback.aidl
@@ -33,4 +33,5 @@
     void deliverAbortVoiceResult(IVoiceInteractorRequest request, in Bundle result);
     void deliverCommandResult(IVoiceInteractorRequest request, boolean finished, in Bundle result);
     void deliverCancel(IVoiceInteractorRequest request);
+    void destroy();
 }
diff --git a/core/tests/coretests/src/android/app/servertransaction/ObjectPoolTests.java b/core/tests/coretests/src/android/app/servertransaction/ObjectPoolTests.java
index 5c8bced..1e49c0a 100644
--- a/core/tests/coretests/src/android/app/servertransaction/ObjectPoolTests.java
+++ b/core/tests/coretests/src/android/app/servertransaction/ObjectPoolTests.java
@@ -33,6 +33,7 @@
 import android.content.res.Configuration;
 import android.os.Binder;
 import android.os.Bundle;
+import android.os.IBinder;
 import android.os.PersistableBundle;
 import android.platform.test.annotations.Presubmit;
 
@@ -140,13 +141,14 @@
         bundle.putString("key", "value");
         PersistableBundle persistableBundle = new PersistableBundle();
         persistableBundle.putInt("k", 4);
+        IBinder assistToken = new Binder();
 
         LaunchActivityItem emptyItem = LaunchActivityItem.obtain(null, 0, null, null, null, null,
-                null, null, 0, null, null, null, null, false, null);
+                null, null, 0, null, null, null, null, false, null, null);
         LaunchActivityItem item = LaunchActivityItem.obtain(intent, ident, activityInfo,
                 config(), overrideConfig, compat, referrer, null /* voiceInteractor */,
                 procState, bundle, persistableBundle, resultInfoList(), referrerIntentList(),
-                true /* isForward */, null /* profilerInfo */);
+                true /* isForward */, null /* profilerInfo */, assistToken);
         assertNotSame(item, emptyItem);
         assertFalse(item.equals(emptyItem));
 
@@ -156,7 +158,7 @@
         LaunchActivityItem item2 = LaunchActivityItem.obtain(intent, ident, activityInfo,
                 config(), overrideConfig, compat, referrer, null /* voiceInteractor */,
                 procState, bundle, persistableBundle, resultInfoList(), referrerIntentList(),
-                true /* isForward */, null /* profilerInfo */);
+                true /* isForward */, null /* profilerInfo */, assistToken);
         assertSame(item, item2);
         assertFalse(item2.equals(emptyItem));
     }
diff --git a/core/tests/coretests/src/android/app/servertransaction/TransactionExecutorTests.java b/core/tests/coretests/src/android/app/servertransaction/TransactionExecutorTests.java
index 1cca799..1410f4f 100644
--- a/core/tests/coretests/src/android/app/servertransaction/TransactionExecutorTests.java
+++ b/core/tests/coretests/src/android/app/servertransaction/TransactionExecutorTests.java
@@ -266,7 +266,8 @@
                 null, /* overrideConfig */ null /* compatInfo */, null /* referrer */ ,
                 null /* voiceInteractor */, 0 /* procState */, null /* state */,
                 null /* persistentState */, null /* pendingResults */,
-                null /* pendingNewIntents */, false /* isForward */, null /* profilerInfo */));
+                null /* pendingNewIntents */, false /* isForward */, null /* profilerInfo */,
+                null /* assistToken*/));
         launchTransaction.addCallback(launchItem);
         mExecutor.execute(launchTransaction);
 
diff --git a/core/tests/coretests/src/android/app/servertransaction/TransactionParcelTests.java b/core/tests/coretests/src/android/app/servertransaction/TransactionParcelTests.java
index bffeb2a..1b63b7e 100644
--- a/core/tests/coretests/src/android/app/servertransaction/TransactionParcelTests.java
+++ b/core/tests/coretests/src/android/app/servertransaction/TransactionParcelTests.java
@@ -242,7 +242,7 @@
         LaunchActivityItem item = LaunchActivityItem.obtain(intent, ident, activityInfo,
                 config(), overrideConfig, compat, referrer, null /* voiceInteractor */,
                 procState, bundle, persistableBundle, resultInfoList(), referrerIntentList(),
-                true /* isForward */, null /* profilerInfo */);
+                true /* isForward */, null /* profilerInfo */, new Binder());
         writeAndPrepareForReading(item);
 
         // Read from parcel and assert
@@ -626,5 +626,15 @@
         @Override
         public final void runIsolatedEntryPoint(String entryPoint, String[] entryPointArgs) {
         }
+
+        @Override
+        public void requestDirectActions(IBinder activityToken, IVoiceInteractor intractor,
+                RemoteCallback callback) {
+        }
+
+        @Override
+        public void performDirectAction(IBinder activityToken, String actionId, Bundle arguments,
+                RemoteCallback cancellationCallback, RemoteCallback resultCallback) {
+        }
     }
 }
diff --git a/core/tests/mockingcoretests/src/android/app/activity/ActivityThreadClientTest.java b/core/tests/mockingcoretests/src/android/app/activity/ActivityThreadClientTest.java
index 86d55ea..b3f6fdb 100644
--- a/core/tests/mockingcoretests/src/android/app/activity/ActivityThreadClientTest.java
+++ b/core/tests/mockingcoretests/src/android/app/activity/ActivityThreadClientTest.java
@@ -347,7 +347,7 @@
                     CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null /* referrer */,
                     null /* voiceInteractor */, null /* state */, null /* persistentState */,
                     null /* pendingResults */, null /* pendingNewIntents */, true /* isForward */,
-                    null /* profilerInfo */,  mThread /* client */);
+                    null /* profilerInfo */,  mThread /* client */, null /* asssitToken */);
         }
 
         @Override
diff --git a/services/core/java/com/android/server/wm/ActivityRecord.java b/services/core/java/com/android/server/wm/ActivityRecord.java
index ed3ec94..9408ef7 100644
--- a/services/core/java/com/android/server/wm/ActivityRecord.java
+++ b/services/core/java/com/android/server/wm/ActivityRecord.java
@@ -417,6 +417,9 @@
     private final Configuration mTmpConfig = new Configuration();
     private final Rect mTmpBounds = new Rect();
 
+    // Token for targeting this activity for assist purposes.
+    final Binder assistToken = new Binder();
+
     private static String startingWindowStateToString(int state) {
         switch (state) {
             case STARTING_WINDOW_NOT_SHOWN:
diff --git a/services/core/java/com/android/server/wm/ActivityStackSupervisor.java b/services/core/java/com/android/server/wm/ActivityStackSupervisor.java
index 9c97674..c992a69 100644
--- a/services/core/java/com/android/server/wm/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/wm/ActivityStackSupervisor.java
@@ -837,7 +837,8 @@
                         mergedConfiguration.getOverrideConfiguration(), r.compat,
                         r.launchedFromPackage, task.voiceInteractor, proc.getReportedProcState(),
                         r.icicle, r.persistentState, results, newIntents,
-                        dc.isNextTransitionForward(), proc.createProfilerInfoIfNeeded()));
+                        dc.isNextTransitionForward(), proc.createProfilerInfoIfNeeded(),
+                                r.assistToken));
 
                 // Set desired final state.
                 final ActivityLifecycleItem lifecycleItem;
diff --git a/services/core/java/com/android/server/wm/ActivityTaskManagerInternal.java b/services/core/java/com/android/server/wm/ActivityTaskManagerInternal.java
index 48aee20..ed56501 100644
--- a/services/core/java/com/android/server/wm/ActivityTaskManagerInternal.java
+++ b/services/core/java/com/android/server/wm/ActivityTaskManagerInternal.java
@@ -34,6 +34,7 @@
 import android.os.RemoteException;
 import android.os.SystemClock;
 import android.service.voice.IVoiceInteractionSession;
+import android.util.Pair;
 import android.util.SparseIntArray;
 import android.util.proto.ProtoOutputStream;
 
@@ -89,6 +90,16 @@
             AppProtoEnums.APP_TRANSITION_RECENTS_ANIM; // 5
 
     /**
+     * The id of the task source of assist state.
+     */
+    public static final String ASSIST_TASK_ID = "taskId";
+
+    /**
+     * The id of the activity source of assist state.
+     */
+    public static final String ASSIST_ACTIVITY_ID = "activityId";
+
+    /**
      * The bundle key to extract the assist data.
      */
     public static final String ASSIST_KEY_DATA = "data";
@@ -328,6 +339,40 @@
 
     public abstract CompatibilityInfo compatibilityInfoForPackage(ApplicationInfo ai);
 
+    public final class ActivityTokens {
+        private final @NonNull IBinder mActivityToken;
+        private final @NonNull IBinder mAssistToken;
+        private final @NonNull IApplicationThread mAppThread;
+
+        public ActivityTokens(@NonNull IBinder activityToken,
+                @NonNull IBinder assistToken, @NonNull IApplicationThread appThread) {
+            mActivityToken = activityToken;
+            mAssistToken = assistToken;
+            mAppThread = appThread;
+        }
+
+        /**
+         * @return The activity token.
+         */
+        public @NonNull IBinder getActivityToken() {
+            return mActivityToken;
+        }
+
+        /**
+         * @return The assist token.
+         */
+        public @NonNull IBinder getAssistToken() {
+            return mAssistToken;
+        }
+
+        /**
+         * @return The assist token.
+         */
+        public @NonNull IApplicationThread getApplicationThread() {
+            return mAppThread;
+        }
+    }
+
     /**
      * Set the corresponding display information for the process global configuration. To be called
      * when we need to show IME on a different display.
@@ -341,6 +386,14 @@
             String resultWho, int requestCode, int resultCode, Intent data);
     public abstract void clearPendingResultForActivity(
             IBinder activityToken, WeakReference<PendingIntentRecord> pir);
+
+    /**
+     * @return the activity token and IApplicationThread for the top activity in the task or null
+     * if there isn't a top activity with a valid process.
+     */
+    @Nullable
+    public abstract ActivityTokens getTopActivityForTask(int taskId);
+
     public abstract IIntentSender getIntentSender(int type, String packageName,
             int callingUid, int userId, IBinder token, String resultWho,
             int requestCode, Intent[] intents, String[] resolvedTypes, int flags,
diff --git a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
index 0820b0d..7bc9600 100644
--- a/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
+++ b/services/core/java/com/android/server/wm/ActivityTaskManagerService.java
@@ -108,10 +108,12 @@
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.POSTFIX_VISIBILITY;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_ATM;
 import static com.android.server.wm.ActivityTaskManagerDebugConfig.TAG_WITH_CLASS_NAME;
+import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_ACTIVITY_ID;
 import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_CONTENT;
 import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_DATA;
 import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_RECEIVER_EXTRAS;
 import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_STRUCTURE;
+import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_TASK_ID;
 import static com.android.server.wm.ActivityTaskManagerService.H.REPORT_TIME_TRACKER_MSG;
 import static com.android.server.wm.ActivityTaskManagerService.UiHandler.DISMISS_DIALOG_UI_MSG;
 import static com.android.server.wm.RecentsAnimationController.REORDER_KEEP_IN_PLACE;
@@ -214,6 +216,7 @@
 import android.util.ArrayMap;
 import android.util.EventLog;
 import android.util.Log;
+import android.util.Pair;
 import android.util.Slog;
 import android.util.SparseArray;
 import android.util.SparseIntArray;
@@ -3000,6 +3003,10 @@
             if ((sendReceiver = pae.receiver) != null) {
                 // Caller wants result sent back to them.
                 sendBundle = new Bundle();
+                sendBundle.putInt(ActivityTaskManagerInternal.ASSIST_TASK_ID,
+                        pae.activity.getTaskRecord().taskId);
+                sendBundle.putBinder(ActivityTaskManagerInternal.ASSIST_ACTIVITY_ID,
+                        pae.activity.assistToken);
                 sendBundle.putBundle(ASSIST_KEY_DATA, pae.extras);
                 sendBundle.putParcelable(ASSIST_KEY_STRUCTURE, pae.structure);
                 sendBundle.putParcelable(ASSIST_KEY_CONTENT, pae.content);
@@ -6547,6 +6554,31 @@
         }
 
         @Override
+        public ActivityTokens getTopActivityForTask(int taskId) {
+            synchronized (mGlobalLock) {
+                final TaskRecord taskRecord = mRootActivityContainer.anyTaskForId(taskId);
+                if (taskRecord == null) {
+                    Slog.w(TAG, "getApplicationThreadForTopActivity failed:"
+                            + " Requested task not found");
+                    return null;
+                }
+                final ActivityRecord activity = taskRecord.getTopActivity();
+                if (activity == null) {
+                    Slog.w(TAG, "getApplicationThreadForTopActivity failed:"
+                            + " Requested activity not found");
+                    return null;
+                }
+                if (!activity.attachedToProcess()) {
+                    Slog.w(TAG, "getApplicationThreadForTopActivity failed: No process for "
+                            + activity);
+                    return null;
+                }
+                return new ActivityTokens(activity.appToken, activity.assistToken,
+                        activity.app.getThread());
+            }
+        }
+
+        @Override
         public IIntentSender getIntentSender(int type, String packageName,
                 int callingUid, int userId, IBinder token, String resultWho,
                 int requestCode, Intent[] intents, String[] resolvedTypes, int flags,
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
index 4d7ae73..5903005 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
@@ -19,6 +19,7 @@
 import android.Manifest;
 import android.annotation.CallbackExecutor;
 import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.app.ActivityManager;
 import android.app.ActivityManagerInternal;
 import android.app.AppGlobals;
@@ -47,6 +48,7 @@
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Parcel;
+import android.os.RemoteCallback;
 import android.os.RemoteCallbackList;
 import android.os.RemoteException;
 import android.os.UserHandle;
@@ -712,6 +714,45 @@
         }
 
         @Override
+        public void requestDirectActions(@NonNull IBinder token, int taskId, IBinder assistToken,
+                @NonNull RemoteCallback callback) {
+            synchronized (this) {
+                if (mImpl == null) {
+                    Slog.w(TAG, "requestDirectActions without running voice interaction service");
+                    callback.sendResult(null);
+                    return;
+                }
+                final long caller = Binder.clearCallingIdentity();
+                try {
+                    mImpl.requestDirectActionsLocked(token, taskId, assistToken, callback);
+                } finally {
+                    Binder.restoreCallingIdentity(caller);
+                }
+            }
+        }
+
+        @Override
+        public void performDirectAction(@NonNull IBinder token, @NonNull String actionId,
+                @NonNull Bundle arguments, int taskId, IBinder assistToken,
+                @Nullable RemoteCallback cancellationCallback,
+                @NonNull RemoteCallback resultCallback) {
+            synchronized (this) {
+                if (mImpl == null) {
+                    Slog.w(TAG, "performDirectAction without running voice interaction service");
+                    resultCallback.sendResult(null);
+                    return;
+                }
+                final long caller = Binder.clearCallingIdentity();
+                try {
+                    mImpl.performDirectActionLocked(token, actionId, arguments, taskId,
+                            assistToken, cancellationCallback, resultCallback);
+                } finally {
+                    Binder.restoreCallingIdentity(caller);
+                }
+            }
+        }
+
+        @Override
         public void setKeepAwake(IBinder token, boolean keepAwake) {
             synchronized (this) {
                 if (mImpl == null) {
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
index ea52377..0d80e60 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
@@ -22,11 +22,14 @@
 import static android.app.ActivityManager.START_VOICE_NOT_ACTIVE_SESSION;
 import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
 
+import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.app.ActivityManager;
 import android.app.ActivityOptions;
 import android.app.ActivityTaskManager;
 import android.app.IActivityManager;
 import android.app.IActivityTaskManager;
+import android.app.IApplicationThread;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
@@ -37,6 +40,7 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
+import android.os.RemoteCallback;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.UserHandle;
@@ -44,6 +48,7 @@
 import android.service.voice.IVoiceInteractionSession;
 import android.service.voice.VoiceInteractionService;
 import android.service.voice.VoiceInteractionServiceInfo;
+import android.util.Pair;
 import android.util.PrintWriterPrinter;
 import android.util.Slog;
 import android.view.IWindowManager;
@@ -53,6 +58,7 @@
 import com.android.internal.app.IVoiceInteractor;
 import com.android.server.LocalServices;
 import com.android.server.wm.ActivityTaskManagerInternal;
+import com.android.server.wm.ActivityTaskManagerInternal.ActivityTokens;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
@@ -256,6 +262,55 @@
         }
     }
 
+    public void requestDirectActionsLocked(@NonNull IBinder token, int taskId,
+            IBinder assistToken, @NonNull RemoteCallback callback) {
+        if (mActiveSession == null || token != mActiveSession.mToken) {
+            Slog.w(TAG, "requestDirectActionsLocked does not match active session");
+            callback.sendResult(null);
+            return;
+        }
+        final ActivityTokens tokens = LocalServices.getService(
+                ActivityTaskManagerInternal.class).getTopActivityForTask(taskId);
+        if (tokens == null || tokens.getAssistToken() != assistToken) {
+            Slog.w(TAG, "Unknown activity to query for direct actions");
+            callback.sendResult(null);
+        } else {
+            try {
+                tokens.getApplicationThread().requestDirectActions(tokens.getActivityToken(),
+                        mActiveSession.mInteractor, callback);
+            } catch (RemoteException e) {
+                Slog.w("Unexpected remote error", e);
+                callback.sendResult(null);
+            }
+        }
+    }
+
+    void performDirectActionLocked(@NonNull IBinder token, @NonNull String actionId,
+            @Nullable Bundle arguments, int taskId, IBinder assistToken,
+            @Nullable RemoteCallback cancellationCallback,
+            @NonNull RemoteCallback resultCallback) {
+        if (mActiveSession == null || token != mActiveSession.mToken) {
+            Slog.w(TAG, "performDirectActionLocked does not match active session");
+            resultCallback.sendResult(null);
+            return;
+        }
+        final ActivityTokens tokens = LocalServices.getService(
+                ActivityTaskManagerInternal.class).getTopActivityForTask(taskId);
+        if (tokens == null || tokens.getAssistToken() != assistToken) {
+            Slog.w(TAG, "Unknown activity to perform a direct action");
+            resultCallback.sendResult(null);
+        } else {
+            try {
+                tokens.getApplicationThread().performDirectAction(tokens.getActivityToken(),
+                        actionId, arguments, cancellationCallback,
+                        resultCallback);
+            } catch (RemoteException e) {
+                Slog.w("Unexpected remote error", e);
+                resultCallback.sendResult(null);
+            }
+        }
+    }
+
     public void setKeepAwakeLocked(IBinder token, boolean keepAwake) {
         try {
             if (mActiveSession == null || token != mActiveSession.mToken) {
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java
index 24690f5..000a347 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java
@@ -23,9 +23,11 @@
 import static android.view.Display.DEFAULT_DISPLAY;
 import static android.view.WindowManager.LayoutParams.TYPE_VOICE_INTERACTION;
 
+import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_ACTIVITY_ID;
 import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_CONTENT;
 import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_DATA;
 import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_KEY_STRUCTURE;
+import static com.android.server.wm.ActivityTaskManagerInternal.ASSIST_TASK_ID;
 
 import android.app.ActivityManager;
 import android.app.ActivityTaskManager;
@@ -251,11 +253,13 @@
 
         if (data == null) {
             try {
-                mSession.handleAssist(null, null, null, 0, 0);
+                mSession.handleAssist(-1, null, null, null, null, -1, 0);
             } catch (RemoteException e) {
                 // Ignore
             }
         } else {
+            final int taskId = data.getInt(ASSIST_TASK_ID);
+            final IBinder activityId = data.getBinder(ASSIST_ACTIVITY_ID);
             final Bundle assistData = data.getBundle(ASSIST_KEY_DATA);
             final AssistStructure structure = data.getParcelable(ASSIST_KEY_STRUCTURE);
             final AssistContent content = data.getParcelable(ASSIST_KEY_CONTENT);
@@ -279,8 +283,8 @@
                 }
             }
             try {
-                mSession.handleAssist(assistData, structure, content, activityIndex,
-                        activityCount);
+                mSession.handleAssist(taskId, activityId, assistData, structure,
+                        content, activityIndex, activityCount);
             } catch (RemoteException e) {
                 // Ignore
             }