A cross platform Audiobookshelf client for Android and iOS
Dart
Make the widget cover sharper and stop fading it out over dark artwork
Barnabas13680be2698a
diff --git a/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt b/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.ktindex a37a413f..5ce9e47c 100644--- a/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt+++ b/android/app/src/main/kotlin/com/barnabas/absorb/NowPlayingWidgetTiny.kt@@ -12,6 +12,7 @@ import android.graphics.Canvasimport android.graphics.Paintimport android.graphics.PorterDuffimport android.graphics.PorterDuffXfermode+import android.graphics.Rectimport android.graphics.RectFimport android.os.Buildimport android.os.Bundle@@ -118,6 +119,49 @@ class NowPlayingWidgetTiny : AppWidgetProvider() {context, MainActivity::class.java, Uri.parse("absorb://widget/open"))+ /// Mean brightness of the bottom slice of the cover - the part the+ /// player panel sits over. Sampled on a coarse grid rather than every+ /// pixel; this runs on every widget update and the answer only has to+ /// be right enough to pick between two scrims.+ private fun bottomBrightness(bitmap: Bitmap): Double {+ val h = bitmap.height+ val w = bitmap.width+ if (h <= 0 || w <= 0) return 0.0+ val startY = (h * 0.55).toInt().coerceIn(0, h - 1)+ val stepX = (w / 16).coerceAtLeast(1)+ val stepY = ((h - startY) / 12).coerceAtLeast(1)+ var total = 0.0+ var count = 0+ var y = startY+ while (y < h) {+ var x = 0+ while (x < w) {+ val c = bitmap.getPixel(x, y)+ if (((c shr 24) and 0xFF) > 8) {+ val r = (c shr 16) and 0xFF+ val g = (c shr 8) and 0xFF+ val b = c and 0xFF+ // Rec. 601 luma - closer to perceived brightness than a+ // plain channel average, which reads blue as too light.+ total += 0.299 * r + 0.587 * g + 0.114 * b+ count+++ }+ x += stepX+ }+ y += stepY+ }+ return if (count == 0) 0.0 else total / count+ }++ // Above this the cover is bright enough that white text needs the full+ // scrim; below it the artwork already does the job.+ // Widest cover edge handed to RemoteViews. 768px in ARGB_8888 is about+ // 2.4MB, well clear of what a widget update will carry, and still twice+ // the linear detail of the 400px the player uses.+ private const val MAX_COVER_PX = 768++ private const val SCRIM_BRIGHTNESS_THRESHOLD = 88.0+// 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.@@ -138,12 +182,29 @@ class NowPlayingWidgetTiny : AppWidgetProvider() {val displayPx = targetPx.toFloat().coerceAtLeast(1f)val scale = minOf(bitmap.width, bitmap.height).toFloat() / displayPxval radiusPx = radiusDp * density * scale- val output = Bitmap.createBitmap(bitmap.width, bitmap.height, Bitmap.Config.ARGB_8888)+ // Hard ceiling on what gets handed to RemoteViews. inSampleSize only+ // halves, so a 1000px source against a 1050px target stays at full+ // size - 1000x1000 in ARGB_8888 is 4MB, and an oversized bitmap does+ // not degrade, it makes the widget update fail silently. Scale to a+ // known-safe edge instead of hoping.+ val edge = minOf(bitmap.width, bitmap.height)+ val outEdge = minOf(edge, MAX_COVER_PX)+ val output = Bitmap.createBitmap(outEdge, outEdge, Bitmap.Config.ARGB_8888)val canvas = Canvas(output)val paint = Paint(Paint.ANTI_ALIAS_FLAG)- canvas.drawRoundRect(RectF(0f, 0f, bitmap.width.toFloat(), bitmap.height.toFloat()), radiusPx, radiusPx, paint)+ val outRadius = radiusPx * outEdge / edge.toFloat()+ canvas.drawRoundRect(RectF(0f, 0f, outEdge.toFloat(), outEdge.toFloat()), outRadius, outRadius, paint)paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)- canvas.drawBitmap(bitmap, 0f, 0f, paint)+ // Centre-crop the source square into the output, matching what the+ // ImageView would have done anyway.+ val srcLeft = (bitmap.width - edge) / 2+ val srcTop = (bitmap.height - edge) / 2+ canvas.drawBitmap(+ bitmap,+ Rect(srcLeft, srcTop, srcLeft + edge, srcTop + edge),+ RectF(0f, 0f, outEdge.toFloat(), outEdge.toFloat()),+ paint,+ )// Recycle only the source: RemoteViews serialises `output` later in// updateAppWidget, so recycling it here crashes the app.bitmap.recycle()@@ -201,6 +262,19 @@ class NowPlayingWidgetTiny : AppWidgetProvider() {views.setImageViewResource(R.id.widget_cover, R.mipmap.ic_launcher)}+ // The panel's background IS the scrim, so the taller the panel gets+ // the more artwork it fades out - worst on a big widget, which is+ // exactly where the cover is worth looking at. Match the fade to+ // the cover instead of always assuming the worst case: a dark cover+ // carries white text unaided, a pale one still needs the full wash.+ val darkCover = cover != null && bottomBrightness(cover) < SCRIM_BRIGHTNESS_THRESHOLD+ views.setInt(+ R.id.widget_panel,+ "setBackgroundResource",+ if (darkCover) R.drawable.widget_controls_scrim_soft+ else R.drawable.widget_controls_scrim,+ )+val playPauseIcon = if (isPlaying) R.drawable.ic_widget_pause_dark else R.drawable.ic_widget_play_dark// Tap registered, audio not started yet (cold start can take// seconds) - spin instead of looking dead.diff --git a/android/app/src/main/res/drawable/widget_controls_scrim_soft.xml b/android/app/src/main/res/drawable/widget_controls_scrim_soft.xmlnew file mode 100644index 00000000..76479181--- /dev/null+++ b/android/app/src/main/res/drawable/widget_controls_scrim_soft.xml@@ -0,0 +1,12 @@+<?xml version="1.0" encoding="utf-8"?>+<!-- Lighter companion to widget_controls_scrim, used when the bottom of the+ cover is already dark enough to carry white text on its own. Just enough+ to stop the panel floating, without flattening the artwork. -->+<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">+ <gradient+ android:startColor="#99000000"+ android:centerColor="#40000000"+ android:endColor="#00000000"+ android:centerY="0.5"+ android:angle="90" />+</shape>diff --git a/android/app/src/main/res/drawable/widget_play_btn_bg.xml b/android/app/src/main/res/drawable/widget_play_btn_bg.xmlindex a94ebf01..0d46cd38 100644--- a/android/app/src/main/res/drawable/widget_play_btn_bg.xml+++ b/android/app/src/main/res/drawable/widget_play_btn_bg.xml@@ -1,6 +1,12 @@<?xml version="1.0" encoding="utf-8"?>+<!-- A true pill at whatever size it is used. The radius is deliberately far+ larger than any button here: GradientDrawable clamps it to half the+ shorter side, so one drawable gives fully round ends on both the small+ centre indicator and the larger panel button. The old fixed 20dp was less+ than half the 52dp panel button's height, which is what made it read as a+ squashed rectangle rather than a pill. --><shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solid android:color="#FFFFFF" />- <corners android:radius="20dp" />+ <corners android:radius="100dp" /></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 eb7067a4..c585841b 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@@ -192,7 +192,7 @@android:layout_height="wrap_content"><ImageButtonandroid:id="@+id/widget_pp"- android:layout_width="72dp"+ android:layout_width="84dp"android:layout_height="52dp"android:padding="12dp"android:src="@drawable/ic_widget_play_dark"diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dartindex f1c09317..2cb0737d 100644--- a/lib/services/home_widget_service.dart+++ b/lib/services/home_widget_service.dart@@ -1103,6 +1103,12 @@ class HomeWidgetService {return count;}+ /// Width requested from the server for the widget's copy of the cover.+ /// Larger than the player's 400 because the widget can be far bigger than a+ /// notification icon; the Kotlin side downscales to its own safe ceiling+ /// before handing anything to RemoteViews.+ static const int _widgetCoverWidth = 1000;+Future<void> _updateCoverArt(String itemId) async {final player = AudioPlayerService();final coverUrl = player.currentCoverUrl;@@ -1130,8 +1136,18 @@ class HomeWidgetService {final coverDir = await _getCoverDirectory();final coverFile = File('${coverDir.path}/$itemId.jpg');+ // The player's cover URL asks the server for 400px, which is plenty+ // for a notification and soft on a large home screen widget - a big+ // tile is around 1000px on a modern phone, so 400 gets upscaled 2.5x.+ // Ask for a bigger one just for the widget file. The Kotlin side caps+ // what it actually hands to RemoteViews, so this only improves the+ // source it has to work from.+ final widgetCoverUrl = coverUrl.replaceAllMapped(+ RegExp(r'([?&])width=\d+'),+ (m) => '${m[1]}width=$_widgetCoverWidth',+ );final response = await http- .get(Uri.parse(coverUrl))+ .get(Uri.parse(widgetCoverUrl)).timeout(const Duration(seconds: 10));if (response.statusCode == 200 && response.bodyBytes.isNotEmpty) {await coverFile.writeAsBytes(response.bodyBytes);