widgets: fix layout issues

Fix an issue in the responsive layout backport where padding was not
factored into the calculations. This was the original issue that I
thought I fixed in e81d4b6d, but this fix seems to work alot better
from the launchers I tested [Trebuchet/Launcher3, Lawnchair].
This commit is contained in:
OxygenCobalt 2021-08-06 19:02:57 -06:00
parent a566a30c15
commit 3a01a13b9d
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
4 changed files with 20 additions and 15 deletions

View file

@ -1,6 +1,7 @@
package org.oxycblt.auxio.widgets package org.oxycblt.auxio.widgets
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.appwidget.AppWidgetHostView
import android.appwidget.AppWidgetManager import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider import android.appwidget.AppWidgetProvider
import android.content.ComponentName import android.content.ComponentName
@ -129,13 +130,20 @@ class WidgetProvider : AppWidgetProvider() {
height = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT) height = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT)
} }
// Widget dimens pre-12 are weird. Basically, they correspond to columns // Most of the major launchers seem to provide widget sizes that don't include
// but with 2 columns worth of DiP added for some insane reason. Take // padding. Re-add the padding to the width/height and then use that.
// the dimens, normalize them into cells, and then turn them back into dimens. val padding = AppWidgetHostView.getDefaultPaddingForWidget(
// This may result in wonky layouts, but it *seems* to work well enough context,
// in practice. ComponentName(context, WidgetProvider::class.java),
width = normalizeDimen(width) null
height = normalizeDimen(height) )
val density = context.resources.displayMetrics.density
val paddingX = ((padding.left + padding.right) / density).toInt()
val paddingY = ((padding.top + padding.bottom) / density).toInt()
width += paddingX
height += paddingY
logD("Assuming true widget dimens are ${width}x$height") logD("Assuming true widget dimens are ${width}x$height")
@ -179,6 +187,7 @@ class WidgetProvider : AppWidgetProvider() {
cells++ cells++
} }
// Change the (cells - X) value depending on your widget's minimum cells
return 70 * (cells - 2) - 30 return 70 * (cells - 2) - 30
} }

View file

@ -220,15 +220,11 @@
<item name="cornerRadius">0dp</item> <item name="cornerRadius">0dp</item>
</style> </style>
<style name="Widget.Button.Vibrant.Primary" parent="@style/Widget.Button.Vibrant.Base"> <style name="Widget.Button.Vibrant.Primary" parent="@style/Widget.Button.Vibrant.Base" />
<item name="rippleColor">?attr/colorControlHighlight</item>
<item name="fontFamily">@font/inter_semibold</item>
<item name="textAllCaps">false</item>
<item name="android:textColor">@color/surface_color</item>
</style>
<style name="Widget.Button.Vibrant.Secondary" parent="@style/Widget.Button.Vibrant.Base"> <style name="Widget.Button.Vibrant.Secondary" parent="@style/Widget.Button.Vibrant.Base">
<item name="strokeColor">@color/divider_color</item> <item name="strokeColor">@color/divider_color</item>
<item name="strokeWidth">@dimen/width_play_stroke</item> <item name="strokeWidth">@dimen/width_play_stroke</item>
<item name="android:textColor">?attr/colorAccent</item>
</style> </style>
</resources> </resources>

View file

@ -2,7 +2,7 @@
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android" <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/info_widget_desc" android:description="@string/info_widget_desc"
android:initialLayout="@layout/widget_small" android:initialLayout="@layout/widget_small"
android:minResizeWidth="110dp" android:minResizeWidth="180dp"
android:minResizeHeight="110dp" android:minResizeHeight="110dp"
android:previewLayout="@layout/widget_small" android:previewLayout="@layout/widget_small"
android:resizeMode="horizontal|vertical" android:resizeMode="horizontal|vertical"

View file

@ -3,7 +3,7 @@
android:initialLayout="@layout/widget_small" android:initialLayout="@layout/widget_small"
android:minWidth="180dp" android:minWidth="180dp"
android:minHeight="110dp" android:minHeight="110dp"
android:minResizeWidth="110dp" android:minResizeWidth="180dp"
android:minResizeHeight="110dp" android:minResizeHeight="110dp"
android:previewImage="@drawable/ui_widget_preview" android:previewImage="@drawable/ui_widget_preview"
android:resizeMode="horizontal|vertical" android:resizeMode="horizontal|vertical"