# Token

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

*** ** * ** ***


```
public final class Token
```

<br />

*** ** * ** ***

Stores a package's identity, a combination of its package name and signing certificate. It is designed to be persistable as a `byte[]`, so you can do this:

```rust
Token token = Token.create(packageName, packageManager);
byte[] serialized = token.serialize();
// Persist serialized.

// Some time later...
Token verified = Token.deserialize(serialized);
verified.matches(packageName, packageManager);
```

## Summary

| ### Public methods |
|---|---|
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/browser/trusted/Token` | `https://developer.android.com/reference/androidx/browser/trusted/Token#create(java.lang.String,android.content.pm.PackageManager)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html packageName, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/pm/PackageManager.html packageManager)` Creates a `https://developer.android.com/reference/androidx/browser/trusted/Token` for the given package, taking into account the package name and its signing signature. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/browser/trusted/Token` | `https://developer.android.com/reference/androidx/browser/trusted/Token#deserialize(byte[])(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html byte[] serialized)` Reconstructs a `https://developer.android.com/reference/androidx/browser/trusted/Token` from the output of its `https://developer.android.com/reference/androidx/browser/trusted/Token#serialize()`. |
| `boolean` | `https://developer.android.com/reference/androidx/browser/trusted/Token#matches(java.lang.String,android.content.pm.PackageManager)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html packageName, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/pm/PackageManager.html packageManager )` Whether the given package matches the package that was used to create the original Token. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html byte[]` | `https://developer.android.com/reference/androidx/browser/trusted/Token#serialize()()` Serializes the `https://developer.android.com/reference/androidx/browser/trusted/Token` to a form that can later be restored by the `https://developer.android.com/reference/androidx/browser/trusted/Token#deserialize(byte[])` method. |

## Public methods

### create

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

```
public static @Nullable Token create(@NonNull String packageName, @NonNull PackageManager packageManager)
```

Creates a `https://developer.android.com/reference/androidx/browser/trusted/Token` for the given package, taking into account the package name and its signing signature.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html packageName` | The name of the package. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/pm/PackageManager.html packageManager` | A `https://developer.android.com/reference/android/content/pm/PackageManager.html` to determine the signing information. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/browser/trusted/Token` | A `https://developer.android.com/reference/androidx/browser/trusted/Token`. `null` if the provided package cannot be found (the app is not installed). |

### deserialize

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

```
public static @NonNull Token deserialize(@NonNull byte[] serialized)
```

Reconstructs a `https://developer.android.com/reference/androidx/browser/trusted/Token` from the output of its `https://developer.android.com/reference/androidx/browser/trusted/Token#serialize()`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html byte[] serialized` | The result of a `https://developer.android.com/reference/androidx/browser/trusted/Token#serialize()` call. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/browser/trusted/Token` | The deserialized Token. |

### matches

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

```
public boolean matches(
    @NonNull String packageName,
    @NonNull PackageManager packageManager
)
```

Whether the given package matches the package that was used to create the original Token.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html packageName` | The name of the package. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/pm/PackageManager.html packageManager` | A `https://developer.android.com/reference/android/content/pm/PackageManager.html` to get information about the package. |

| Returns |
|---|---|
| `boolean` | Whether the given package currently is the same as the one used to create the Token. |

### serialize

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

```
public @NonNull byte[] serialize()
```

Serializes the `https://developer.android.com/reference/androidx/browser/trusted/Token` to a form that can later be restored by the `https://developer.android.com/reference/androidx/browser/trusted/Token#deserialize(byte[])` method.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html byte[]` | A serialization of the `https://developer.android.com/reference/androidx/browser/trusted/Token`. |