Move the FGS delegation API into ActivityManagerInternal

As it needs to be in the boot classpath.

Bug: 256873819
Test: atest CtsAppTestCases:ActivityManagerFgsDelegateTest
Change-Id: Ica98c62dd95ac1bb854843bd8871c3471999035f
diff --git a/core/java/android/app/ActivityManagerInternal.java b/core/java/android/app/ActivityManagerInternal.java
index 6826b67..594df58 100644
--- a/core/java/android/app/ActivityManagerInternal.java
+++ b/core/java/android/app/ActivityManagerInternal.java
@@ -28,6 +28,7 @@
 import android.content.IIntentReceiver;
 import android.content.IIntentSender;
 import android.content.Intent;
+import android.content.ServiceConnection;
 import android.content.pm.ActivityInfo;
 import android.content.pm.ActivityPresentationInfo;
 import android.content.pm.ApplicationInfo;
@@ -922,4 +923,30 @@
      * @param callingPid The PID mapped with the callback.
      */
     public abstract void unregisterStrictModeCallback(int callingPid);
+
+    /**
+     * Start a foreground service delegate.
+     * @param options foreground service delegate options.
+     * @param connection a service connection served as callback to caller.
+     * @return true if delegate is started successfully, false otherwise.
+     * @hide
+     */
+    public abstract boolean startForegroundServiceDelegate(
+            @NonNull ForegroundServiceDelegationOptions options,
+            @Nullable ServiceConnection connection);
+
+    /**
+     * Stop a foreground service delegate.
+     * @param options the foreground service delegate options.
+     * @hide
+     */
+    public abstract void stopForegroundServiceDelegate(
+            @NonNull ForegroundServiceDelegationOptions options);
+
+    /**
+     * Stop a foreground service delegate by service connection.
+     * @param connection service connection used to start delegate previously.
+     * @hide
+     */
+    public abstract void stopForegroundServiceDelegate(@NonNull ServiceConnection connection);
 }
diff --git a/services/core/java/com/android/server/am/ForegroundServiceDelegationOptions.java b/core/java/android/app/ForegroundServiceDelegationOptions.java
similarity index 91%
rename from services/core/java/com/android/server/am/ForegroundServiceDelegationOptions.java
rename to core/java/android/app/ForegroundServiceDelegationOptions.java
index 5eb5a55..875e01f 100644
--- a/services/core/java/com/android/server/am/ForegroundServiceDelegationOptions.java
+++ b/core/java/android/app/ForegroundServiceDelegationOptions.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2022 The Android Open Source Project
+ * Copyright (C) 2023 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.
@@ -14,12 +14,11 @@
  * limitations under the License.
  */
 
-package com.android.server.am;
+package android.app;
 
 import android.annotation.IntDef;
 import android.annotation.NonNull;
 import android.annotation.Nullable;
-import android.app.IApplicationThread;
 import android.content.ComponentName;
 
 import java.lang.annotation.Retention;
@@ -32,6 +31,8 @@
  * is higher than the app's actual process state if the app is in the background. This can help to
  * keep the app in the memory and extra run-time.
  * The app does not need to define an actual service component nor add it into manifest file.
+ *
+ * @hide
  */
 public class ForegroundServiceDelegationOptions {
 
@@ -191,6 +192,11 @@
         }
     }
 
+    /**
+     * The helper class to build the instance of {@link ForegroundServiceDelegate}.
+     *
+     * @hide
+     */
     public static class Builder {
         int mClientPid; // The actual app PID
         int mClientUid; // The actual app UID
@@ -202,51 +208,81 @@
         int mForegroundServiceTypes; // The foreground service types it consists of
         @DelegationService int mDelegationService; // The internal service's name, i.e. VOIP
 
+        /**
+         * Set the client app's PID.
+         */
         public Builder setClientPid(int clientPid) {
             mClientPid = clientPid;
             return this;
         }
 
+        /**
+         * Set the client app's UID.
+         */
         public Builder setClientUid(int clientUid) {
             mClientUid = clientUid;
             return this;
         }
 
+        /**
+         * Set the client app's package name.
+         */
         public Builder setClientPackageName(@NonNull String clientPackageName) {
             mClientPackageName = clientPackageName;
             return this;
         }
 
+        /**
+         * Set the notification ID from the client app.
+         */
         public Builder setClientNotificationId(int clientNotificationId) {
             mClientNotificationId = clientNotificationId;
             return this;
         }
 
+        /**
+         * Set the client app's application thread.
+         */
         public Builder setClientAppThread(@NonNull IApplicationThread clientAppThread) {
             mClientAppThread = clientAppThread;
             return this;
         }
 
+        /**
+         * Set the client instance of this service.
+         */
         public Builder setClientInstanceName(@NonNull String clientInstanceName) {
             mClientInstanceName = clientInstanceName;
             return this;
         }
 
+        /**
+         * Set stickiness of this service.
+         */
         public Builder setSticky(boolean isSticky) {
             mSticky = isSticky;
             return this;
         }
 
+        /**
+         * Set the foreground service type.
+         */
         public Builder setForegroundServiceTypes(int foregroundServiceTypes) {
             mForegroundServiceTypes = foregroundServiceTypes;
             return this;
         }
 
+        /**
+         * Set the delegation service type.
+         */
         public Builder setDelegationService(@DelegationService int delegationService) {
             mDelegationService = delegationService;
             return this;
         }
 
+        /**
+         * @return An instance of {@link ForegroundServiceDelegationOptions}.
+         */
         public ForegroundServiceDelegationOptions build() {
             return new ForegroundServiceDelegationOptions(mClientPid,
                 mClientUid,
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index bcea40e5..3ec25c5 100644
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -112,6 +112,7 @@
 import android.app.ActivityThread;
 import android.app.AppGlobals;
 import android.app.AppOpsManager;
+import android.app.ForegroundServiceDelegationOptions;
 import android.app.ForegroundServiceStartNotAllowedException;
 import android.app.ForegroundServiceTypePolicy;
 import android.app.ForegroundServiceTypePolicy.ForegroundServicePolicyCheckCode;
diff --git a/services/core/java/com/android/server/am/ActivityManagerLocal.java b/services/core/java/com/android/server/am/ActivityManagerLocal.java
index 5175a31..5948971 100644
--- a/services/core/java/com/android/server/am/ActivityManagerLocal.java
+++ b/services/core/java/com/android/server/am/ActivityManagerLocal.java
@@ -17,7 +17,6 @@
 package com.android.server.am;
 
 import android.annotation.NonNull;
-import android.annotation.Nullable;
 import android.annotation.SuppressLint;
 import android.annotation.SystemApi;
 import android.content.Context;
@@ -103,28 +102,4 @@
      *        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.
-     * @return true if delegate is started successfully, false otherwise.
-     * @hide
-     */
-    boolean startForegroundServiceDelegate(@NonNull ForegroundServiceDelegationOptions options,
-            @Nullable ServiceConnection connection);
-
-    /**
-     * Stop a foreground service delegate.
-     * @param options the foreground service delegate options.
-     * @hide
-     */
-    void stopForegroundServiceDelegate(@NonNull ForegroundServiceDelegationOptions options);
-
-    /**
-     * Stop a foreground service delegate by service connection.
-     * @param connection service connection used to start delegate previously.
-     * @hide
-     */
-    void stopForegroundServiceDelegate(@NonNull ServiceConnection connection);
 }
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index fc6d30b..4f17816 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -184,6 +184,7 @@
 import android.app.BroadcastOptions;
 import android.app.ComponentOptions;
 import android.app.ContentProviderHolder;
+import android.app.ForegroundServiceDelegationOptions;
 import android.app.IActivityController;
 import android.app.IActivityManager;
 import android.app.IApplicationThread;
@@ -18549,10 +18550,10 @@
                 for (int i = delegates.size() - 1; i >= 0; i--) {
                     final ForegroundServiceDelegationOptions options = delegates.get(i);
                     if (isStart) {
-                        ((ActivityManagerLocal) mInternal).startForegroundServiceDelegate(options,
+                        mInternal.startForegroundServiceDelegate(options,
                                 null /* connection */);
                     } else {
-                        ((ActivityManagerLocal) mInternal).stopForegroundServiceDelegate(options);
+                        mInternal.stopForegroundServiceDelegate(options);
                     }
                 }
             }
diff --git a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java
index 80684bf..824be32 100644
--- a/services/core/java/com/android/server/am/ActivityManagerShellCommand.java
+++ b/services/core/java/com/android/server/am/ActivityManagerShellCommand.java
@@ -46,6 +46,7 @@
 import android.app.ActivityTaskManager.RootTaskInfo;
 import android.app.AppGlobals;
 import android.app.BroadcastOptions;
+import android.app.ForegroundServiceDelegationOptions;
 import android.app.IActivityController;
 import android.app.IActivityManager;
 import android.app.IActivityTaskManager;
diff --git a/services/core/java/com/android/server/am/ForegroundServiceDelegation.java b/services/core/java/com/android/server/am/ForegroundServiceDelegation.java
index a051d17..a17848e 100644
--- a/services/core/java/com/android/server/am/ForegroundServiceDelegation.java
+++ b/services/core/java/com/android/server/am/ForegroundServiceDelegation.java
@@ -18,6 +18,7 @@
 
 import android.annotation.NonNull;
 import android.annotation.Nullable;
+import android.app.ForegroundServiceDelegationOptions;
 import android.content.ServiceConnection;
 import android.os.Binder;
 import android.os.IBinder;