Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
Play downloads at once when mobile data has no signal
Lelonio5760c94e1531
diff --git a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.ktindex 00069de..4e3707a 100644--- a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt+++ b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt@@ -614,6 +614,14 @@ object NativeBridge {*/fun setOfflineOnly(only: Boolean) = nativeSetOfflineOnly(only)+ /**+ * Whether the phone has a network carrying anything.+ *+ * A handshake under way is given up on when it goes, so the engine starts+ * on the downloads instead of waiting on a link with no signal.+ */+ fun setNetworkGone(gone: Boolean) = nativeSetNetworkGone(gone)+/*** Where downloaded tracks are kept.*@@ -668,6 +676,7 @@ object NativeBridge {private external fun nativeCanvas(trackUri: String): Stringprivate external fun nativeIsOffline(): Booleanprivate external fun nativeSetOfflineOnly(only: Boolean)+ private external fun nativeSetNetworkGone(gone: Boolean)private external fun nativeSetDownloadRoot(path: String)private external fun nativeDownloadTrack(trackUri: String, bitrateKbps: Int): Stringprivate external fun nativeDownloadState(trackUri: String): Stringdiff --git a/app/src/main/java/dev/lelonio/square/playback/NetworkWatch.kt b/app/src/main/java/dev/lelonio/square/playback/NetworkWatch.ktindex 93f4349..aa3f1bf 100644--- a/app/src/main/java/dev/lelonio/square/playback/NetworkWatch.kt+++ b/app/src/main/java/dev/lelonio/square/playback/NetworkWatch.kt@@ -96,6 +96,10 @@ class NetworkWatch(context: Context, private val scope: CoroutineScope) {private fun evaluate() {val connected = connected()+ // At once, unlike offline below: a handshake under way is given up on+ // the moment it can only hang, and the downloads play (see engine.rs,+ // NETWORK_GONE).+ runCatching { dev.lelonio.square.nativecore.NativeBridge.setNetworkGone(!connected) }probeIfDoubtful()val returned = connected && lastConnected == falselastConnected = connecteddiff --git a/native/src/engine.rs b/native/src/engine.rsindex b0c4215..6820eac 100644--- a/native/src/engine.rs+++ b/native/src/engine.rs@@ -708,17 +708,33 @@ fn build_bundle(move || Box::new(crate::sink::AndroidSink::new(AudioFormat::S16, generation)),);- let connected = Spirc::new(+ // Given up on when the network goes, or after a while. On mobile+ // data with no coverage the handshake neither finishes nor fails:+ // the socket opens and nothing comes back. No player exists until+ // it ends, so a downloaded song tapped in the meantime was+ // refused as "reconnecting" for as long as the phone had no+ // signal. Given up on, it fails like any other handshake, and the+ // engine starts on what is on the phone.+ let handshake = Spirc::new(connect_config.clone(),session.clone(),credentials.clone(),player.clone(),mixer_for_spirc.clone(),- )- .await;+ );+ let connected = tokio::select! {+ result = handshake => result.map_err(|e| e.to_string()),+ why = handshake_abandoned() => Err(why.to_string()),+ };let (spirc, spirc_task) = match connected {Ok(pair) => pair,+ Err(e) if NETWORK_GONE.load(Ordering::SeqCst) || e == HANDSHAKE_TIMED_OUT => {+ // Not the credential's fault: it is kept, and the next+ // one is not tried on a network that answers nothing.+ session.shutdown();+ return Err(format!("connect failed: {e}"));+ }Err(e) => {session.shutdown();failure = format!("connect failed: {e}");@@ -827,6 +843,39 @@ fn local_load(uris: &[String], index: u32, play: bool, position_ms: u32) -> Engiwith_bundle(|e| e.player.load(parsed, play, position_ms))}+/// Set by the app while it has no network that carries anything.+///+/// What [`handshake_abandoned`] watches. Not [`OFFLINE_ONLY`]: that one is+/// also on while a session is being looked for again, and giving up on that+/// handshake because of it would mean never getting the session back.+static NETWORK_GONE: AtomicBool = AtomicBool::new(false);++/// Told by the app's network watch whether a network is carrying anything.+pub fn set_network_gone(gone: bool) {+ if NETWORK_GONE.swap(gone, Ordering::SeqCst) != gone {+ log::info!("network {}", if gone { "gone" } else { "back" });+ }+}++/// How long a handshake may take before it is given up as going nowhere.+const HANDSHAKE_LIMIT: Duration = Duration::from_secs(15);+const HANDSHAKE_TIMED_OUT: &str = "the handshake timed out";++/// Ends when a handshake is not worth waiting for: the network went, or+/// [`HANDSHAKE_LIMIT`] passed.+async fn handshake_abandoned() -> &'static str {+ let started = std::time::Instant::now();+ loop {+ if NETWORK_GONE.load(Ordering::SeqCst) {+ return "the network is gone";+ }+ if started.elapsed() >= HANDSHAKE_LIMIT {+ return HANDSHAKE_TIMED_OUT;+ }+ tokio::time::sleep(Duration::from_millis(250)).await;+ }+}+/// Set by the app when only downloads may be played.////// Distinct from [`OFFLINE`], which is about how the engine started: this isdiff --git a/native/src/ffi.rs b/native/src/ffi.rsindex 0cf3170..dcdf532 100644--- a/native/src/ffi.rs+++ b/native/src/ffi.rs@@ -942,6 +942,17 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSetengine::set_offline_only(only == JNI_TRUE);}+/// Whether the phone has a network carrying anything; see+/// `engine::set_network_gone`.+#[no_mangle]+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSetNetworkGone(+ _env: JNIEnv,+ _class: JClass,+ gone: jboolean,+) {+ engine::set_network_gone(gone == JNI_TRUE);+}+// --------------------------------------------------------------- downloads/// Where downloads are kept. Called before anything is downloaded, and again