Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Say when Spotify refuses songs, and a download still to happen is one

Lelonioa9d42238d4d9
diff --git a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
index 8031128..c91a5ce 100644
--- a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
@@ -471,6 +471,9 @@ class LibrespotPlayer(
/** How long a track dissolves into the next; the listener's own setting. */
private val crossfade = dev.lelonio.square.data.CrossfadeStore(context)
+ /** For the notices this says to the listener; see onKeyRefused. */
+ private val appContext = context.applicationContext
+
/** Watches the one thing outside this class that changes what it must do. */
private val watch = kotlinx.coroutines.CoroutineScope(
kotlinx.coroutines.SupervisorJob() + kotlinx.coroutines.Dispatchers.Default,
@@ -1308,9 +1311,37 @@ class LibrespotPlayer(
// handler, where a progress tick several times a second would queue
// behind whatever the player is in the middle of.
if (dev.lelonio.square.download.DownloadEvents.accept(type, uri, positionMs)) return
+ if (type == "key_refused") {
+ handler.post { onKeyRefused(positionMs == 1L) }
+ return
+ }
handler.post { applyEvent(type, uri, positionMs) }
}
+ /** Whether the listener has been told Spotify is refusing songs. */
+ private var refusalShown = false
+
+ /**
+ * Spotify refusing the key a song needs to play, or giving them again.
+ *
+ * Said once for a run of refusals, in words for the listener rather than
+ * the reason: otherwise the song sits at the start with no time on it, and
+ * what that looks like is the app broken.
+ */
+ private fun onKeyRefused(refused: Boolean) {
+ if (!refused) {
+ refusalShown = false
+ return
+ }
+ if (refusalShown) return
+ refusalShown = true
+ android.widget.Toast.makeText(
+ appContext,
+ appContext.getString(dev.lelonio.square.R.string.playback_refused),
+ android.widget.Toast.LENGTH_LONG,
+ ).show()
+ }
+
/**
* Jumps to the next track if it is one the engine cannot know about.
*
diff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
index e36051c..f3b20c5 100644
--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
@@ -3316,7 +3316,15 @@ fun SquareApp(
val kept = onItsOwn
TrackSheetAction(
stringResource(
- if (kept) R.string.remove_download else R.string.download,
+ // Asked for and not here yet is a download
+ // still to happen, not one to remove: said
+ // as "remove", it read as a song already on
+ // the phone that the downloads did not list.
+ when {
+ !kept -> R.string.download
+ here -> R.string.remove_download
+ else -> R.string.cancel_download
+ },
),
if (kept) {
PhosphorIcons.Fill.ArrowCircleDown
diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml
index d68055c..4c673ad 100644
--- a/app/src/main/res/values-de/strings.xml
+++ b/app/src/main/res/values-de/strings.xml
@@ -351,6 +351,8 @@
<string name="quality_link_unknown">unbekannt, also Ton mit %1$d kbps</string>
<string name="download">Herunterladen</string>
<string name="remove_download">Download entfernen</string>
+ <string name="cancel_download">Download abbrechen</string>
+ <string name="playback_refused">Spotify lässt gerade keine Songs abspielen. Versuch es in ein paar Minuten noch einmal. Wenn es weiter passiert, melde dich ab und wieder an.</string>
<string name="downloaded">Heruntergeladen</string>
<string name="downloading">Wird heruntergeladen</string>
<string name="downloads">Downloads</string>
diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml
index fabf7ec..dc1847c 100644
--- a/app/src/main/res/values-es/strings.xml
+++ b/app/src/main/res/values-es/strings.xml
@@ -351,6 +351,8 @@
<string name="quality_link_unknown">desconocida, así que audio a %1$d kbps</string>
<string name="download">Descargar</string>
<string name="remove_download">Quitar descarga</string>
+ <string name="cancel_download">Cancelar descarga</string>
+ <string name="playback_refused">Spotify no permite reproducir canciones en este momento. Vuelve a intentarlo en unos minutos. Si sigue pasando, cierra sesión y vuelve a entrar.</string>
<string name="downloaded">Descargado</string>
<string name="downloading">Descargando</string>
<string name="downloads">Descargas</string>
diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml
index 0ad7a4f..08925b9 100644
--- a/app/src/main/res/values-fr/strings.xml
+++ b/app/src/main/res/values-fr/strings.xml
@@ -351,6 +351,8 @@
<string name="quality_link_unknown">inconnue, donc audio à %1$d kbps</string>
<string name="download">Télécharger</string>
<string name="remove_download">Retirer le téléchargement</string>
+ <string name="cancel_download">Annuler le téléchargement</string>
+ <string name="playback_refused">Spotify ne permet pas de lire des titres pour le moment. Réessaie dans quelques minutes. Si ça continue, déconnecte-toi puis reconnecte-toi.</string>
<string name="downloaded">Téléchargé</string>
<string name="downloading">Téléchargement</string>
<string name="downloads">Téléchargements</string>
diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml
index 24cfaed..566f4c9 100644
--- a/app/src/main/res/values-hi/strings.xml
+++ b/app/src/main/res/values-hi/strings.xml
@@ -366,6 +366,8 @@
<string name="quality_link_unknown">अज्ञात, इसलिए %1$d kbps ऑडियो</string>
<string name="download">डाउनलोड</string>
<string name="remove_download">डाउनलोड हटाएँ</string>
+ <string name="cancel_download">डाउनलोड रद्द करें</string>
+ <string name="playback_refused">Spotify अभी गाने चलाने नहीं दे रहा है। कुछ मिनट बाद फिर कोशिश करें। अगर ऐसा होता रहे, तो साइन आउट करके फिर से साइन इन करें।</string>
<string name="downloaded">डाउनलोड किया</string>
<string name="downloading">डाउनलोड हो रहा है</string>
<string name="downloads">डाउनलोड</string>
diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml
index 44cd85b..cd5aa79 100644
--- a/app/src/main/res/values-it/strings.xml
+++ b/app/src/main/res/values-it/strings.xml
@@ -352,6 +352,8 @@
<string name="quality_link_unknown">sconosciuta, quindi audio a %1$d kbps</string>
<string name="download">Scarica</string>
<string name="remove_download">Rimuovi download</string>
+ <string name="cancel_download">Annulla download</string>
+ <string name="playback_refused">Spotify al momento non permette di riprodurre i brani. Riprova tra qualche minuto. Se continua, esci e accedi di nuovo.</string>
<string name="downloaded">Scaricato</string>
<string name="downloading">Download in corso</string>
<string name="downloads">Download</string>
diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml
index 47dc6c6..8d79e95 100644
--- a/app/src/main/res/values-pt/strings.xml
+++ b/app/src/main/res/values-pt/strings.xml
@@ -351,6 +351,8 @@
<string name="quality_link_unknown">desconhecida, portanto áudio a %1$d kbps</string>
<string name="download">Baixar</string>
<string name="remove_download">Remover download</string>
+ <string name="cancel_download">Cancelar download</string>
+ <string name="playback_refused">O Spotify não está deixando tocar músicas agora. Tente de novo em alguns minutos. Se continuar, saia e entre de novo na conta.</string>
<string name="downloaded">Baixado</string>
<string name="downloading">Baixando</string>
<string name="downloads">Downloads</string>
diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml
index 5b747cb..6250a59 100644
--- a/app/src/main/res/values-ru/strings.xml
+++ b/app/src/main/res/values-ru/strings.xml
@@ -368,6 +368,8 @@
<string name="quality_link_unknown">неизвестно, значит звук %1$d кбит/с</string>
<string name="download">Скачать</string>
<string name="remove_download">Удалить загрузку</string>
+ <string name="cancel_download">Отменить загрузку</string>
+ <string name="playback_refused">Сейчас Spotify не даёт воспроизводить треки. Попробуйте ещё раз через несколько минут. Если не поможет, выйдите из аккаунта и войдите снова.</string>
<string name="downloaded">Скачано</string>
<string name="downloading">Загрузка</string>
<string name="downloads">Загрузки</string>
diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml
index 7bb011f..143367e 100644
--- a/app/src/main/res/values-tr/strings.xml
+++ b/app/src/main/res/values-tr/strings.xml
@@ -366,6 +366,8 @@
<string name="quality_link_unknown">bilinmiyor, yani %1$d kbps ses</string>
<string name="download">İndir</string>
<string name="remove_download">İndirmeyi kaldır</string>
+ <string name="cancel_download">İndirmeyi iptal et</string>
+ <string name="playback_refused">Spotify şu anda şarkı çalmaya izin vermiyor. Birkaç dakika sonra tekrar dene. Devam ederse çıkış yapıp yeniden giriş yap.</string>
<string name="downloaded">İndirildi</string>
<string name="downloading">İndiriliyor</string>
<string name="downloads">İndirilenler</string>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 3588cc9..8889c12 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -372,6 +372,8 @@
<string name="quality_link_unknown">unknown, so %1$d kbps audio</string>
<string name="download">Download</string>
<string name="remove_download">Remove download</string>
+ <string name="cancel_download">Cancel download</string>
+ <string name="playback_refused">Spotify isn\'t letting songs play right now. Try again in a few minutes. If it keeps happening, sign out and sign in again.</string>
<string name="downloaded">Downloaded</string>
<string name="downloading">Downloading</string>
<string name="downloads">Downloads</string>
diff --git a/docs/DEVELOPMENT.md b/docs/DEVELOPMENT.md
index e7da365..54fedd4 100644
--- a/docs/DEVELOPMENT.md
+++ b/docs/DEVELOPMENT.md
@@ -106,7 +106,7 @@ with `[patch.crates-io]`. Five changes: the advertised OS is pinned to
`"linux"` so it agrees with the desktop client id, Mercury gained POST and
header fields so listening events can be posted, the session carries a language
so artwork comes back in it, a non-premium account no longer exits the process,
-and audio keys are retried and remembered. All five are explained in
+and audio keys are retried, remembered and their refusals reported. All five are explained in
[native/vendor/README.md](../native/vendor/README.md).
**OAuth uses the keymaster client id on a fixed port.** The redirect has to be
diff --git a/native/src/engine.rs b/native/src/engine.rs
index a7b378d..c442376 100644
--- a/native/src/engine.rs
+++ b/native/src/engine.rs
@@ -498,6 +498,11 @@ pub fn start(
// attached to the JVM. Tying it to a player would mean dropping it whenever
// a reconnection replaced one.
let (events_tx, events_rx) = std::sync::mpsc::channel();
+ // Spotify refusing the keys a song needs, told to the app so it can say so
+ // rather than sit on a song that never starts. See the patch in audio_key.
+ librespot_core::audio_key::on_playback_refusal(|refused| {
+ emit_app("key_refused", "", refused as i64)
+ });
spawn_event_pump(
events_rx,
listener,
diff --git a/native/vendor/README.md b/native/vendor/README.md
index 36684ac..100b374 100644
--- a/native/vendor/README.md
+++ b/native/vendor/README.md
@@ -109,6 +109,12 @@ all: going back and forth between two songs asks once for each.
The memory is per session and never written to disk. It holds keys, so it goes
when the session does.
+A refusal is also reported, through `on_playback_refusal`, so the app can say
+why a song is not starting instead of leaving it at the start with no time on
+it. Only refusals that land within a minute of the player asking for a key are
+reported, once for a run of them, and the next key that comes back is reported
+as the end of it. Downloads refused while nothing is playing say nothing.
+
### Maintenance
Re-apply this patch when bumping `librespot-core`. If the whole file is replaced,
diff --git a/native/vendor/librespot-core/src/audio_key.rs b/native/vendor/librespot-core/src/audio_key.rs
index 1760982..3140784 100644
--- a/native/vendor/librespot-core/src/audio_key.rs
+++ b/native/vendor/librespot-core/src/audio_key.rs
@@ -1,7 +1,10 @@
use std::{
collections::HashMap,
io::Write,
- sync::Mutex,
+ sync::{
+ Mutex, OnceLock,
+ atomic::{AtomicBool, Ordering},
+ },
time::{Duration, Instant},
};
@@ -103,6 +106,40 @@ pub fn since_playback_request() -> Option<Duration> {
.map(|at| Instant::now().saturating_duration_since(at))
}
+/// LOCAL PATCH: told when a key playback asked for is refused, with `true`,
+/// and with `false` when a key comes back after that.
+///
+/// So the app can say why nothing is starting. Without it a refused song sits
+/// at the start with no time on it and nothing else happens, which to the
+/// listener is the app broken rather than Spotify saying no. Downloads refused
+/// on their own, with playback quiet, are not reported: nobody is waiting on a
+/// song then, and the download queue shows its own wait.
+static ON_PLAYBACK_REFUSAL: OnceLock<fn(bool)> = OnceLock::new();
+static PLAYBACK_REFUSED: AtomicBool = AtomicBool::new(false);
+
+/// How recent a playback request has to be for a refusal to be its.
+const PLAYBACK_WAITING: Duration = Duration::from_secs(60);
+
+/// Sets where refusals are reported; see [ON_PLAYBACK_REFUSAL]. Only the first
+/// call counts.
+pub fn on_playback_refusal(hook: fn(bool)) {
+ let _ = ON_PLAYBACK_REFUSAL.set(hook);
+}
+
+fn report_refusal(refused: bool) {
+ if refused {
+ let waiting = since_playback_request().is_some_and(|ago| ago < PLAYBACK_WAITING);
+ if !waiting || PLAYBACK_REFUSED.swap(true, Ordering::SeqCst) {
+ return;
+ }
+ } else if !PLAYBACK_REFUSED.swap(false, Ordering::SeqCst) {
+ return;
+ }
+ if let Some(hook) = ON_PLAYBACK_REFUSAL.get() {
+ hook(refused);
+ }
+}
+
const FIRST_COOL_OFF: Duration = Duration::from_secs(3);
const MAX_COOL_OFF: Duration = Duration::from_secs(30);
@@ -195,6 +232,7 @@ impl AudioKeyManager {
inner.cool_off_until = None;
inner.cool_off = FIRST_COOL_OFF;
});
+ report_refusal(false);
return Ok(key);
}
Err(e) => {
@@ -215,6 +253,7 @@ impl AudioKeyManager {
inner.cool_off = (wait * 2).min(MAX_COOL_OFF);
warn!("audio key refused, holding off for {wait:?}");
});
+ report_refusal(true);
}
if !transient || attempt + 1 == ATTEMPTS {
return Err(e);