Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fit the one row widgets on launchers with short rows like Nothing and OnePlus

Barnabas4c8185ec487f
diff --git a/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetCompact.kt b/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetCompact.kt
index a87d9313..27d27964 100644
--- a/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetCompact.kt
+++ b/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetCompact.kt
@@ -17,6 +17,8 @@ import android.view.KeyEvent
import android.view.View
import android.net.Uri
import android.os.Build
+import android.os.Bundle
+import android.util.TypedValue
import android.widget.RemoteViews
import es.antonborri.home_widget.HomeWidgetLaunchIntent
import es.antonborri.home_widget.HomeWidgetPlugin
@@ -39,6 +41,21 @@ class NowPlayingWidgetCompact : AppWidgetProvider() {
}
}
+ // Re-render on resize so the short-row layout follows the height the
+ // launcher actually gives the widget.
+ override fun onAppWidgetOptionsChanged(
+ context: Context,
+ appWidgetManager: AppWidgetManager,
+ appWidgetId: Int,
+ newOptions: Bundle
+ ) {
+ try {
+ updateWidget(context, appWidgetManager, appWidgetId)
+ } catch (e: Exception) {
+ android.util.Log.e("NowPlayingWidgetCompact", "resize update failed", e)
+ }
+ }
+
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
WidgetClock.syncTicker(context)
}
@@ -110,6 +127,25 @@ class NowPlayingWidgetCompact : AppWidgetProvider() {
views.setViewPadding(R.id.widget_outer, 0, 0, 0, 0)
}
+ // A launcher row shorter than the layout was drawn for: pull the
+ // vertical slack out and shrink the transport so the title and
+ // the buttons both fit instead of the pill being cut off.
+ val heightDp = WidgetClock.portraitHeightDp(appWidgetManager.getAppWidgetOptions(appWidgetId))
+ val shortRow = heightDp in 1 until WidgetClock.SHORT_ROW_MAX_HEIGHT_DP
+ if (shortRow) {
+ val density = context.resources.displayMetrics.density
+ val pad = (2 * density).toInt()
+ views.setViewPadding(R.id.widget_text_col, 0, pad, 0, pad)
+ views.setTextViewTextSize(R.id.widget_title, TypedValue.COMPLEX_UNIT_SP, 12f)
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
+ views.setViewLayoutMargin(R.id.widget_controls, RemoteViews.MARGIN_TOP, 2f, TypedValue.COMPLEX_UNIT_DIP)
+ views.setViewLayoutHeight(R.id.widget_play_pause, 32f, TypedValue.COMPLEX_UNIT_DIP)
+ views.setViewLayoutWidth(R.id.widget_play_pause, 48f, TypedValue.COMPLEX_UNIT_DIP)
+ views.setViewLayoutHeight(R.id.widget_skip_back, 32f, TypedValue.COMPLEX_UNIT_DIP)
+ views.setViewLayoutHeight(R.id.widget_skip_forward, 32f, TypedValue.COMPLEX_UNIT_DIP)
+ }
+ }
+
val title = widgetData.getString("widget_title", null)
val hasBook = widgetData.getBoolean("widget_has_book", false)
val canControl = (hasBook || !title.isNullOrEmpty()) && WidgetClock.isEngineAlive()
diff --git a/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt b/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt
index c4d4f560..b000ea2c 100644
--- a/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt
+++ b/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt
@@ -235,7 +235,10 @@ class NowPlayingWidgetTiny : AppWidgetProvider() {
// height via MAX_HEIGHT; use the largest edge to size the cover.
val options = appWidgetManager.getAppWidgetOptions(appWidgetId)
val widthDp = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, 40)
- val heightDp = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, 40)
+ val heightDp = WidgetClock.portraitHeightDp(options).let { if (it > 0) it else 40 }
+ // Launchers with short rows (Nothing, OnePlus) give a 1-row widget
+ // too little height for title + progress + transport at once.
+ val shortRow = heightDp < WidgetClock.SHORT_ROW_MAX_HEIGHT_DP
val density = context.resources.displayMetrics.density
val targetPx = (maxOf(widthDp, heightDp, 80) * density).toInt()
@@ -287,7 +290,7 @@ class NowPlayingWidgetTiny : AppWidgetProvider() {
// When it's only tall enough for the button row, keep the panel
// tight; give it breathing room once text/progress appear.
- val pad = ((if (showInfo) 22 else 4) * density).toInt()
+ val pad = ((if (showInfo) 22 else if (shortRow) 2 else 4) * density).toInt()
views.setViewPadding(R.id.widget_panel, 0, pad, 0, pad)
// The short tier now also carries a title line (see below), so
@@ -297,10 +300,14 @@ class NowPlayingWidgetTiny : AppWidgetProvider() {
// also exactly what the pill drawable's corner radius is tuned
// for.
if (!showInfo && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
- views.setViewLayoutHeight(R.id.widget_pp, 40f, TypedValue.COMPLEX_UNIT_DIP)
- views.setViewLayoutWidth(R.id.widget_pp, 56f, TypedValue.COMPLEX_UNIT_DIP)
- views.setViewLayoutHeight(R.id.widget_skip_back, 40f, TypedValue.COMPLEX_UNIT_DIP)
- views.setViewLayoutHeight(R.id.widget_skip_forward, 40f, TypedValue.COMPLEX_UNIT_DIP)
+ val h = if (shortRow) 32f else 40f
+ views.setViewLayoutHeight(R.id.widget_pp, h, TypedValue.COMPLEX_UNIT_DIP)
+ views.setViewLayoutWidth(R.id.widget_pp, if (shortRow) 48f else 56f, TypedValue.COMPLEX_UNIT_DIP)
+ views.setViewLayoutHeight(R.id.widget_skip_back, h, TypedValue.COMPLEX_UNIT_DIP)
+ views.setViewLayoutHeight(R.id.widget_skip_forward, h, TypedValue.COMPLEX_UNIT_DIP)
+ if (shortRow) {
+ views.setViewLayoutMargin(R.id.widget_controls, RemoteViews.MARGIN_TOP, 2f, TypedValue.COMPLEX_UNIT_DIP)
+ }
}
// Transport + progress are in every panel size, including the
@@ -324,7 +331,8 @@ class NowPlayingWidgetTiny : AppWidgetProvider() {
R.id.widget_progress,
WidgetClock.drawProgressBar(WidgetClock.tinyBarWidthPx(widthDp, showTimes, density), (12 * density).toInt(), fraction, isPlaying, density)
)
- views.setViewVisibility(R.id.widget_progress_row, View.VISIBLE)
+ // On a short row the bar is the line that does not fit.
+ views.setViewVisibility(R.id.widget_progress_row, if (shortRow && !showInfo) View.GONE else View.VISIBLE)
// Elapsed / remaining clocks flank the bar on wide widgets;
// the per-second ticker keeps them live while playing.
diff --git a/android/app/src/main/kotlin/com/barnabas/absorb/WidgetClock.kt b/android/app/src/main/kotlin/com/barnabas/absorb/WidgetClock.kt
index 68314be0..0c11c83f 100644
--- a/android/app/src/main/kotlin/com/barnabas/absorb/WidgetClock.kt
+++ b/android/app/src/main/kotlin/com/barnabas/absorb/WidgetClock.kt
@@ -33,6 +33,20 @@ internal object WidgetClock {
// Android's own cell-size formula: 70dp per cell, 30dp inter-cell gap.
fun cellsFor(dp: Int): Int = if (dp <= 0) 1 else (dp + 30) / 70
+ // Launcher rows are not one size: a 1-row widget is ~121dp on the Pixel,
+ // ~102dp on One UI and under 90dp on Nothing and OnePlus launchers. A
+ // row-height widget below this gets the tightened one-line layout, or
+ // the play pill is cut off at the bottom.
+ const val SHORT_ROW_MAX_HEIGHT_DP = 95
+
+ /// Portrait height in dp from the widget options (MAX_HEIGHT is the
+ /// portrait figure; fall back to the landscape one when it is missing).
+ fun portraitHeightDp(options: android.os.Bundle): Int {
+ val max = options.getInt(android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, 0)
+ if (max > 0) return max
+ return options.getInt(android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, 0)
+ }
+
// widget_has_book / widget_title are stashed prefs that outlive the app
// process, so they still read true after a swipe-close. Routing a tap
// through the MediaSession broadcast in that state is a dead end -
diff --git a/android/app/src/main/res/layout/now_playing_widget_compact.xml b/android/app/src/main/res/layout/now_playing_widget_compact.xml
index 7c50116b..e89b2590 100644
--- a/android/app/src/main/res/layout/now_playing_widget_compact.xml
+++ b/android/app/src/main/res/layout/now_playing_widget_compact.xml
@@ -33,6 +33,7 @@
<!-- Right side: title above the transport, centered as a group -->
<LinearLayout
+ android:id="@+id/widget_text_col"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"