TrustedWebActivityServiceConnectionPool

public final class TrustedWebActivityServiceConnectionPool


A TrustedWebActivityServiceConnectionPool will be used by a Trusted Web Activity provider and takes care of connecting to and communicating with TrustedWebActivityServices. This is done through the connect method.

Multiple Trusted Web Activity client apps may be suitable for a given scope. These are passed in to connect and serviceExistsForScope and the most appropriate one for the scope is chosen.

Summary

Public methods

@NonNull ListenableFuture<TrustedWebActivityServiceConnection>
@MainThread
connect(
    @NonNull Uri scope,
    @NonNull Set<Token> possiblePackages,
    @NonNull Executor executor
)

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

static @NonNull TrustedWebActivityServiceConnectionPool

Creates a TrustedWebActivityServiceConnectionPool.

boolean
@MainThread
serviceExistsForScope(
    @NonNull Uri scope,
    @NonNull Set<Token> possiblePackages
)

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

Public methods

connect

Added in 1.2.0
@MainThread
public @NonNull ListenableFuture<TrustedWebActivityServiceConnectionconnect(
    @NonNull Uri scope,
    @NonNull Set<Token> possiblePackages,
    @NonNull Executor executor
)

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 ACTION_VIEW Intent with the scope as data. The first of the resolved packages to be contained in the possiblePackages set will be chosen. Finally, an Intent with the action ACTION_TRUSTED_WEB_ACTIVITY_SERVICE will be used to find the Service.

This method should be called on the UI thread.

Parameters
@NonNull Uri scope

The scope used in an Intent to find packages that may have a TrustedWebActivityService.

@NonNull Set<Token> possiblePackages

A collection of packages to consider. These would be the packages that have previously launched a Trusted Web Activity for the origin.

@NonNull Executor executor

The Executor to connect to the Service on if a new connection is required.

Returns
@NonNull ListenableFuture<TrustedWebActivityServiceConnection>

A ListenableFuture for the resulting TrustedWebActivityServiceConnection. This may be set to an IllegalArgumentException if no service exists for the scope (you can check for this beforehand by calling serviceExistsForScope). It may be set to a SecurityException if the Service does not accept connections from this app. It may be set to an IllegalStateException if connecting to the Service fails.

create

Added in 1.2.0
public static @NonNull TrustedWebActivityServiceConnectionPool create(@NonNull Context context)

Creates a TrustedWebActivityServiceConnectionPool.

Parameters
@NonNull Context context

A Context used for accessing SharedPreferences.

serviceExistsForScope

Added in 1.2.0
@MainThread
public boolean serviceExistsForScope(
    @NonNull Uri scope,
    @NonNull Set<Token> possiblePackages
)

Checks if a TrustedWebActivityService exists to handle requests for the given scope and origin. This method uses the same logic as connect. If this method returns false, connect will return a Future containing an IllegalStateException.

This method should be called on the UI thread.

Parameters
@NonNull Uri scope

The scope used in an Intent to find packages that may have a TrustedWebActivityService.

@NonNull Set<Token> possiblePackages

A collection of packages to consider. These would be the packages that have previously launched a Trusted Web Activity for the origin.

Returns
boolean

Whether a TrustedWebActivityService was found.