A cross platform Audiobookshelf client for Android and iOS
Dart
Let the cover art widget resize into a full player
Barnabasab7e62ff6337
diff --git a/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt b/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.ktindex eb91df0c..1a9c5071 100644--- a/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt+++ b/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt@@ -10,9 +10,11 @@ import android.graphics.Bitmapimport android.graphics.BitmapFactoryimport android.graphics.Canvasimport android.graphics.Paint+import android.graphics.Pathimport android.graphics.PorterDuffimport android.graphics.PorterDuffXfermodeimport android.graphics.RectF+import android.os.Bundleimport android.view.KeyEventimport android.view.Viewimport android.net.Uri@@ -21,7 +23,10 @@ import es.antonborri.home_widget.HomeWidgetLaunchIntentimport es.antonborri.home_widget.HomeWidgetPluginimport java.io.File-// 1x1 widget: just the cover art. Tapping anywhere toggles play/pause.+// Adaptive now-playing widget. At 1x1 it's just the cover with a centered+// play/pause tap target. Resize it larger and a notification-style player panel+// (title / author / chapter / progress / transport) fades in over the bottom of+// the cover; rows appear as the widget grows enough to hold them.class NowPlayingWidgetTiny : AppWidgetProvider() {override fun onUpdate(@@ -39,6 +44,21 @@ class NowPlayingWidgetTiny : AppWidgetProvider() {}}+ // Re-render when the user resizes so the player panel appears/disappears+ // and the cover is decoded at a resolution that fits the new size.+ override fun onAppWidgetOptionsChanged(+ context: Context,+ appWidgetManager: AppWidgetManager,+ appWidgetId: Int,+ newOptions: Bundle+ ) {+ try {+ updateWidget(context, appWidgetManager, appWidgetId)+ } catch (e: Exception) {+ android.util.Log.e("NowPlayingWidgetTiny", "resize update failed", e)+ }+ }+override fun onReceive(context: Context, intent: Intent) {if (intent.action == ACTION_TOGGLE_PLAYBACK) {// Optimistic UI: flip the icon immediately, then send the real media button.@@ -64,23 +84,129 @@ class NowPlayingWidgetTiny : AppWidgetProvider() {companion object {const val ACTION_TOGGLE_PLAYBACK = "com.barnabas.absorb.ACTION_TOGGLE_PLAYBACK_TINY"- private fun roundBitmap(bitmap: Bitmap, radiusDp: Float, context: Context): Bitmap {- val density = context.resources.displayMetrics.density- // ~80dp target tile; scale the radius to the source bitmap so corners- // look right after centerCrop.- val displayPx = 80f * density+ // Android's own cell-size formula: 70dp per cell, 30dp inter-cell gap.+ private fun cellsFor(dp: Int): Int = if (dp <= 0) 1 else (dp + 30) / 70++ private fun mediaButtonPendingIntent(+ context: Context,+ keyCode: Int,+ requestCode: Int+ ): PendingIntent {+ val intent = Intent(Intent.ACTION_MEDIA_BUTTON).apply {+ component = ComponentName(context, "com.ryanheise.audioservice.MediaButtonReceiver")+ putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(KeyEvent.ACTION_DOWN, keyCode))+ }+ return PendingIntent.getBroadcast(+ context, requestCode, intent,+ PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT+ )+ }++ // Active-session play/pause: flip the icon instantly via our receiver,+ // which then forwards to the MediaSession.+ private fun togglePendingIntent(context: Context): PendingIntent {+ val toggleIntent = Intent(context, NowPlayingWidgetTiny::class.java).apply {+ action = ACTION_TOGGLE_PLAYBACK+ }+ return PendingIntent.getBroadcast(+ context, 1, toggleIntent,+ PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT+ )+ }++ private fun openAppPendingIntent(context: Context): PendingIntent =+ HomeWidgetLaunchIntent.getActivity(+ context, MainActivity::class.java, Uri.parse("absorb://widget/open")+ )++ // Decode the cover no larger than the widget needs, then mask rounded+ // corners. targetPx keeps a big cover crisp without decoding a huge+ // bitmap that RemoteViews would refuse to serialise.+ private fun loadCover(path: String, targetPx: Int, radiusDp: Float, density: Float): Bitmap? {+ val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }+ BitmapFactory.decodeFile(path, bounds)+ if (bounds.outWidth <= 0 || bounds.outHeight <= 0) return null++ val srcEdge = minOf(bounds.outWidth, bounds.outHeight)+ var sample = 1+ while (srcEdge / (sample * 2) >= targetPx) sample *= 2++ val opts = BitmapFactory.Options().apply { inSampleSize = sample }+ val bitmap = BitmapFactory.decodeFile(path, opts) ?: return null++ // Scale the corner radius to the decoded bitmap so it looks right+ // after the ImageView's centerCrop maps it onto the tile.+ val displayPx = targetPx.toFloat().coerceAtLeast(1f)val scale = minOf(bitmap.width, bitmap.height).toFloat() / displayPxval radiusPx = radiusDp * density * scaleval output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)val canvas = Canvas(output)val paint = Paint(Paint.ANTI_ALIAS_FLAG)- val rect = RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat())- canvas.drawRoundRect(rect, radiusPx, radiusPx, paint)+ canvas.drawRoundRect(RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat()), radiusPx, radiusPx, paint)paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)canvas.drawBitmap(bitmap, 0f, 0f, paint)+ // Recycle only the source: RemoteViews serialises `output` later in+ // updateAppWidget, so recycling it here crashes the app.+ bitmap.recycle()return output}+ // Progress bar drawn by hand so we can give it the Android-Auto squiggle:+ // the played portion waves while playing and flattens when paused, the+ // rest stays a dim straight track. The wave is static, not animated.+ private fun drawProgressBar(+ widthPx: Int,+ heightPx: Int,+ fraction: Float,+ playing: Boolean,+ density: Float+ ): Bitmap {+ val w = widthPx.coerceAtLeast(1)+ val h = heightPx.coerceAtLeast(1)+ val bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)+ val canvas = Canvas(bmp)+ val centerY = h / 2f+ val stroke = 2.5f * density+ val inset = stroke+ val progressX = (w * fraction.coerceIn(0f, 1f)).coerceIn(inset, w - inset)++ val trackPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {+ color = 0x66FFFFFF+ style = Paint.Style.STROKE+ strokeWidth = stroke+ strokeCap = Paint.Cap.ROUND+ }+ val activePaint = Paint(Paint.ANTI_ALIAS_FLAG).apply {+ color = 0xFFFFFFFF.toInt()+ style = Paint.Style.STROKE+ strokeWidth = stroke+ strokeCap = Paint.Cap.ROUND+ }++ // Remaining track: straight, dim.+ canvas.drawLine(progressX, centerY, w - inset, centerY, trackPaint)++ // Played portion.+ if (playing) {+ val amplitude = 3f * density+ val wavelength = 24f * density+ val path = Path()+ path.moveTo(inset, centerY)+ var x = inset+ while (x < progressX) {+ val y = centerY + amplitude * Math.sin((x / wavelength) * 2.0 * Math.PI).toFloat()+ path.lineTo(x, y)+ x += 1.5f+ }+ path.lineTo(progressX, centerY + amplitude * Math.sin((progressX / wavelength) * 2.0 * Math.PI).toFloat())+ canvas.drawPath(path, activePaint)+ } else {+ canvas.drawLine(inset, centerY, progressX, centerY, activePaint)+ }++ return bmp+ }+fun updateWidget(context: Context,appWidgetManager: AppWidgetManager,@@ -93,53 +219,121 @@ class NowPlayingWidgetTiny : AppWidgetProvider() {val isPlaying = widgetData.getBoolean("widget_is_playing", false)val coverPath = widgetData.getString("widget_cover_path", null)+ // Current widget size. Portrait reports width via MIN_WIDTH and+ // 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 density = context.resources.displayMetrics.density+ val targetPx = (maxOf(widthDp, heightDp, 80) * density).toInt()++ val cellsW = cellsFor(widthDp)+ val cellsH = cellsFor(heightDp)+ // Wide enough (2+ cells) for the transport row, and a session to control.+ val showPanel = hasBook && cellsW >= 2+ // Tall enough to also carry the title/author, then chapter.+ val showInfo = showPanel && cellsH >= 2+ val showChapter = showPanel && cellsH >= 3+// Cover art (rounded corners)- var setCover = false+ var cover: Bitmap? = nullif (coverPath != null) {val file = File(coverPath)if (file.exists()) {- val options = BitmapFactory.Options().apply { inSampleSize = 2 }- val bitmap = BitmapFactory.decodeFile(file.absolutePath, options)- if (bitmap != null) {- val rounded = roundBitmap(bitmap, 16f, context)- views.setImageViewBitmap(R.id.widget_cover, rounded)- // Recycle only the source: RemoteViews serialises `rounded`- // later in updateAppWidget, so recycling it here crashes the app.- bitmap.recycle()- setCover = true- }+ cover = loadCover(file.absolutePath, targetPx, 16f, density)}}- if (!setCover) views.setImageViewResource(R.id.widget_cover, R.mipmap.ic_launcher)-- // Play/pause indicator - only when there's a session to control.- if (hasBook) {- views.setViewVisibility(R.id.widget_play_pause, View.VISIBLE)- views.setImageViewResource(- R.id.widget_play_pause,- if (isPlaying) R.drawable.ic_widget_pause_dark else R.drawable.ic_widget_play_dark- )+ if (cover != null) {+ views.setImageViewBitmap(R.id.widget_cover, cover)} else {- views.setViewVisibility(R.id.widget_play_pause, View.GONE)+ views.setImageViewResource(R.id.widget_cover, R.mipmap.ic_launcher)}- // Tap anywhere on the tile: toggle if there's a session, else open the app.- val tapIntent = if (hasBook) {- val toggleIntent = Intent(context, NowPlayingWidgetTiny::class.java).apply {- action = ACTION_TOGGLE_PLAYBACK+ val playPauseIcon = if (isPlaying) R.drawable.ic_widget_pause_dark else R.drawable.ic_widget_play_dark++ if (showPanel) {+ // Larger size: hide the centered dot, show the player panel.+ views.setViewVisibility(R.id.widget_play_pause, View.GONE)+ views.setViewVisibility(R.id.widget_panel, View.VISIBLE)++ // When it's only tall enough for the button row, keep the panel+ // tight; give it breathing room once text/progress appear.+ val padTop = ((if (showInfo) 22 else 8) * density).toInt()+ val padBottom = ((if (showInfo) 12 else 8) * density).toInt()+ views.setViewPadding(R.id.widget_panel, 0, padTop, 0, padBottom)++ // Transport is always in the panel.+ views.setTextViewText(R.id.widget_skip_back_text, widgetData.getInt("widget_skip_back", 10).toString())+ views.setTextViewText(R.id.widget_skip_forward_text, widgetData.getInt("widget_skip_forward", 30).toString())+ views.setImageViewResource(R.id.widget_pp, playPauseIcon)++ // Progress + title/author appear once there's vertical room;+ // at 1 cell tall it's just the buttons.+ if (showInfo) {+ val progress = widgetData.getInt("widget_progress", 0)+ val barW = ((widthDp - 36).coerceAtLeast(40) * density).toInt()+ val barH = (12 * density).toInt()+ views.setImageViewBitmap(+ R.id.widget_progress,+ drawProgressBar(barW, barH, progress / 1000f, isPlaying, density)+ )+ views.setViewVisibility(R.id.widget_progress, View.VISIBLE)++ val title = widgetData.getString("widget_title", null)+ val author = widgetData.getString("widget_author", null)+ views.setTextViewText(R.id.widget_title, if (title.isNullOrEmpty()) "Absorb" else title)+ views.setViewVisibility(R.id.widget_title, View.VISIBLE)+ if (author.isNullOrEmpty()) {+ views.setViewVisibility(R.id.widget_author, View.GONE)+ } else {+ views.setTextViewText(R.id.widget_author, author)+ views.setViewVisibility(R.id.widget_author, View.VISIBLE)+ }+ } else {+ views.setViewVisibility(R.id.widget_progress, View.GONE)+ views.setViewVisibility(R.id.widget_title, View.GONE)+ views.setViewVisibility(R.id.widget_author, View.GONE)}- PendingIntent.getBroadcast(- context, 1, toggleIntent,- PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT++ val chapter = widgetData.getString("widget_chapter", null)+ if (showChapter && !chapter.isNullOrEmpty()) {+ views.setTextViewText(R.id.widget_chapter, chapter)+ views.setViewVisibility(R.id.widget_chapter, View.VISIBLE)+ } else {+ views.setViewVisibility(R.id.widget_chapter, View.GONE)+ }++ views.setOnClickPendingIntent(+ R.id.widget_skip_back,+ mediaButtonPendingIntent(context, KeyEvent.KEYCODE_MEDIA_REWIND, 22))- } else {- HomeWidgetLaunchIntent.getActivity(- context,- MainActivity::class.java,- Uri.parse("absorb://widget/play_pause")+ views.setOnClickPendingIntent(R.id.widget_pp, togglePendingIntent(context))+ views.setOnClickPendingIntent(+ R.id.widget_skip_forward,+ mediaButtonPendingIntent(context, KeyEvent.KEYCODE_MEDIA_FAST_FORWARD, 23))++ // Tapping the cover itself opens the app (buttons handle playback).+ views.setOnClickPendingIntent(R.id.widget_root, openAppPendingIntent(context))+ } else {+ // 1x1: centered indicator, whole tile toggles play/pause.+ views.setViewVisibility(R.id.widget_panel, View.GONE)+ if (hasBook) {+ views.setViewVisibility(R.id.widget_play_pause, View.VISIBLE)+ views.setImageViewResource(R.id.widget_play_pause, playPauseIcon)+ } else {+ views.setViewVisibility(R.id.widget_play_pause, View.GONE)+ }++ val tapIntent = if (hasBook) {+ togglePendingIntent(context)+ } else {+ HomeWidgetLaunchIntent.getActivity(+ context, MainActivity::class.java, Uri.parse("absorb://widget/play_pause")+ )+ }+ views.setOnClickPendingIntent(R.id.widget_root, tapIntent)}- views.setOnClickPendingIntent(R.id.widget_root, tapIntent)appWidgetManager.updateAppWidget(appWidgetId, views)}diff --git a/android/app/src/main/res/drawable/widget_controls_scrim.xml b/android/app/src/main/res/drawable/widget_controls_scrim.xmlnew file mode 100644index 00000000..a3429964--- /dev/null+++ b/android/app/src/main/res/drawable/widget_controls_scrim.xml@@ -0,0 +1,11 @@+<?xml version="1.0" encoding="utf-8"?>+<!-- Bottom fade behind the player panel so titles, progress and controls+ stay legible over any cover. Dark at the bottom, clear at the top. -->+<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">+ <gradient+ android:startColor="#EE000000"+ android:centerColor="#99000000"+ android:endColor="#00000000"+ android:centerY="0.5"+ android:angle="90" />+</shape>diff --git a/android/app/src/main/res/layout/now_playing_widget_tiny.xml b/android/app/src/main/res/layout/now_playing_widget_tiny.xmlindex 9159fd46..2e0a4315 100644--- a/android/app/src/main/res/layout/now_playing_widget_tiny.xml+++ b/android/app/src/main/res/layout/now_playing_widget_tiny.xml@@ -1,5 +1,8 @@<?xml version="1.0" encoding="utf-8"?>-<!-- Tiny 1x1 now-playing widget: cover art, tap anywhere toggles play/pause -->+<!-- Adaptive now-playing widget: full-bleed cover. At 1x1 it's cover + a+ centered play/pause tap target. Resize it larger and a notification-style+ player panel (title / author / chapter / progress / transport) fades in+ over the bottom of the cover; rows appear as there's room for them. --><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/widget_root"android:layout_width="match_parent"@@ -15,7 +18,7 @@android:src="@mipmap/ic_launcher"android:contentDescription="Cover art" />- <!-- Subtle centered play/pause indicator (reflects state; whole tile is tappable) -->+ <!-- Tiny-size indicator: reflects state; the whole tile is the tap target --><ImageViewandroid:id="@+id/widget_play_pause"android:layout_width="40dp"@@ -27,4 +30,151 @@android:scaleType="centerInside"android:contentDescription="Play or pause" />+ <!-- Player panel pinned to the bottom of the cover. The scrim is the panel's+ own background so it always wraps whatever rows are visible. -->+ <LinearLayout+ android:id="@+id/widget_panel"+ android:layout_width="match_parent"+ android:layout_height="wrap_content"+ android:layout_gravity="bottom"+ android:orientation="vertical"+ android:background="@drawable/widget_controls_scrim"+ android:paddingTop="22dp"+ android:paddingBottom="12dp"+ android:visibility="gone">++ <TextView+ android:id="@+id/widget_title"+ android:layout_width="match_parent"+ android:layout_height="wrap_content"+ android:paddingStart="18dp"+ android:paddingEnd="18dp"+ android:textColor="#FFFFFFFF"+ android:textSize="15sp"+ android:textStyle="bold"+ android:maxLines="1"+ android:ellipsize="end"+ android:fontFamily="sans-serif-medium"+ android:visibility="gone" />++ <TextView+ android:id="@+id/widget_author"+ android:layout_width="match_parent"+ android:layout_height="wrap_content"+ android:paddingStart="18dp"+ android:paddingEnd="18dp"+ android:textColor="#CCFFFFFF"+ android:textSize="12sp"+ android:maxLines="1"+ android:ellipsize="end"+ android:visibility="gone" />++ <TextView+ android:id="@+id/widget_chapter"+ android:layout_width="match_parent"+ android:layout_height="wrap_content"+ android:paddingStart="18dp"+ android:paddingEnd="18dp"+ android:paddingTop="1dp"+ android:textColor="#99FFFFFF"+ android:textSize="11sp"+ android:maxLines="1"+ android:ellipsize="end"+ android:visibility="gone" />++ <!-- Progress is drawn as a bitmap (see drawProgressBar): a static+ Android-Auto-style squiggle while playing, flat when paused.+ fitXY fills the panel width exactly, so the played fraction+ lines up regardless of our width estimate. -->+ <ImageView+ android:id="@+id/widget_progress"+ android:layout_width="match_parent"+ android:layout_height="12dp"+ android:layout_marginStart="18dp"+ android:layout_marginEnd="18dp"+ android:layout_marginTop="6dp"+ android:layout_marginBottom="2dp"+ android:scaleType="fitXY"+ android:contentDescription="Progress"+ android:visibility="gone" />++ <!-- Transport row: skip buttons pushed toward the edges, play/pause+ centered. Weighted so the cluster spreads across the full width. -->+ <LinearLayout+ android:id="@+id/widget_controls"+ android:layout_width="match_parent"+ android:layout_height="wrap_content"+ android:layout_marginTop="6dp"+ android:paddingStart="12dp"+ android:paddingEnd="12dp"+ android:orientation="horizontal"+ android:gravity="center_vertical">++ <!-- Skip back: arrow icon with duration overlay -->+ <FrameLayout+ android:id="@+id/widget_skip_back"+ android:layout_width="0dp"+ android:layout_height="52dp"+ android:layout_weight="1"+ android:contentDescription="Skip backward">+ <ImageView+ android:layout_width="34dp"+ android:layout_height="34dp"+ android:layout_gravity="center"+ android:src="@drawable/ic_widget_skip_back"+ android:scaleType="centerInside" />+ <TextView+ android:id="@+id/widget_skip_back_text"+ android:layout_width="wrap_content"+ android:layout_height="wrap_content"+ android:layout_gravity="center"+ android:layout_marginTop="2dp"+ android:text="10"+ android:textColor="#FFFFFFFF"+ android:textSize="9sp"+ android:textStyle="bold"+ android:fontFamily="sans-serif-medium" />+ </FrameLayout>++ <!-- Play/pause: white circle with dark icon -->+ <ImageButton+ android:id="@+id/widget_pp"+ android:layout_width="52dp"+ android:layout_height="52dp"+ android:padding="12dp"+ android:src="@drawable/ic_widget_play_dark"+ android:background="@drawable/widget_play_btn_bg"+ android:contentDescription="Play or pause"+ android:scaleType="centerInside" />++ <!-- Skip forward: arrow icon with duration overlay -->+ <FrameLayout+ android:id="@+id/widget_skip_forward"+ android:layout_width="0dp"+ android:layout_height="52dp"+ android:layout_weight="1"+ android:contentDescription="Skip forward">+ <ImageView+ android:layout_width="34dp"+ android:layout_height="34dp"+ android:layout_gravity="center"+ android:src="@drawable/ic_widget_skip_forward"+ android:scaleType="centerInside" />+ <TextView+ android:id="@+id/widget_skip_forward_text"+ android:layout_width="wrap_content"+ android:layout_height="wrap_content"+ android:layout_gravity="center"+ android:layout_marginTop="2dp"+ android:text="30"+ android:textColor="#FFFFFFFF"+ android:textSize="9sp"+ android:textStyle="bold"+ android:fontFamily="sans-serif-medium" />+ </FrameLayout>++ </LinearLayout>++ </LinearLayout>+</FrameLayout>diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xmlindex 337e3617..2195086f 100644--- a/android/app/src/main/res/values/strings.xml+++ b/android/app/src/main/res/values/strings.xml@@ -2,6 +2,6 @@<resources><string name="widget_description">Shows the currently playing audiobook with playback controls.</string><string name="widget_compact_description">Compact now-playing bar with playback controls.</string>- <string name="widget_tiny_description">Cover art only - tap to play or pause.</string>+ <string name="widget_tiny_description">Cover art - tap to play or pause. Resize it larger for a full player.</string><string name="widget_stats_description">Listening stats: today, this week, streak, and books finished this year.</string></resources>diff --git a/android/app/src/main/res/xml/now_playing_widget_tiny_info.xml b/android/app/src/main/res/xml/now_playing_widget_tiny_info.xmlindex 4d509368..219587a8 100644--- a/android/app/src/main/res/xml/now_playing_widget_tiny_info.xml+++ b/android/app/src/main/res/xml/now_playing_widget_tiny_info.xml@@ -3,9 +3,13 @@android:initialLayout="@layout/now_playing_widget_tiny"android:minWidth="40dp"android:minHeight="40dp"+ android:minResizeWidth="40dp"+ android:minResizeHeight="40dp"+ android:maxResizeWidth="400dp"+ android:maxResizeHeight="400dp"android:targetCellWidth="1"android:targetCellHeight="1"- android:resizeMode="none"+ android:resizeMode="horizontal|vertical"android:widgetCategory="home_screen"android:previewImage="@mipmap/ic_launcher"android:previewLayout="@layout/now_playing_widget_tiny"diff --git a/pubspec.yaml b/pubspec.yamlindex 0656389f..9a693edd 100644--- a/pubspec.yaml+++ b/pubspec.yaml@@ -1,7 +1,7 @@name: absorbdescription: An Audiobookshelf Clientpublish_to: 'none'-version: 1.9.1+202+version: 1.9.1+203environment:sdk: '>=3.10.0 <4.0.0'