Skip to content

Native Android client for Spotify, built with Jetpack Compose

Downloaded songs: no Spotify reports, no Web Player takeover

liliapk4859f79cb846
diff --git a/README.md b/README.md
index 6cab0227..39b00cd1 100644
--- a/README.md
+++ b/README.md
@@ -55,7 +55,7 @@ Details for developers: [`base/api-spotify/README.md`](base/api-spotify/README.m
- No DRM circumvention. No Amazon stream decryption.
- No Spotify audio download. Offline downloads are resolved from lossless catalogues through the open-backends community of SpotiFLAC; a track with no lossless source is simply marked unavailable.
-- Downloaded songs played on the phone are reported to Spotify as if Spotify had streamed them, although their audio did not come from Spotify (play time as measured on the phone; sent later when offline). Spotify counts them like streamed listens, in Wrapped and in artists' play counts, and may treat such reports as artificial streaming. No ad is marked as heard.
+- Downloaded songs played on the phone are not reported to Spotify: they do not count in Wrapped or in artists' play counts. They are kept in Myspoti's own Recap only. No ad is marked as heard.
## Credits
diff --git a/base/app/src/main/java/app/myspoti/MyspotiApplication.kt b/base/app/src/main/java/app/myspoti/MyspotiApplication.kt
index c1a18e64..30d3682e 100644
--- a/base/app/src/main/java/app/myspoti/MyspotiApplication.kt
+++ b/base/app/src/main/java/app/myspoti/MyspotiApplication.kt
@@ -21,8 +21,9 @@ class MyspotiApplication : Application() {
SongPlayer.attachEngine(Graph.currentSongState)
// Headphones or Bluetooth gone: pause rather than play on the speaker.
com.music.spotui.di.NoisyAudio.register(this)
- // Offline listens kept for Wrapped reporting are replayed once signed in.
- com.music.spotui.data.offline.WrappedSync.resumePending()
+ // Downloaded songs are no longer reported to Spotify: drop the old report queue,
+ // whose replays sent play commands that took over the Web Player.
+ runCatching { java.io.File(filesDir, "wrapped_queue.json").delete() }
PlaybackService.library = object : MediaLibrary {
override suspend fun children(context: Context, parentId: String): List<MediaBrowserCompat.MediaItem> =
AutoBrowser.children(context, parentId)
diff --git a/base/app/src/main/java/com/music/spotui/data/offline/WrappedSync.kt b/base/app/src/main/java/com/music/spotui/data/offline/WrappedSync.kt
deleted file mode 100644
index 20d52145..00000000
--- a/base/app/src/main/java/com/music/spotui/data/offline/WrappedSync.kt
+++ /dev/null
@@ -1,382 +0,0 @@
-package com.music.spotui.data.offline
-
-import android.util.Log
-import app.myspoti.host.android.RelayCredentials
-import com.music.spotui.data.local.LocalTrack
-import com.music.spotui.di.Graph
-import kotlinx.coroutines.CoroutineScope
-import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.SupervisorJob
-import kotlinx.coroutines.launch
-import kotlinx.coroutines.sync.withLock
-import okhttp3.MediaType.Companion.toMediaType
-import okhttp3.OkHttpClient
-import okhttp3.Request
-import okhttp3.RequestBody.Companion.toRequestBody
-import org.json.JSONArray
-import org.json.JSONObject
-import java.io.File
-import java.util.concurrent.TimeUnit
-
-/**
- * Reports offline listens so they count where Spotify counts them (Wrapped, top lists).
- *
- * Only real listens are reported: a downloaded song that actually played on this
- * phone, for the time it actually played. The reporting replays what the Web
- * Player sends for a streamed listen (a captured Web Player session),
- * with the honest values: the played-time facts come from the
- * player, nothing is invented. Listens that could not be reported right away
- * (offline, data saver) are kept and re-reported once the network is back, so a
- * download-based history stays in sync.
- */
-object WrappedSync {
- private const val TAG = "Wrapped"
-
- /** Spotify's own countable threshold: reaching it makes the listen real. */
- internal const val THRESHOLD_MS = 30_000L
-
- private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
- private val http = OkHttpClient.Builder()
- .connectTimeout(15, TimeUnit.SECONDS)
- .readTimeout(15, TimeUnit.SECONDS)
- .build()
-
- /** The Spotify context of the list the downloads play from (a playlist/album uri); null = the track's album. */
- @Volatile var contextUri: String? = null
-
- /** One listening session of this app run (the melody session_id form). */
- private val sessionId = System.currentTimeMillis().toString()
-
- private var seq = 0
- private var current: Active? = null
- private val lock = kotlinx.coroutines.sync.Mutex()
- private val queue = ArrayList<Listen>()
- private val persistLock = Any()
- @Volatile private var queueFile: File? = null
-
- /** A whole listen, kept until Spotify has it. The facts are frozen (real times). */
- data class Listen(
- val trackId: String,
- val albumId: String,
- val startedAt: Long,
- val playedMs: Long,
- val durationMs: Long,
- val threshold: Boolean,
- /** The Spotify context the play started from (playlist/album). */
- val context: String,
- )
-
- private class Active(val trackId: String, val albumId: String, val durationMs: Long, val context: String) {
- val playbackId = randomHex(16)
- val startedAt = System.currentTimeMillis()
- var playedMs = 0L
- var threshold = false
- var lastTick = System.currentTimeMillis()
- var playing = false
- var broken = false
- /** The last position the player showed, for the state PUTs. */
- var positionMs = 0L
- }
-
- /** Called with every state change of a local track (the LocalPlayer states). */
- fun onLocalState(t: LocalTrack, isPlaying: Boolean, pos: Long, dur: Long) {
- scope.launch { lock.withLock { step(t.id, isPlaying, pos, dur) } }
- }
-
- /** The local player stopped (a Spotify play took over): close the listen. */
- fun finish() = scope.launch { lock.withLock { finishLocked() } }
-
- /** Replays the kept listens: on every new play and after a failure that came back. */
- fun flush() = scope.launch { lock.withLock { flushLocked() } }
-
- /** Reads the queue back once at app start, then tries to sync it. */
- fun resumePending() {
- runCatching {
- val a = JSONArray(queueFile().readText())
- synchronized(persistLock) {
- queue.clear()
- (0 until a.length()).forEach { i ->
- val o = a.getJSONObject(i)
- queue.add(Listen(
- o.getString("trackId"), o.optString("albumId"),
- o.optLong("startedAt"), o.optLong("playedMs"),
- o.optLong("durationMs"), o.optBoolean("threshold"),
- o.optString("context", ""),
- ))
- }
- }
- Log.i(TAG, "queued listens to sync: ${queue.size}")
- }
- if (queue.isNotEmpty()) flush()
- }
-
- private suspend fun step(localId: Long, isPlaying: Boolean, pos: Long, dur: Long) {
- // Runs under [lock] already (onLocalState): the Mutex is not reentrant.
- val offline = OfflineStore.byLocalId(localId)
- if (offline == null) {
- if (current != null) finishLocked() // a plain local file: nothing countable
- return
- }
- if (current?.trackId != offline.trackId) {
- finishLocked()
- val ctx = contextUri?.takeIf { it.isNotBlank() } ?: contextFallback(offline.albumId, offline.trackId)
- current = Active(offline.trackId, offline.albumId, dur, ctx).also { beginLocked(it, pos) }
- }
- val a = current ?: return
- val now = System.currentTimeMillis()
- a.positionMs = pos
- // Only the time since the last tick that was playing counts, never a pause.
- if (a.playing) a.playedMs += now - a.lastTick
- if (isPlaying) {
- if (!a.threshold && a.playedMs >= THRESHOLD_MS) {
- a.threshold = true
- if (!a.broken && !postState(a, "played_threshold_reached", pos, false)) a.broken = true
- }
- } else if (a.playing) { // paused: close this tick honestly
- if (!a.broken && !postState(a, "pause", pos, true)) a.broken = true
- }
- a.lastTick = now
- a.playing = isPlaying
- }
-
- private suspend fun beginLocked(a: Active, pos: Long) {
- a.lastTick = System.currentTimeMillis()
- Log.i(TAG, "${a.trackId}: listening, playback ${a.playbackId}")
- // The play command and the first states, as the player itself sends on a play.
- var broken = !postCommand(a)
- if (!broken && !postState(a, "before_track_load", 0, false)) broken = true
- if (!broken && !postState(a, "started_playing", pos, false)) broken = true
- if (!broken) postMelody(listOf(playbackStartMessage(a)))
- a.broken = broken
- flushLocked()
- }
-
- private suspend fun finishLocked() {
- val a = current ?: return
- current = null
- // The tail tick: what the last pause did not account for still counts.
- if (a.playing) a.playedMs += System.currentTimeMillis() - a.lastTick
- // The player only publishes on state changes: a song played straight through
- // crosses the threshold here, at its end.
- if (!a.threshold && a.playedMs >= THRESHOLD_MS) {
- a.threshold = true
- if (!a.broken && !postState(a, "played_threshold_reached", a.positionMs, false)) a.broken = true
- }
- val listen = listenOf(a)
- if (a.playedMs < 1_000) {
- Log.d(TAG, "${a.trackId}: dropped (${a.playedMs} ms)")
- return
- }
- var broken = a.broken
- if (!broken && !postState(a, "track_data_finalized", a.positionMs, false)) broken = true
- if (!broken && !postMelody(listOf(streamVerificationMessage(a, listen.playedMs)))) broken = true
- a.broken = broken
- if (listen.threshold && broken) {
- enqueue(listen)
- Log.i(TAG, "${a.trackId}: kept for later sync (${listen.playedMs} ms)")
- } else if (listen.threshold) {
- Log.i(TAG, "${a.trackId}: reported (${listen.playedMs} ms)")
- } else {
- Log.i(TAG, "${a.trackId}: under the threshold, not counted (${listen.playedMs} ms)")
- }
- }
-
- /** Replays the kept listens now that the network is back. Keeps what still fails. */
- private suspend fun flushLocked() {
- if (queue.isEmpty()) return
- if (OfflineStore.isOnline(Graph.app) && RelayCredentials.ready()) {
- val left = ArrayList<Listen>()
- for (listen in queue) {
- if (replay(listen)) Log.i(TAG, "synced: ${listen.trackId} (${listen.playedMs} ms)")
- else left.add(listen)
- }
- queue.clear(); queue.addAll(left)
- persistLocked()
- }
- }
-
- private fun enqueue(listen: Listen) = synchronized(persistLock) {
- queue.add(listen); persistLocked()
- }
-
- /** A kept listen replays on the same context it started from, its facts, its own fresh playback id. */
- private suspend fun replay(listen: Listen): Boolean {
- if (OfflineStore.isOnline(Graph.app) && !RelayCredentials.ready()) return false
- val a = Active(listen.trackId, listen.albumId, listen.durationMs, contextFor(listen)).apply {
- playedMs = listen.playedMs
- threshold = listen.threshold
- positionMs = listen.playedMs
- }
- return runCatching {
- val command = postCommand(a)
- val states = command &&
- postState(a, "before_track_load", 0, false) &&
- postState(a, "played_threshold_reached", listen.playedMs, false) &&
- postState(a, "track_data_finalized", listen.playedMs, false)
- val facts = states && postMelody(listOf(
- playbackStartMessage(a),
- streamVerificationMessage(a, listen.playedMs),
- ))
- command && states && facts
- }.getOrDefault(false)
- }
-
- // ── The three request shapes the player itself uses (capture-decoded) ────
-
- /** The play command that opens a listening session on Spotify's cluster. */
- private suspend fun postCommand(a: Active): Boolean {
- val ctx = a.context
- val body = JSONObject().put("command", JSONObject()
- .put("context", JSONObject().put("uri", ctx).put("url", "context://$ctx").put("metadata", JSONObject()))
- .put("play_origin", JSONObject()
- .put("feature_identifier", "playlist")
- .put("feature_version", FEATURE_VERSION)
- .put("referrer_identifier", "library"))
- .put("options", JSONObject()
- .put("license", "tft")
- .put("skip_to", JSONObject())
- .put("player_options_override", JSONObject()))
- .put("logging_params", JSONObject().put("page_instance_ids", JSONArray()).put("interaction_ids", JSONArray()))
- .put("endpoint", "play"))
- return send(
- "/connect-state/v1/player/command/from/${RelayCredentials.deviceId}/to/${RelayCredentials.deviceId}",
- body,
- )
- }
-
- /** The play state machine: milestones at the real position, never a padded one. */
- private suspend fun postState(a: Active, debugSource: String, position: Long, paused: Boolean): Boolean {
- val body = JSONObject()
- .put("seq_num", seq++)
- .put("state_ref", JSONObject()
- .put("state_machine_id", randomB64(21))
- .put("state_id", a.playbackId)
- .put("paused", paused))
- .put("sub_state", JSONObject()
- .put("playback_speed", if (paused) 0 else 1)
- .put("position", position)
- .put("duration", a.durationMs)
- .put("media_type", "AUDIO")
- .put("bitrate", 128000) // the player's own report shape for a streamed track
- .put("audio_quality", "HIGH")
- .put("format", 10)
- .put("is_video_on", false))
- .put("debug_source", debugSource)
- return send("/track-playback/v1/devices/${RelayCredentials.deviceId}/state", body)
- }
-
- /** The SDK telemetry batch: when a play started, and how long it really played. */
- private suspend fun postMelody(messages: List<JSONObject>): Boolean {
- val body = JSONObject()
- .put("messages", JSONArray().apply { messages.forEach { put(it) } })
- .put("sdk_id", "harmony:4.81.1")
- .put("platform", PLAY_PLATFORM)
- .put("client_version", "0.0.0")
- return send("/melody/v1/msg/batch", body)
- }
-
- private fun playbackStartMessage(a: Active) = JSONObject()
- .put("type", "jssdk_playback_start")
- .put("message", JSONObject()
- .put("play_track", a.trackUri())
- .put("playback_id", a.playbackId)
- .put("session_id", sessionId)
- .put("ms_start_position", 0)
- .put("initially_paused", false)
- .put("feature_identifier", ""))
-
- private fun streamVerificationMessage(a: Active, playedMs: Long) = JSONObject()
- .put("type", "track_stream_verification")
- .put("message", JSONObject()
- .put("play_track", a.trackUri())
- .put("playback_id", a.playbackId)
- .put("ms_played", playedMs)
- .put("ms_nominal_played", playedMs)
- .put("session_id", sessionId)
- .put("sequence_id", 0)
- .put("next_playback_id", "")
- .put("playback_service", "track-playback"))
-
- private suspend fun send(path: String, body: JSONObject): Boolean {
- val c = RelayCredentials
- val host = c.clusterHost.ifBlank { "spclient.wg.spotify.com" }
- val url = "https://$host$path"
- try {
- val req = Request.Builder().url(url)
- .post(body.toString().toRequestBody("application/json".toMediaType()))
- .header("Authorization", c.authorization)
- .header("Client-Token", c.clientToken)
- .header("User-Agent", PLAY_UA)
- .header("Origin", "https://open.spotify.com")
- .header("Referer", "https://open.spotify.com/")
- .build()
- http.newCall(req).execute().use { resp ->
- if (resp.isSuccessful) {
- Log.d(TAG, "${path.substringBefore('?')} -> ${resp.code}")
- return true
- }
- Log.w(TAG, "${path.substringBefore('?')} -> ${resp.code}")
- if (resp.code == 401) c.authorization = "" // the page refreshes it on its next call
- return false
- }
- } catch (e: Exception) {
- Log.d(TAG, "${path.substringBefore('?')} failed: ${e.message}")
- return false
- }
- }
-
- // ── Queue persistence (the offline part) ─────────────────────────────────
-
- private fun listenOf(a: Active) = Listen(
- trackId = a.trackId, albumId = a.albumId, startedAt = a.startedAt,
- playedMs = a.playedMs, durationMs = a.durationMs, threshold = a.threshold,
- context = a.context,
- )
-
- private fun persistLocked() = synchronized(persistLock) {
- runCatching {
- val a = JSONArray()
- queue.forEach { l ->
- a.put(JSONObject()
- .put("trackId", l.trackId).put("albumId", l.albumId)
- .put("startedAt", l.startedAt).put("playedMs", l.playedMs)
- .put("durationMs", l.durationMs).put("threshold", l.threshold)
- .put("context", l.context))
- }
- queueFile().writeText(a.toString())
- }
- Unit
- }
-
- private fun queueFile() = queueFile ?: File(Graph.app.filesDir, "wrapped_queue.json").also { queueFile = it }
-
- // ── Pure helpers (unit-tested) ────────────────────────────────────────────
-
- /** A deferred listen replays on the context it started from. */
- internal fun contextFor(l: Listen): String =
- l.context.ifBlank { contextFallback(l.albumId, l.trackId) }
-
- private fun contextFallback(albumId: String, trackId: String): String = when {
- albumId.isNotBlank() -> "spotify:album:$albumId"
- else -> "spotify:track:$trackId"
- }
-
- /** The UA the Web Player's own requests use (one device, one voice). */
- internal const val PLAY_UA =
- "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36"
-
- /** The melody platform string the player sends. */
- internal const val PLAY_PLATFORM = "web_player linux undefined;chrome 155.0.0.0;desktop"
-
- /** The player version string, as the capture kept it. */
- internal const val FEATURE_VERSION = "web-player_2026-10-15_1792092088506_c66299947cb4"
-
- internal fun randomHex(bytes: Int): String =
- (0 until bytes).joinToString("") { "%02x".format(java.security.SecureRandom().nextInt(256)) }
-
- internal fun randomB64(bytes: Int): String = java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(
- ByteArray(bytes).also { java.security.SecureRandom().nextBytes(it) },
- )
-
- private fun Active.trackUri() = "spotify:track:$trackId"
-}
diff --git a/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt b/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt
index a3c57fdd..b671bc22 100644
--- a/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt
+++ b/base/app/src/main/java/com/music/spotui/di/LocalPlayer.kt
@@ -54,8 +54,6 @@ object LocalPlayer {
if (!active.value) return
active.value = false
exo?.let { it.stop(); it.clearMediaItems() }
- // Whatever this run played gets closed honestly (Wrapped reporting).
- com.music.spotui.data.offline.WrappedSync.finish()
}
fun resume() { exo?.play() }
diff --git a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt b/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt
index 20ecb6e9..1eaea4d3 100644
--- a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt
+++ b/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt
@@ -29,6 +29,8 @@ import java.util.concurrent.ConcurrentHashMap
object SongPlayer {
private const val SPOTIFY_TRACK_PREFIX = "spotify:track:"
private const val PENDING_MS = 6000L
+ /** After a local file starts, engine states this recent still predate the pause. */
+ private const val LOCAL_GRACE_MS = 2500L
private const val TAG = "MspPlayer"
private const val LIKED_CONTEXT = "spotify:collection:tracks"
@@ -255,7 +257,7 @@ object SongPlayer {
private fun applyState(s: PlayerState?) {
if (s == null) return
stateSeen = true
- if (LocalPlayer.active.value) return
+ if (LocalPlayer.active.value && engineWhileLocal(s)) return
if (resumeWhenReady) {
resumeWhenReady = false
// The Web Player restored the last session paused: play what was tapped.
@@ -783,8 +785,40 @@ object SongPlayer {
/** A local file starts: Spotify pauses (wherever it plays). */
fun pauseEngineForLocal() {
+ localStartedAt = SystemClock.elapsedRealtime()
+ engineUriBeforeLocal = _nowPlaying.value.uri
+ engineRepaused = false
if (playing) command { api.player.pause() }
playing = false
+ // The local player keeps the output open itself; the engine's states, which
+ // would close the silent stream, are ignored while it plays.
+ SpeakerKeepAlive.set(false)
+ }
+
+ private var localStartedAt = 0L
+ /** What the engine played when the local file started (to tell a pause that did not take from a takeover). */
+ private var engineUriBeforeLocal = ""
+ private var engineRepaused = false
+
+ /**
+ * The engine reports playing while a local file plays. Two streams must never play
+ * at once: a pause that did not take (the engine was not ready) is sent once more;
+ * anything else is Spotify taking over (a Connect "play on this phone", a resume from
+ * another device), and the local player yields. True when the state is to be ignored.
+ */
+ private fun engineWhileLocal(s: PlayerState): Boolean {
+ if (s.isPaused) return true
+ // The page's last states from before the pause reached it.
+ if (SystemClock.elapsedRealtime() - localStartedAt < LOCAL_GRACE_MS) return true
+ if (s.item?.uri == engineUriBeforeLocal && !engineRepaused) {
+ engineRepaused = true
+ Log.i(TAG, "engine still playing under a local file: pausing it again")
+ command { api.player.pause() }
+ return true
+ }
+ Log.i(TAG, "engine started playing: the local player yields")
+ LocalPlayer.stop()
+ return false
}
/** The local player's track / state, shown like any other (player, mini player, notification). */
@@ -805,9 +839,6 @@ object SongPlayer {
val source = if (t.uri.scheme == "file") com.music.spotui.data.local.ListenHistory.Source.DOWNLOAD
else com.music.spotui.data.local.ListenHistory.Source.LOCAL
trackListen(uri, song, source, isPlaying)
- // The offline report: a downloaded song this app really played gets its
- // play counted on Spotify's side (Wrapped), now or once back online.
- com.music.spotui.data.offline.WrappedSync.onLocalState(t, isPlaying, pos, dur)
}
}
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt
index afe9d449..f62295b2 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/ContextScreen.kt
@@ -259,9 +259,6 @@ class ContextViewModel(
if (local.size < p.tracks.size)
android.widget.Toast.makeText(app, app.getString(R.string.offline_only_downloaded, local.size, p.tracks.size), android.widget.Toast.LENGTH_SHORT).show()
com.music.spotui.di.LocalPlayer.play(local.map(com.music.spotui.data.offline.OfflineStore::toLocalTrack), at, context = p.name)
- // The downloads play from the list the user chose: the Wrapped report
- // starts on that context (playlist/album), like a streamed play.
- com.music.spotui.data.offline.WrappedSync.contextUri = p.uri.takeIf { it.isNotBlank() }
return
}
state.updateQueue(p.tracks)
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/DownloadsScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/DownloadsScreen.kt
index c807638f..a279eded 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/DownloadsScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/DownloadsScreen.kt
@@ -79,9 +79,6 @@ fun DownloadsScreen(navController: NavController) {
fun play(index: Int, shuffle: Boolean = false) {
if (tracks.isEmpty()) return
LocalPlayer.play(tracks.map(OfflineStore::toLocalTrack), index, shuffle = shuffle, context = title)
- // The whole Downloaded list has no Spotify context of its own: each listen
- // reports under the track's album.
- com.music.spotui.data.offline.WrappedSync.contextUri = null
}
Box(Modifier.fillMaxSize().background(AppBackground)) {
diff --git a/base/app/src/test/java/com/music/spotui/data/offline/WrappedSyncTest.kt b/base/app/src/test/java/com/music/spotui/data/offline/WrappedSyncTest.kt
deleted file mode 100644
index e95c5269..00000000
--- a/base/app/src/test/java/com/music/spotui/data/offline/WrappedSyncTest.kt
+++ /dev/null
@@ -1,38 +0,0 @@
-package com.music.spotui.data.offline
-
-import org.junit.Assert.assertEquals
-import org.junit.Assert.assertFalse
-import org.junit.Assert.assertTrue
-import org.junit.Test
-
-/** The honest-reporting rules: real listens only, same context as the play. */
-class WrappedSyncTest {
-
- private fun listen(context: String = "") = WrappedSync.Listen(
- trackId = "3URIq3iShZ0nBF3gcCwqHy", albumId = "4aeweJs3H2T3YfTzzzW2qL",
- startedAt = 1, playedMs = 45_000, durationMs = 200_000, threshold = true, context = context,
- )
-
- @Test fun deferredListensReplayOnTheContextTheyStartedFrom() {
- assertEquals("spotify:playlist:abc123", WrappedSync.contextFor(listen("spotify:playlist:abc123")))
- // No recorded context (the Downloaded list): the track's album.
- assertEquals("spotify:album:4aeweJs3H2T3YfTzzzW2qL", WrappedSync.contextFor(listen()))
- }
-
- @Test fun albumFallbackNeverProducesAUriWithoutAnId() {
- val noAlbum = WrappedSync.Listen("0K4Ym9aQqD06mG6rJ0SfWZ", "", 1, 40_000, 180_000, true, "")
- assertEquals("spotify:track:0K4Ym9aQqD06mG6rJ0SfWZ", WrappedSync.contextFor(noAlbum))
- }
-
- @Test fun thePlayerIdsAreFullyAllocated() {
- assertEquals(32, WrappedSync.randomHex(16).length)
- assertTrue(WrappedSync.randomB64(21).matches(Regex("[A-Za-z0-9_-]{28}")))
- }
-
- @Test fun theCountableThresholdIsSpotifyS() {
- assertEquals(30_000L, WrappedSync.THRESHOLD_MS)
- // sanity: a 29 s listen does not cross it.
- assertFalse(29_000L >= WrappedSync.THRESHOLD_MS)
- assertTrue(30_500L >= WrappedSync.THRESHOLD_MS)
- }
-}
diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/ControlRelay.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/ControlRelay.kt
index 66f8d584..27454d7f 100644
--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/ControlRelay.kt
+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/ControlRelay.kt
@@ -68,7 +68,6 @@ internal class ControlRelay(private val view: WebView) {
headers["Origin"] = "https://open.spotify.com"
headers["Referer"] = "https://open.spotify.com/"
val built = headers.build()
- captureCredentials(url, built)
// The player's registration leaves out the ad formats (see AdBlock).
val sent = if (body != null && app.myspoti.spotify.bridge.AdBlock.isDeviceRegistration(method, url)) app.myspoti.spotify.bridge.AdBlock.withoutAdFormats(body) else body
// Bytes, not a String body: OkHttp would append "; charset=utf-8" to the content type.
@@ -101,28 +100,6 @@ internal class ControlRelay(private val view: WebView) {
private fun shortPath(url: String) = url.substringBefore('?').substringAfter(".com").replace(Regex("[0-9a-f]{20,}"), "…")
- /**
- * Keeps the player's own credentials so the app's playback reports can use
- * them (see [RelayCredentials]): the device id from the reported paths, the
- * cluster host from the URL, and the auth headers the player itself sent.
- */
- private fun captureCredentials(url: String, headers: Headers) {
- try {
- val c = RelayCredentials
- Regex("/track-playback/v1/devices/([0-9a-fA-F]{32,64})/?").find(url)?.groupValues?.get(1)
- ?.let { if (c.deviceId.isBlank()) c.deviceId = it.lowercase() }
- Regex("/connect-state/v1/player/command/from/([0-9a-fA-F]{32,64})").find(url)?.groupValues?.get(1)
- ?.let { c.deviceId = it.lowercase() }
- val host = Regex("https://([a-z0-9.-]+\\.spotify\\.com)/").find(url)?.groupValues?.get(1)
- if (host != null && host.contains("spclient")) c.clusterHost = host
- headers["authorization"]?.takeIf { it.isNotBlank() && it.startsWith("Bearer ", true) }
- ?.let { c.authorization = it } // the latest token the page sent; never an empty one
- headers["client-token"]?.takeIf { it.isNotBlank() }?.let { c.clientToken = it }
- if (c.ready()) c.seenAt = System.currentTimeMillis()
- } catch (_: Exception) {
- }
- }
-
companion object {
private val RELAYED = Regex("^https://[a-z0-9-]+(\\.[a-z0-9-]+)*\\.spotify\\.com/(connect-state|track-playback)/v1/")
diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/RelayCredentials.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/RelayCredentials.kt
deleted file mode 100644
index 1b05e5f8..00000000
--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/RelayCredentials.kt
+++ /dev/null
@@ -1,29 +0,0 @@
-package app.myspoti.host.android
-
-/**
- * The playback credentials the app's own requests need, captured from the Web
- * Player's own traffic as [ControlRelay] relays it: the device id, its cluster
- * host, and the page's auth tokens. Nothing is guessed — every value was sent
- * by the page itself.
- */
-object RelayCredentials {
-
- /** The Web Player's Spotify device id (bare hex, ~40 chars). */
- @Volatile var deviceId: String = ""
-
- /** The cluster host the player talks to, e.g. "gew4-spclient.spotify.com". */
- @Volatile var clusterHost: String = ""
-
- /** The page's Bearer token (rotates; refreshed whenever the player calls out). */
- @Volatile var authorization: String = ""
-
- /** The page's client token (long lived, per app instance). */
- @Volatile var clientToken: String = ""
-
- /** Last time any of these were seen and used successfully by the player. */
- @Volatile var seenAt: Long = 0
-
- /** All of it or nothing: a play report needs every piece. */
- fun ready(): Boolean = deviceId.isNotBlank() && clusterHost.isNotBlank() &&
- authorization.isNotBlank() && clientToken.isNotBlank()
-}