# 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
class ShortcutAdapter
```

<br />

*** ** * ** ***

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

## Summary

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | `https://developer.android.com/reference/kotlin/androidx/appsearch/app/ShortcutAdapter#DEFAULT_NAMESPACE() = "__shortcut_adapter_ns__"` Represents the default namespace which should be used as the `https://developer.android.com/reference/kotlin/androidx/appsearch/annotation/Document.Namespace` for documents that are meant to be donated as a shortcut through `https://developer.android.com/reference/kotlin/androidx/core/content/pm/ShortcutManagerCompat`. |

| ### Public functions |
|---|---|
| `java-static https://developer.android.com/reference/kotlin/androidx/core/content/pm/ShortcutInfoCompat.Builder` | `https://developer.android.com/reference/kotlin/androidx/appsearch/app/ShortcutAdapter#createShortcutBuilderFromDocument(android.content.Context,java.lang.Object)(context: https://developer.android.com/reference/android/content/Context.html, document: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)` Converts given document to a `https://developer.android.com/reference/kotlin/androidx/core/content/pm/ShortcutInfoCompat.Builder`, which can be used to construct a shortcut for donation through `https://developer.android.com/reference/kotlin/androidx/core/content/pm/ShortcutManagerCompat`. |
| `java-static https://developer.android.com/reference/android/net/Uri.html` | `https://developer.android.com/reference/kotlin/androidx/appsearch/app/ShortcutAdapter#getDocumentUri(java.lang.Object)(document: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)` Returns an uri that uniquely identifies the given document object. |
| `java-static https://developer.android.com/reference/android/net/Uri.html` | `https://developer.android.com/reference/kotlin/androidx/appsearch/app/ShortcutAdapter#getDocumentUri(java.lang.String)(id: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)` 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)

```
const val DEFAULT_NAMESPACE = "__shortcut_adapter_ns__": String!
```

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

## Public functions

### createShortcutBuilderFromDocument

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

```
java-static fun createShortcutBuilderFromDocument(context: Context, document: Any): ShortcutInfoCompat.Builder
```

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

Note: Namespace of the given document is required to be set to `https://developer.android.com/reference/kotlin/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/kotlin/androidx/appsearch/annotation/Document.Namespace`

Note: The ShortcutID in `https://developer.android.com/reference/kotlin/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/kotlin/androidx/core/content/pm/ShortcutInfoCompat#getId()`

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

`https://developer.android.com/reference/kotlin/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/kotlin/androidx/core/content/pm/ShortcutManagerCompat#getMaxShortcutCountPerActivity(android.content.Context)`

See `https://developer.android.com/reference/kotlin/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/kotlin/androidx/core/content/pm/ShortcutInfoCompat`.

The document that was stored in `https://developer.android.com/reference/kotlin/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/kotlin/androidx/core/content/pm/ShortcutManagerCompat` would not carry any document at all.

| Parameters |
|---|---|
| `context: https://developer.android.com/reference/android/content/Context.html` | the context used to provide the package and resources |
| `document: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html` | a document object annotated with `https://developer.android.com/reference/kotlin/androidx/appsearch/annotation/Document` that carries structured data in a pre-defined format. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/core/content/pm/ShortcutInfoCompat.Builder` | a `https://developer.android.com/reference/kotlin/androidx/core/content/pm/ShortcutInfoCompat.Builder` which can be used to construct a shortcut for donation through `https://developer.android.com/reference/kotlin/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/kotlin/androidx/appsearch/app/ShortcutAdapter#DEFAULT_NAMESPACE()`. |
| `https://developer.android.com/reference/kotlin/androidx/appsearch/exceptions/AppSearchException` | An exception would be thrown if the given document object is not annotated with `https://developer.android.com/reference/kotlin/androidx/appsearch/annotation/Document` or encountered an unexpected error during the conversion to `https://developer.android.com/reference/kotlin/androidx/appsearch/app/GenericDocument`. |

### getDocumentUri

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

```
java-static fun getDocumentUri(document: Any): Uri
```

Returns an uri that uniquely identifies the given document object.

| Parameters |
|---|---|
| `document: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html` | a document object annotated with `https://developer.android.com/reference/kotlin/androidx/appsearch/annotation/Document` that carries structured data in a pre-defined format. |

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

### getDocumentUri

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

```
java-static fun getDocumentUri(id: String): Uri
```

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

| Parameters |
|---|---|
| `id: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | id of the document. |