1 /*
2  * Copyright 2022 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.material3.darkColorScheme
21 import androidx.compose.material3.lightColorScheme
22 import androidx.compose.runtime.Composable
23 import androidx.compose.ui.graphics.Color
24 import androidx.compose.ui.unit.dp
25 import androidx.glance.GlanceId
26 import androidx.glance.GlanceModifier
27 import androidx.glance.GlanceTheme
28 import androidx.glance.ImageProvider
29 import androidx.glance.LocalSize
30 import androidx.glance.appwidget.CircularProgressIndicator
31 import androidx.glance.appwidget.GlanceAppWidget
32 import androidx.glance.appwidget.GlanceAppWidgetReceiver
33 import androidx.glance.appwidget.LinearProgressIndicator
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.demos.ProgressIndicatorDemoColorScheme.totalHorizontalContentPadding
38 import androidx.glance.appwidget.lazy.LazyColumn
39 import androidx.glance.appwidget.provideContent
40 import androidx.glance.color.ColorProvider
41 import androidx.glance.layout.Alignment
42 import androidx.glance.layout.Box
43 import androidx.glance.layout.Row
44 import androidx.glance.layout.RowScope
45 import androidx.glance.layout.fillMaxWidth
46 import androidx.glance.layout.padding
47 import androidx.glance.layout.width
48 import androidx.glance.material3.ColorProviders
49 import androidx.glance.text.FontWeight
50 import androidx.glance.text.Text
51 import androidx.glance.text.TextAlign
52 import androidx.glance.text.TextStyle
53 import androidx.glance.unit.ColorProvider
54 
55 class ProgressIndicatorAppWidget : GlanceAppWidget() {
56     override val sizeMode: SizeMode = SizeMode.Exact
57 
<lambda>null58     override suspend fun provideGlance(context: Context, id: GlanceId) = provideContent {
59         Content()
60     }
61 
<lambda>null62     override suspend fun providePreview(context: Context, widgetCategory: Int) = provideContent {
63         Content()
64     }
65 
66     @Composable
Contentnull67     private fun Content() {
68         GlanceTheme(ProgressIndicatorDemoColorScheme.colors) {
69             Scaffold(
70                 backgroundColor = GlanceTheme.colors.widgetBackground,
71                 titleBar = {
72                     TitleBar(
73                         startIcon = ImageProvider(R.drawable.ic_android),
74                         title = "Progress Indicator Demo"
75                     )
76                 }
77             ) {
78                 ProgressIndicatorDemo()
79             }
80         }
81     }
82 
83     @Composable
ProgressIndicatorDemonull84     private fun ProgressIndicatorDemo() {
85         LazyColumn {
86             item { IndeterminateDemoRow() }
87             item { ZeroProgressDemoRow() }
88             item { DefaultColorsDemoRow() }
89             item { ThemeColorsDemoRow() }
90             item { ColorResourceDemoRow() }
91             item { DayNightColorsDemoRow() }
92             item { FixedColorsDemoRow() }
93         }
94     }
95 
96     @Composable
Cellnull97     private fun Cell(content: @Composable () -> Unit) {
98         // { 1/3 - row description} , {1/3 - linear demo}, {1/3 - circular demo}
99         val cellWidth = (LocalSize.current.width - totalHorizontalContentPadding) / 3
100 
101         Box(
102             modifier = GlanceModifier.width(cellWidth),
103             contentAlignment = Alignment.Center,
104             content = content
105         )
106     }
107 
108     @Composable
DemoRownull109     private fun DemoRow(content: @Composable RowScope.() -> Unit) {
110         Row(
111             modifier = GlanceModifier.fillMaxWidth().padding(5.dp),
112             verticalAlignment = Alignment.CenterVertically,
113             content = content
114         )
115     }
116 
117     @Composable
RowDescriptionnull118     private fun RowDescription(text: String) {
119         Text(
120             text = text,
121             maxLines = 2,
122             style =
123                 TextStyle(
124                     textAlign = TextAlign.Start,
125                     color = GlanceTheme.colors.onSurface,
126                     fontWeight = FontWeight.Medium,
127                 ),
128             modifier = GlanceModifier.fillMaxWidth()
129         )
130     }
131 
132     @Composable
ProgressIndicatorAppWidgetnull133     private fun ProgressIndicatorAppWidget.FixedColorsDemoRow() {
134         DemoRow {
135             Cell { RowDescription(text = "Fixed Color") }
136             Cell {
137                 LinearProgressIndicator(
138                     progress = 0.8f,
139                     color = ColorProvider(Color.Yellow),
140                     backgroundColor = ColorProvider(Color.White)
141                 )
142             }
143             Cell { CircularProgressIndicator(color = ColorProvider(Color.Yellow)) }
144         }
145     }
146 
147     @Composable
ProgressIndicatorAppWidgetnull148     private fun ProgressIndicatorAppWidget.DayNightColorsDemoRow() {
149         DemoRow {
150             Cell { RowDescription(text = "Fixed day/night Colors") }
151             Cell {
152                 LinearProgressIndicator(
153                     progress = .66f,
154                     modifier = GlanceModifier.padding(bottom = 8.dp),
155                     color = ColorProvider(day = Color.White, night = Color.Red),
156                     backgroundColor = ColorProvider(day = Color.Red, night = Color.White)
157                 )
158             }
159             Cell {
160                 CircularProgressIndicator(
161                     color = ColorProvider(day = Color.White, night = Color.Red)
162                 )
163             }
164         }
165     }
166 
167     @Composable
ProgressIndicatorAppWidgetnull168     private fun ProgressIndicatorAppWidget.ColorResourceDemoRow() {
169         DemoRow {
170             Cell { RowDescription(text = "Color resources") }
171             Cell {
172                 LinearProgressIndicator(
173                     progress = .66f,
174                     modifier = GlanceModifier.padding(bottom = 8.dp),
175                     color = ColorProvider(androidx.glance.R.color.glance_colorError),
176                     backgroundColor =
177                         ColorProvider(resId = androidx.glance.R.color.glance_colorSecondary)
178                 )
179             }
180             Cell {
181                 CircularProgressIndicator(
182                     color = ColorProvider(androidx.glance.R.color.glance_colorSecondary)
183                 )
184             }
185         }
186     }
187 
188     @Composable
ProgressIndicatorAppWidgetnull189     private fun ProgressIndicatorAppWidget.ThemeColorsDemoRow() {
190         DemoRow {
191             Cell { RowDescription(text = "Themed") }
192             Cell {
193                 LinearProgressIndicator(
194                     progress = 0.5f,
195                     color = GlanceTheme.colors.primary,
196                     backgroundColor = GlanceTheme.colors.onBackground
197                 )
198             }
199             Cell { CircularProgressIndicator(color = GlanceTheme.colors.primary) }
200         }
201     }
202 
203     @Composable
ProgressIndicatorAppWidgetnull204     private fun ProgressIndicatorAppWidget.IndeterminateDemoRow() {
205         DemoRow {
206             Cell { RowDescription(text = "Indeterminate") }
207             Cell { LinearProgressIndicator() }
208             Cell { CircularProgressIndicator() }
209         }
210     }
211 
212     @Composable
ProgressIndicatorAppWidgetnull213     private fun ProgressIndicatorAppWidget.DefaultColorsDemoRow() {
214         DemoRow {
215             Cell { RowDescription(text = "Default colors") }
216             Cell { LinearProgressIndicator(progress = 0.2f) }
217             Cell { CircularProgressIndicator() }
218         }
219     }
220 
221     @Composable
ZeroProgressDemoRownull222     private fun ProgressIndicatorAppWidget.ZeroProgressDemoRow() {
223         DemoRow {
224             Cell { RowDescription(text = "progress = 0") }
225             Cell { LinearProgressIndicator(progress = 0f) }
226             Cell { Text("Not supported") }
227         }
228     }
229 }
230 
231 class ProgressIndicatorAppWidgetReceiver : GlanceAppWidgetReceiver() {
232     override val glanceAppWidget: GlanceAppWidget = ProgressIndicatorAppWidget()
233 }
234 
235 internal object ProgressIndicatorDemoColorScheme {
236     private val md_theme_light_primary = Color(0xFF026E00)
237     private val md_theme_light_onBackground = Color(0xFF1E1C00)
238 
239     private val md_theme_dark_primary = Color(0xFF02E600)
240     private val md_theme_dark_onBackground = Color(0xFFF2E720)
241 
242     private val LightColors =
243         lightColorScheme(
244             primary = md_theme_light_primary,
245             onBackground = md_theme_light_onBackground,
246         )
247 
248     private val DarkColors =
249         darkColorScheme(
250             primary = md_theme_dark_primary,
251             onBackground = md_theme_dark_onBackground,
252         )
253 
254     val colors = ColorProviders(light = LightColors, dark = DarkColors)
255 
256     val totalHorizontalContentPadding = 32.dp // 2 * 16.dp
257 }
258