ExtensionsAppFunctionCompatService


@RequiresApi(value = 34)
public 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 methods

abstract @NonNull ExecuteAppFunctionResponse

Called by the system to execute a specific app function.

void

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

final void

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

Public constructors

ExtensionsAppFunctionCompatService

Added in 1.0.0-alpha05
public ExtensionsAppFunctionCompatService()

Public methods

executeFunction

public abstract @NonNull ExecuteAppFunctionResponse executeFunction(@NonNull ExecuteAppFunctionRequest request)

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
@NonNull ExecuteAppFunctionRequest request

The function execution request.

onDestroy

@CallSuper
public void onDestroy()

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

onExecuteFunction

public final void onExecuteFunction(
    @NonNull <Error class: unknown class> request,
    @NonNull String callingPackage,
    @NonNull CancellationSignal cancellationSignal,
    @NonNull OutcomeReceiver<@NonNull <Error class: unknown class>, @NonNull <Error class: unknown class>> callback
)

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

Parameters
@NonNull <Error class: unknown class> request

The function execution request.

@NonNull String callingPackage

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.

@NonNull CancellationSignal cancellationSignal

A signal to cancel the execution.

@NonNull OutcomeReceiver<@NonNull <Error class: unknown class>, @NonNull <Error class: unknown class>> callback

A callback to report back the result or error.