TrustedWebActivityServiceConnectionManager

public class TrustedWebActivityServiceConnectionManager
extends Object

java.lang.Object
   ↳ 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(Context, String, String), 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(Uri, String, TrustedWebActivityServiceConnectionManager.ExecutionCallback).

Summary

Nested classes

interface TrustedWebActivityServiceConnectionManager.ExecutionCallback

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

Public constructors

TrustedWebActivityServiceConnectionManager(Context context)

Creates a TrustedWebActivityServiceConnectionManager.

Public methods

boolean execute(Uri scope, String origin, TrustedWebActivityServiceConnectionManager.ExecutionCallback callback)

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

static Set<String> getVerifiedPackages(Context context, String origin)

Gets the verified packages for the given origin.

static void registerClient(Context context, String origin, String clientPackage)

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

boolean serviceExistsForScope(Uri scope, String origin)

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

Inherited methods

Public constructors

TrustedWebActivityServiceConnectionManager

public TrustedWebActivityServiceConnectionManager (Context context)

Creates a TrustedWebActivityServiceConnectionManager.

Parameters
context Context: A Context used for accessing SharedPreferences.

Public methods

execute

public boolean execute (Uri scope, 
                String origin, 
                TrustedWebActivityServiceConnectionManager.ExecutionCallback callback)

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(Context, String, String)) 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 String: An origin that the TrustedWebActivityService package must be registered to.

callback TrustedWebActivityServiceConnectionManager.ExecutionCallback: A TrustedWebActivityServiceConnectionManager.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.

Returns
boolean Whether a TrustedWebActivityService was found.

getVerifiedPackages

public static Set<String> getVerifiedPackages (Context context, 
                String origin)

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 String: The origin that was previously used with registerClient(Context, String, String).

Returns
Set<String> A set of package names. This set is safe to be modified.

registerClient

public static void registerClient (Context context, 
                String origin, 
                String clientPackage)

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 String: The origin for which the package is relevant.

clientPackage String: The packages to register.

serviceExistsForScope

public boolean serviceExistsForScope (Uri scope, 
                String origin)

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(Uri, String, TrustedWebActivityServiceConnectionManager.ExecutionCallback) 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 String: An origin that the TrustedWebActivityService package must be registered to.

Returns
boolean Whether a TrustedWebActivityService was found.