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.

File a listen under the playlist it happened in

Lelonio493ecbc2e540
diff --git a/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin b/app/.cxx/Debug/286k5v33/arm64-v8a/configure_fingerprint.bin
index 88886eb..e618c96 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/app/src/main/java/dev/emanuele/spot/nativecore/NativeBridge.kt b/app/src/main/java/dev/emanuele/spot/nativecore/NativeBridge.kt
index dc7440d..f28fecc 100644
--- a/app/src/main/java/dev/emanuele/spot/nativecore/NativeBridge.kt
+++ b/app/src/main/java/dev/emanuele/spot/nativecore/NativeBridge.kt
@@ -74,12 +74,20 @@ object NativeBridge {
* queue belongs to the Connect state — it is what the account and every
* other device see, and it is Spirc that advances at the end of a track.
*/
- fun loadQueue(uris: List<String>, index: Int, startPlaying: Boolean, positionMs: Int = 0) =
+ fun loadQueue(
+ uris: List<String>,
+ index: Int,
+ startPlaying: Boolean,
+ positionMs: Int = 0,
+ /** The playlist or album the queue came from; empty when it came from none. */
+ contextUri: String = "",
+ ) =
nativeLoadQueue(
Json.encodeToString(ListSerializer(String.serializer()), uris),
index,
startPlaying,
positionMs,
+ contextUri,
)
fun play() = nativePlay()
@@ -158,6 +166,7 @@ object NativeBridge {
index: Int,
startPlaying: Boolean,
positionMs: Int,
+ contextUri: String,
)
private external fun nativeNext()
private external fun nativePrevious()
diff --git a/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt b/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt
index 8f27a8f..41452bd 100644
--- a/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt
+++ b/app/src/main/java/dev/emanuele/spot/playback/LibrespotPlayer.kt
@@ -68,7 +68,15 @@ class LibrespotPlayer(
if (uris.isEmpty()) return
val index = queue.currentIndex
fadeOutThen {
- runCatching { NativeBridge.loadQueue(uris, index, startPlaying, positionMs) }
+ runCatching {
+ NativeBridge.loadQueue(
+ uris,
+ index,
+ startPlaying,
+ positionMs,
+ queue.contextUri.orEmpty(),
+ )
+ }
.onFailure { android.util.Log.e("SpotPlayer", "load failed: ${it.message}") }
if (startPlaying) fadeIn()
}
diff --git a/app/src/main/java/dev/emanuele/spot/playback/PlayQueue.kt b/app/src/main/java/dev/emanuele/spot/playback/PlayQueue.kt
index cb6d837..a0d819e 100644
--- a/app/src/main/java/dev/emanuele/spot/playback/PlayQueue.kt
+++ b/app/src/main/java/dev/emanuele/spot/playback/PlayQueue.kt
@@ -2,6 +2,7 @@ package dev.emanuele.spot.playback
import android.net.Uri
import androidx.media3.common.MediaItem
+import dev.emanuele.spot.ui.EXTRA_CONTEXT_URI
/**
* The queue the engine plays through.
@@ -162,9 +163,21 @@ class PlayQueue {
* without one are dropped rather than queued and skipped later.
*/
fun replaceFromMediaItems(mediaItems: List<MediaItem>, startIndex: Int) {
+ contextUri = mediaItems.firstNotNullOfOrNull {
+ it.mediaMetadata.extras?.getString(EXTRA_CONTEXT_URI)
+ }
replace(mediaItems.mapNotNull(::toTrack), startIndex)
}
+ /**
+ * The playlist or album this queue came from, when it came from one.
+ *
+ * Carried for the listening history: Spotify files a play under the context
+ * it happened in, and a queue of loose tracks is filed under nothing.
+ */
+ var contextUri: String? = null
+ private set
+
/** Insert controller-supplied items; see [replaceFromMediaItems] for the id rule. */
fun addFromMediaItems(index: Int, mediaItems: List<MediaItem>) {
add(index, mediaItems.mapNotNull(::toTrack))
diff --git a/app/src/main/java/dev/emanuele/spot/ui/MainActivity.kt b/app/src/main/java/dev/emanuele/spot/ui/MainActivity.kt
index b87bc2c..918c09e 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/MainActivity.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/MainActivity.kt
@@ -100,9 +100,9 @@ class MainActivity : ComponentActivity() {
}
/** Sends the visible list to the session, starting at the tapped track. */
- private fun play(tracks: List<CatalogTrack>, index: Int) {
+ private fun play(tracks: List<CatalogTrack>, index: Int, contextUri: String? = null) {
val player = controller ?: return
- player.setMediaItems(tracks.map(::toMediaItem), index, 0L)
+ player.setMediaItems(tracks.map { toMediaItem(it, contextUri) }, index, 0L)
player.prepare()
player.play()
}
@@ -123,7 +123,7 @@ class MainActivity : ComponentActivity() {
}
}
- private fun toMediaItem(track: CatalogTrack): MediaItem =
+ private fun toMediaItem(track: CatalogTrack, contextUri: String? = null): MediaItem =
MediaItem.Builder()
// The media id carries the Spotify URI; PlayQueue refuses anything else.
.setMediaId(track.uri)
@@ -134,7 +134,18 @@ class MainActivity : ComponentActivity() {
.setAlbumTitle(track.album)
.setDurationMs(track.durationMs)
.setArtworkUri(track.artworkUrl?.let(android.net.Uri::parse))
+ // Where the queue came from, carried with the item because
+ // the engine lives in the service and this is the only
+ // channel between them that survives the session boundary.
+ .setExtras(
+ contextUri?.let {
+ android.os.Bundle().apply { putString(EXTRA_CONTEXT_URI, it) }
+ },
+ )
.build(),
)
.build()
}
+
+/** Key for the context URI carried in a media item's metadata extras. */
+const val EXTRA_CONTEXT_URI = "dev.emanuele.spot.CONTEXT_URI"
diff --git a/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt b/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
index 4fe9d4e..3afbf04 100644
--- a/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
+++ b/app/src/main/java/dev/emanuele/spot/ui/SpotApp.kt
@@ -153,7 +153,12 @@ private val BackdropBlur = BlurTransformation(radius = 14, passes = 2)
@Composable
fun SpotApp(
player: Player?,
- onPlay: (List<CatalogTrack>, Int) -> Unit,
+ /**
+ * Plays a queue. The third argument is the context it came from — a
+ * playlist or album URI — which is what the account's listening history is
+ * filed under; null when the tracks are a selection rather than a place.
+ */
+ onPlay: (List<CatalogTrack>, Int, String?) -> Unit,
/** Appends one track to the end of the queue; see the swipe gesture on rows. */
onEnqueue: (CatalogTrack) -> Unit,
/**
@@ -383,7 +388,7 @@ fun SpotApp(
onOpenPlaylist = { navController.openPlaylist(viewModel, it) },
playlistOrder = playlistOrder,
recent = recent,
- onPlayRecent = onPlay,
+ onPlayRecent = { tracks, index -> onPlay(tracks, index, null) },
feed = feed,
onOpenItem = { item ->
viewModel.openContext(item.uri, item.title, item.artworkUrl)
@@ -403,7 +408,7 @@ fun SpotApp(
onQueryChange = viewModel::onSearchQuery,
onClientIdChange = viewModel::onWebApiClientIdChange,
onConnectWebApi = { viewModel.connectWebApi() },
- onPlayTrack = onPlay,
+ onPlayTrack = { tracks, index -> onPlay(tracks, index, null) },
onEnqueue = onEnqueue,
onOpenContext = { item ->
viewModel.openContext(item.uri, item.title, item.artworkUrl)
@@ -455,14 +460,16 @@ fun SpotApp(
contentPadding = listPadding,
nowPlayingUri = playback.mediaId,
onBack = { navController.popBackStack() },
- onPlay = onPlay,
+ onPlay = { tracks, index ->
+ onPlay(tracks, index, playlist.uri)
+ },
onEnqueue = onEnqueue,
onShuffle = { tracks ->
// Order does not matter: the player
// treats shuffle as a mode and stamps it
// onto whatever queue arrives next.
player?.shuffleModeEnabled = true
- onPlay(tracks, 0)
+ onPlay(tracks, 0, playlist.uri)
},
onAddToPlaylist = { track ->
viewModel.openAddToPlaylist(track.uri, track.name)
@@ -618,7 +625,7 @@ fun SpotApp(
if (menu != null) {
TrackSheetAction("Riproduci", PhosphorIcons.Fill.Play) {
trackMenu = null
- onPlay(listOf(menu.track), 0)
+ onPlay(listOf(menu.track), 0, null)
}
TrackSheetAction("Aggiungi alla coda", PhosphorIcons.Regular.Queue) {
trackMenu = null
diff --git a/native/src/engine.rs b/native/src/engine.rs
index f737b6a..d630688 100644
--- a/native/src/engine.rs
+++ b/native/src/engine.rs
@@ -465,11 +465,18 @@ impl History {
return;
}
- // A context is opened once and every track played inside it is
- // attributed to it. Playing a track on its own makes the track its own
- // context, which is what the official client does too.
- if self.context_uri.is_empty() {
- self.context_uri = uri.clone();
+ // The playlist or album this queue came from, or the track itself when
+ // it came from neither — which is what the official client sends for a
+ // track played on its own.
+ let context = match current_context() {
+ context if context.is_empty() => uri.clone(),
+ context => context,
+ };
+ if self.context_uri != context {
+ self.context_uri = context;
+ // A context is a session: leaving one and starting another is a new
+ // session id, not a continuation of the last.
+ self.session_id = events::random_id();
self.events
.new_session(&self.session_id, &self.context_uri, 1);
}
@@ -575,11 +582,23 @@ fn emit(listener: &GlobalRef, kind: &str, uri: &str, position_ms: i64) -> Result
///
/// `index` selects the starting track; anything out of range starts at the
/// beginning, which is librespot's own behaviour rather than an error.
+/// The playlist or album the current queue came from, for the listening events.
+///
+/// A global rather than a field on the engine: the event pump reads it from its
+/// own thread, and it changes whenever a queue is loaded — which is a different
+/// lock from the one that owns playback.
+static CONTEXT_URI: Mutex<String> = Mutex::new(String::new());
+
+pub fn current_context() -> String {
+ CONTEXT_URI.lock().map(|c| c.clone()).unwrap_or_default()
+}
+
pub fn load_queue(
uris: Vec<String>,
index: u32,
start_playing: bool,
position_ms: u32,
+ context_uri: String,
) -> EngineResult<()> {
if uris.is_empty() {
return Err("empty queue".into());
@@ -591,6 +610,10 @@ pub fn load_queue(
}
}
+ if let Ok(mut context) = CONTEXT_URI.lock() {
+ *context = context_uri;
+ }
+
let options = LoadRequestOptions {
start_playing,
seek_to: position_ms,
diff --git a/native/src/ffi.rs b/native/src/ffi.rs
index bbc35f2..a238c2f 100644
--- a/native/src/ffi.rs
+++ b/native/src/ffi.rs
@@ -115,10 +115,12 @@ pub extern "system" fn Java_dev_emanuele_spot_nativecore_NativeBridge_nativeLoad
index: jint,
start_playing: jboolean,
position_ms: jint,
+ context_uri: JString,
) {
// JSON rather than a jobjectArray: the Kotlin side already serialises track
// lists for the catalogue calls, and one decoder is easier to keep correct
// than two ways of crossing the same boundary.
+ let context = read_string(&mut env, &context_uri).unwrap_or_default();
let result = read_string(&mut env, &uris_json)
.and_then(|raw| {
serde_json::from_str::<Vec<String>>(&raw).map_err(|e| format!("bad queue: {e}"))
@@ -129,6 +131,7 @@ pub extern "system" fn Java_dev_emanuele_spot_nativecore_NativeBridge_nativeLoad
index.max(0) as u32,
start_playing == JNI_TRUE,
position_ms.max(0) as u32,
+ context,
)
});
or_throw(&mut env, result);