Native Android client for Spotify, built with Jetpack Compose
Kotlin ad-freeadblockandroidjetpack-composekotlinlyricsmusic-playermusic-videomyspotispotifyspotify-adblockerspotify-clientspotify-connectspotui
Diagnostics for refused licences and silent songs: the media element line of a stall says whether the element has its keys and how often it waited for one (waitingforkey), the report's summary says how long ago this WebView was signed in (no account named), and the reload after a pull-back that hangs is written in the log [skip ci]
kota-zob9285f968245
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.ktindex fcbb1b90..c9993b8f 100644--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyPlayer.kt@@ -539,8 +539,11 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScopval readyState: Int = -1,val networkState: Int = -1,val error: String? = null,+ /** The element has media keys (1) or not (0), and how often it waited for a key ("waitingforkey"); -1: not said. */+ val keys: Int = -1,+ val keyWaits: Int = -1,) {- fun describe() = "paused=$paused muted=$muted volume=$level own=$own t=${"%.1f".format(time)} ready=$readyState net=$networkState error=${error ?: "none"} active=$active"+ fun describe() = "paused=$paused muted=$muted volume=$level own=$own t=${"%.1f".format(time)} ready=$readyState net=$networkState error=${error ?: "none"} keys=$keys keyWaits=$keyWaits active=$active"}private suspend fun readMedia(): Media? {@@ -559,6 +562,8 @@ class SpotifyPlayer(private val bridge: Bridge, private val scope: CoroutineScopreadyState = p("readyState")?.intOrNull ?: -1,networkState = p("networkState")?.intOrNull ?: -1,error = p("error")?.contentOrNull?.takeIf { it != "null" },+ keys = p("keys")?.intOrNull ?: -1,+ keyWaits = p("keyWaits")?.intOrNull ?: -1,)}diff --git a/base/api-spotify/src/main/resources/bridge/player-bridge.js b/base/api-spotify/src/main/resources/bridge/player-bridge.jsindex cfbdf202..dbee9fb1 100644--- a/base/api-spotify/src/main/resources/bridge/player-bridge.js+++ b/base/api-spotify/src/main/resources/bridge/player-bridge.js@@ -494,14 +494,20 @@// level: the element's volume (0..1), which local fades move; own: the level the page// player itself wants there. playerVolume: what the page player itself is set to, read// here too so the host's periodic volume check costs no second call.- // -> {paused, muted, volume, level, own, time, readyState, networkState, error, keys, ended, playerVolume, active} | null (no element yet)+ // keyWaits: how many times the element stopped for a decryption key it did not have+ // ("waitingforkey": a licence refused, B4), counted from the first read of it.+ // -> {paused, muted, volume, level, own, time, readyState, networkState, error, keys, keyWaits, ended, playerVolume, active} | null (no element yet)'media.state': async () => {const player = await localPlayer();const el = player && player._player;if (!(el instanceof HTMLMediaElement)) return null;+ if (!el.__mspKeyWatch) {+ el.__mspKeyWatch = true;+ el.addEventListener('waitingforkey', () => { el.__mspKeyWaits = (el.__mspKeyWaits || 0) + 1; });+ }let playerVolume = null;try { playerVolume = await need().playback.getVolume(); } catch (e) { /* unknown */ }- return { paused: el.paused, muted: el.muted, volume: el.volume, level: el.volume, own: ownLevel(player), time: el.currentTime, readyState: el.readyState, networkState: el.networkState, error: el.error ? el.error.code + ':' + String(el.error.message || '').slice(0, 80) : null, keys: el.mediaKeys ? 1 : 0, ended: el.ended, playerVolume, active: await activeDevice() };+ return { paused: el.paused, muted: el.muted, volume: el.volume, level: el.volume, own: ownLevel(player), time: el.currentTime, readyState: el.readyState, networkState: el.networkState, error: el.error ? el.error.code + ':' + String(el.error.message || '').slice(0, 80) : null, keys: el.mediaKeys ? 1 : 0, keyWaits: el.__mspKeyWaits || 0, ended: el.ended, playerVolume, active: await activeDevice() };},// The media element's volume, on this phone only: no Spotify Connect command (refused on// some accounts, slow on a busy network) and nothing reported to Spotify, as the player'sdiff --git a/base/app/src/main/java/com/music/spotui/di/SongPlayer.kt b/base/app/src/main/java/com/music/spotui/di/SongPlayer.ktindex d3055f81..b288b8a4 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@@ -2118,6 +2118,7 @@ object SongPlayer {// command. A new page claims playback for itself at the first command.if (pulled == null) {Log.w(TAG, "pull here hangs: player page reloaded")+ app.myspoti.host.android.Logbook.log("pull here hangs (no answer in ${PULL_BACK_WAIT_MS / 1000} s): player page reloaded")MyspotiRuntime.spotify.player.reloadPage()}_activeDevice.value = nulldiff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/Logbook.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/Logbook.ktindex 7d073ed4..971147cb 100644--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/Logbook.kt+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/Logbook.kt@@ -162,7 +162,15 @@ object Logbook {audioDetails(context)?.let { add("audio" to it) }runCatching { headerExtras?.invoke() }.getOrNull()?.let { addAll(it) }}+ // With how long ago this WebView was signed in (a new sign-in had its licences refused, 30/10).+ val signedIn = if (MyspotiRuntime.isStarted) runCatching { MyspotiRuntime.host.signedInSince }.getOrNull() else null+ val signedInAge = when {+ signedIn == null || signedIn == 0L -> null+ signedIn < 0 -> "signed in here before this was noted"+ else -> "signed in here ${ageBucket(System.currentTimeMillis() - signedIn)}"+ }val installAge = info?.firstInstallTime?.let { ageBucket(System.currentTimeMillis() - it) }+ ?.let { age -> listOfNotNull(age, signedInAge).joinToString(", ") }val summary = app.myspoti.spotify.report.Refusal402.summary(facts["account"], installAge)// Masked and cut down (DiagnosticDigest): small enough to paste, nothing that opens an account.return buildString {diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.ktindex b30ed63e..7b57497d 100644--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.kt+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/MyspotiRuntime.kt@@ -52,6 +52,8 @@ object MyspotiRuntime {scope.launch {var last = ""var planLogged = false+ // A login page or an anonymous session seen in this run: the sign-in that follows is new.+ var sawSignedOut = false// A page that loaded but whose player never became ready (no bridge, a script that// failed): loaded again, after 60 s, then twice as long each time, up to 10 min.var notReadySince = 0L@@ -73,6 +75,7 @@ object MyspotiRuntime {// (402) needs. No name, no id — only what decides what the account may play.if (!planLogged && spotify.ready.value && runCatching { isSignedIn() }.getOrDefault(false)) {planLogged = true+ host.noteSignedIn(fresh = sawSignedOut)runCatching {val a = spotify.raw.gql("accountAttributes", kotlinx.serialization.json.buildJsonObject { }).let { (it as kotlinx.serialization.json.JsonObject)["data"] as kotlinx.serialization.json.JsonObject }@@ -91,6 +94,7 @@ object MyspotiRuntime {// The token in hand: a fresh one every 10 s held every request behind the page's answer.else -> when (spotify.signedInHint()) { true -> "signed-in"; false -> "anonymous"; null -> "unknown" }}+ if (session == "login-page" || session == "anonymous") sawSignedOut = trueval line = "status ready=${spotify.ready.value} session=$session " +"track=${s?.item?.name} paused=${s?.isPaused} buffering=${s?.isBuffering} url=${host.url.value.take(60)}"if (line != last) {diff --git a/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt b/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.ktindex 5a1f8053..6af744ea 100644--- a/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt+++ b/base/host-android/src/main/kotlin/app/myspoti/host/android/WebPlayerHost.kt@@ -414,6 +414,19 @@ class WebPlayerHost(private val context: Context) : BridgeHost {private val roots by lazy { context.getSharedPreferences("web_player", Context.MODE_PRIVATE) }+ /**+ * When a signed-in session was first seen in this WebView since the last sign-out: 0 never,+ * -1 one already signed in when this was first noted (its age unknown). For the report's+ * summary only (audio licences refused on sign-ins less than a day old, 30/10): a date, no account.+ */+ val signedInSince: Long get() = roots.getLong(SIGNED_IN_KEY, 0L)++ /** A signed-in session is there; [fresh]: it came after a login page or an anonymous one in this run. */+ fun noteSignedIn(fresh: Boolean) {+ if (signedInSince != 0L) return+ roots.edit().putLong(SIGNED_IN_KEY, if (fresh) System.currentTimeMillis() else -1L).apply()+ }+/*** Where Spotify sends "/" for this account's language (`…/intl-fr/`), loaded directly: the* redirect cost a whole navigation, about 0.5 s, on every start and reload (measured on the@@ -497,6 +510,7 @@ class WebPlayerHost(private val context: Context) : BridgeHost {/** Signs out of Spotify in this WebView, then shows the login page. */fun logout() = main.post {+ roots.edit().remove(SIGNED_IN_KEY).apply()CookieManager.getInstance().removeAllCookies {main.post {// Web storage too: localStorage/IndexedDB hold the player's session@@ -587,6 +601,7 @@ class WebPlayerHost(private val context: Context) : BridgeHost {const val PLAYER_URL = "https://open.spotify.com/"private val PLAYER_ROOT = Regex("""https://open\.spotify\.com/intl-[a-z]{2,3}(?:-[a-zA-Z]{2,4})?/""")private const val ROOT_KEY = "player_root"+ private const val SIGNED_IN_KEY = "signed_in_since"private const val PLAYER_HOST = "open.spotify.com"private const val PAGE_LOAD_TIMEOUT_MS = 25_000Lprivate const val DRM_WATCH_MS = 10_000L