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 androidx.compose.runtime.Composable 21 import androidx.compose.ui.graphics.Color 22 import androidx.glance.GlanceId 23 import androidx.glance.GlanceModifier 24 import androidx.glance.LocalSize 25 import androidx.glance.appwidget.GlanceAppWidget 26 import androidx.glance.appwidget.GlanceAppWidgetReceiver 27 import androidx.glance.appwidget.SizeMode 28 import androidx.glance.appwidget.background 29 import androidx.glance.appwidget.cornerRadius 30 import androidx.glance.appwidget.provideContent 31 import androidx.glance.color.ColorProvider 32 import androidx.glance.layout.Column 33 import androidx.glance.layout.fillMaxSize 34 import androidx.glance.layout.padding 35 import androidx.glance.text.FontWeight 36 import androidx.glance.text.Text 37 import androidx.glance.text.TextDecoration 38 import androidx.glance.text.TextStyle 39 import java.text.DecimalFormat 40 41 class ExactAppWidget : GlanceAppWidget() { 42 override val sizeMode: SizeMode = SizeMode.Exact 43 <lambda>null44 override suspend fun provideGlance(context: Context, id: GlanceId) = provideContent { 45 Content(context) 46 } 47 <lambda>null48 override suspend fun providePreview(context: Context, widgetCategory: Int) = provideContent { 49 Content(context) 50 } 51 52 @Composable Contentnull53 private fun Content(context: Context) { 54 Column( 55 modifier = 56 GlanceModifier.fillMaxSize() 57 .background(day = Color.LightGray, night = Color.DarkGray) 58 .padding(R.dimen.external_padding) 59 .cornerRadius(R.dimen.corner_radius) 60 ) { 61 Text( 62 context.getString(R.string.exact_widget_title), 63 style = 64 TextStyle( 65 color = ColorProvider(day = Color.DarkGray, night = Color.LightGray), 66 fontWeight = FontWeight.Bold, 67 textDecoration = TextDecoration.Underline 68 ), 69 ) 70 val size = LocalSize.current 71 val dec = DecimalFormat("#.##") 72 val width = dec.format(size.width.value) 73 val height = dec.format(size.height.value) 74 Text("Current layout: ${width}dp x ${height}dp") 75 for (i in 0 until 20) { 76 Text("Text $i") 77 } 78 } 79 } 80 } 81 82 class ExactAppWidgetReceiver : GlanceAppWidgetReceiver() { 83 override val glanceAppWidget = ExactAppWidget() 84 } 85