TrustedWebActivityServiceConnectionManager

open class TrustedWebActivityServiceConnectionManager
kotlin.Any
   ↳ androidx.browser.trusted.TrustedWebActivityServiceConnectionManager

A TrustedWebActivityServiceConnectionManager will be used by a Trusted Web Activity provider and takes care of connecting to and communicating with TrustedWebActivityServices.

Trusted Web Activity client apps are registered with registerClient, associating a package with an origin. There may be multiple packages associated with a single origin. Note, the origins are essentially keys to a map of origin to package name - while they semantically are web origins, they aren't used that way.

To interact with a TrustedWebActivityService, call execute.

Summary

Nested classes

abstract

A callback to be executed once a connection to a TrustedWebActivityService is open.

Public constructors

<init>(@NonNull context: Context)

Creates a TrustedWebActivityServiceConnectionManager.

Public methods

open Boolean
execute(@NonNull scope: Uri, @NonNull origin: String, @NonNull callback: TrustedWebActivityServiceConnectionManager.ExecutionCallback)

Connects to the appropriate TrustedWebActivityService or uses an existing connection if available and runs code once connected.

open static MutableSet<String!>
getVerifiedPackages(@NonNull context: Context, @NonNull origin: String)

Gets the verified packages for the given origin.

open static Unit
registerClient(@NonNull context: Context, @NonNull origin: String, @NonNull clientPackage: String)

Registers (and persists) a package to be used for an origin.

open Boolean
serviceExistsForScope(@NonNull scope: Uri, @NonNull origin: String)

Checks if a TrustedWebActivityService exists to handle requests for the given scope and origin.

Public constructors

<init>

TrustedWebActivityServiceConnectionManager(@NonNull context: Context)

Creates a TrustedWebActivityServiceConnectionManager.

Parameters
context Context: A Context used for accessing SharedPreferences.

Public methods

execute

@MainThread open fun execute(@NonNull scope: Uri, @NonNull origin: String, @NonNull callback: TrustedWebActivityServiceConnectionManager.ExecutionCallback): Boolean

Connects to the appropriate TrustedWebActivityService or uses an existing connection if available and runs code once connected.

To find a Service to connect to, this method attempts to resolve an Intent#ACTION_VIEW Intent with the scope as data. The first of the resolved packages that is registered (through registerClient) to origin will be chosen. Finally, an Intent with the action TrustedWebActivityService#ACTION_TRUSTED_WEB_ACTIVITY_SERVICE will be used to find the Service.

This method should be called on the UI thread.

Parameters
scope Uri: The scope used in an Intent to find packages that may have a TrustedWebActivityService.
origin Uri: An origin that the TrustedWebActivityService package must be registered to.
callback Uri: A ExecutionCallback that will be run with a connection. It will be run on a background thread from the ThreadPool as most methods from TrustedWebActivityServiceWrapper require this. Any RemoteException or RuntimeException exceptions thrown by the callback will be swallowed. This is to allow users to deal with exceptions thrown by TrustedWebActivityServiceWrapper if they wish, but to fail gracefully if they don't.
Return
Boolean: Whether a TrustedWebActivityService was found.

getVerifiedPackages

@NonNull open static fun getVerifiedPackages(@NonNull context: Context, @NonNull origin: String): MutableSet<String!>

Gets the verified packages for the given origin. This is safe to be called on any thread, however it may hit disk the first time it is called.

Parameters
context Context: A Context to be used for accessing SharedPreferences.
origin Context: The origin that was previously used with registerClient.
Return
MutableSet<String!>: A set of package names. This set is safe to be modified.

registerClient

open static fun registerClient(@NonNull context: Context, @NonNull origin: String, @NonNull clientPackage: String): Unit

Registers (and persists) a package to be used for an origin. This information is persisted in SharedPreferences. Although this method can be called on any thread, it may read SharedPreferences and hit the disk, so call it on a background thread if possible.

Parameters
context Context: A Context to access SharedPreferences.
origin Context: The origin for which the package is relevant.
clientPackage Context: The packages to register.

serviceExistsForScope

@MainThread open fun serviceExistsForScope(@NonNull scope: Uri, @NonNull origin: String): Boolean

Checks if a TrustedWebActivityService exists to handle requests for the given scope and origin. The value will be the same as that returned from execute so calling that and checking the return may be more convenient. This method should be called on the UI thread.

Parameters
scope Uri: The scope used in an Intent to find packages that may have a TrustedWebActivityService.
origin Uri: An origin that the TrustedWebActivityService package must be registered to.
Return
Boolean: Whether a TrustedWebActivityService was found.