widgets: add preview for <31

Add an image widget preview for android versions below 12.
This commit is contained in:
OxygenCobalt 2021-08-05 16:09:27 -06:00
parent b4d9a197af
commit 8c07f8cd8c
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
5 changed files with 12 additions and 11 deletions

View file

@ -133,13 +133,15 @@ class WidgetProvider : AppWidgetProvider() {
} else {
// Otherwise, we try our best to backport the responsive behavior to older versions.
// This is mostly a guess based on RemoteView's documentation, and it has some
// problems [most notably UI jittering when resizing], but it works. It may be
// improved once Android 12's source is released.
// problems [most notably UI jittering when resizing]. Its better than just using
// one layout though. It may be improved once Android 12's source is released.
// Theres a non-zero likelihood that this code ends up being copy-pasted all over
// by other apps that are trying to refresh their widgets for Android 12. So, if
// you're doing that than uh...hi.
// Each widget has independent dimensions, so we iterate through them all
// and do this for each.
val ids = getAppWidgetIds(ComponentName(context, WidgetProvider::class.java))
for (id in ids) {
@ -170,21 +172,19 @@ class WidgetProvider : AppWidgetProvider() {
}
}
// Find the layout with the greatest area. This is what we use.
val layout = candidates.maxByOrNull { it.height * it.width }
if (layout != null) {
logD("Using widget layout $layout")
updateAppWidget(
ComponentName(context, WidgetProvider::class.java),
views[layout]
)
updateAppWidget(id, views[layout])
continue
}
}
// No layout works. Just use the smallest view.
// No layout fits. Just use the smallest view.
logD("No widget layout found")

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View file

@ -15,7 +15,7 @@
android:background="?attr/colorSurface"
android:scaleType="centerCrop"
tools:ignore="contentDescription"
tools:src="@drawable/ic_song" />
android:src="@drawable/ic_song" />
<LinearLayout
android:layout_width="match_parent"
@ -36,7 +36,7 @@
android:textAppearance="@style/TextAppearance.MaterialComponents.Headline6"
android:textColor="?android:attr/textColorPrimary"
android:textStyle="bold"
tools:text="Song Name" />
android:text="@string/placeholder_widget_song" />
<TextView
android:id="@+id/widget_artist"
@ -47,7 +47,7 @@
android:singleLine="true"
android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
android:textColor="?android:attr/textColorSecondary"
tools:text="Artist Name" />
android:text="@string/placeholder_widget_artist" />
<LinearLayout
android:layout_width="match_parent"

View file

@ -4,7 +4,7 @@
<!-- Info namespace | App labels -->
<string name="info_app_desc">A simple, rational music player for android.</string>
<string name="info_channel_name">Music Playback</string>
<string name="info_channel_name">Now Playing</string>
<string name="info_widget_desc">See and control playing music</string>
<!-- Label Namespace | Static Labels -->

View file

@ -7,4 +7,5 @@
android:minResizeHeight="110dp"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="0"
android:previewImage="@drawable/ui_widget_preview"
android:widgetCategory="home_screen" />