|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | + * https://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 com.example.compose.snippets.glance |
| 18 | + |
| 19 | +import android.content.Context |
| 20 | +import androidx.compose.material3.Button |
| 21 | +import androidx.compose.runtime.Composable |
| 22 | +import androidx.compose.runtime.rememberCoroutineScope |
| 23 | +import androidx.compose.ui.platform.LocalContext |
| 24 | +import androidx.compose.ui.unit.DpSize |
| 25 | +import androidx.compose.ui.unit.dp |
| 26 | +import androidx.glance.GlanceId |
| 27 | +import androidx.glance.appwidget.GlanceAppWidget |
| 28 | +import androidx.glance.appwidget.GlanceAppWidgetManager |
| 29 | +import androidx.glance.appwidget.GlanceAppWidgetReceiver |
| 30 | +import kotlinx.coroutines.launch |
| 31 | + |
| 32 | +class MyWidgetReceiver : GlanceAppWidgetReceiver() { |
| 33 | + override val glanceAppWidget: GlanceAppWidget = MyWidget() |
| 34 | +} |
| 35 | + |
| 36 | +class MyWidget : GlanceAppWidget() { |
| 37 | + override suspend fun provideGlance( |
| 38 | + context: Context, |
| 39 | + id: GlanceId |
| 40 | + ) {} |
| 41 | +} |
| 42 | + |
| 43 | +// [START android_compose_glance_in_app_pinning] |
| 44 | +@Composable |
| 45 | +fun AnInAppComposable() { |
| 46 | + val context = LocalContext.current |
| 47 | + val coroutineScope = rememberCoroutineScope() |
| 48 | + Button( |
| 49 | + onClick = { |
| 50 | + coroutineScope.launch { |
| 51 | + GlanceAppWidgetManager(context).requestPinGlanceAppWidget( |
| 52 | + receiver = MyWidgetReceiver::class.java, |
| 53 | + preview = MyWidget(), |
| 54 | + previewState = DpSize(245.dp, 115.dp) |
| 55 | + ) |
| 56 | + } |
| 57 | + } |
| 58 | + ) {} |
| 59 | +} |
| 60 | +// [END android_compose_glance_in_app_pinning] |
0 commit comments