1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.glance.appwidget.demos
18 
19 import android.content.Context
20 import androidx.compose.runtime.Composable
21 import androidx.compose.runtime.getValue
22 import androidx.compose.runtime.mutableStateOf
23 import androidx.compose.runtime.remember
24 import androidx.compose.runtime.setValue
25 import androidx.compose.ui.unit.dp
26 import androidx.compose.ui.unit.sp
27 import androidx.glance.Button
28 import androidx.glance.GlanceId
29 import androidx.glance.GlanceModifier
30 import androidx.glance.GlanceTheme
31 import androidx.glance.ImageProvider
32 import androidx.glance.appwidget.GlanceAppWidget
33 import androidx.glance.appwidget.GlanceAppWidgetReceiver
34 import androidx.glance.appwidget.SizeMode
35 import androidx.glance.appwidget.components.Scaffold
36 import androidx.glance.appwidget.components.TitleBar
37 import androidx.glance.appwidget.provideContent
38 import androidx.glance.layout.Column
39 import androidx.glance.layout.Spacer
40 import androidx.glance.layout.fillMaxSize
41 import androidx.glance.layout.padding
42 import androidx.glance.layout.size
43 import androidx.glance.text.FontFamily
44 import androidx.glance.text.Text
45 import androidx.glance.text.TextStyle
46 
47 /** Sample AppWidget to demonstrate font changes. */
48 class FontDemoWidget : GlanceAppWidget() {
49     override val sizeMode: SizeMode = SizeMode.Exact
50 
<lambda>null51     override suspend fun provideGlance(context: Context, id: GlanceId) = provideContent {
52         Content()
53     }
54 
<lambda>null55     override suspend fun providePreview(context: Context, widgetCategory: Int) = provideContent {
56         Content()
57     }
58 
59     @Composable
Contentnull60     private fun Content() {
61         // This will reset any time the process is killed, but it isn't important as it's just to
62         // showcase different fonts.
63         var font by remember { mutableStateOf(FontFamily.Serif) }
64         GlanceTheme {
65             Scaffold(
66                 titleBar = {
67                     TitleBar(
68                         startIcon = ImageProvider(R.drawable.ic_demo_app),
69                         title = "Font Demo Widget"
70                     )
71                 },
72                 backgroundColor = GlanceTheme.colors.widgetBackground
73             ) {
74                 Column(modifier = GlanceModifier.fillMaxSize().padding(bottom = 16.dp)) {
75                     Text(
76                         "Font: " + font.family,
77                         style = TextStyle(fontSize = 20.sp, fontFamily = FontFamily.SansSerif)
78                     )
79                     Spacer(GlanceModifier.size(15.dp))
80                     Text(
81                         "The quick brown fox jumps over the lazy dog.",
82                         style = TextStyle(fontSize = 18.sp, fontFamily = font)
83                     )
84                     Spacer(GlanceModifier.defaultWeight())
85                     Button(
86                         text = "Toggle font",
87                         onClick = {
88                             font =
89                                 when (font) {
90                                     FontFamily.Serif -> FontFamily.SansSerif
91                                     FontFamily.SansSerif -> FontFamily.Cursive
92                                     FontFamily.Cursive -> FontFamily.Monospace
93                                     FontFamily.Monospace -> FontFamily.Serif
94                                     else -> FontFamily.SansSerif
95                                 }
96                         }
97                     )
98                 }
99             }
100         }
101     }
102 }
103 
104 class FontDemoAppWidgetReceiver : GlanceAppWidgetReceiver() {
105     override val glanceAppWidget: GlanceAppWidget = FontDemoWidget()
106 }
107