Share
Embed
Shooting a “Glance” at Android App Widgets
The Android community adopts Jetpack Compose more and more every day. But what about our app widgets?
In this talk, we will see how we can use “Glance” library to create app widgets. We will briefly talk about how to create “glanceable” UI using composables. We will also see how to use actions for user interactions and create widgets with different size modes. Moreover, we will touch upon the internal state of the Glance app widget and how to update this state outside of the widget process. Finally, we will discuss how to apply theming and use dynamic colors in the app widgets.
Fatih Giriş
June 25, 2022
Transcript
98 slides
-
FATIH GIRIS ANDROID LEAD @DNB SHOOTING A “GLANCE” AT ANDROID
APP WIDGETS @fatih_grs -
• What is Glance? • Actions • Size modes •
State • Theming AGENDA -
• Library to create App Widgets & Wear Tiles •
Uses compose runtime GLANCE -
• Library to create App Widgets & Wear Tiles •
Uses compose runtime • Converts Composables to RemoteViews & Tiles • Android 12 widget improvements • Backward compatible (API 23) GLANCE -
GLANCE class MyAppWidgetReceiver : GlanceAppWidgetReceiver() { override val glanceAppWidget: GlanceAppWidget
get() = MyAppWidget() } AppWidgetReceiver -
GLANCE class MyAppWidgetReceiver : GlanceAppWidgetReceiver() { override val glanceAppWidget: GlanceAppWidget
get() = MyAppWidget() } BroadcastReceiver -
GLANCE class MyAppWidget : GlanceAppWidget() { @Composable override fun Content()
{ // Write your App Widget composable } } -
GLANCE class MyAppWidget : GlanceAppWidget() { @Composable override fun Content()
{ Text( text = "Hello from a Glance widget!" ) } } -
GLANCE class MyAppWidget : GlanceAppWidget() { @Composable override fun Content()
{ Text( text = "Hello from a Glance widget!" ) } } -
GLANCE class MyAppWidget : GlanceAppWidget() { @Composable override fun Content()
{ androidx.compose.material.Text( text = "Hello from a Glance widget!" ) } } -
GLANCE class MyAppWidget : GlanceAppWidget() { @Composable override fun Content()
{ androidx.glance.text.Text( text = "Hello from a Glance widget!" ) } } -
GLANCE class MyAppWidget : GlanceAppWidget() { @Composable override fun Content()
{ androidx.glance.text.Text( modifier = androidx.compose.ui.Modifier, text = "Hello from a Glance widget!" ) } } -
GLANCE class MyAppWidget : GlanceAppWidget() { @Composable override fun Content()
{ androidx.glance.text.Text( modifier = androidx.glance.GlanceModifier, text = "Hello from a Glance widget!" ) } } -
• Use actions in response to user interactions • 4
types of actions - actionStartActivity - actionSendBroadcast - actionStartService - actionRunCallback ACTIONS -
ACTIONS class MyAppWidget : GlanceAppWidget() { @Composable override fun Content()
{ Text( modifier = GlanceModifier.clickable( actionStartActivity<MyActivity>() ), text = "Hello from a Glance widget!" ) } } -
ACTIONS class MyAppWidget : GlanceAppWidget() { @Composable override fun Content()
{ Text( modifier = GlanceModifier.clickable( actionStartActivity<MyActivity>() ), text = "Hello from a Glance widget!" ) } } -
ACTIONS class MyAppWidget : GlanceAppWidget() { @Composable override fun Content()
{ Text( modifier = GlanceModifier.clickable( actionStartActivity<MyActivity>() ), text = "Hello from a Glance widget!" ) } } -
ACTIONS class MyAppWidget : GlanceAppWidget() { @Composable override fun Content()
{ Text( modifier = GlanceModifier.clickable( actionRunCallback<MyCustomActionCallback>() ), text = "Hello from a Glance widget!" ) } } -
• Provides a composition local LocalState • Uses DataStore under
the hood • Default state de fi nition supports: Boolean, Int, Long, Float, String, Set • Get the current state using currentState() STATE -
STATE class MyAppWidget : GlanceAppWidget() { // The key for
the data store private val COUNTER_KEY = intPreferencesKey("MY_COUNTER_KEY") // The key for the custom action parameter private val COUNTER_ACTION_PARAMETER_KEY = ActionParameters.Key<Int>( “COUNTER_INCREMENT_ACTION” ) @Composable override fun Content() { // Get the saved number of clicks from the current state val numberOfClicks = currentState(key = COUNTER_KEY) ?: 0 Text( modifier = GlanceModifier.clickable( actionRunCallback<CounterIncrementActionCallback>( parameters = actionParametersOf( COUNTER_ACTION_PARAMETER_KEY to numberOfClicks + 1 ) ) ), text = numberOfClicks.toString() ) } } -
STATE class MyAppWidget : GlanceAppWidget() { // The key for
the data store private val COUNTER_KEY = intPreferencesKey("MY_COUNTER_KEY") // The key for the custom action parameter private val COUNTER_ACTION_PARAMETER_KEY = ActionParameters.Key<Int>( “COUNTER_INCREMENT_ACTION” ) @Composable override fun Content() { // Get the saved number of clicks from the current state val numberOfClicks = currentState(key = COUNTER_KEY) ?: 0 Text( modifier = GlanceModifier.clickable( actionRunCallback<CounterIncrementActionCallback>( parameters = actionParametersOf( -
class MyAppWidget : GlanceAppWidget() { // The key for the
data store private val COUNTER_KEY = intPreferencesKey("MY_COUNTER_KEY") // The key for the custom action parameter private val COUNTER_ACTION_PARAMETER_KEY = ActionParameters.Key<Int>( “COUNTER_INCREMENT_ACTION” ) @Composable override fun Content() { // Get the saved number of clicks from the current state val numberOfClicks = currentState(key = COUNTER_KEY) ?: 0 Text( modifier = GlanceModifier.clickable( actionRunCallback<CounterIncrementActionCallback>( parameters = actionParametersOf( COUNTER_ACTION_PARAMETER_KEY to numberOfClicks + 1 ) ) ), text = numberOfClicks.toString() STATE -
// The key for the custom action parameter private val
COUNTER_ACTION_PARAMETER_KEY = ActionParameters.Key<Int>( “COUNTER_INCREMENT_ACTION” ) @Composable override fun Content() { // Get the saved number of clicks from the current state val numberOfClicks = currentState(key = COUNTER_KEY) ?: 0 Text( modifier = GlanceModifier.clickable( actionRunCallback<CounterIncrementActionCallback>( parameters = actionParametersOf( COUNTER_ACTION_PARAMETER_KEY to numberOfClicks + 1 ) ) ), text = numberOfClicks.toString() ) } } STATE -
// The key for the custom action parameter private val
COUNTER_ACTION_PARAMETER_KEY = ActionParameters.Key<Int>( “COUNTER_INCREMENT_ACTION” ) @Composable override fun Content() { // Get the saved number of clicks from the current state val numberOfClicks = currentState(key = COUNTER_KEY) ?: 0 Text( modifier = GlanceModifier.clickable( actionRunCallback<CounterIncrementActionCallback>( parameters = actionParametersOf( COUNTER_ACTION_PARAMETER_KEY to numberOfClicks + 1 ) ) ), text = numberOfClicks.toString() ) } } STATE -
private val COUNTER_ACTION_PARAMETER_KEY = ActionParameters.Key<Int>( “COUNTER_INCREMENT_ACTION” ) @Composable override fun
Content() { // Get the saved number of clicks from the current state val numberOfClicks = currentState(key = COUNTER_KEY) ?: 0 Text( modifier = GlanceModifier.clickable( actionRunCallback<CounterIncrementActionCallback>( parameters = actionParametersOf( COUNTER_ACTION_PARAMETER_KEY to numberOfClicks + 1 ) ) ), text = numberOfClicks.toString() ) } } STATE -
“COUNTER_INCREMENT_ACTION” ) @Composable override fun Content() { // Get the
saved number of clicks from the current state val numberOfClicks = currentState(key = COUNTER_KEY) ?: 0 Text( modifier = GlanceModifier.clickable( actionRunCallback<CounterIncrementActionCallback>( parameters = actionParametersOf( COUNTER_ACTION_PARAMETER_KEY to numberOfClicks + 1 ) ) ), text = numberOfClicks.toString() ) } } STATE -
“COUNTER_INCREMENT_ACTION” ) @Composable override fun Content() { // Get the
saved number of clicks from the current state val numberOfClicks = currentState(key = COUNTER_KEY) ?: 0 Text( modifier = GlanceModifier.clickable( actionRunCallback<CounterIncrementActionCallback>( parameters = actionParametersOf( COUNTER_ACTION_PARAMETER_KEY to numberOfClicks + 1 ) ) ), text = numberOfClicks.toString() ) } } STATE -
@Composable override fun Content() { // Get the saved number
of clicks from the current state val numberOfClicks = currentState(key = COUNTER_KEY) ?: 0 Text( modifier = GlanceModifier.clickable( actionRunCallback<CounterIncrementActionCallback>( parameters = actionParametersOf( COUNTER_ACTION_PARAMETER_KEY to numberOfClicks + 1 ) ) ), text = numberOfClicks.toString() ) } } STATE -
STATE class CounterIncrementActionCallback : ActionCallback { override suspend fun onRun(
context: Context, glanceId: GlanceId, parameters: ActionParameters ) { // Get the incremented value from the parameters since // we are sending it from the composable val counter = parameters[MyAppWidget.COUNTER_ACTION_PARAMETER_KEY] ?: 0 // Save the counter into the data store updateAppWidgetState( context = context, definition = PreferencesGlanceStateDefinition, glanceId = glanceId ) { it.toMutablePreferences().apply { this[MyAppWidget.COUNTER_KEY] = counter } } // Need to update the widget as well in order // to invoke the content function again MyAppWidget().update(context, glanceId) } } -
STATE class CounterIncrementActionCallback : ActionCallback { override suspend fun onRun(
context: Context, glanceId: GlanceId, parameters: ActionParameters ) { // Get the incremented value from the parameters since // we are sending it from the composable val counter = parameters[MyAppWidget.COUNTER_ACTION_PARAMETER_KEY] ?: 0 // Save the counter into the data store updateAppWidgetState( context = context, definition = PreferencesGlanceStateDefinition, glanceId = glanceId ) { it.toMutablePreferences().apply { this[MyAppWidget.COUNTER_KEY] = counter -
class CounterIncrementActionCallback : ActionCallback { override suspend fun onRun( context:
Context, glanceId: GlanceId, parameters: ActionParameters ) { // Get the incremented value from the parameters since // we are sending it from the composable val counter = parameters[MyAppWidget.COUNTER_ACTION_PARAMETER_KEY] ?: 0 // Save the counter into the data store updateAppWidgetState( context = context, definition = PreferencesGlanceStateDefinition, glanceId = glanceId ) { it.toMutablePreferences().apply { this[MyAppWidget.COUNTER_KEY] = counter STATE -
class CounterIncrementActionCallback : ActionCallback { override suspend fun onRun( context:
Context, glanceId: GlanceId, parameters: ActionParameters ) { // Get the incremented value from the parameters since // we are sending it from the composable val counter = parameters[MyAppWidget.COUNTER_ACTION_PARAMETER_KEY] ?: 0 // Save the counter into the data store updateAppWidgetState( context = context, definition = PreferencesGlanceStateDefinition, glanceId = glanceId ) { it.toMutablePreferences().apply { this[MyAppWidget.COUNTER_KEY] = counter } } // Need to update the widget as well in order // to invoke the content function again STATE -
context: Context, glanceId: GlanceId, parameters: ActionParameters ) { // Get
the incremented value from the parameters since // we are sending it from the composable val counter = parameters[MyAppWidget.COUNTER_ACTION_PARAMETER_KEY] ?: 0 // Save the counter into the data store updateAppWidgetState( context = context, definition = PreferencesGlanceStateDefinition, glanceId = glanceId ) { it.toMutablePreferences().apply { this[MyAppWidget.COUNTER_KEY] = counter } } // Need to update the widget as well in order // to invoke the content function again MyAppWidget().update(context, glanceId) } } STATE -
// Get the incremented value from the parameters since //
we are sending it from the composable val counter = parameters[MyAppWidget.COUNTER_ACTION_PARAMETER_KEY] ?: 0 // Save the counter into the data store updateAppWidgetState( context = context, definition = PreferencesGlanceStateDefinition, glanceId = glanceId ) { it.toMutablePreferences().apply { this[MyAppWidget.COUNTER_KEY] = counter } } // Need to update the widget as well in order // to invoke the content function again MyAppWidget().update(context, glanceId) } } STATE -
val counter = parameters[MyAppWidget.COUNTER_ACTION_PARAMETER_KEY] ?: 0 // Save the counter
into the data store updateAppWidgetState( context = context, definition = PreferencesGlanceStateDefinition, glanceId = glanceId ) { it.toMutablePreferences().apply { this[MyAppWidget.COUNTER_KEY] = counter } } // Need to update the widget as well in order // to invoke the content function again MyAppWidget().update(context, glanceId) } } STATE -
• LocalSize to keep the size of a widget •
3 di ff erent size modes - SizeMode.Single - SizeMode.Exact - SizeMode.Responsive SIZING -
SizeMode.Single • Content is called once with minimum size •
LocalSize does not change -
SizeMode.Single class MyAppWidget : GlanceAppWidget() { override val sizeMode: SizeMode
get() = SizeMode.Single @Composable override fun Content() { Box { Text( text = "I am a using SizeMode.Single" + "Content will be called only once" + "${LocalSize.current}" ) } } } -
SizeMode.Single class MyAppWidget : GlanceAppWidget() { override val sizeMode: SizeMode
get() = SizeMode.Single @Composable override fun Content() { Box { Text( text = "I am a using SizeMode.Single" + "Content will be called only once" + "${LocalSize.current}" ) } } } -
SizeMode.Exact • Content is called every time size changes •
LocalSize always has the latest size -
SizeMode.Exact class MyAppWidget : GlanceAppWidget() { override val sizeMode: SizeMode
get() = SizeMode.Exact @Composable override fun Content() { Box { Text( text = "I am a using SizeMode.Single" + "Content will be called only once" + "${LocalSize.current}" ) } } } -
SizeMode.Exact class MyAppWidget : GlanceAppWidget() { override val sizeMode: SizeMode
get() = SizeMode.Exact @Composable override fun Content() { Box { Text( text = "I am a using SizeMode.Single" + "Content will be called only once" + "${LocalSize.current}" ) } } } -
SizeMode.Responsive • Content is called for the de fi ned
sizes • LocalSize has one of the de fi ned sizes -
SizeMode.Responsive class MyAppWidget : GlanceAppWidget() { companion object { //
Responsive sizes val SMALL_BOX = DpSize(100.dp, 100.dp) val LARGE_BOX = DpSize(200.dp, 200.dp) } override val sizeMode: SizeMode get() = SizeMode.Responsive( setOf(SMALL_BOX, LARGE_BOX) ) @Composable override fun Content() { when (LocalSize.current) { SMALL_BOX -> SmallBox() LARGE_BOX -> LargeBox() } } } -
SizeMode.Responsive class MyAppWidget : GlanceAppWidget() { companion object { //
Responsive sizes val SMALL_BOX = DpSize(100.dp, 100.dp) val LARGE_BOX = DpSize(200.dp, 200.dp) } override val sizeMode: SizeMode get() = SizeMode.Responsive( setOf(SMALL_BOX, LARGE_BOX) ) @Composable override fun Content() { when (LocalSize.current) { SMALL_BOX -> SmallBox() LARGE_BOX -> LargeBox() } } } -
SizeMode.Responsive class MyAppWidget : GlanceAppWidget() { companion object { //
Responsive sizes val SMALL_BOX = DpSize(100.dp, 100.dp) val LARGE_BOX = DpSize(200.dp, 200.dp) } override val sizeMode: SizeMode get() = SizeMode.Responsive( setOf(SMALL_BOX, LARGE_BOX) ) @Composable override fun Content() { when (LocalSize.current) { SMALL_BOX -> SmallBox() LARGE_BOX -> LargeBox() } } } -
SizeMode.Responsive class MyAppWidget : GlanceAppWidget() { companion object { //
Responsive sizes val SMALL_BOX = DpSize(100.dp, 100.dp) val LARGE_BOX = DpSize(200.dp, 200.dp) } override val sizeMode: SizeMode get() = SizeMode.Responsive( setOf(SMALL_BOX, LARGE_BOX) ) @Composable override fun Content() { when (LocalSize.current) { SMALL_BOX -> SmallBox() LARGE_BOX -> LargeBox() } } } -
THEMING Box( modifier = GlanceModifier .background( com.google.android.material.R.color.m3_sys_color_dynamic_dark_primary ), contentAlignment =
Alignment.Center ) { Text(text = "🙈") } -
THEMING Box( modifier = GlanceModifier .background( com.google.android.material.R.color.m3_sys_color_dynamic_dark_primary ), contentAlignment =
Alignment.Center ) { Text(text = "🙈") } -
THEMING The widget background using the dark dynamic color from
the system color pallet -
THEMING Box( modifier = GlanceModifier .background( com.google.android.material.R.color.m3_sys_color_dynamic_dark_primary ), contentAlignment =
Alignment.Center ) { Text(text = "🙈") } -
THEMING Box( modifier = GlanceModifier .background( ColorProvider( day = Color.White,
night = Color.Yellow ) ), contentAlignment = Alignment.Center ) { Text(text = "🙈") } -
THEMING Box( modifier = GlanceModifier .background( ColorProvider( day = Color.White,
night = Color.Yellow ) ), contentAlignment = Alignment.Center ) { Text(text = "🙈") } -
• Not possible to use MaterialTheme ❌ • Dynamic colors
• Use ColorProvider for day & night con fi guration THEMING -
• Glance converts Composables to RemoteViews (and Tiles) • Not
possible to use the composables from the app • Actions: • actionStartActivity • actionStartService • actionSendBroadcast • actionRunCallback • Size modes: Single, Exact and Responsive TL;DR -
• currentState() provides the current state of the widget •
Dynamic colors on Android 12 and above • ColorProvider()supports day & night colors • Remember that it’s still in alpha TL;DR