The Android Developer Challenge is back! Submit your idea before December 2.

TrustedWebActivityServiceConnectionPool

class TrustedWebActivityServiceConnectionPool
kotlin.Any
   ↳ androidx.browser.trusted.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

ListenableFuture<TrustedWebActivityServiceConnection!>
connect(@NonNull scope: Uri, @NonNull possiblePackages: MutableSet<Token!>, @NonNull executor: Executor)

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

static TrustedWebActivityServiceConnectionPool
create(@NonNull context: Context)

Creates a TrustedWebActivityServiceConnectionPool.

Boolean
serviceExistsForScope(@NonNull scope: Uri, @NonNull possiblePackages: MutableSet<Token!>)

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

Public methods

connect

@MainThread @NonNull fun connect(@NonNull scope: Uri, @NonNull possiblePackages: MutableSet<Token!>, @NonNull executor: Executor): ListenableFuture<TrustedWebActivityServiceConnection!>

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 to be contained in the possiblePackages set 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.
possiblePackages Uri: A collection of packages to consider. These would be the packages that have previously launched a Trusted Web Activity for the origin.
executor Uri: The Executor to connect to the Service on if a new connection is required.
Return
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

@NonNull static fun create(@NonNull context: Context): TrustedWebActivityServiceConnectionPool

Creates a TrustedWebActivityServiceConnectionPool.

Parameters
context Context: A Context used for accessing SharedPreferences.

serviceExistsForScope

@MainThread fun serviceExistsForScope(@NonNull scope: Uri, @NonNull possiblePackages: MutableSet<Token!>): Boolean

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
scope Uri: The scope used in an Intent to find packages that may have a TrustedWebActivityService.
possiblePackages Uri: A collection of packages to consider. These would be the packages that have previously launched a Trusted Web Activity for the origin.
Return
Boolean: Whether a TrustedWebActivityService was found.