Add DeviceConfig listener for AFMS

Flag: android.app.appfunctions.flags.enable_app_function_manager
Test: atest CtsAppFunctionTestCases
Bug: 357551503
Change-Id: Ie65324ab48ccc5dc344393d2502b44e935841b66
diff --git a/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java
index 6b8e8c7..9c6bcdf 100644
--- a/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java
+++ b/services/appfunctions/java/com/android/server/appfunctions/AppFunctionManagerServiceImpl.java
@@ -43,9 +43,12 @@
  */
 public class AppFunctionManagerServiceImpl extends IAppFunctionManager.Stub {
     private static final String TAG = AppFunctionManagerServiceImpl.class.getSimpleName();
+
     private final RemoteServiceCaller<IAppFunctionService> mRemoteServiceCaller;
     private final CallerValidator mCallerValidator;
     private final ServiceHelper mInternalServiceHelper;
+    private final ServiceConfig mServiceConfig;
+
 
     public AppFunctionManagerServiceImpl(@NonNull Context context) {
         this(new RemoteServiceCallerImpl<>(
@@ -57,17 +60,20 @@
                         /*unit=*/ TimeUnit.SECONDS,
                         /*workQueue=*/ new LinkedBlockingQueue<>())),
                 new CallerValidatorImpl(context),
-                new ServiceHelperImpl(context));
+                new ServiceHelperImpl(context),
+                new ServiceConfigImpl());
     }
 
     @VisibleForTesting
     AppFunctionManagerServiceImpl(RemoteServiceCaller<IAppFunctionService> remoteServiceCaller,
                                   CallerValidator callerValidator,
-                                  ServiceHelper appFunctionInternalServiceHelper) {
+                                  ServiceHelper appFunctionInternalServiceHelper,
+                                  ServiceConfig serviceConfig) {
         mRemoteServiceCaller = Objects.requireNonNull(remoteServiceCaller);
         mCallerValidator = Objects.requireNonNull(callerValidator);
         mInternalServiceHelper =
                 Objects.requireNonNull(appFunctionInternalServiceHelper);
+        mServiceConfig = serviceConfig;
     }
 
     @Override
@@ -131,12 +137,10 @@
             ).build());
             return;
         }
-
         bindAppFunctionServiceUnchecked(requestInternal, serviceIntent, targetUser,
                 safeExecuteAppFunctionCallback,
                 /*bindFlags=*/ Context.BIND_AUTO_CREATE,
-                // TODO(b/357551503): Make timeout configurable.
-                /*timeoutInMillis=*/ 30_000L);
+                /*timeoutInMillis=*/ mServiceConfig.getExecutionTimeoutConfig());
     }
 
     private void bindAppFunctionServiceUnchecked(
diff --git a/services/appfunctions/java/com/android/server/appfunctions/ServiceConfig.java b/services/appfunctions/java/com/android/server/appfunctions/ServiceConfig.java
new file mode 100644
index 0000000..35c6c78
--- /dev/null
+++ b/services/appfunctions/java/com/android/server/appfunctions/ServiceConfig.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2024 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 com.android.server.appfunctions;
+
+/**
+ * This interface is used to expose configs to the AppFunctionManagerService.
+ */
+public interface ServiceConfig {
+    // TODO(b/357551503): Obtain namespace from DeviceConfig.
+    String NAMESPACE_APP_FUNCTIONS = "appfunctions";
+
+    /**
+     * Returns the maximum time to wait for an app function execution to be complete.
+     */
+    long getExecutionTimeoutConfig();
+}
diff --git a/services/appfunctions/java/com/android/server/appfunctions/ServiceConfigImpl.java b/services/appfunctions/java/com/android/server/appfunctions/ServiceConfigImpl.java
new file mode 100644
index 0000000..b203ead
--- /dev/null
+++ b/services/appfunctions/java/com/android/server/appfunctions/ServiceConfigImpl.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2024 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 com.android.server.appfunctions;
+
+import android.provider.DeviceConfig;
+
+/**
+ * Implementation of {@link ServiceConfig}
+ */
+public class ServiceConfigImpl implements ServiceConfig {
+    static final String DEVICE_CONFIG_PROPERTY_EXECUTION_TIMEOUT = "execution_timeout";
+    static final long DEFAULT_EXECUTION_TIMEOUT_MS = 5000L;
+
+
+    @Override
+    public long getExecutionTimeoutConfig() {
+        return DeviceConfig.getLong(
+                NAMESPACE_APP_FUNCTIONS,
+                DEVICE_CONFIG_PROPERTY_EXECUTION_TIMEOUT,
+                DEFAULT_EXECUTION_TIMEOUT_MS
+        );
+    }
+}