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.

A change of language reaches Spotify without a restart

Lelonioc7deb7ed99a3
diff --git a/app/src/main/java/dev/lelonio/square/data/LanguageStore.kt b/app/src/main/java/dev/lelonio/square/data/LanguageStore.kt
index f8bbb5d..67f9330 100644
--- a/app/src/main/java/dev/lelonio/square/data/LanguageStore.kt
+++ b/app/src/main/java/dev/lelonio/square/data/LanguageStore.kt
@@ -48,6 +48,7 @@ class LanguageStore(context: Context) {
fun set(tag: String) {
_tag.value = tag
prefs.edit().putString(KEY_LANGUAGE, tag).apply()
+ tellEngine()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val manager = app.getSystemService(android.app.LocaleManager::class.java)
manager?.applicationLocales =
@@ -56,6 +57,28 @@ class LanguageStore(context: Context) {
}
}
+ /**
+ * The engine's session speaks the language it was built with, so a change
+ * here has to be handed over.
+ *
+ * Spotify answers in the language the session asks for, and that covers
+ * more than the shelf titles: an artist the catalogue holds in two scripts
+ * comes back in the one the session named. The session is replaced when
+ * nothing is playing, which is what makes the change take hold at once;
+ * while music is on, the recipe is updated and the next session picks it
+ * up, since stopping the music to relabel it is not a trade anybody asked
+ * for.
+ */
+ private fun tellEngine() {
+ val language = language()
+ val audio = app.getSystemService(android.media.AudioManager::class.java)
+ val quiet = audio?.isMusicActive != true
+ Thread {
+ runCatching { dev.lelonio.square.nativecore.NativeBridge.setLanguage(language, quiet) }
+ .onFailure { android.util.Log.w("SquareLanguage", "language not passed on: ${it.message}") }
+ }.start()
+ }
+
/**
* The same context, reading in the chosen language.
*
diff --git a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
index dd3994b..b96166f 100644
--- a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
+++ b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
@@ -205,6 +205,24 @@ object NativeBridge {
*/
fun setSkipFade(ms: Int) = nativeSetSkipFade(ms)
+ /**
+ * The language Spotify should answer in, on a session already running.
+ *
+ * It is read when the session is built, so without this a listener who
+ * changes the app's language goes on being answered in the old one until
+ * the process is restarted: an artist the catalogue holds in Cyrillic and
+ * in Latin keeps arriving in whichever was in force at sign-in.
+ *
+ * [rebuild] replaces the session there and then, which is the only way the
+ * language reaches requests already in flight. It stops the music, so the
+ * caller does it when nothing is playing and lets the next session pick it
+ * up otherwise.
+ *
+ * Blocks while rebuilding. Never call it from the main thread.
+ */
+ fun setLanguage(language: String, rebuild: Boolean) =
+ nativeSetLanguage(language, if (rebuild) 1 else 0)
+
/**
* Builds a new session, player and Connect device, keeping everything else.
*
@@ -470,6 +488,7 @@ object NativeBridge {
private external fun nativeSetHoldEnd(enabled: Boolean)
private external fun nativeSetSkipFade(ms: Int)
+ private external fun nativeSetLanguage(language: String, rebuild: Int)
private external fun nativeReconnect()
private external fun nativePlaybackElsewhere(): Boolean
private external fun nativePublishContext(positionMs: Int): Boolean
diff --git a/native/src/engine.rs b/native/src/engine.rs
index c442376..6dec550 100644
--- a/native/src/engine.rs
+++ b/native/src/engine.rs
@@ -1028,6 +1028,29 @@ pub fn set_skip_fade(ms: u32) -> EngineResult<()> {
Ok(())
}
+/// The language Spotify answers in, changed on a session that is already up.
+///
+/// The language rides on every request the session makes, and it is read once,
+/// when the session is built. Someone who changes the app's language then goes
+/// on being answered in the old one for as long as the process lives: names
+/// that the catalogue holds in more than one script, an artist written in
+/// Cyrillic and in Latin, keep arriving in whichever was in force at sign-in.
+///
+/// The recipe is always updated, so the next session has it whatever happens
+/// here. Rebuilding now is the caller's choice, since it takes the player with
+/// it and nobody changing a language expects the music to stop.
+pub fn set_language(language: &str, rebuild: bool) -> EngineResult<()> {
+ {
+ let mut guard = ENGINE.lock().map_err(|_| "engine mutex poisoned")?;
+ let engine = guard.as_mut().ok_or("engine not started")?;
+ if engine.recipe.session_config.language == language {
+ return Ok(());
+ }
+ engine.recipe.session_config.language = language.to_string();
+ }
+ if rebuild { reconnect() } else { Ok(()) }
+}
+
/// Throws away a bundle and builds another one, leaving the runtime and the
/// audio output alone.
///
diff --git a/native/src/ffi.rs b/native/src/ffi.rs
index 80ec911..8d9460e 100644
--- a/native/src/ffi.rs
+++ b/native/src/ffi.rs
@@ -537,6 +537,23 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSet
});
}
+/// The language Spotify answers in; see engine::set_language.
+#[no_mangle]
+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSetLanguage(
+ mut env: JNIEnv,
+ _class: JClass,
+ language: JString,
+ rebuild: jint,
+) {
+ let language = match env.get_string(&language) {
+ Ok(value) => String::from(value),
+ Err(_) => return,
+ };
+ guard(&mut env, "SetLanguage", || {
+ engine::set_language(&language, rebuild != 0)
+ });
+}
+
/// Holds the end of the playing track for the sleep timer, with no rebuild.
#[no_mangle]
pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSetSkipFade(