Add a mainline-accessible API to kill an app on sandbox death

This API can be accessed by mainline modules to kill a specific app
process when its associated sandbox dies. It takes in the IBinder value
of the corresponding IApplicationThread of the app to kill it.

REASON_DEPENDENCY_DIED and SUBREASON_SDK_SANDBOX_DIED are used in the
emitted AppProcessDied atom.

Bug: 261558210
Test: atest SdkSandboxLifecycleHostTest
Test: m statsd && statsd_testdrive 373 emits an atom with
SUBREASON_SDK_SANDBOX_DIED

Change-Id: Id37023eabaa5f734bb67bee07a0f779c355cc100
diff --git a/services/api/current.txt b/services/api/current.txt
index da5b1fc..5d3c88c 100644
--- a/services/api/current.txt
+++ b/services/api/current.txt
@@ -40,6 +40,7 @@
   public interface ActivityManagerLocal {
     method public boolean bindSdkSandboxService(@NonNull android.content.Intent, @NonNull android.content.ServiceConnection, int, @NonNull String, @NonNull String, int) throws android.os.RemoteException;
     method public boolean canStartForegroundService(int, int, @NonNull String);
+    method public void killSdkSandboxClientAppProcess(@NonNull android.os.IBinder);
   }
 
 }
diff --git a/services/core/java/com/android/server/am/ActivityManagerLocal.java b/services/core/java/com/android/server/am/ActivityManagerLocal.java
index 9f2cc7f..5175a31 100644
--- a/services/core/java/com/android/server/am/ActivityManagerLocal.java
+++ b/services/core/java/com/android/server/am/ActivityManagerLocal.java
@@ -23,6 +23,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.ServiceConnection;
+import android.os.IBinder;
 import android.os.RemoteException;
 
 /**
@@ -95,6 +96,15 @@
             throws RemoteException;
 
     /**
+     * Kill an app process associated with an SDK sandbox.
+     *
+     * @param clientApplicationThreadBinder binder value of the
+     *        {@link android.app.IApplicationThread} of a client app process associated with a
+     *        sandbox. This is obtained using {@link Context#getIApplicationThreadBinder()}.
+     */
+    void killSdkSandboxClientAppProcess(@NonNull IBinder clientApplicationThreadBinder);
+
+    /**
      * Start a foreground service delegate.
      * @param options foreground service delegate options.
      * @param connection a service connection served as callback to caller.
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index e7e2081..316e0d5 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -16940,6 +16940,20 @@
         }
 
         @Override
+        public void killSdkSandboxClientAppProcess(IBinder clientApplicationThreadBinder) {
+            synchronized (ActivityManagerService.this) {
+                ProcessRecord r = getRecordForAppLOSP(clientApplicationThreadBinder);
+                if (r != null) {
+                    r.killLocked(
+                            "sdk sandbox died",
+                            ApplicationExitInfo.REASON_DEPENDENCY_DIED,
+                            ApplicationExitInfo.SUBREASON_SDK_SANDBOX_DIED,
+                            true);
+                }
+            }
+        }
+
+        @Override
         public void onUserRemoved(@UserIdInt int userId) {
             // Clean up any ActivityTaskManager state (by telling it the user is stopped)
             mAtmInternal.onUserStopped(userId);