-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Widget preview updates #1541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Widget preview updates #1541
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0d08da5
Adding Glance to Jetcaster Readme
a209fe0
cleanup minor size issues
12ff20e
Tweaking small widget and landscape layouts
86eae5e
Adding Dynamic color, removing unused field to disable
1dda6e9
Generated Preview and Preview Layout
d30ebbc
SpotlessApply
1698f4d
fix tint issue
5349056
Add comment to widget_preview
2c6678f
removing runBlocking
8cf60ea
Moving updateWidgetPreview coroutine out of MainActivity
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...lancewidget/src/main/java/com/example/jetcaster/glancewidget/JetcasterAppWidgetPreview.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /* | ||
| * Copyright 2024 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.jetcaster.glancewidget | ||
|
|
||
| import android.appwidget.AppWidgetManager | ||
| import android.appwidget.AppWidgetProviderInfo | ||
| import android.content.ComponentName | ||
| import android.content.Context | ||
| import android.os.Build | ||
| import android.util.Log | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.unit.DpSize | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.glance.GlanceId | ||
| import androidx.glance.GlanceModifier | ||
| import androidx.glance.GlanceTheme | ||
| import androidx.glance.Image | ||
| import androidx.glance.ImageProvider | ||
| import androidx.glance.appwidget.GlanceAppWidget | ||
| import androidx.glance.appwidget.SizeMode | ||
| import androidx.glance.appwidget.components.Scaffold | ||
| import androidx.glance.appwidget.components.SquareIconButton | ||
| import androidx.glance.appwidget.compose | ||
| import androidx.glance.appwidget.provideContent | ||
| import androidx.glance.layout.Alignment | ||
| import androidx.glance.layout.Row | ||
| import androidx.glance.layout.Spacer | ||
| import androidx.glance.layout.fillMaxSize | ||
| import androidx.glance.layout.size | ||
| import androidx.glance.layout.wrapContentSize | ||
|
|
||
| private object SizesPreview { | ||
| val medium = 56.dp | ||
| } | ||
|
|
||
| suspend fun updateWidgetPreview(context: Context) { | ||
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) { | ||
| try { | ||
| val appwidgetManager = AppWidgetManager.getInstance(context) | ||
|
|
||
| appwidgetManager.setWidgetPreview( | ||
| ComponentName(context, JetcasterAppWidgetReceiver::class.java), | ||
| AppWidgetProviderInfo.WIDGET_CATEGORY_HOME_SCREEN, | ||
| JetcasterAppWidgetPreview().compose( | ||
| context, | ||
| size = DpSize(160.dp, 64.dp) | ||
| ), | ||
| ) | ||
| } catch (e: Exception) { | ||
| Log.e(TAG, e.message, e) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class JetcasterAppWidgetPreview : GlanceAppWidget() { | ||
| override val sizeMode: SizeMode | ||
| get() = SizeMode.Exact | ||
|
|
||
| override suspend fun provideGlance(context: Context, id: GlanceId) { | ||
|
|
||
| provideContent { | ||
| GlanceTheme { | ||
| Widget() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Composable | ||
| private fun Widget() { | ||
riggaroo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Scaffold { | ||
| Row( | ||
| modifier = GlanceModifier.fillMaxSize(), | ||
| verticalAlignment = Alignment.Vertical.CenterVertically | ||
| ) { | ||
| Image( | ||
| modifier = GlanceModifier.wrapContentSize().size(SizesPreview.medium), | ||
| provider = ImageProvider(R.drawable.widget_preview_thumbnail), | ||
| contentDescription = "" | ||
| ) | ||
| Spacer(GlanceModifier.defaultWeight()) | ||
| SquareIconButton( | ||
| modifier = GlanceModifier.size(SizesPreview.medium), | ||
| imageProvider = ImageProvider(R.drawable.outline_play_arrow_24), | ||
| contentDescription = "", | ||
| onClick = { } | ||
| ) | ||
| } | ||
| } | ||
| } | ||
2 changes: 1 addition & 1 deletion
2
Jetcaster/glancewidget/src/main/res/drawable/outline_play_arrow_24.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions
20
Jetcaster/glancewidget/src/main/res/drawable/widget_preview_image_shape.xml
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this in xml? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <?xml version="1.0" encoding="utf-8"?><!-- | ||
| ~ Copyright (C) 2024 The Android Open Source Project | ||
| ~ | ||
| ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| ~ you may not use this file except in compliance with the License. | ||
| ~ You may obtain a copy of the License at | ||
| ~ | ||
| ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| ~ | ||
| ~ Unless required by applicable law or agreed to in writing, software | ||
| ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| ~ See the License for the specific language governing permissions and | ||
| ~ limitations under the License. | ||
| --> | ||
|
|
||
| <shape xmlns:android="http://schemas.android.com/apk/res/android" | ||
| android:shape="rectangle"> | ||
| <corners android:radius="16dp"/> | ||
| </shape> |
Binary file added
BIN
+324 KB
Jetcaster/glancewidget/src/main/res/drawable/widget_preview_thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be getting the style from the theme itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does, fgColor is set to GlanceTheme.colors.onPrimaryContainer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The whole TextStyle though I mean, fontSize, weight etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, but I feel like that refactor is outside of the scope of this PR. The goal was to add widget previews.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking more, yes it could, no I don't think we should.
We'd need to build a bunch of XML resources, and that's not the pattern we're tyring to demonstrate with Glance widgets. See : https://github.com/android/platform-samples/blob/main/samples/user-interface/appwidgets/src/main/java/com/example/platform/ui/appwidgets/glance/layout/collections/layout/ImageTextListLayout.kt#L383 for an example