Skip to content

Native Android client for Spotify, built with Jetpack Compose

Downloaded songs played from their files online are played by Spotify too, unheard, as an extension's stream is: Spotify counts them and Spotify Connect sees and commands them, with the queue sync; a new setting "Sync downloaded songs with Spotify" in the playback settings of both looks (on by default; off, or offline, the file plays alone as before), and an extension that mirrors a download itself still gets the page [skip ci]

kota-zo8dea5fdb5b12
diff --git a/base/app/src/main/java/com/music/spotui/data/preferences/SettingsPref.kt b/base/app/src/main/java/com/music/spotui/data/preferences/SettingsPref.kt
index 22a7bf27..65562a80 100644
--- a/base/app/src/main/java/com/music/spotui/data/preferences/SettingsPref.kt
+++ b/base/app/src/main/java/com/music/spotui/data/preferences/SettingsPref.kt
@@ -96,6 +96,24 @@ object ConnectMirrorPref {
}
}
+/**
+ * "Sync downloaded songs with Spotify" (on by default): a downloaded song with a Spotify id, played
+ * from its file while the phone is online, is played by the Web Player too, unheard, as an
+ * extension's stream is (see [com.music.spotui.di.ConnectMirror]): Spotify counts it, Spotify Connect
+ * sees and controls it. Off, or offline: the file plays alone, as before.
+ */
+object DownloadSyncPref {
+ private const val KEY = "download_spotify_sync"
+ var on by androidx.compose.runtime.mutableStateOf(prefs(com.music.spotui.di.Graph.app).getBoolean(KEY, true))
+ private set
+
+ fun set(c: Context, value: Boolean) {
+ on = value
+ prefs(c).edit().putBoolean(KEY, value).apply()
+ com.music.spotui.di.ConnectMirror.settingChanged()
+ }
+}
+
/**
* The automatic update check at launch (on by default: the app behaved that way before this
* option existed). Off means no check when the app starts — a check asked for in Settings still
diff --git a/base/app/src/main/java/com/music/spotui/di/ConnectMirror.kt b/base/app/src/main/java/com/music/spotui/di/ConnectMirror.kt
index 086f7f92..2a2692e4 100644
--- a/base/app/src/main/java/com/music/spotui/di/ConnectMirror.kt
+++ b/base/app/src/main/java/com/music/spotui/di/ConnectMirror.kt
@@ -8,6 +8,7 @@ import app.myspoti.host.android.MyspotiRuntime
import app.myspoti.spotify.bridge.PageDevice
import com.music.spotui.data.local.LocalTrack
import com.music.spotui.data.preferences.ConnectMirrorPref
+import com.music.spotui.data.preferences.DownloadSyncPref
import com.music.spotui.di.MirrorRules.Follow
import com.music.spotui.di.MirrorRules.Lead
import com.music.spotui.di.MirrorRules.Want
@@ -26,7 +27,8 @@ import kotlinx.coroutines.sync.withLock
* another phone) see the song and command it. Their pause, seek or skip reach the page, whose state
* the local player follows here; the phone's own controls drive the local player as before, and the
* page follows it. The play is the page's own, counted by Spotify as any other: the app reports
- * nothing itself. Downloads and files of the device are never mirrored.
+ * nothing itself. A download with a Spotify id is mirrored the same way while the phone is online
+ * ("Sync downloaded songs with Spotify", [DownloadSyncPref]); files of the device never are.
*
* Everything runs on the main thread, on the two players' own events (and one slow drift check).
*/
@@ -164,9 +166,15 @@ object ConnectMirror {
val now = SystemClock.elapsedRealtime()
val expected = if (uri != localUri) null else localAtMs + if (localWasPlaying) now - localAtTime else 0L
val moved = MirrorRules.localMoved(localWasPlaying, playing, expected, positionMs)
- if (uri != localUri) { localUri = uri; failedUri = null; backoffStep = 0 }
+ if (uri != localUri) { localUri = uri; failedUri = null; backoffStep = 0; yieldedUri = null }
localWasPlaying = playing; localAtMs = positionMs; localAtTime = now
- if (!MirrorRules.eligible(origin, id, ConnectMirrorPref.on, com.music.spotui.data.DataSaver.active)) {
+ localOrigin = origin
+ val download = origin == MirrorRules.Origin.DOWNLOAD
+ // A download is mirrored by its own setting, only online (offline, the page cannot stream).
+ val online = !download || com.music.spotui.data.offline.OfflineStore.isOnline(com.music.spotui.di.Graph.app)
+ // An extension that mirrors the download itself (playback.mirror) keeps the page, as before that setting.
+ val extensionHolds = download && (yieldedUri == uri || com.music.spotui.extensions.Extensions.mirror.active != null)
+ if (extensionHolds || !MirrorRules.eligible(origin, id, ConnectMirrorPref.on, com.music.spotui.data.DataSaver.active, DownloadSyncPref.on, online)) {
if (running) stop(if (origin == MirrorRules.Origin.AUDIO_SOURCE) "not mirrored: ${t.title}" else "a download or a file of the phone plays")
return
}
@@ -495,10 +503,31 @@ object ConnectMirror {
/** "Count extension plays on Spotify" was switched: off ends the mirror, on starts it on the song playing. */
fun settingChanged() {
- if (!ConnectMirrorPref.on) { stop("the setting was turned off"); failedUri = null }
+ val on = if (localOrigin == MirrorRules.Origin.DOWNLOAD) DownloadSyncPref.on else ConnectMirrorPref.on
+ if (!on) { stop("the setting was turned off"); failedUri = null }
else main.post(::poke)
}
+ /** Where the local player's song at the last event came from (which setting mirrors it). */
+ @Volatile private var localOrigin: MirrorRules.Origin? = null
+ /** The download whose page an extension's own mirror asked for: not mirrored here until the song changes. */
+ private var yieldedUri: String? = null
+
+ /** The mirror holds the page for a download (an extension's own mirror may take it, see [yieldToExtension]). */
+ val mirrorsDownload: Boolean get() = running && localOrigin == MirrorRules.Origin.DOWNLOAD
+
+ /**
+ * An extension's own mirror (player.setLocalMute) was let in while this mirror holds the page for
+ * a download: it gets the page, as it did before downloads were mirrored, once this mirror's end
+ * reached the page (its unmute must not undo the extension's mute).
+ */
+ suspend fun yieldToExtension() {
+ kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.Main) {
+ if (mirrorsDownload) { yieldedUri = localUri; stop("an extension mirrors this download itself") }
+ }
+ awaitEnded()
+ }
+
/** Another device of the account took playback while mirrored: the local player stops, as it would have. */
private fun wentAway() {
if (!running || want?.paused != false) return
diff --git a/base/app/src/main/java/com/music/spotui/di/MirrorRules.kt b/base/app/src/main/java/com/music/spotui/di/MirrorRules.kt
index 9bd4ee5e..cd5a1019 100644
--- a/base/app/src/main/java/com/music/spotui/di/MirrorRules.kt
+++ b/base/app/src/main/java/com/music/spotui/di/MirrorRules.kt
@@ -19,12 +19,17 @@ object MirrorRules {
private val SPOTIFY_ID = Regex("[A-Za-z0-9]{22}")
/**
- * Whether the song is mirrored: one an extension streams ([Origin.AUDIO_SOURCE]) with a Spotify
- * id, the setting [on], and Spotify able to stream ([dataSaver] off). Downloads and files of the
- * device never are: nothing of Spotify plays them.
+ * Whether the song is mirrored, always with a Spotify id and Spotify able to stream ([dataSaver]
+ * off): one an extension streams ([Origin.AUDIO_SOURCE]) with the setting [on]; a download with
+ * its own setting [downloads] while the phone is [online] (offline, its file plays alone, as
+ * before). Files of the device never are: Spotify has no song for them.
*/
- fun eligible(origin: Origin, spotifyId: String, on: Boolean, dataSaver: Boolean): Boolean =
- on && !dataSaver && origin == Origin.AUDIO_SOURCE && SPOTIFY_ID.matches(spotifyId)
+ fun eligible(origin: Origin, spotifyId: String, on: Boolean, dataSaver: Boolean, downloads: Boolean = false, online: Boolean = true): Boolean =
+ !dataSaver && SPOTIFY_ID.matches(spotifyId) && when (origin) {
+ Origin.AUDIO_SOURCE -> on
+ Origin.DOWNLOAD -> downloads && online
+ Origin.LOCAL_FILE -> false
+ }
/** What the Web Player was last asked to be on (by the mirror, or a remote command it followed). */
data class Want(val uri: String, val paused: Boolean)
diff --git a/base/app/src/main/java/com/music/spotui/di/QueueSync.kt b/base/app/src/main/java/com/music/spotui/di/QueueSync.kt
index 17d9b3fb..a1e712fe 100644
--- a/base/app/src/main/java/com/music/spotui/di/QueueSync.kt
+++ b/base/app/src/main/java/com/music/spotui/di/QueueSync.kt
@@ -62,7 +62,7 @@ object QueueSync {
}
private fun usable(): Boolean =
- ConnectMirrorPref.on && LocalPlayer.active.value && ConnectMirror.running && !SongPlayer.routing &&
+ (ConnectMirrorPref.on || com.music.spotui.data.preferences.DownloadSyncPref.on) && LocalPlayer.active.value && ConnectMirror.running && !SongPlayer.routing &&
api.ready.value && !page.playsElsewhere.value && !page.adPlaying.value
private val sync = Runnable {
diff --git a/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt b/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt
index 8b4b984f..56e4d4ee 100644
--- a/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt
+++ b/base/app/src/main/java/com/music/spotui/extensions/Extensions.kt
@@ -1376,13 +1376,16 @@ object Extensions {
try { return dispatcher.execute(call) } finally { Extensions.mirrorEndedBy(extensionId) }
}
// Myspoti's own Connect mirror has the page already ("Count extension plays on Spotify"): it wins.
- if (com.music.spotui.di.ConnectMirror.running) {
+ // A download mirrored by "Sync downloaded songs with Spotify" gives way instead, as before that setting.
+ val download = com.music.spotui.di.ConnectMirror.mirrorsDownload
+ if (com.music.spotui.di.ConnectMirror.running && !download) {
val why = "setLocalMute: Myspoti already plays this song on Spotify (Count extension plays on Spotify)"
Extensions.log(extensionId, "refused: $why"); throw IllegalStateException(why)
}
if (!arbitrate(extensionId, "setLocalMute")) return "null"
val (playing, uri, dur) = SongPlayer.localSnapshot3()
Extensions.mirror.enable(extensionId, playing, uri, dur)?.let { Extensions.log(extensionId, "refused: $it"); throw IllegalStateException(it) }
+ if (download) com.music.spotui.di.ConnectMirror.yieldToExtension()
return try { dispatcher.execute(call).also { Extensions.mirrorStartedBy(extensionId) } }
catch (t: Throwable) { Extensions.mirror.disable(extensionId); throw t }
}
diff --git a/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveSettingsScreen.kt b/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveSettingsScreen.kt
index 156fafc6..1e35ff87 100644
--- a/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveSettingsScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/immersive/pages/ImmersiveSettingsScreen.kt
@@ -263,6 +263,15 @@ private fun PlaybackPage(navController: NavController, open: (SettingsPage) -> U
detail = stringResource(R.string.settings_connect_mirror_detail),
)
}
+ // Downloaded songs, played from their files online, played by Spotify too (unheard), as an extension's.
+ row {
+ ImmersiveSwitchRow(
+ stringResource(R.string.settings_download_sync_title),
+ checked = com.music.spotui.data.preferences.DownloadSyncPref.on,
+ onChecked = { com.music.spotui.data.preferences.DownloadSyncPref.set(ctx, it) },
+ detail = stringResource(R.string.settings_download_sync_detail),
+ )
+ }
// Songs marked "Not interested": skipped by lists, listed here to be shown again.
row {
ImmersiveGroupRow(
diff --git a/base/app/src/main/java/com/music/spotui/ui/screens/SettingsScreen.kt b/base/app/src/main/java/com/music/spotui/ui/screens/SettingsScreen.kt
index 6f38a1d4..c2a78960 100644
--- a/base/app/src/main/java/com/music/spotui/ui/screens/SettingsScreen.kt
+++ b/base/app/src/main/java/com/music/spotui/ui/screens/SettingsScreen.kt
@@ -152,6 +152,13 @@ fun SettingsScreen(navController: NavController) {
checked = com.music.spotui.data.preferences.ConnectMirrorPref.on,
onChecked = { com.music.spotui.data.preferences.ConnectMirrorPref.set(ctx, it) },
)
+ // Downloaded songs, played from their files online, played by Spotify too (unheard), as an extension's.
+ OptionRow(
+ label = stringResource(R.string.settings_download_sync_title),
+ detail = stringResource(R.string.settings_download_sync_detail),
+ checked = com.music.spotui.data.preferences.DownloadSyncPref.on,
+ onChecked = { com.music.spotui.data.preferences.DownloadSyncPref.set(ctx, it) },
+ )
// Songs marked "Not interested": skipped by lists, listed here to be shown again.
Column(
Modifier.fillMaxWidth().clickable { navController.navigate(com.music.spotui.ui.navigation.Routes.HiddenSongs.route) }
diff --git a/base/app/src/main/res/values/myspoti_strings.xml b/base/app/src/main/res/values/myspoti_strings.xml
index 0c907423..f0f58838 100644
--- a/base/app/src/main/res/values/myspoti_strings.xml
+++ b/base/app/src/main/res/values/myspoti_strings.xml
@@ -593,6 +593,8 @@
<string name="settings_headset_skip_title">Headphone buttons skip 15 s on podcasts</string>
<string name="settings_headset_skip_detail">When off, they go to the next or previous episode</string>
<string name="settings_connect_mirror_title">Count extension plays on Spotify</string>
+ <string name="settings_download_sync_title">Sync downloaded songs with Spotify</string>
+ <string name="settings_download_sync_detail">When you\'re online, a downloaded song also plays muted on Spotify, so it counts and your devices see and control it. Off, downloads play only from the phone.</string>
<string name="settings_connect_mirror_detail">Plays the Spotify song muted in the background, so it counts and your devices can control it. Uses a little more data and battery: turn it off if you don\'t need it.</string>
<string name="explicit_badge">E</string>
<string name="spotify_plan">Spotify %1$s</string>
diff --git a/base/app/src/test/java/com/music/spotui/di/MirrorRulesTest.kt b/base/app/src/test/java/com/music/spotui/di/MirrorRulesTest.kt
index 871ca513..16426105 100644
--- a/base/app/src/test/java/com/music/spotui/di/MirrorRulesTest.kt
+++ b/base/app/src/test/java/com/music/spotui/di/MirrorRulesTest.kt
@@ -59,6 +59,17 @@ class MirrorRulesTest {
assertFalse(MirrorRules.eligible(Origin.AUDIO_SOURCE, "not-an-id", on = true, dataSaver = false))
}
+ @Test fun `a download with a Spotify id is mirrored online by its own setting`() {
+ assertTrue(MirrorRules.eligible(Origin.DOWNLOAD, id, on = false, dataSaver = false, downloads = true, online = true))
+ assertFalse(MirrorRules.eligible(Origin.DOWNLOAD, id, on = true, dataSaver = false, downloads = false, online = true))
+ assertFalse(MirrorRules.eligible(Origin.DOWNLOAD, id, on = true, dataSaver = false, downloads = true, online = false))
+ assertFalse(MirrorRules.eligible(Origin.DOWNLOAD, id, on = true, dataSaver = true, downloads = true, online = true))
+ assertFalse(MirrorRules.eligible(Origin.DOWNLOAD, "", on = true, dataSaver = false, downloads = true, online = true))
+ // The downloads' setting changes nothing for an extension's stream or a file of the phone.
+ assertFalse(MirrorRules.eligible(Origin.AUDIO_SOURCE, id, on = false, dataSaver = false, downloads = true, online = true))
+ assertFalse(MirrorRules.eligible(Origin.LOCAL_FILE, id, on = true, dataSaver = false, downloads = true, online = true))
+ }
+
@Test fun `the setting off or the data saver leave the stream alone`() {
assertFalse(MirrorRules.eligible(Origin.AUDIO_SOURCE, id, on = false, dataSaver = false))
assertFalse(MirrorRules.eligible(Origin.AUDIO_SOURCE, id, on = true, dataSaver = true))