1 /*
2  * Copyright 2021 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 android.util.Log
21 import androidx.compose.runtime.Composable
22 import androidx.compose.ui.graphics.Color
23 import androidx.compose.ui.unit.dp
24 import androidx.glance.GlanceId
25 import androidx.glance.GlanceModifier
26 import androidx.glance.ImageProvider
27 import androidx.glance.action.clickable
28 import androidx.glance.appwidget.GlanceAppWidget
29 import androidx.glance.appwidget.GlanceAppWidgetReceiver
30 import androidx.glance.appwidget.SizeMode
31 import androidx.glance.appwidget.provideContent
32 import androidx.glance.background
33 import androidx.glance.layout.Column
34 import androidx.glance.layout.Row
35 import androidx.glance.layout.fillMaxSize
36 import androidx.glance.layout.fillMaxWidth
37 import androidx.glance.layout.height
38 import androidx.glance.layout.padding
39 import androidx.glance.layout.width
40 import androidx.glance.text.Text
41 import androidx.glance.text.TextAlign
42 import androidx.glance.text.TextDecoration
43 import androidx.glance.text.TextStyle
44 
45 class ResizingAppWidget : GlanceAppWidget() {
46 
47     override val sizeMode: SizeMode = SizeMode.Single
48 
<lambda>null49     override suspend fun provideGlance(context: Context, id: GlanceId) = provideContent {
50         Content()
51     }
52 
<lambda>null53     override suspend fun providePreview(context: Context, widgetCategory: Int) = provideContent {
54         Content()
55     }
56 
57     @Composable
Contentnull58     private fun Content() {
59         Column(modifier = GlanceModifier.fillMaxSize().padding(16.dp).background(Color.LightGray)) {
60             Row(modifier = GlanceModifier.fillMaxWidth()) {
61                 Text(
62                     "first",
63                     modifier =
64                         GlanceModifier.width(50.dp).background(Color(0xFFBBBBBB)).clickable {
65                             Log.i("GlanceAppWidget", "first clicked")
66                         },
67                     style = TextStyle(textAlign = TextAlign.Start)
68                 )
69                 Text(
70                     "second",
71                     style =
72                         TextStyle(
73                             textDecoration = TextDecoration.LineThrough,
74                             textAlign = TextAlign.Center,
75                         ),
76                     modifier =
77                         GlanceModifier.defaultWeight().height(50.dp).clickable {
78                             Log.i("GlanceAppWidget", "second clicked")
79                         },
80                 )
81                 Text(
82                     "third",
83                     modifier =
84                         GlanceModifier.width(50.dp).background(Color(0xFFBBBBBB)).clickable {
85                             Log.i("GlanceAppWidget", "third clicked")
86                         },
87                     style = TextStyle(textAlign = TextAlign.End)
88                 )
89             }
90             Text(
91                 "middle",
92                 modifier =
93                     GlanceModifier.defaultWeight()
94                         .fillMaxWidth()
95                         .clickable { Log.i("GlanceAppWidget", "middle clicked") }
96                         .background(ImageProvider(R.drawable.compose)),
97                 style = TextStyle(textAlign = TextAlign.Center)
98             )
99             Column(modifier = GlanceModifier.fillMaxWidth().background(Color.LightGray)) {
100                 Text(
101                     "left",
102                     style = TextStyle(textAlign = TextAlign.Left),
103                     modifier = GlanceModifier.fillMaxWidth()
104                 )
105                 Text(
106                     "right",
107                     style = TextStyle(textAlign = TextAlign.Right),
108                     modifier = GlanceModifier.fillMaxWidth()
109                 )
110                 Text(
111                     "start",
112                     style = TextStyle(textAlign = TextAlign.Start),
113                     modifier = GlanceModifier.fillMaxWidth()
114                 )
115                 Text(
116                     "end",
117                     style = TextStyle(textAlign = TextAlign.End),
118                     modifier = GlanceModifier.fillMaxWidth()
119                 )
120             }
121             Row(modifier = GlanceModifier.fillMaxWidth()) {
122                 Text("", modifier = GlanceModifier.defaultWeight())
123                 Text("bottom center")
124                 Text("", modifier = GlanceModifier.defaultWeight())
125             }
126         }
127     }
128 }
129 
130 class ResizingAppWidgetReceiver : GlanceAppWidgetReceiver() {
131     override val glanceAppWidget: GlanceAppWidget = ResizingAppWidget()
132 }
133