GlanceWearWidget

abstract class GlanceWearWidget


Object that handles providing the contents of a Wear Widget.

The widget UI is defined by an instance of WearWidgetData such as WearWidgetDocument, provided in the implementation of provideWidgetData.

An implementation of this class can be associated with a GlanceWearWidgetService for receiving content requests and events from the Host.

Summary

Public constructors

Public functions

open suspend Unit
@MainThread
onAdded(context: Context, widgetHandle: ActiveWearWidgetHandle)

Called when a widget provider linked to this widget class is added to the host.

open suspend Unit

Called when the system sends a batch of interaction events.

open suspend Unit

Called when a widget provider linked to this widget class is removed from the host.

abstract suspend WearWidgetData

Override this method to provide data for this Widget.

suspend Unit
triggerUpdate(context: Context, instanceId: WidgetInstanceId)

Triggers a content update for the given widget instanceId, resulting in a call to provideWidgetData, after which the results to the Host.

Public constructors

GlanceWearWidget

Added in 1.0.0-alpha07
GlanceWearWidget()

Public functions

onAdded

Added in 1.0.0-alpha07
@MainThread
open suspend fun onAdded(context: Context, widgetHandle: ActiveWearWidgetHandle): Unit

Called when a widget provider linked to this widget class is added to the host.

This occurs when a widget is added to the carousel.

This method is called from the main thread.

Parameters
context: Context

the context from which this method is called

widgetHandle: ActiveWearWidgetHandle

the handle of the active widget.

onEvents

@MainThread
open suspend fun onEvents(context: Context, events: List<WearWidgetEvent>): Unit

Called when the system sends a batch of interaction events. The time between calls to this method may vary, do not depend on it for time-sensitive or critical tasks.

Interaction events represent user direct interaction with a widget, for example when a widget was visible.

This function must complete within 10 seconds of being called. If the timeout is exceeded, the operation will be canceled.

This method is called from the main thread.

Parameters
context: Context

the context from which this method is called

events: List<WearWidgetEvent>

A list WearWidgetEvent representing interactions that occurred.

onRemoved

Added in 1.0.0-alpha07
@MainThread
open suspend fun onRemoved(context: Context, widgetHandle: ActiveWearWidgetHandle): Unit

Called when a widget provider linked to this widget class is removed from the host.

This occurs when a widget is removed from the carousel.

This method is called from the main thread.

Parameters
context: Context

the context from which this method is called

widgetHandle: ActiveWearWidgetHandle

the handle of the widget.

provideWidgetData

@MainThread
abstract suspend fun provideWidgetData(context: Context, params: WearWidgetParams): WearWidgetData

Override this method to provide data for this Widget.

This method is called from the main thread.

Parameters
context: Context

the context from which this method is called

params: WearWidgetParams

the parameters that describe the widget for which the data is being provided.

triggerUpdate

Added in 1.0.0-alpha07
suspend fun triggerUpdate(context: Context, instanceId: WidgetInstanceId): Unit

Triggers a content update for the given widget instanceId, resulting in a call to provideWidgetData, after which the results to the Host.

All WidgetInstanceId for active widgets associated with this class can be retrieved from GlanceWearWidgetManager.fetchActiveWidgets(KClass).

The coroutine will be canceled if it doesn't complete within 10 seconds of being called.

Parameters
context: Context

the context from which this method is called.

instanceId: WidgetInstanceId

the ID of the widget instance to update.

Throws
IllegalArgumentException

if the provided WidgetInstanceId is invalid or not owned by the calling application.