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.

Keep the listen in progress on disk, so a kill does not eat it

Leloniofc527e888d44
diff --git a/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin b/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin
index 76f7bd1..e08d19a 100644
--- a/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin
+++ b/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin
@@ -2,27 +2,27 @@ C/C++ Structured Logc
a
_/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/additional_project_files.txtC
A
-?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  �����3  ��߈�3`
+?com.android.build.gradle.internal.cxx.io.EncodedFileFingerPrint  ��А�3  ��߈�3`
^
-\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build.json  �����3� ��߈�3e
+\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build.json  ��А�3� ��߈�3e
c
-a/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build_mini.json  �����3� ��߈�3R
+a/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/android_gradle_build_mini.json  ��А�3� ��߈�3R
P
-N/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja  �����3� ��߈�3V
+N/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja  ��А�3� ��߈�3V
T
-R/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja.txt  �����3[
+R/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build.ninja.txt  ��А�3[
Y
-W/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build_file_index.txt  �����3 @ ��߈�3\
+W/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/build_file_index.txt  ��А�3 @ ��߈�3\
Z
-X/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json  �����3֚ ��߈�3`
+X/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json  ��А�3֚ ��߈�3`
^
-\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json.bin  �����3 �" ��߈�3f
+\/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/compile_commands.json.bin  ��А�3 �" ��߈�3f
d
-b/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/metadata_generation_command.txt  �����3
+b/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/metadata_generation_command.txt  ��А�3
� ��߈�3Y
W
-U/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/prefab_config.json  �����3  ( ��߈�3^
+U/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/prefab_config.json  ��А�3  ( ��߈�3^
\
-Z/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/symbol_folder_index.txt  �����3  Y ��߈�3D
+Z/Users/emanuele/Progetto Spotify/app/.cxx/Debug/286k5v33/arm64-v8a/symbol_folder_index.txt  ��А�3  Y ��߈�3D
B
-@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  �����3 � �Ȅ��3
\ No newline at end of file
+@/Users/emanuele/Progetto Spotify/app/src/main/cpp/CMakeLists.txt  ��А�3 � �Ȅ��3
\ No newline at end of file
diff --git a/native/src/catalog.rs b/native/src/catalog.rs
index 7e1de81..592c2e8 100644
--- a/native/src/catalog.rs
+++ b/native/src/catalog.rs
@@ -299,14 +299,29 @@ pub fn playlist_cover(uri: &str) -> EngineResult<String> {
.map_err(|e| format!("playlist was not a SelectedListContent: {e}"))?;
let attributes = &list.attributes;
- let cover = image_url(attributes.picture())
+
+ // Format attributes come first, and the exact `image_url` before any
+ // other key that merely ends in it.
+ //
+ // The generated lists — "i tuoi brani più ascoltati", the daily mixes —
+ // carry several: a header image, a background, and the cover the web
+ // client actually shows. Taking whichever came first put the wrong art
+ // on those tiles. Their `picture` is stale or empty, which is why this
+ // is preferred over it rather than used as a fallback.
+ let format_cover = attributes
+ .format_attributes
+ .iter()
+ .find(|attribute| attribute.key() == "image_url")
.or_else(|| {
attributes
.format_attributes
.iter()
- .find(|attribute| attribute.key().contains("image_url"))
- .and_then(|attribute| cdn_url(attribute.value()))
+ .find(|attribute| attribute.key().ends_with("image_url"))
})
+ .and_then(|attribute| cdn_url(attribute.value()));
+
+ let cover = format_cover
+ .or_else(|| image_url(attributes.picture()))
.or_else(|| {
// The sized variants, when there is no single picture: the
// largest is last in Spotify's own order, and any of them is a
diff --git a/native/src/engine.rs b/native/src/engine.rs
index 718209f..dddeba5 100644
--- a/native/src/engine.rs
+++ b/native/src/engine.rs
@@ -265,6 +265,7 @@ pub fn start(
listener,
device_name.to_string(),
session.clone(),
+ cache_dir.to_string(),
);
*guard = Some(Engine {
@@ -292,6 +293,7 @@ fn spawn_event_pump(
listener: GlobalRef,
device_name: String,
session: Session,
+ state_dir: String,
) {
let mut events = player.get_player_event_channel();
// Keep the player alive for as long as we are reading its events.
@@ -313,7 +315,7 @@ fn spawn_event_pump(
log::info!("event pump started for device {device_name}");
// What the account's listening history is built from; see events.rs.
- let mut history = History::new(session);
+ let mut history = History::new(session, state_dir);
while let Some(event) = events.blocking_recv() {
history.observe(&event);
@@ -367,6 +369,8 @@ fn spawn_event_pump(
/// event that says so.
struct History {
events: EventService,
+ /// Where the in-progress listen is kept, so a killed process loses nothing.
+ state_dir: String,
session_id: String,
/// The context every listen is attributed to, once one has been started.
context_uri: String,
@@ -394,10 +398,14 @@ struct Playing {
}
impl History {
- fn new(session: Session) -> Self {
+ fn new(session: Session, state_dir: String) -> Self {
let session_id = session.session_id();
+ let events = EventService::new(session);
+ // Whatever the last run was in the middle of when it went away.
+ events::pending::flush(&state_dir, &events);
History {
- events: EventService::new(session),
+ events,
+ state_dir,
session_id: if session_id.is_empty() {
events::random_id()
} else {
@@ -436,6 +444,8 @@ impl History {
if let Some(playing) = self.current.as_mut() {
playing.position_ms = *position_ms;
}
+ // Once a second, which is what this event arrives at.
+ self.remember();
}
// A pause ends the stretch that was being listened to.
@@ -526,6 +536,26 @@ impl History {
started_at: events::now_ms(),
reason_start: "playbtn",
});
+ self.remember();
+ }
+
+ /// Keeps the in-progress listen on disk; see `events::pending`.
+ fn remember(&self) {
+ let Some(playing) = self.current.as_ref() else {
+ return;
+ };
+ events::pending::write(
+ &self.state_dir,
+ &events::pending::Pending {
+ track_hex: playing.hex.clone(),
+ playback_id: playing.playback_id.clone(),
+ context_uri: self.context_uri.clone(),
+ start_ms: playing.start_ms,
+ position_ms: playing.position_ms,
+ duration_ms: playing.duration_ms,
+ started_at: playing.started_at,
+ },
+ );
}
fn finish(&mut self, reason: EndReason) {
@@ -551,6 +581,9 @@ impl History {
end_ms
};
+ // Sent, so it must not be sent again on the next start.
+ events::pending::clear(&self.state_dir);
+
self.events.track_transition(&Listen {
track_hex: &playing.hex,
playback_id: &playing.playback_id,
diff --git a/native/src/events.rs b/native/src/events.rs
index e58779f..06a458b 100644
--- a/native/src/events.rs
+++ b/native/src/events.rs
@@ -263,6 +263,87 @@ fn kind_of(body: &[u8]) -> String {
.unwrap_or_default()
}
+/// A listen interrupted by the process going away.
+///
+/// Written to disk while a track plays and cleared as soon as the transition is
+/// sent. A phone kills a music app the moment it needs the memory, and until
+/// this existed everything heard since the last pause went with it: the event
+/// only ever left at a boundary, and being killed is not a boundary.
+///
+/// Reported on the next start, with the position it had reached and its
+/// original timestamp. Late, but true — the alternative was silence.
+pub mod pending {
+ use super::{EndReason, EventService, Listen};
+ use std::path::{Path, PathBuf};
+
+ const FILE_NAME: &str = "pending-listen";
+
+ pub struct Pending {
+ pub track_hex: String,
+ pub playback_id: String,
+ pub context_uri: String,
+ pub start_ms: u32,
+ pub position_ms: u32,
+ pub duration_ms: u32,
+ pub started_at: u128,
+ }
+
+ fn path(dir: &str) -> PathBuf {
+ Path::new(dir).join(FILE_NAME)
+ }
+
+ /// Tab-separated and hand-parsed: six values, written once a second.
+ pub fn write(dir: &str, listen: &Pending) {
+ let line = format!(
+ "{}\t{}\t{}\t{}\t{}\t{}\t{}",
+ listen.track_hex,
+ listen.playback_id,
+ listen.context_uri,
+ listen.start_ms,
+ listen.position_ms,
+ listen.duration_ms,
+ listen.started_at,
+ );
+ let _ = std::fs::write(path(dir), line);
+ }
+
+ pub fn clear(dir: &str) {
+ let _ = std::fs::remove_file(path(dir));
+ }
+
+ /// Sends whatever the last run was in the middle of, and forgets it.
+ pub fn flush(dir: &str, events: &EventService) {
+ let Ok(raw) = std::fs::read_to_string(path(dir)) else {
+ return;
+ };
+ clear(dir);
+
+ let parts: Vec<&str> = raw.split('\t').collect();
+ if parts.len() != 7 {
+ return;
+ }
+ let number = |index: usize| parts[index].parse().unwrap_or(0);
+ let position_ms: u32 = number(4);
+ let start_ms: u32 = number(3);
+ if position_ms <= start_ms {
+ return;
+ }
+
+ log::info!("reporting a listen the last run did not finish");
+ events.track_transition(&Listen {
+ track_hex: parts[0],
+ playback_id: parts[1],
+ context_uri: parts[2],
+ start_ms,
+ end_ms: position_ms,
+ duration_ms: number(5),
+ reason_start: "playbtn",
+ reason_end: EndReason::EndPlay,
+ started_at: parts[6].parse().unwrap_or(0),
+ });
+ }
+}
+
pub fn now_ms() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)