# 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 />

```
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/kotlin/androidx/browser/trusted/TrustedWebActivityService`s. This is done through the `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/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 functions |
|---|---|
| `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/kotlin/androidx/browser/trusted/TrustedWebActivityServiceConnection!>` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#connect(android.net.Uri,java.util.Set<androidx.browser.trusted.Token>,java.util.concurrent.Executor)( scope: https://developer.android.com/reference/android/net/Uri.html, possiblePackages: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://developer.android.com/reference/kotlin/androidx/browser/trusted/Token!>, executor: https://developer.android.com/reference/java/util/concurrent/Executor.html )` Connects to the appropriate `https://developer.android.com/reference/kotlin/androidx/browser/trusted/TrustedWebActivityService` or uses an existing connection if available and runs code once connected. |
| `java-static https://developer.android.com/reference/kotlin/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool` | `https://developer.android.com/reference/kotlin/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#create(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Creates a TrustedWebActivityServiceConnectionPool. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/browser/trusted/TrustedWebActivityServiceConnectionPool#serviceExistsForScope(android.net.Uri,java.util.Set<androidx.browser.trusted.Token>)(scope: https://developer.android.com/reference/android/net/Uri.html, possiblePackages: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://developer.android.com/reference/kotlin/androidx/browser/trusted/Token!>)` Checks if a TrustedWebActivityService exists to handle requests for the given scope and origin. |

## Public functions

### connect

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

```
@MainThread
fun connect(
    scope: Uri,
    possiblePackages: (Mutable)Set<Token!>,
    executor: Executor
): ListenableFuture<TrustedWebActivityServiceConnection!>
```

Connects to the appropriate `https://developer.android.com/reference/kotlin/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/kotlin/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 |
|---|---|
| `scope: https://developer.android.com/reference/android/net/Uri.html` | The scope used in an Intent to find packages that may have a `https://developer.android.com/reference/kotlin/androidx/browser/trusted/TrustedWebActivityService`. |
| `possiblePackages: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://developer.android.com/reference/kotlin/androidx/browser/trusted/Token!>` | A collection of packages to consider. These would be the packages that have previously launched a Trusted Web Activity for the origin. |
| `executor: https://developer.android.com/reference/java/util/concurrent/Executor.html` | 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://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/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)

```
java-static fun create(context: Context): TrustedWebActivityServiceConnectionPool
```

Creates a TrustedWebActivityServiceConnectionPool.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | A Context used for accessing SharedPreferences. |

### serviceExistsForScope

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

```
@MainThread
fun serviceExistsForScope(scope: Uri, possiblePackages: (Mutable)Set<Token!>): Boolean
```

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/kotlin/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/kotlin/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 |
|---|---|
| `scope: https://developer.android.com/reference/android/net/Uri.html` | The scope used in an Intent to find packages that may have a `https://developer.android.com/reference/kotlin/androidx/browser/trusted/TrustedWebActivityService`. |
| `possiblePackages: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-set/index.html)https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://developer.android.com/reference/kotlin/androidx/browser/trusted/Token!>` | A collection of packages to consider. These would be the packages that have previously launched a Trusted Web Activity for the origin. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether a `https://developer.android.com/reference/kotlin/androidx/browser/trusted/TrustedWebActivityService` was found. |