Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
List Square among the phone's media players and resume from media buttons
Lelonioab60ac26056e
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xmlindex 0a05a60..ec7ad33 100644--- a/app/src/main/AndroidManifest.xml+++ b/app/src/main/AndroidManifest.xml@@ -71,6 +71,8 @@<intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />+ <!-- A music player, for whatever lists the phone's music apps. -->+ <category android:name="android.intent.category.APP_MUSIC" /></intent-filter><!--@@ -129,6 +131,20 @@Held while the listener signs in to Spotify in the browser, so theprocess is not frozen before the redirect to 127.0.0.1 arrives.-->+ <!--+ Media buttons with the app closed: a headset, a car, the system's own+ media controls. Also what apps such as Good Lock's Sound Assist read+ to list the phone's media players; without it Square was not among+ them. Handed to PlaybackService, which resumes the saved session.+ -->+ <receiver+ android:name="androidx.media3.session.MediaButtonReceiver"+ android:exported="true">+ <intent-filter>+ <action android:name="android.intent.action.MEDIA_BUTTON" />+ </intent-filter>+ </receiver>+<serviceandroid:name=".auth.LoginKeepAlive"android:exported="false"diff --git a/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt b/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.ktindex 26d10ab..e49e610 100644--- a/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt+++ b/app/src/main/java/dev/lelonio/square/playback/MediaBrowseTree.kt@@ -65,8 +65,29 @@ class MediaBrowseTree(* player than either streaming service. See PlaybackService.ensurePlayerFor.*/private val ensurePlayerFor: suspend (String?) -> Unit = {},+ /**+ * What to play when something asks to resume with nothing loaded: a media+ * button, a headset, the system's media controls after a restart. Null when+ * there is nothing saved either.+ */+ private val resumption: () -> MediaSession.MediaItemsWithStartPosition? = { null },) : MediaLibrarySession.Callback {+ /**+ * Play pressed on a player with nothing in it.+ *+ * Without an answer here Media3 has nothing to start, and a service it has+ * already brought to the foreground for the button has to show a playing+ * notification within seconds or Android ends the app. The answer is the+ * session last saved, which is also what the app itself opens on.+ */+ override fun onPlaybackResumption(+ mediaSession: MediaSession,+ controller: MediaSession.ControllerInfo,+ ): ListenableFuture<MediaSession.MediaItemsWithStartPosition> =+ resumption()?.let { Futures.immediateFuture(it) }+ ?: Futures.immediateFailedFuture(UnsupportedOperationException("nothing to resume"))+private val app = context.applicationContext as SquareApplication/**diff --git a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.ktindex e5bf401..0e449fe 100644--- a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt+++ b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt@@ -286,7 +286,7 @@ class PlaybackService : MediaLibraryService() {player = buildPlayer(container.preferences.backend.value)startListeningRecorder()- browseTree = MediaBrowseTree(this, scope, ::ensurePlayerFor)+ browseTree = MediaBrowseTree(this, scope, ::ensurePlayerFor, ::resumption)session = MediaLibrarySession.Builder(this, player, browseTree)// Without this the notification is inert to a tap: Media3 has no way// to know which activity owns the session. `SINGLE_TOP` so an app@@ -1533,6 +1533,25 @@ class PlaybackService : MediaLibraryService() {}}+ /**+ * The queue to resume from a media button; see MediaBrowseTree.onPlaybackResumption.+ *+ * Whatever the player already holds, which is the saved session put back+ * when the service started; failing that, the saved session itself.+ */+ private fun resumption(): MediaSession.MediaItemsWithStartPosition? {+ if (player.mediaItemCount == 0) {+ runCatching { restoreTimeline(container.preferences.backend.value) }+ }+ val count = player.mediaItemCount+ if (count == 0) return null+ return MediaSession.MediaItemsWithStartPosition(+ (0 until count).map(player::getMediaItemAt),+ player.currentMediaItemIndex.coerceIn(0, count - 1),+ player.currentPosition.coerceAtLeast(0),+ )+ }+/** Measures only playback on this device, independently of the activity. */private fun startListeningRecorder() {val sampler = dev.lelonio.square.data.ListeningSampler()