AppFunctionManager


class AppFunctionManager


Provides access to interact with App Functions. This is a backward-compatible wrapper for the platform class android.app.appfunctions.AppFunctionManager.

Summary

Constants

const Int

The default state of the app function.

const Int

The app function is disabled.

const Int

The app function is enabled.

Public companion functions

AppFunctionManager?
getInstance(context: Context)

Gets an instance of AppFunctionManager if the AppFunction feature is supported.

Public functions

suspend ExecuteAppFunctionResponse
@RequiresPermission(value = "android.permission.EXECUTE_APP_FUNCTIONS", conditional = true)
executeAppFunction(request: ExecuteAppFunctionRequest)

Execute the app function.

suspend List<AppFunctionActivityState>
@RequiresApi(value = 37)
@RequiresPermission(anyOf = ["android.permission.EXECUTE_APP_FUNCTIONS", "android.permission.DISCOVER_APP_FUNCTIONS", "android.permission.EXECUTE_APP_FUNCTIONS_SYSTEM"], conditional = true)
getAppFunctionActivityStates(activityIds: Set<AppFunctionActivityId>)

Retrieves the registered app functions for the specified activities.

suspend List<AppFunctionState>
@RequiresPermission(anyOf = ["android.permission.EXECUTE_APP_FUNCTIONS", "android.permission.DISCOVER_APP_FUNCTIONS", "android.permission.EXECUTE_APP_FUNCTIONS_SYSTEM"], conditional = true)
getAppFunctionStates(appFunctionNames: List<AppFunctionName>)

Retrieves the runtime state of the specified app functions.

HandleAppFunctionRequestAdapter<T>

Returns an HandleAppFunctionRequestAdapter for an interface annotated with AppFunctionSignature.

suspend Nothing

Registers a runtime implementation of an app function bound to the calling coroutine's lifecycle.

suspend Nothing
@RequiresApi(value = 37)
@ExperimentalAppFunctionsApi
handleAppFunction(
    functionIdentifier: String,
    appFunction: SuspendingAppFunction
)

Registers a runtime implementation of an app function bound to the calling coroutine's lifecycle.

suspend Nothing

Registers multiple runtime implementations of app functions bound to the calling coroutine's lifecycle.

Flow<ObserveAppFunctionsEvent>
@RequiresPermission(anyOf = ["android.permission.EXECUTE_APP_FUNCTIONS", "android.permission.DISCOVER_APP_FUNCTIONS", "android.permission.EXECUTE_APP_FUNCTIONS_SYSTEM"], conditional = true)
observeAppFunctions()

Observes changes to app functions within packages the caller can query.

suspend List<AppFunctionMetadata>
@RequiresPermission(anyOf = ["android.permission.EXECUTE_APP_FUNCTIONS", "android.permission.DISCOVER_APP_FUNCTIONS", "android.permission.EXECUTE_APP_FUNCTIONS_SYSTEM"], conditional = true)
searchAppFunctions(searchSpec: AppFunctionSearchSpec)

Searches app function AppFunctionMetadatas.

suspend Unit
setAppFunctionEnabled(functionId: String, newEnabledState: Int)

Sets newEnabledState to an app function functionId owned by the calling package.

Constants

APP_FUNCTION_STATE_DEFAULT

const val APP_FUNCTION_STATE_DEFAULTInt

The default state of the app function. Call setAppFunctionEnabled with this to reset enabled state to the default value.

APP_FUNCTION_STATE_DISABLED

const val APP_FUNCTION_STATE_DISABLEDInt

The app function is disabled. To disable an app function, call setAppFunctionEnabled with this value.

APP_FUNCTION_STATE_ENABLED

const val APP_FUNCTION_STATE_ENABLEDInt

The app function is enabled. To enable an app function, call setAppFunctionEnabled with this value.

Public companion functions

getInstance

Added in 1.0.0-alpha11
fun getInstance(context: Context): AppFunctionManager?

Gets an instance of AppFunctionManager if the AppFunction feature is supported.

The AppFunction feature is supported if the calling user is not a profile and either of the following conditions is met:

  • SDK version is 36 or higher.

  • SDK version is 34 or higher, and the device implements the App Function extension ibrary.

Returns
AppFunctionManager?

an instance of AppFunctionManager if the AppFunction feature is supported or null.

Public functions

executeAppFunction

@RequiresPermission(value = "android.permission.EXECUTE_APP_FUNCTIONS", conditional = true)
suspend fun executeAppFunction(request: ExecuteAppFunctionRequest): ExecuteAppFunctionResponse

Execute the app function.

This method matches the platform behavior defined in android.app.appfunctions.AppFunctionManager.executeAppFunction.

Parameters
request: ExecuteAppFunctionRequest

the app function details and the arguments.

Returns
ExecuteAppFunctionResponse

the result of the attempt to execute the function.

getAppFunctionActivityStates

@RequiresApi(value = 37)
@RequiresPermission(anyOf = ["android.permission.EXECUTE_APP_FUNCTIONS", "android.permission.DISCOVER_APP_FUNCTIONS", "android.permission.EXECUTE_APP_FUNCTIONS_SYSTEM"], conditional = true)
suspend fun getAppFunctionActivityStates(activityIds: Set<AppFunctionActivityId>): List<AppFunctionActivityState>

Retrieves the registered app functions for the specified activities.

Each AppFunctionActivityState contains the set of registered AppFunctionNames associated with a requested android.app.appfunctions.AppFunctionActivityId.

Functions that do not exist or are not visible to the calling application will be silently omitted from the result. Requested activities that have no registered functions will be omitted from the result.

See android.app.appfunctions.AppFunctionActivityId for potential usages, including conversion from android.service.voice.VoiceInteractionSession.ActivityId.

This method follows the same permission rules as searchAppFunctions.

See getAppFunctionStates for retrieving the runtime state of app functions based on their names.

See searchAppFunctions on how to retrieve the AppFunctionMetadata of app functions.

See observeAppFunctions for observing changes to app functions' AppFunctionMetadata and AppFunctionStates.

Parameters
activityIds: Set<AppFunctionActivityId>

The set of activity IDs to retrieve function states for.

getAppFunctionStates

@RequiresPermission(anyOf = ["android.permission.EXECUTE_APP_FUNCTIONS", "android.permission.DISCOVER_APP_FUNCTIONS", "android.permission.EXECUTE_APP_FUNCTIONS_SYSTEM"], conditional = true)
suspend fun getAppFunctionStates(appFunctionNames: List<AppFunctionName>): List<AppFunctionState>

Retrieves the runtime state of the specified app functions.

This includes runtime-changing properties such as whether the functions are currently enabled or disabled. Functions that do not exist or are not visible to the calling application will be silently omitted from the result list.

This method follows the same permission rules as searchAppFunctions.

See android.app.appfunctions.AppFunctionManager.getAppFunctionActivityStates for retrieving the states of app functions associated with a specific activity.

See searchAppFunctions on how to retrieve the AppFunctionMetadata of app functions.

See observeAppFunctions for observing changes to app functions' AppFunctionMetadata and AppFunctionStates.

Parameters
appFunctionNames: List<AppFunctionName>

The names of the app functions to request the state for.

Returns
List<AppFunctionState>

the AppFunctionStates of the specified app functions.

getHandleAppFunctionRequestAdapter

Added in 1.0.0-alpha11
@RequiresApi(value = 37)
@ExperimentalAppFunctionsApi
fun <T : Any> getHandleAppFunctionRequestAdapter(interfaceClass: Class<T>): HandleAppFunctionRequestAdapter<T>

Returns an HandleAppFunctionRequestAdapter for an interface annotated with AppFunctionSignature.

Retrieves a generated HandleAppFunctionRequestAdapter that bridges ExecuteAppFunctionRequest and ExecuteAppFunctionResponse with the strongly-typed signature of the passed interface. Because the method uses reflection under the hood to instantiate the adapter, we recommend loading it in advance to avoid runtime latency.

This adapter allows wrapping a concrete implementation of the passed interface into a HandleAppFunctionRequest. The resulting request can then be registered using handleAppFunction or handleAppFunctions.

Parameters
interfaceClass: Class<T>

The interface class annotated with AppFunctionSignature.

Throws
IllegalArgumentException

if the adapter class for interfaceClass cannot be found or instantiated.

handleAppFunction

@RequiresApi(value = 37)
@ExperimentalAppFunctionsApi
suspend fun handleAppFunction(request: HandleAppFunctionRequest): Nothing

Registers a runtime implementation of an app function bound to the calling coroutine's lifecycle.

This method suspends and keeps the function registered until the calling coroutine scope is cancelled. Under the hood, it delegates the registration to registerAppFunction and ensures it is unregistered when the coroutine is cancelled.

For a callback-based API that does not require a coroutine scope, see registerAppFunction.

Parameters
request: HandleAppFunctionRequest

The request containing the function identifier and implementation.

handleAppFunction

@RequiresApi(value = 37)
@ExperimentalAppFunctionsApi
suspend fun handleAppFunction(
    functionIdentifier: String,
    appFunction: SuspendingAppFunction
): Nothing

Registers a runtime implementation of an app function bound to the calling coroutine's lifecycle.

This method suspends and keeps the function registered until the calling coroutine scope is cancelled. Under the hood, it delegates the registration to registerAppFunction and ensures it is unregistered when the coroutine is cancelled.

For a callback-based API that does not require a coroutine scope, see registerAppFunction.

Parameters
functionIdentifier: String

The unique identifier of the app function.

appFunction: SuspendingAppFunction

The implementation of the app function to handle execution requests.

handleAppFunctions

@RequiresApi(value = 37)
@ExperimentalAppFunctionsApi
suspend fun handleAppFunctions(requests: List<HandleAppFunctionRequest>): Nothing

Registers multiple runtime implementations of app functions bound to the calling coroutine's lifecycle.

This method suspends and keeps the functions registered until the calling coroutine scope is cancelled. Under the hood, it delegates the registration to registerAppFunctions and ensures they are unregistered when the coroutine is cancelled.

For a callback-based API that does not require a coroutine scope, see registerAppFunctions.

Parameters
requests: List<HandleAppFunctionRequest>

The list of requests containing the function identifiers and implementations.

observeAppFunctions

Added in 1.0.0-alpha11
@RequiresPermission(anyOf = ["android.permission.EXECUTE_APP_FUNCTIONS", "android.permission.DISCOVER_APP_FUNCTIONS", "android.permission.EXECUTE_APP_FUNCTIONS_SYSTEM"], conditional = true)
fun observeAppFunctions(): Flow<ObserveAppFunctionsEvent>

Observes changes to app functions within packages the caller can query.

The returned flow only emits changes that occur after collection starts. Any changes before collection are not reported.

An example usage flow is:

  1. Start collecting from the Flow to monitor app function changes.

  2. Call searchAppFunctions and getAppFunctionStates to get the initial list of app functions and their states.

  3. When receiving ObserveAppFunctionsEvent.MetadataChanged, call searchAppFunctions with a AppFunctionSearchSpec that matches the changed packages to get the updated metadata.

  4. When receiving ObserveAppFunctionsEvent.StatesChanged, call getAppFunctionStates with the list of androidx.appfunctions.metadata.AppFunctionNames matching the changed functions to get the updated states. Note that this is guaranteed to trigger after ObserveAppFunctionsEvent.MetadataChanged for new functions or functions that also changed states. There is no need to call getAppFunctionStates when receiving ObserveAppFunctionsEvent.MetadataChanged.

Returns
Flow<ObserveAppFunctionsEvent>

a Flow emitting ObserveAppFunctionsEvents representing metadata or state changes

searchAppFunctions

@RequiresPermission(anyOf = ["android.permission.EXECUTE_APP_FUNCTIONS", "android.permission.DISCOVER_APP_FUNCTIONS", "android.permission.EXECUTE_APP_FUNCTIONS_SYSTEM"], conditional = true)
suspend fun searchAppFunctions(searchSpec: AppFunctionSearchSpec): List<AppFunctionMetadata>

Searches app function AppFunctionMetadatas.

Note that the state is not guaranteed to be the latest, as metadata can change between request and execute times when apps are updated.

The calling app can search for:

Parameters
searchSpec: AppFunctionSearchSpec

The spec of app functions to search for.

setAppFunctionEnabled

Added in 1.0.0-alpha11
suspend fun setAppFunctionEnabled(functionId: String, newEnabledState: Int): Unit

Sets newEnabledState to an app function functionId owned by the calling package.

This method matches the platform behavior defined in android.app.appfunctions.AppFunctionManager.setAppFunctionEnabled.

Parameters
functionId: String

The identifier of the app function.

newEnabledState: Int

The new state of the app function.

Throws
IllegalArgumentException

If the functionId is not available.