@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class ShortcutAdapter


Util methods for Document <-> shortcut conversion.

Summary

Constants

static final String
DEFAULT_NAMESPACE = "__shortcut_adapter_ns__"

Represents the default namespace which should be used as the androidx.appsearch.annotation.Document.Namespace for documents that are meant to be donated as a shortcut through androidx.core.content.pm.ShortcutManagerCompat.

Public methods

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

Converts given document to a ShortcutInfoCompat.Builder, which can be used to construct a shortcut for donation through androidx.core.content.pm.ShortcutManagerCompat.

static @NonNull Uri

Returns an uri that uniquely identifies the given document object.

static @NonNull Uri

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

Constants

DEFAULT_NAMESPACE

Added in 1.1.0-alpha04
public static final String DEFAULT_NAMESPACE = "__shortcut_adapter_ns__"

Represents the default namespace which should be used as the androidx.appsearch.annotation.Document.Namespace for documents that are meant to be donated as a shortcut through androidx.core.content.pm.ShortcutManagerCompat.

Public methods

createShortcutBuilderFromDocument

Added in 1.1.0-alpha04
public static @NonNull ShortcutInfoCompat.Builder createShortcutBuilderFromDocument(
    @NonNull Context context,
    @NonNull Object document
)

Converts given document to a ShortcutInfoCompat.Builder, which can be used to construct a shortcut for donation through androidx.core.content.pm.ShortcutManagerCompat. Applicable data in the given document will be used to populate corresponding fields in ShortcutInfoCompat.Builder.

Note: Namespace of the given document is required to be set to DEFAULT_NAMESPACE if it will be used to create a shortcut; Otherwise an exception would be thrown.

See androidx.appsearch.annotation.Document.Namespace

Note: The ShortcutID in 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 getId

see androidx.appsearch.annotation.Document.Id

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 getMaxShortcutCountPerActivity

See setExcludedFromSurfaces.

Given document object will be stored in the form of Bundle in ShortcutInfoCompat.

The document that was stored in ShortcutInfoCompat is discarded when the shortcut is converted into android.content.pm.ShortcutInfo, 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 androidx.core.content.pm.ShortcutManagerCompat would not carry any document at all.

Parameters
@NonNull Object document

a document object annotated with androidx.appsearch.annotation.Document that carries structured data in a pre-defined format.

Returns
@NonNull ShortcutInfoCompat.Builder

a ShortcutInfoCompat.Builder which can be used to construct a shortcut for donation through androidx.core.content.pm.ShortcutManagerCompat.

Throws
java.lang.IllegalArgumentException

An exception would be thrown if the namespace in the given document object does not match DEFAULT_NAMESPACE.

androidx.appsearch.exceptions.AppSearchException

An exception would be thrown if the given document object is not annotated with androidx.appsearch.annotation.Document or encountered an unexpected error during the conversion to GenericDocument.

getDocumentUri

Added in 1.1.0-alpha04
public static @NonNull Uri getDocumentUri(@NonNull Object document)

Returns an uri that uniquely identifies the given document object.

Parameters
@NonNull Object document

a document object annotated with androidx.appsearch.annotation.Document that carries structured data in a pre-defined format.

Throws
androidx.appsearch.exceptions.AppSearchException

if the given document object is not annotated with androidx.appsearch.annotation.Document or encountered an unexpected error during the conversion to GenericDocument.

getDocumentUri

Added in 1.1.0-alpha04
public static @NonNull Uri getDocumentUri(@NonNull String id)

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

Parameters
@NonNull String id

id of the document.