ExtensionsAppFunctionCompatService


@RequiresApi(value = 34)
abstract class ExtensionsAppFunctionCompatService


Abstract base class to provide app functions to the system for Android versions 14-16 (inclusive), if the AppFunctions extensions library is available on the device.

This class wraps com.android.extensions.appfunctions.AppFunctionService functionalities and provides an API that uses androidx.appfunctions classes.

Include the following in the manifest:

<service android:name=".YourService"
android:permission="android.permission.BIND_APP_FUNCTION_SERVICE">
<intent-filter>
<action android:name="android.app.appfunctions.AppFunctionService" />
</intent-filter>
</service>

Summary

Public constructors

Public functions

abstract suspend ExecuteAppFunctionResponse

Called by the system to execute a specific app function.

open Unit

Implementing class can override this method to perform cleanup but should always call the superclass implementation.

Unit
onExecuteFunction(
    request: <Error class: unknown class>,
    callingPackage: String,
    cancellationSignal: CancellationSignal,
    callback: OutcomeReceiver<<Error class: unknown class><Error class: unknown class>>
)

Implements AppFunctionService.onExecuteFunction and delegates the execution to executeFunction when called by the system.

Public constructors

ExtensionsAppFunctionCompatService

Added in 1.0.0-alpha05
ExtensionsAppFunctionCompatService()

Public functions

executeFunction

abstract suspend fun executeFunction(request: ExecuteAppFunctionRequest): ExecuteAppFunctionResponse

Called by the system to execute a specific app function.

This method is the entry point for handling all app function requests in an app. When the system needs your AppFunctionService to perform a function, it will invoke this method.

Each function you've registered is identified by a unique identifier. This identifier doesn't need to be globally unique, but it must be unique within your app. For example, a function to order food could be identified as "orderFood".

You can determine which function to execute by using ExecuteAppFunctionRequest.functionIdentifier. This allows your service to route the incoming request to the appropriate logic for handling the specific function.

This method is always triggered in the main thread. You should run heavy tasks on a worker thread.

Exception Handling

When an error occurs during execution, implementations have two options to report the failure:

  1. Throw an appropriate androidx.appfunctions.AppFunctionException.

  2. Return an ExecuteAppFunctionResponse.Error by wrapping an androidx.appfunctions.AppFunctionException.

This allows the agent to better understand the cause of the failure. For example, if an input argument is invalid, throw or wrap an androidx.appfunctions.AppFunctionInvalidArgumentException with a detailed message explaining why it is invalid.

Any unhandled exception other than androidx.appfunctions.AppFunctionException will be reported as androidx.appfunctions.AppFunctionAppUnknownException.

Cancellation

The agent app can cancel the execution of an app function at any time. When this happens, the coroutine executing this executeFunction will be cancelled. Implementations should handle the kotlinx.coroutines.CancellationException appropriately, for example, by ceasing any ongoing work and releasing resources.

Parameters
request: ExecuteAppFunctionRequest

The function execution request.

onDestroy

@CallSuper
open fun onDestroy(): Unit

Implementing class can override this method to perform cleanup but should always call the superclass implementation.

onExecuteFunction

fun onExecuteFunction(
    request: <Error class: unknown class>,
    callingPackage: String,
    cancellationSignal: CancellationSignal,
    callback: OutcomeReceiver<<Error class: unknown class><Error class: unknown class>>
): Unit

Implements AppFunctionService.onExecuteFunction and delegates the execution to executeFunction when called by the system.

Parameters
request: <Error class: unknown class>

The function execution request.

callingPackage: String

The package name of the app that is requesting the execution. It is strongly recommended that you do not alter your function’s behavior based on this value. Your function should behave consistently for all callers to ensure a predictable experience.

cancellationSignal: CancellationSignal

A signal to cancel the execution.

callback: OutcomeReceiver<<Error class: unknown class><Error class: unknown class>>

A callback to report back the result or error.