Skip to content

Native Android client for Spotify, built with Jetpack Compose

TV: the Ko-fi and GitHub Sponsors links become QR codes

kota-zo5c9f61590869
diff --git a/base/app/build.gradle.kts b/base/app/build.gradle.kts
index 6ee1b9e1..185bde0e 100644
--- a/base/app/build.gradle.kts
+++ b/base/app/build.gradle.kts
@@ -138,6 +138,8 @@ dependencies {
implementation("sh.calvin.reorderable:reorderable:3.1.0")
// Jam: scan the invite QR code (Google code scanner, no camera permission).
implementation("com.google.android.gms:play-services-code-scanner:16.1.0")
+ // Myspoti TV: the support links as QR codes (Apache-2.0), the encoder alone.
+ implementation("com.google.zxing:core:3.5.3")
testImplementation("junit:junit:4.13.2")
// The real org.json in unit tests (android.jar only has stubs).
testImplementation("org.json:json:20240303")
diff --git a/base/app/src/main/java/com/music/spotui/ui/tv/TvApp.kt b/base/app/src/main/java/com/music/spotui/ui/tv/TvApp.kt
index df1c6269..f558ba0c 100644
--- a/base/app/src/main/java/com/music/spotui/ui/tv/TvApp.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/tv/TvApp.kt
@@ -189,6 +189,7 @@ internal fun TvHomeShelves(greeting: String, feedSections: List<HomeSection>, on
}
}
}
+ item(key = "support") { TvSupport() }
}
LaunchedEffect(sections.isNotEmpty()) { if (sections.isNotEmpty()) runCatching { delay(80); first.requestFocus() } }
}
diff --git a/base/app/src/main/java/com/music/spotui/ui/tv/TvSupport.kt b/base/app/src/main/java/com/music/spotui/ui/tv/TvSupport.kt
new file mode 100644
index 00000000..bf06a55c
--- /dev/null
+++ b/base/app/src/main/java/com/music/spotui/ui/tv/TvSupport.kt
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+package com.music.spotui.ui.tv
+
+import androidx.compose.foundation.Canvas
+import androidx.compose.foundation.background
+import androidx.compose.foundation.border
+import androidx.compose.foundation.focusable
+import androidx.compose.foundation.layout.Arrangement
+import androidx.compose.foundation.layout.Box
+import androidx.compose.foundation.layout.Column
+import androidx.compose.foundation.layout.Row
+import androidx.compose.foundation.layout.fillMaxWidth
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.layout.size
+import androidx.compose.foundation.layout.width
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+import androidx.compose.runtime.mutableStateOf
+import androidx.compose.runtime.remember
+import androidx.compose.runtime.setValue
+import androidx.compose.ui.Alignment
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.focus.onFocusChanged
+import androidx.compose.ui.geometry.Offset
+import androidx.compose.ui.geometry.Size
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.res.stringResource
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import app.myspoti.engine.R
+import com.google.zxing.BarcodeFormat
+import com.google.zxing.EncodeHintType
+import com.google.zxing.common.BitMatrix
+import com.google.zxing.qrcode.QRCodeWriter
+import kotlin.math.floor
+
+/** A QR code's modules, one bit each: the screen scales them and adds the quiet zone. */
+internal object TvQr {
+ fun modules(text: String): BitMatrix =
+ QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, 0, 0, mapOf(EncodeHintType.MARGIN to 0))
+}
+
+/**
+ * The ways to support Myspoti, at the end of Home. A TV has no browser to send anyone to, so the links
+ * are codes for the phone. The block can take the focus, or the remote could never scroll down to it.
+ */
+@Composable
+internal fun TvSupport(modifier: Modifier = Modifier) {
+ var focused by remember { mutableStateOf(false) }
+ val shape = RoundedCornerShape(16.dp)
+ Row(
+ modifier.fillMaxWidth().padding(horizontal = 48.dp)
+ .onFocusChanged { focused = it.isFocused }.focusable()
+ .background(Color(0x14FFFFFF), shape)
+ .border(2.dp, if (focused) Color.White else Color.Transparent, shape)
+ .padding(24.dp),
+ verticalAlignment = Alignment.CenterVertically,
+ horizontalArrangement = Arrangement.spacedBy(32.dp),
+ ) {
+ Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(10.dp)) {
+ Text(stringResource(R.string.support_title), color = Color.White, fontSize = 24.sp, fontWeight = FontWeight.Bold)
+ Text(stringResource(R.string.support_body), color = Color(0xB3FFFFFF), fontSize = 16.sp)
+ Text(stringResource(R.string.support_scan), color = Color.White, fontSize = 16.sp, fontWeight = FontWeight.SemiBold)
+ }
+ TvQrCode(com.music.spotui.data.api.SupportReminder.URL, "Ko-fi")
+ TvQrCode(com.music.spotui.data.api.SupportReminder.SPONSORS_URL, "GitHub Sponsors")
+ }
+}
+
+/** Black on white whatever the theme, with its quiet zone: what a phone's camera reads best. */
+@Composable
+private fun TvQrCode(url: String, label: String) {
+ val matrix = remember(url) { TvQr.modules(url) }
+ Column(Modifier.width(190.dp), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.spacedBy(10.dp)) {
+ Box(Modifier.size(190.dp).background(Color.White, RoundedCornerShape(10.dp))) {
+ Canvas(Modifier.size(190.dp)) {
+ val quiet = 3
+ // A whole number of pixels per module: no seam between two of them.
+ val cell = floor(size.minDimension / (matrix.width + 2 * quiet))
+ val origin = (size.minDimension - cell * matrix.width) / 2
+ for (y in 0 until matrix.height) for (x in 0 until matrix.width) {
+ if (matrix[x, y]) drawRect(Color.Black, Offset(origin + x * cell, origin + y * cell), Size(cell, cell))
+ }
+ }
+ }
+ Text(label, color = Color.White, fontSize = 15.sp, fontWeight = FontWeight.SemiBold)
+ }
+}
diff --git a/base/app/src/main/res/values-de/myspoti_strings.xml b/base/app/src/main/res/values-de/myspoti_strings.xml
index 770a8388..d9722992 100644
--- a/base/app/src/main/res/values-de/myspoti_strings.xml
+++ b/base/app/src/main/res/values-de/myspoti_strings.xml
@@ -97,6 +97,7 @@
<string name="download_kept_by_named">Bleibt heruntergeladen: „%1$s“ ist heruntergeladen und enthält ihn</string>
<string name="support_title">Myspoti unterstützen</string>
<string name="support_body">Myspoti ist kostenlos, werbefrei und wird in meiner Freizeit entwickelt. Wenn es dir gefällt, hilft eine kleine Spende, es am Laufen zu halten.</string>
+ <string name="support_scan">Mit dem Handy scannen</string>
<string name="support_action">Auf Ko-fi unterstützen</string>
<string name="support_decline">Nein danke</string>
<string name="support_row_detail">Eine kleine Spende über Ko-fi oder GitHub Sponsors hält die App am Laufen</string>
diff --git a/base/app/src/main/res/values-el/myspoti_strings.xml b/base/app/src/main/res/values-el/myspoti_strings.xml
index ab13d0ff..8780c41c 100644
--- a/base/app/src/main/res/values-el/myspoti_strings.xml
+++ b/base/app/src/main/res/values-el/myspoti_strings.xml
@@ -97,6 +97,7 @@
<string name="download_kept_by_named">Εξακολουθεί να έχει ληφθεί: το «%1$s» έχει ληφθεί και το περιέχει</string>
<string name="support_title">Στήριξε το Myspoti</string>
<string name="support_body">Το Myspoti είναι δωρεάν, χωρίς διαφημίσεις, και φτιάχνεται στον ελεύθερο χρόνο μου. Αν σου αρέσει, μια μικρή δωρεά βοηθά να συνεχίσει.</string>
+ <string name="support_scan">Σάρωσε με το κινητό σου</string>
<string name="support_action">Στήριξε στο Ko-fi</string>
<string name="support_decline">Όχι, ευχαριστώ</string>
<string name="support_row_detail">Μια μικρή δωρεά στο Ko-fi ή στο GitHub Sponsors βοηθά την εφαρμογή να συνεχίσει</string>
diff --git a/base/app/src/main/res/values-es/myspoti_strings.xml b/base/app/src/main/res/values-es/myspoti_strings.xml
index b37c760d..18882287 100644
--- a/base/app/src/main/res/values-es/myspoti_strings.xml
+++ b/base/app/src/main/res/values-es/myspoti_strings.xml
@@ -97,6 +97,7 @@
<string name="download_kept_by_named">Sigue descargada: «%1$s» está descargado y la incluye</string>
<string name="support_title">Apoya Myspoti</string>
<string name="support_body">Myspoti es gratis, sin anuncios, y lo desarrollo en mi tiempo libre. Si te gusta, una pequeña donación ayuda a que siga adelante.</string>
+ <string name="support_scan">Escanea con tu móvil</string>
<string name="support_action">Apoyar en Ko-fi</string>
<string name="support_decline">No, gracias</string>
<string name="support_row_detail">Una pequeña donación en Ko-fi o GitHub Sponsors ayuda a mantener la app</string>
diff --git a/base/app/src/main/res/values-fr/myspoti_strings.xml b/base/app/src/main/res/values-fr/myspoti_strings.xml
index 356bb985..684fe73e 100644
--- a/base/app/src/main/res/values-fr/myspoti_strings.xml
+++ b/base/app/src/main/res/values-fr/myspoti_strings.xml
@@ -110,6 +110,7 @@
<string name="download_kept_by_named">Reste téléchargé : « %1$s » est téléchargé et le contient</string>
<string name="support_title">Soutenir Myspoti</string>
<string name="support_body">Myspoti est gratuit, sans publicité, et développé sur mon temps libre. Si vous l\'appréciez, un petit don aide à le faire avancer.</string>
+ <string name="support_scan">Scanne avec ton téléphone</string>
<string name="support_action">Soutenir sur Ko-fi</string>
<string name="support_decline">Non merci</string>
<string name="support_row_detail">Un petit don sur Ko-fi ou GitHub Sponsors aide l\'app à avancer</string>
diff --git a/base/app/src/main/res/values-it/myspoti_strings.xml b/base/app/src/main/res/values-it/myspoti_strings.xml
index 48b25a90..09db3847 100644
--- a/base/app/src/main/res/values-it/myspoti_strings.xml
+++ b/base/app/src/main/res/values-it/myspoti_strings.xml
@@ -97,6 +97,7 @@
<string name="download_kept_by_named">Ancora scaricato: «%1$s» è scaricato e lo contiene</string>
<string name="support_title">Sostieni Myspoti</string>
<string name="support_body">Myspoti è gratuito, senza pubblicità, e fatto nel mio tempo libero. Se ti piace, una piccola donazione aiuta a farlo andare avanti.</string>
+ <string name="support_scan">Scansiona con il telefono</string>
<string name="support_action">Sostieni su Ko-fi</string>
<string name="support_decline">No grazie</string>
<string name="support_row_detail">Una piccola donazione su Ko-fi o GitHub Sponsors aiuta l\'app ad andare avanti</string>
diff --git a/base/app/src/main/res/values-pl/myspoti_strings.xml b/base/app/src/main/res/values-pl/myspoti_strings.xml
index 956fb4e8..8a58cda5 100644
--- a/base/app/src/main/res/values-pl/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pl/myspoti_strings.xml
@@ -107,6 +107,7 @@
<string name="download_kept_by_named">Nadal pobrane: «%1$s» jest pobrane i zawiera ten utwór</string>
<string name="support_title">Wesprzyj Myspoti</string>
<string name="support_body">Myspoti jest darmowe, bez reklam i powstaje w moim wolnym czasie. Jeśli Ci się podoba, niewielka darowizna pomoże je rozwijać.</string>
+ <string name="support_scan">Zeskanuj telefonem</string>
<string name="support_action">Wesprzyj na Ko-fi</string>
<string name="support_decline">Nie, dziękuję</string>
<string name="support_row_detail">Niewielka darowizna na Ko-fi lub GitHub Sponsors pomaga rozwijać aplikację</string>
diff --git a/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml b/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml
index fc4d2cf0..5a44ed81 100644
--- a/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml
@@ -101,6 +101,7 @@
<string name="download_kept_by_list">Continua baixada: uma playlist baixada a mantém</string>
<string name="support_title">Apoie o Myspoti</string>
<string name="support_body">O Myspoti é gratuito, sem anúncios, e feito no meu tempo livre. Se você gosta dele, uma pequena doação ajuda a mantê-lo vivo.</string>
+ <string name="support_scan">Escaneie com o celular</string>
<string name="support_action">Apoiar no Ko-fi</string>
<string name="support_decline">Não, obrigado</string>
<string name="support_row_detail">Uma pequena doação no Ko-fi ou no GitHub Sponsors ajuda a manter o app</string>
diff --git a/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml b/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml
index ce906f6e..61b23c05 100644
--- a/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml
@@ -94,6 +94,7 @@
<string name="download_kept_by_named">Continua transferida: «%1$s» está transferido e inclui-a</string>
<string name="support_title">Apoiar o Myspoti</string>
<string name="support_body">O Myspoti é gratuito, sem anúncios, e é feito no meu tempo livre. Se gostas, um pequeno donativo ajuda a mantê-lo a funcionar.</string>
+ <string name="support_scan">Digitaliza com o telemóvel</string>
<string name="support_action">Apoiar no Ko-fi</string>
<string name="support_decline">Não, obrigado</string>
<string name="support_row_detail">Um pequeno donativo no Ko-fi ou no GitHub Sponsors ajuda a manter a app</string>
diff --git a/base/app/src/main/res/values-pt/myspoti_strings.xml b/base/app/src/main/res/values-pt/myspoti_strings.xml
index 2e1786a1..12798ee8 100644
--- a/base/app/src/main/res/values-pt/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pt/myspoti_strings.xml
@@ -97,6 +97,7 @@
<string name="download_kept_by_named">Continua baixada: «%1$s» está baixado e a inclui</string>
<string name="support_title">Apoiar o Myspoti</string>
<string name="support_body">O Myspoti é gratuito, sem anúncios, e é feito no meu tempo livre. Se você gosta, um pequeno donativo ajuda a mantê-lo funcionando.</string>
+ <string name="support_scan">Digitaliza com o telemóvel</string>
<string name="support_action">Apoiar no Ko-fi</string>
<string name="support_decline">Não, obrigado</string>
<string name="support_row_detail">Um pequeno donativo no Ko-fi ou no GitHub Sponsors ajuda a manter o app</string>
diff --git a/base/app/src/main/res/values-ru/myspoti_strings.xml b/base/app/src/main/res/values-ru/myspoti_strings.xml
index 089ac9eb..6b1904d8 100644
--- a/base/app/src/main/res/values-ru/myspoti_strings.xml
+++ b/base/app/src/main/res/values-ru/myspoti_strings.xml
@@ -107,6 +107,7 @@
<string name="download_kept_by_named">Остаётся скачанным: «%1$s» скачан и содержит его</string>
<string name="support_title">Поддержать Myspoti</string>
<string name="support_body">Myspoti бесплатен, без рекламы и создан в моё свободное время. Если он вам нравится, небольшое пожертвование поможет проекту жить дальше.</string>
+ <string name="support_scan">Отсканируйте телефоном</string>
<string name="support_action">Поддержать на Ko-fi</string>
<string name="support_decline">Нет, спасибо</string>
<string name="support_row_detail">Небольшое пожертвование на Ko-fi или GitHub Sponsors помогает развивать приложение</string>
diff --git a/base/app/src/main/res/values-zh/myspoti_strings.xml b/base/app/src/main/res/values-zh/myspoti_strings.xml
index 272cb356..9fc24b18 100644
--- a/base/app/src/main/res/values-zh/myspoti_strings.xml
+++ b/base/app/src/main/res/values-zh/myspoti_strings.xml
@@ -104,6 +104,7 @@
<string name="download_kept_by_named">仍保留下载:已下载的“%1$s”包含它</string>
<string name="support_title">支持 Myspoti</string>
<string name="support_body">Myspoti 免费、无广告,由我在业余时间制作。如果你喜欢它,一点小小的捐助能帮助它继续下去。</string>
+ <string name="support_scan">用手机扫描</string>
<string name="support_action">在 Ko-fi 上支持</string>
<string name="support_decline">不用了</string>
<string name="support_row_detail">在 Ko-fi 或 GitHub Sponsors 上的小额捐助有助于应用持续更新</string>
diff --git a/base/app/src/main/res/values/myspoti_strings.xml b/base/app/src/main/res/values/myspoti_strings.xml
index 98921864..6b719c50 100644
--- a/base/app/src/main/res/values/myspoti_strings.xml
+++ b/base/app/src/main/res/values/myspoti_strings.xml
@@ -110,6 +110,7 @@
<string name="download_kept_by_named">Still downloaded: “%1$s” is downloaded and holds it</string>
<string name="support_title">Support Myspoti</string>
<string name="support_body">Myspoti is free, without ads, and made in my spare time. If you enjoy it, a small donation helps keep it going.</string>
+ <string name="support_scan">Scan with your phone</string>
<string name="support_action">Support on Ko-fi</string>
<string name="support_decline">No thanks</string>
<string name="support_row_detail">A small donation on Ko-fi or GitHub Sponsors helps keep the app going</string>
diff --git a/base/app/src/test/java/com/music/spotui/ui/tv/TvQrTest.kt b/base/app/src/test/java/com/music/spotui/ui/tv/TvQrTest.kt
new file mode 100644
index 00000000..93881d8b
--- /dev/null
+++ b/base/app/src/test/java/com/music/spotui/ui/tv/TvQrTest.kt
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: GPL-3.0-only */
+package com.music.spotui.ui.tv
+
+import com.google.zxing.BinaryBitmap
+import com.google.zxing.RGBLuminanceSource
+import com.google.zxing.common.HybridBinarizer
+import com.google.zxing.qrcode.QRCodeReader
+import com.music.spotui.data.api.SupportReminder
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+class TvQrTest {
+ /** Read back as a camera would: scaled up, with a quiet zone. */
+ private fun read(url: String): String {
+ val m = TvQr.modules(url)
+ val scale = 6; val quiet = 3
+ val side = (m.width + 2 * quiet) * scale
+ val pixels = IntArray(side * side) { 0xFFFFFF }
+ for (y in 0 until m.height) for (x in 0 until m.width) if (m[x, y]) {
+ for (dy in 0 until scale) for (dx in 0 until scale) pixels[((y + quiet) * scale + dy) * side + (x + quiet) * scale + dx] = 0
+ }
+ return QRCodeReader().decode(BinaryBitmap(HybridBinarizer(RGBLuminanceSource(side, side, pixels)))).text
+ }
+
+ @Test fun theKoFiCodeReadsBackAsItsLink() = assertEquals(SupportReminder.URL, read(SupportReminder.URL))
+ @Test fun theSponsorsCodeReadsBackAsItsLink() = assertEquals(SupportReminder.SPONSORS_URL, read(SupportReminder.SPONSORS_URL))
+}