# ShortcutAdapter

Artifact: [androidx.appsearch:appsearch-builtin-types](https://developer.android.com/jetpack/androidx/releases/appsearch) [View Source](https://cs.android.com/search?q=file:androidx/appsearch/app/ShortcutAdapter.java+class:androidx.appsearch.app.ShortcutAdapter) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/appsearch#1.1.0)

*** ** * ** ***


```
@ExperimentalAppSearchApi
public class ShortcutAdapter
```

<br />

*** ** * ** ***

Util methods for Document \<-\> shortcut conversion.

## Summary

| ### Constants |
|---|---|
| `static final https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/appsearch/app/ShortcutAdapter#DEFAULT_NAMESPACE() = "__shortcut_adapter_ns__"` Represents the default namespace which should be used as the `https://developer.android.com/reference/androidx/appsearch/annotation/Document.Namespace` for documents that are meant to be donated as a shortcut through `https://developer.android.com/reference/androidx/core/content/pm/ShortcutManagerCompat`. |

| ### Public methods |
|---|---|
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat.Builder` | `https://developer.android.com/reference/androidx/appsearch/app/ShortcutAdapter#createShortcutBuilderFromDocument(android.content.Context,java.lang.Object)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Object.html document )` Converts given document to a `https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat.Builder`, which can be used to construct a shortcut for donation through `https://developer.android.com/reference/androidx/core/content/pm/ShortcutManagerCompat`. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/Uri.html` | `https://developer.android.com/reference/androidx/appsearch/app/ShortcutAdapter#getDocumentUri(java.lang.Object)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Object.html document)` Returns an uri that uniquely identifies the given document object. |
| `static @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/Uri.html` | `https://developer.android.com/reference/androidx/appsearch/app/ShortcutAdapter#getDocumentUri(java.lang.String)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html id)` Returns an uri that identifies to the document associated with given document id. |

## Constants

### DEFAULT_NAMESPACE

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

```
public static final String DEFAULT_NAMESPACE = "__shortcut_adapter_ns__"
```

Represents the default namespace which should be used as the `https://developer.android.com/reference/androidx/appsearch/annotation/Document.Namespace` for documents that are meant to be donated as a shortcut through `https://developer.android.com/reference/androidx/core/content/pm/ShortcutManagerCompat`.

## Public methods

### createShortcutBuilderFromDocument

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

```
public static @NonNull ShortcutInfoCompat.Builder createShortcutBuilderFromDocument(
    @NonNull Context context,
    @NonNull Object document
)
```

Converts given document to a `https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat.Builder`, which can be used to construct a shortcut for donation through `https://developer.android.com/reference/androidx/core/content/pm/ShortcutManagerCompat`. Applicable data in the given document will be used to populate corresponding fields in `https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat.Builder`.

Note: Namespace of the given document is required to be set to `https://developer.android.com/reference/androidx/appsearch/app/ShortcutAdapter#DEFAULT_NAMESPACE()` if it will be used to create a shortcut; Otherwise an exception would be thrown.

See `https://developer.android.com/reference/androidx/appsearch/annotation/Document.Namespace`

Note: The ShortcutID in `https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat.Builder` will be set to match the id of given document. So an unique id across all documents should be chosen if the document is to be used to create a shortcut.

see `https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat#getId()`

see `https://developer.android.com/reference/androidx/appsearch/annotation/Document.Id`

`https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat.Builder` created this way by default will be set to hidden from launcher. If remain hidden, they will not appear in launcher's surfaces (e.g. long press menu) nor do they count toward the quota defined in `https://developer.android.com/reference/androidx/core/content/pm/ShortcutManagerCompat#getMaxShortcutCountPerActivity(android.content.Context)`

See `https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat.Builder#setExcludedFromSurfaces(int)`.

Given document object will be stored in the form of `https://developer.android.com/reference/android/os/Bundle.html` in `https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat`.

The document that was stored in `https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat` is discarded when the shortcut is converted into `https://developer.android.com/reference/android/content/pm/ShortcutInfo.html`, meaning that the document will not be persisted in the shortcut object itself once the shortcut is published. i.e. Any shortcut returned from queries toward `https://developer.android.com/reference/androidx/core/content/pm/ShortcutManagerCompat` would not carry any document at all.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | the context used to provide the package and resources |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Object.html document` | a document object annotated with `https://developer.android.com/reference/androidx/appsearch/annotation/Document` that carries structured data in a pre-defined format. |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat.Builder` | a `https://developer.android.com/reference/androidx/core/content/pm/ShortcutInfoCompat.Builder` which can be used to construct a shortcut for donation through `https://developer.android.com/reference/androidx/core/content/pm/ShortcutManagerCompat`. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | An exception would be thrown if the namespace in the given document object does not match `https://developer.android.com/reference/androidx/appsearch/app/ShortcutAdapter#DEFAULT_NAMESPACE()`. |
| `https://developer.android.com/reference/androidx/appsearch/exceptions/AppSearchException` | An exception would be thrown if the given document object is not annotated with `https://developer.android.com/reference/androidx/appsearch/annotation/Document` or encountered an unexpected error during the conversion to `https://developer.android.com/reference/androidx/appsearch/app/GenericDocument`. |

### getDocumentUri

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

```
public static @NonNull Uri getDocumentUri(@NonNull Object document)
```

Returns an uri that uniquely identifies the given document object.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/Object.html document` | a document object annotated with `https://developer.android.com/reference/androidx/appsearch/annotation/Document` that carries structured data in a pre-defined format. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/appsearch/exceptions/AppSearchException` | if the given document object is not annotated with `https://developer.android.com/reference/androidx/appsearch/annotation/Document` or encountered an unexpected error during the conversion to `https://developer.android.com/reference/androidx/appsearch/app/GenericDocument`. |

### getDocumentUri

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

```
public static @NonNull Uri getDocumentUri(@NonNull String id)
```

Returns an uri that identifies to the document associated with given document id.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html id` | id of the document. |