Skip to content

Native Android client for Spotify, built with Jetpack Compose

Jam on a free account: joining by link or QR works (sent to Spotify directly, as in the room: the page's service gave up on an account it marks restricted), leaving and the session's state go to Spotify too, and the Jam sheet says that starting one needs Premium (Spotify answers FREE_USER_NOT_ALLOWED)

kota-zo85ffbb35f50a
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyApi.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyApi.kt
index 46f05ef7..53048be8 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyApi.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyApi.kt
@@ -61,7 +61,7 @@ class SpotifyApi(
override val users = SpotifyUsers(gql, bridge, this.http)
override val extras = SpotifyTrackExtras(gql, bridge, this.http)
override val devices = SpotifyDevices(bridge)
- override val jam = SpotifyJam(bridge)
+ override val jam = SpotifyJam(bridge, this.http)
override val blends = SpotifyBlends(this.http)
override val recents = SpotifyRecents(gql)
diff --git a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyMore.kt b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyMore.kt
index 271932ba..c711a80f 100644
--- a/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyMore.kt
+++ b/base/api-spotify/src/main/kotlin/app/myspoti/spotify/SpotifyMore.kt
@@ -46,6 +46,9 @@ import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.serialization.json.JsonArray
+import kotlinx.serialization.json.put
+import kotlinx.serialization.json.buildJsonObject
+import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive
import java.net.URLEncoder
@@ -481,7 +484,8 @@ class SpotifyDevices(private val bridge: Bridge) : Devices {
}
/** Jam goes through the page's SocialConnectAPI. Free accounts get RESTRICTED from Spotify. */
-class SpotifyJam(private val bridge: Bridge) : Jam {
+class SpotifyJam(private val bridge: Bridge, private val http: SpotifyHttp) : Jam {
+ private companion object { const val SOCIAL = "https://spclient.wg.spotify.com/social-connect" }
private val _session = MutableStateFlow<JamSession?>(null)
override val session: StateFlow<JamSession?> = _session
@@ -499,7 +503,9 @@ class SpotifyJam(private val bridge: Bridge) : Jam {
userUri = "spotify:user:" + (m.str("username") ?: m.str("id") ?: ""),
name = m.str("display_name") ?: m.str("displayName") ?: m.str("username") ?: "",
imageUrl = m.str("image_url") ?: m.str("imageUrl") ?: m.str("large_image_url"),
- host = m.bool("is_host") ?: m.bool("isHost") ?: (m.str("username") == s.str("session_owner_id")),
+ // session_owner_id is the member's id (hex), not its username.
+ host = m.bool("is_host") ?: m.bool("isHost")
+ ?: ((s.str("session_owner_id") ?: s.str("sessionOwnerId")).let { o -> o != null && (m.str("id") == o || m.str("username") == o) }),
)
}
return JamSession(
@@ -512,7 +518,12 @@ class SpotifyJam(private val bridge: Bridge) : Jam {
)
}
- override suspend fun refresh() { _session.value = parse(bridge.service("SocialConnectAPI", "fetchCurrentSession")) }
+ /** The session this account is in, read from Spotify (404: none): also a Jam joined in the room, which the page does not know. */
+ override suspend fun refresh() {
+ val (code, body) = http.send("GET", "$SOCIAL/v2/sessions/current")
+ _session.value = if (code in 200..299) parse(runCatching { Json.parseToJsonElement(body) }.getOrNull()) else if (code == 404) null
+ else parse(bridge.service("SocialConnectAPI", "fetchCurrentSession"))
+ }
override suspend fun start(): JamSession {
if (availability() == JamAvailability.RESTRICTED) throw UnsupportedOperationException("Jam is restricted on this account")
@@ -524,26 +535,31 @@ class SpotifyJam(private val bridge: Bridge) : Jam {
}
override suspend fun join(joinToken: String): JamSession {
- // As the page's own join dialog does (the token alone sent an incomplete join that did nothing):
- // a link join ("deeplinking") listening here (REMOTE: listen and control); an account that may
- // not (Spotify keeps that for Premium) joins as in the room (IN_PERSON: it controls the host's
- // playback). POST social-connect/v3/sessions/join, each try with its own attempt id.
- var failure: Throwable? = null
- for (mode in listOf("REMOTE", "IN_PERSON")) {
- val r = runCatching {
- bridge.service("SocialConnectAPI", "joinSession", JsonPrimitive(joinToken), JsonPrimitive("deeplinking"),
- JsonPrimitive(mode), JsonPrimitive(java.util.UUID.randomUUID().toString()))
+ // Sent to Spotify directly, as the page's own join dialog sends it (docs/api/JAM.md): the page's
+ // service gives up at once on an account it marks RESTRICTED, while Spotify lets a free account
+ // join as in the room. Listening here first (LISTEN_AND_CONTROL, Premium), else in the room
+ // (CONTROL: this phone controls the host's playback), each try with its own attempt id.
+ var last = 0 to ""
+ for (control in listOf("LISTEN_AND_CONTROL", "CONTROL")) {
+ val body = buildJsonObject {
+ put("join_token", joinToken); put("join_type", "deeplinking"); put("playback_control", control)
+ put("join_attempt_id", java.util.UUID.randomUUID().toString()); put("permission_mode", "FULL")
}
- failure = r.exceptionOrNull() ?: failure
- if (r.getOrNull().let { it != null && it !is kotlinx.serialization.json.JsonNull }) { failure = null; break }
+ last = http.send("POST", "$SOCIAL/v3/sessions/join", body.toString())
+ if (last.first in 200..299) parse(runCatching { Json.parseToJsonElement(last.second) }.getOrNull())?.let { _session.value = it; return it }
}
- refresh()
- failure?.takeIf { _session.value == null }?.let { throw it }
- return _session.value ?: throw GqlException("jam", "join failed")
+ throw GqlException("jam", "join failed: HTTP ${last.first} ${last.second.take(200)}")
}
- override suspend fun leave() { bridge.service("SocialConnectAPI", "leaveSession"); _session.value = null }
- override suspend fun end() { bridge.service("SocialConnectAPI", "deleteSession"); _session.value = null }
+ override suspend fun leave() {
+ _session.value?.let { http.send("POST", "$SOCIAL/v3/sessions/${it.id}/leave") }
+ _session.value = null
+ }
+
+ override suspend fun end() {
+ _session.value?.let { http.send("DELETE", "$SOCIAL/v3/sessions/${it.id}") }
+ _session.value = null
+ }
override suspend fun inviteLink(): String? {
// The page shortens a link from the join token and the session's URI (not its id: that gave nothing).
diff --git a/base/app/src/main/java/com/music/spotui/ui/components/PlayerSheets.kt b/base/app/src/main/java/com/music/spotui/ui/components/PlayerSheets.kt
index 4abcc072..6d72397b 100644
--- a/base/app/src/main/java/com/music/spotui/ui/components/PlayerSheets.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/components/PlayerSheets.kt
@@ -412,6 +412,9 @@ private fun JamSection(onDismiss: () -> Unit) {
modifier = Modifier.fillMaxWidth().clickable {
scope.launch { if (runCatching { jam.start() }.isFailure) failed() }
}.padding(vertical = 10.dp))
+ // Spotify refuses to start a Jam on a free account (FREE_USER_NOT_ALLOWED); joining one works.
+ if (availability == app.myspoti.api.JamAvailability.RESTRICTED && session == null) Text(stringResource(R.string.jam_start_premium),
+ color = AppMuted, fontSize = 13.sp, modifier = Modifier.padding(vertical = 6.dp))
// Join: scan the QR code another phone shows in its Jam.
Text(stringResource(R.string.jam_scan), color = Green, fontSize = 15.sp,
modifier = Modifier.fillMaxWidth().clickable {
diff --git a/base/app/src/main/res/values-de/myspoti_strings.xml b/base/app/src/main/res/values-de/myspoti_strings.xml
index 36b9669f..f983df90 100644
--- a/base/app/src/main/res/values-de/myspoti_strings.xml
+++ b/base/app/src/main/res/values-de/myspoti_strings.xml
@@ -707,4 +707,5 @@
<string name="settings_automix_pulse_subtle">Dezent</string>
<string name="settings_automix_pulse_strong">Stark</string>
<string name="playback_speed_note">Tempo %1$s× · tippen für 1×</string>
+ <string name="jam_start_premium">Einen Jam starten braucht Spotify Premium. Beitreten geht mit dem Link oder QR-Code.</string>
</resources>
diff --git a/base/app/src/main/res/values-el/myspoti_strings.xml b/base/app/src/main/res/values-el/myspoti_strings.xml
index 8a87b0ac..ae7fca71 100644
--- a/base/app/src/main/res/values-el/myspoti_strings.xml
+++ b/base/app/src/main/res/values-el/myspoti_strings.xml
@@ -707,4 +707,5 @@
<string name="settings_automix_pulse_subtle">Διακριτική</string>
<string name="settings_automix_pulse_strong">Έντονη</string>
<string name="playback_speed_note">Ταχύτητα %1$s× · πάτησε για 1×</string>
+ <string name="jam_start_premium">Η έναρξη Jam απαιτεί Spotify Premium. Μπορείς να μπεις σε ένα με τον σύνδεσμο ή τον κωδικό QR.</string>
</resources>
diff --git a/base/app/src/main/res/values-es/myspoti_strings.xml b/base/app/src/main/res/values-es/myspoti_strings.xml
index 5de707a9..e56a6ad6 100644
--- a/base/app/src/main/res/values-es/myspoti_strings.xml
+++ b/base/app/src/main/res/values-es/myspoti_strings.xml
@@ -707,4 +707,5 @@
<string name="settings_automix_pulse_subtle">Suave</string>
<string name="settings_automix_pulse_strong">Fuerte</string>
<string name="playback_speed_note">Velocidad %1$s× · toca para 1×</string>
+ <string name="jam_start_premium">Iniciar una Jam requiere Spotify Premium. Puedes unirte a una con su enlace o código QR.</string>
</resources>
diff --git a/base/app/src/main/res/values-fr/myspoti_strings.xml b/base/app/src/main/res/values-fr/myspoti_strings.xml
index 8023e10c..138f0460 100644
--- a/base/app/src/main/res/values-fr/myspoti_strings.xml
+++ b/base/app/src/main/res/values-fr/myspoti_strings.xml
@@ -712,4 +712,5 @@
<string name="settings_automix_pulse_subtle">Légère</string>
<string name="settings_automix_pulse_strong">Forte</string>
<string name="playback_speed_note">Vitesse %1$s× · toucher pour 1×</string>
+ <string name="jam_start_premium">Lancer un Jam demande Spotify Premium. Tu peux en rejoindre un avec son lien ou son QR code.</string>
</resources>
diff --git a/base/app/src/main/res/values-it/myspoti_strings.xml b/base/app/src/main/res/values-it/myspoti_strings.xml
index 96f153a4..41d61842 100644
--- a/base/app/src/main/res/values-it/myspoti_strings.xml
+++ b/base/app/src/main/res/values-it/myspoti_strings.xml
@@ -707,4 +707,5 @@
<string name="settings_automix_pulse_subtle">Leggera</string>
<string name="settings_automix_pulse_strong">Forte</string>
<string name="playback_speed_note">Velocità %1$s× · tocca per 1×</string>
+ <string name="jam_start_premium">Avviare una Jam richiede Spotify Premium. Puoi unirti a una con il link o il codice QR.</string>
</resources>
diff --git a/base/app/src/main/res/values-pl/myspoti_strings.xml b/base/app/src/main/res/values-pl/myspoti_strings.xml
index 7042a111..e0b8124b 100644
--- a/base/app/src/main/res/values-pl/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pl/myspoti_strings.xml
@@ -722,4 +722,5 @@
<string name="settings_automix_pulse_subtle">Delikatna</string>
<string name="settings_automix_pulse_strong">Mocna</string>
<string name="playback_speed_note">Prędkość %1$s× · dotknij, aby 1×</string>
+ <string name="jam_start_premium">Rozpoczęcie Jamu wymaga Spotify Premium. Możesz dołączyć do jednego przez link lub kod QR.</string>
</resources>
diff --git a/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml b/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml
index 0614d3a3..892ab7e9 100644
--- a/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pt-rBR/myspoti_strings.xml
@@ -685,4 +685,5 @@
<string name="settings_automix_pulse_subtle">Suave</string>
<string name="settings_automix_pulse_strong">Forte</string>
<string name="playback_speed_note">Velocidade %1$s× · toque para 1×</string>
+ <string name="jam_start_premium">Iniciar uma Jam exige Spotify Premium. Você pode entrar em uma pelo link ou QR code.</string>
</resources>
diff --git a/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml b/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml
index 93281c94..e0b35262 100644
--- a/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pt-rPT/myspoti_strings.xml
@@ -654,4 +654,5 @@
<string name="settings_automix_pulse_subtle">Suave</string>
<string name="settings_automix_pulse_strong">Forte</string>
<string name="playback_speed_note">Velocidade %1$s× · toque para 1×</string>
+ <string name="jam_start_premium">Iniciar uma Jam requer Spotify Premium. Pode juntar-se a uma com a ligação ou o código QR.</string>
</resources>
diff --git a/base/app/src/main/res/values-pt/myspoti_strings.xml b/base/app/src/main/res/values-pt/myspoti_strings.xml
index f61f5a6a..ac90abd5 100644
--- a/base/app/src/main/res/values-pt/myspoti_strings.xml
+++ b/base/app/src/main/res/values-pt/myspoti_strings.xml
@@ -707,4 +707,5 @@
<string name="settings_automix_pulse_subtle">Suave</string>
<string name="settings_automix_pulse_strong">Forte</string>
<string name="playback_speed_note">Velocidade %1$s× · toque para 1×</string>
+ <string name="jam_start_premium">Iniciar uma Jam requer Spotify Premium. Pode juntar-se a uma com a ligação ou o código QR.</string>
</resources>
diff --git a/base/app/src/main/res/values-ru/myspoti_strings.xml b/base/app/src/main/res/values-ru/myspoti_strings.xml
index a6440348..e7f44994 100644
--- a/base/app/src/main/res/values-ru/myspoti_strings.xml
+++ b/base/app/src/main/res/values-ru/myspoti_strings.xml
@@ -722,4 +722,5 @@
<string name="settings_automix_pulse_subtle">Слабая</string>
<string name="settings_automix_pulse_strong">Сильная</string>
<string name="playback_speed_note">Скорость %1$s× · нажмите для 1×</string>
+ <string name="jam_start_premium">Чтобы начать Jam, нужен Spotify Premium. Присоединиться можно по ссылке или QR-коду.</string>
</resources>
diff --git a/base/app/src/main/res/values-zh/myspoti_strings.xml b/base/app/src/main/res/values-zh/myspoti_strings.xml
index 053f8ee0..11aa0366 100644
--- a/base/app/src/main/res/values-zh/myspoti_strings.xml
+++ b/base/app/src/main/res/values-zh/myspoti_strings.xml
@@ -707,4 +707,5 @@
<string name="settings_automix_pulse_subtle">轻微</string>
<string name="settings_automix_pulse_strong">强烈</string>
<string name="playback_speed_note">速度 %1$s× · 点按恢复 1×</string>
+ <string name="jam_start_premium">发起 Jam 需要 Spotify Premium。你可以通过链接或二维码加入。</string>
</resources>
diff --git a/base/app/src/main/res/values/myspoti_strings.xml b/base/app/src/main/res/values/myspoti_strings.xml
index c011cb1a..b3f6a80e 100644
--- a/base/app/src/main/res/values/myspoti_strings.xml
+++ b/base/app/src/main/res/values/myspoti_strings.xml
@@ -713,4 +713,5 @@
<string name="settings_automix_pulse_subtle">Subtle</string>
<string name="settings_automix_pulse_strong">Strong</string>
<string name="playback_speed_note">Speed %1$s× · tap for 1×</string>
+ <string name="jam_start_premium">Starting a Jam needs Spotify Premium. You can join one with its link or QR code.</string>
</resources>