# TrustedWebActivityServiceConnectionPool

Artifact: [androidx.browser:browser](https://developer.android.com/jetpack/androidx/releases/browser) [View Source](https://cs.android.com/search?q=file:androidx/browser/trusted/TrustedWebActivityServiceConnectionPool.java+class:androidx.browser.trusted.TrustedWebActivityServiceConnectionPool) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

*** ** * ** ***

<br />

```
public final class TrustedWebActivityServiceConnectionPool
```

<br />

*** ** * ** ***

A TrustedWebActivityServiceConnectionPool will be used by a Trusted Web Activity provider and takes care of connecting to and communicating with `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityService`s. This is done through the `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#connect(android.net.Uri,java.util.Set<androidx.browser.trusted.Token>,java.util.concurrent.Executor)` method.

Multiple Trusted Web Activity client apps may be suitable for a given scope. These are passed in to `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#connect(android.net.Uri,java.util.Set<androidx.browser.trusted.Token>,java.util.concurrent.Executor)` and `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#serviceExistsForScope(android.net.Uri,java.util.Set<androidx.browser.trusted.Token>)` and the most appropriate one for the scope is chosen.

## Summary

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnection>` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#connect(android.net.Uri,java.util.Set<androidx.browser.trusted.Token>,java.util.concurrent.Executor)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/Uri.html scope, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/androidx/browser/trusted/Token> possiblePackages, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/concurrent/Executor.html executor )` Connects to the appropriate `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityService` or uses an existing connection if available and runs code once connected. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool` | `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#create(android.content.Context)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context)` Creates a TrustedWebActivityServiceConnectionPool. |
| `boolean` | `@https://developer.android.com/reference/androidx/annotation/MainThread https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#serviceExistsForScope(android.net.Uri,java.util.Set<androidx.browser.trusted.Token>)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/Uri.html scope, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/androidx/browser/trusted/Token> possiblePackages )` Checks if a TrustedWebActivityService exists to handle requests for the given scope and origin. |

## Public methods

### connect

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

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

Connects to the appropriate `https://developer.android.com/reference/androidx/browser/trusted/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 `https://developer.android.com/reference/android/content/Intent.html#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 `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityService#ACTION_TRUSTED_WEB_ACTIVITY_SERVICE()` will be used to find the Service.

This method should be called on the UI thread.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/Uri.html scope` | The scope used in an Intent to find packages that may have a `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityService`. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/androidx/browser/trusted/Token> possiblePackages` | A collection of packages to consider. These would be the packages that have previously launched a Trusted Web Activity for the origin. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/concurrent/Executor.html executor` | The `https://developer.android.com/reference/java/util/concurrent/Executor.html` to connect to the Service on if a new connection is required. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnection>` | A `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` for the resulting `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnection`. This may be set to an `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` if no service exists for the scope (you can check for this beforehand by calling `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#serviceExistsForScope(android.net.Uri,java.util.Set<androidx.browser.trusted.Token>)`). It may be set to a `https://developer.android.com/reference/java/lang/SecurityException.html` if the Service does not accept connections from this app. It may be set to an `https://developer.android.com/reference/java/lang/IllegalStateException.html` if connecting to the Service fails. |

### create

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#1.2.0)

```
public static @NonNull TrustedWebActivityServiceConnectionPool create(@NonNull Context context)
```

Creates a TrustedWebActivityServiceConnectionPool.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | A Context used for accessing SharedPreferences. |

### serviceExistsForScope

Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/browser#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 `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#connect(android.net.Uri,java.util.Set<androidx.browser.trusted.Token>,java.util.concurrent.Executor)`. If this method returns `false`, `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#connect(android.net.Uri,java.util.Set<androidx.browser.trusted.Token>,java.util.concurrent.Executor)` will return a Future containing an `https://developer.android.com/reference/java/lang/IllegalStateException.html`.

This method should be called on the UI thread.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/Uri.html scope` | The scope used in an Intent to find packages that may have a `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityService`. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/androidx/browser/trusted/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 `https://developer.android.com/reference/androidx/browser/trusted/TrustedWebActivityService` was found. |