GlanceAppWidgetManager

class GlanceAppWidgetManager


Manager for Glance App Widgets.

This is used to query the app widgets currently installed on the system, and some of their properties.

Summary

Public constructors

Public functions

Int

Retrieve the platform AppWidget ID from the provided GlanceId

suspend List<DpSize>

Retrieve the sizes for a given App Widget, if provided by the host.

GlanceId
getGlanceIdBy(appWidgetId: Int)

Retrieve the GlanceId of the provided AppWidget ID.

GlanceId?
getGlanceIdBy(configurationIntent: Intent)

Retrieve the GlanceId from the configuration activity intent or null if not valid

suspend List<GlanceId>
<T : GlanceAppWidget> getGlanceIds(provider: Class<T>)

Returns the GlanceId of the App Widgets installed for a particular provider.

suspend Boolean
<T : GlanceAppWidgetReceiver> requestPinGlanceAppWidget(
    receiver: Class<T>,
    preview: GlanceAppWidget?,
    previewState: Any?,
    successCallback: PendingIntent?
)

Request to pin the GlanceAppWidget of the given receiver on the current launcher (if supported).

Public constructors

GlanceAppWidgetManager

Added in 1.0.0
GlanceAppWidgetManager(context: Context)

Public functions

getAppWidgetId

Added in 1.0.0
fun getAppWidgetId(glanceId: GlanceId): Int

Retrieve the platform AppWidget ID from the provided GlanceId

Important: Do NOT use appwidget ID as identifier, instead create your own and store them in the GlanceStateDefinition. This method should only be used for compatibility or IPC communication reasons in conjunction with getGlanceIdBy

getAppWidgetSizes

suspend fun getAppWidgetSizes(glanceId: GlanceId): List<DpSize>

Retrieve the sizes for a given App Widget, if provided by the host.

The list of sizes will be extracted from the App Widget options bundle, using the content of AppWidgetManager.OPTION_APPWIDGET_SIZES if provided. If not, and if AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT and similar are provided, the landscape and portrait sizes will be estimated from those and returned. Otherwise, the list will contain DpSize.Zero only.

getGlanceIdBy

Added in 1.0.0
fun getGlanceIdBy(appWidgetId: Int): GlanceId

Retrieve the GlanceId of the provided AppWidget ID.

Throws
kotlin.IllegalArgumentException

if the provided id is not associated with an existing GlanceId

getGlanceIdBy

Added in 1.0.0
fun getGlanceIdBy(configurationIntent: Intent): GlanceId?

Retrieve the GlanceId from the configuration activity intent or null if not valid

getGlanceIds

suspend fun <T : GlanceAppWidget> getGlanceIds(provider: Class<T>): List<GlanceId>

Returns the GlanceId of the App Widgets installed for a particular provider.

requestPinGlanceAppWidget

suspend fun <T : GlanceAppWidgetReceiver> requestPinGlanceAppWidget(
    receiver: Class<T>,
    preview: GlanceAppWidget? = null,
    previewState: Any? = null,
    successCallback: PendingIntent? = null
): Boolean

Request to pin the GlanceAppWidget of the given receiver on the current launcher (if supported).

Note: the request is only supported for SDK 26 and beyond, for lower versions this method will be no-op and return false.

Parameters
receiver: Class<T>

the target GlanceAppWidgetReceiver class

preview: GlanceAppWidget? = null

the instance of the GlanceAppWidget to compose the preview that will be shown in the request dialog. When not provided the app widget previewImage (as defined in the meta-data) will be used instead, or the app's icon if not available either.

previewState: Any? = null

the state (as defined by the GlanceAppWidget.stateDefinition to use for the preview

successCallback: PendingIntent? = null

a PendingIntent to be invoked if the app widget pinning is accepted by the user

Returns
Boolean

true if the request was successfully sent to the system, false otherwise

See also
requestPinAppWidget

for more information and limitations