Fix resume playback from Quick Settings
This commit is contained in:
parent
931a0345a4
commit
aee5bfdf50
6 changed files with 70 additions and 26 deletions
|
@ -42,7 +42,6 @@ abstract class AbsThemeActivity : ATHToolbarActivity(), Runnable {
|
||||||
updateTheme()
|
updateTheme()
|
||||||
hideStatusBar()
|
hideStatusBar()
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
println("OnCreate")
|
|
||||||
setEdgeToEdgeOrImmersive()
|
setEdgeToEdgeOrImmersive()
|
||||||
maybeSetScreenOn()
|
maybeSetScreenOn()
|
||||||
setLightNavigationBarAuto()
|
setLightNavigationBarAuto()
|
||||||
|
|
|
@ -40,7 +40,7 @@ import org.koin.core.component.inject
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class MediaSessionCallback(
|
class MediaSessionCallback(
|
||||||
private val musicService: MusicService
|
private val musicService: MusicService,
|
||||||
) : MediaSessionCompat.Callback(), KoinComponent {
|
) : MediaSessionCompat.Callback(), KoinComponent {
|
||||||
|
|
||||||
private val songRepository by inject<SongRepository>()
|
private val songRepository by inject<SongRepository>()
|
||||||
|
@ -53,7 +53,7 @@ class MediaSessionCallback(
|
||||||
override fun onPlayFromMediaId(mediaId: String?, extras: Bundle?) {
|
override fun onPlayFromMediaId(mediaId: String?, extras: Bundle?) {
|
||||||
super.onPlayFromMediaId(mediaId, extras)
|
super.onPlayFromMediaId(mediaId, extras)
|
||||||
val musicId = AutoMediaIDHelper.extractMusicID(mediaId!!)
|
val musicId = AutoMediaIDHelper.extractMusicID(mediaId!!)
|
||||||
logD(musicId)
|
logD("Music Id $musicId")
|
||||||
val itemId = musicId?.toLong() ?: -1
|
val itemId = musicId?.toLong() ?: -1
|
||||||
val songs: ArrayList<Song> = ArrayList()
|
val songs: ArrayList<Song> = ArrayList()
|
||||||
when (val category = AutoMediaIDHelper.extractCategory(mediaId)) {
|
when (val category = AutoMediaIDHelper.extractCategory(mediaId)) {
|
||||||
|
@ -90,7 +90,8 @@ class MediaSessionCallback(
|
||||||
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_HISTORY,
|
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_HISTORY,
|
||||||
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_SUGGESTIONS,
|
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_SUGGESTIONS,
|
||||||
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_TOP_TRACKS,
|
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_TOP_TRACKS,
|
||||||
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_QUEUE -> {
|
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_QUEUE,
|
||||||
|
-> {
|
||||||
val tracks: List<Song> = when (category) {
|
val tracks: List<Song> = when (category) {
|
||||||
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_HISTORY -> topPlayedRepository.recentlyPlayedTracks()
|
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_HISTORY -> topPlayedRepository.recentlyPlayedTracks()
|
||||||
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_SUGGESTIONS -> topPlayedRepository.recentlyPlayedTracks()
|
AutoMediaIDHelper.MEDIA_ID_MUSICS_BY_SUGGESTIONS -> topPlayedRepository.recentlyPlayedTracks()
|
||||||
|
@ -104,8 +105,6 @@ class MediaSessionCallback(
|
||||||
}
|
}
|
||||||
musicService.openQueue(songs, songIndex, true)
|
musicService.openQueue(songs, songIndex, true)
|
||||||
}
|
}
|
||||||
else -> {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
musicService.play()
|
musicService.play()
|
||||||
}
|
}
|
||||||
|
@ -148,9 +147,15 @@ class MediaSessionCallback(
|
||||||
musicService.play()
|
musicService.play()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onPrepare() {
|
||||||
|
super.onPrepare()
|
||||||
|
if (musicService.currentSong != Song.emptySong)
|
||||||
|
musicService.restoreState(::onPlay)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onPlay() {
|
override fun onPlay() {
|
||||||
super.onPlay()
|
super.onPlay()
|
||||||
musicService.play()
|
if (musicService.currentSong != Song.emptySong) musicService.play()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPause() {
|
override fun onPause() {
|
||||||
|
|
|
@ -31,7 +31,6 @@ import android.os.Build.VERSION_CODES
|
||||||
import android.os.PowerManager.WakeLock
|
import android.os.PowerManager.WakeLock
|
||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
import android.support.v4.media.MediaBrowserCompat
|
import android.support.v4.media.MediaBrowserCompat
|
||||||
import android.support.v4.media.MediaDescriptionCompat
|
|
||||||
import android.support.v4.media.MediaMetadataCompat
|
import android.support.v4.media.MediaMetadataCompat
|
||||||
import android.support.v4.media.session.MediaSessionCompat
|
import android.support.v4.media.session.MediaSessionCompat
|
||||||
import android.support.v4.media.session.PlaybackStateCompat
|
import android.support.v4.media.session.PlaybackStateCompat
|
||||||
|
@ -68,7 +67,6 @@ import code.name.monkey.retromusic.service.notification.PlayingNotificationImpl2
|
||||||
import code.name.monkey.retromusic.service.playback.Playback
|
import code.name.monkey.retromusic.service.playback.Playback
|
||||||
import code.name.monkey.retromusic.service.playback.Playback.PlaybackCallbacks
|
import code.name.monkey.retromusic.service.playback.Playback.PlaybackCallbacks
|
||||||
import code.name.monkey.retromusic.util.MusicUtil
|
import code.name.monkey.retromusic.util.MusicUtil
|
||||||
import code.name.monkey.retromusic.util.MusicUtil.getMediaStoreAlbumCoverUri
|
|
||||||
import code.name.monkey.retromusic.util.MusicUtil.toggleFavorite
|
import code.name.monkey.retromusic.util.MusicUtil.toggleFavorite
|
||||||
import code.name.monkey.retromusic.util.PackageValidator
|
import code.name.monkey.retromusic.util.PackageValidator
|
||||||
import code.name.monkey.retromusic.util.PreferenceUtil.crossFadeDuration
|
import code.name.monkey.retromusic.util.PreferenceUtil.crossFadeDuration
|
||||||
|
@ -114,6 +112,7 @@ class MusicService : MediaBrowserServiceCompat(),
|
||||||
|
|
||||||
private var mPackageValidator: PackageValidator? = null
|
private var mPackageValidator: PackageValidator? = null
|
||||||
private val mMusicProvider = get<AutoMusicProvider>(AutoMusicProvider::class.java)
|
private val mMusicProvider = get<AutoMusicProvider>(AutoMusicProvider::class.java)
|
||||||
|
private lateinit var storage: PersistentStorage
|
||||||
private var trackEndedByCrossfade = false
|
private var trackEndedByCrossfade = false
|
||||||
private val serviceScope = CoroutineScope(Job() + Main)
|
private val serviceScope = CoroutineScope(Job() + Main)
|
||||||
|
|
||||||
|
@ -184,6 +183,9 @@ class MusicService : MediaBrowserServiceCompat(),
|
||||||
playingNotification?.updateFavorite(isFavorite)
|
playingNotification?.updateFavorite(isFavorite)
|
||||||
startForegroundOrNotify()
|
startForegroundOrNotify()
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
playingNotification?.updateFavorite(isFavorite)
|
||||||
|
startForegroundOrNotify()
|
||||||
}
|
}
|
||||||
|
|
||||||
appWidgetCircle.notifyChange(this@MusicService, FAVORITE_STATE_CHANGED)
|
appWidgetCircle.notifyChange(this@MusicService, FAVORITE_STATE_CHANGED)
|
||||||
|
@ -304,6 +306,7 @@ class MusicService : MediaBrowserServiceCompat(),
|
||||||
registerBluetoothConnected()
|
registerBluetoothConnected()
|
||||||
mPackageValidator = PackageValidator(this, R.xml.allowed_media_browser_callers)
|
mPackageValidator = PackageValidator(this, R.xml.allowed_media_browser_callers)
|
||||||
mMusicProvider.setMusicService(this)
|
mMusicProvider.setMusicService(this)
|
||||||
|
storage = PersistentStorage.getInstance(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
|
@ -608,16 +611,7 @@ class MusicService : MediaBrowserServiceCompat(),
|
||||||
result: Result<List<MediaBrowserCompat.MediaItem>>,
|
result: Result<List<MediaBrowserCompat.MediaItem>>,
|
||||||
) {
|
) {
|
||||||
if (parentId == AutoMediaIDHelper.RECENT_ROOT) {
|
if (parentId == AutoMediaIDHelper.RECENT_ROOT) {
|
||||||
val song = currentSong
|
result.sendResult(listOf(storage.recentSong()))
|
||||||
val mediaItem = MediaBrowserCompat.MediaItem(
|
|
||||||
MediaDescriptionCompat.Builder()
|
|
||||||
.setMediaId(song.id.toString())
|
|
||||||
.setTitle(song.title)
|
|
||||||
.setSubtitle(song.artistName)
|
|
||||||
.setIconUri(getMediaStoreAlbumCoverUri(song.albumId))
|
|
||||||
.build(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE
|
|
||||||
)
|
|
||||||
result.sendResult(listOf(mediaItem))
|
|
||||||
} else {
|
} else {
|
||||||
result.sendResult(mMusicProvider.getChildren(parentId, resources))
|
result.sendResult(mMusicProvider.getChildren(parentId, resources))
|
||||||
}
|
}
|
||||||
|
@ -658,7 +652,6 @@ class MusicService : MediaBrowserServiceCompat(),
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||||
startForegroundOrNotify()
|
|
||||||
if (intent != null && intent.action != null) {
|
if (intent != null && intent.action != null) {
|
||||||
handleIntent(mediaSession, intent)
|
handleIntent(mediaSession, intent)
|
||||||
serviceScope.launch {
|
serviceScope.launch {
|
||||||
|
@ -960,6 +953,7 @@ class MusicService : MediaBrowserServiceCompat(),
|
||||||
saveQueues()
|
saveQueues()
|
||||||
savePosition()
|
savePosition()
|
||||||
savePositionInTrack()
|
savePositionInTrack()
|
||||||
|
storage.saveSong(currentSong)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
|
@ -973,7 +967,7 @@ class MusicService : MediaBrowserServiceCompat(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// to let other apps know whats playing. i.E. last.fm (scrobbling) or musixmatch
|
// to let other apps know whats playing. i.e. last.fm (scrobbling) or musixmatch
|
||||||
fun sendPublicIntent(what: String) {
|
fun sendPublicIntent(what: String) {
|
||||||
val intent = Intent(what.replace(RETRO_MUSIC_PACKAGE_NAME, MUSIC_PACKAGE_NAME))
|
val intent = Intent(what.replace(RETRO_MUSIC_PACKAGE_NAME, MUSIC_PACKAGE_NAME))
|
||||||
val song = currentSong
|
val song = currentSong
|
||||||
|
@ -1251,7 +1245,7 @@ class MusicService : MediaBrowserServiceCompat(),
|
||||||
mediaSession?.release()
|
mediaSession?.release()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun restoreState() {
|
fun restoreState(completion: () -> Unit = {}) {
|
||||||
shuffleMode = PreferenceManager.getDefaultSharedPreferences(this).getInt(
|
shuffleMode = PreferenceManager.getDefaultSharedPreferences(this).getInt(
|
||||||
SAVED_SHUFFLE_MODE, 0
|
SAVED_SHUFFLE_MODE, 0
|
||||||
)
|
)
|
||||||
|
@ -1260,12 +1254,10 @@ class MusicService : MediaBrowserServiceCompat(),
|
||||||
)
|
)
|
||||||
handleAndSendChangeInternal(SHUFFLE_MODE_CHANGED)
|
handleAndSendChangeInternal(SHUFFLE_MODE_CHANGED)
|
||||||
handleAndSendChangeInternal(REPEAT_MODE_CHANGED)
|
handleAndSendChangeInternal(REPEAT_MODE_CHANGED)
|
||||||
val start= System.currentTimeMillis()
|
serviceScope.launch {
|
||||||
serviceScope.launch(start = CoroutineStart.DEFAULT) {
|
|
||||||
restoreQueuesAndPositionIfNecessary()
|
restoreQueuesAndPositionIfNecessary()
|
||||||
println("Time completion: ${System.currentTimeMillis() - start}")
|
completion()
|
||||||
}
|
}
|
||||||
println("Time: ${System.currentTimeMillis() - start}")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun savePosition() {
|
private fun savePosition() {
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
package code.name.monkey.retromusic.service
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.support.v4.media.MediaBrowserCompat
|
||||||
|
import android.support.v4.media.MediaDescriptionCompat
|
||||||
|
import androidx.core.content.edit
|
||||||
|
import androidx.core.net.toUri
|
||||||
|
import code.name.monkey.retromusic.extensions.albumArtUri
|
||||||
|
import code.name.monkey.retromusic.model.Song
|
||||||
|
|
||||||
|
class PersistentStorage(context: Context) {
|
||||||
|
|
||||||
|
private val prefs = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE)
|
||||||
|
|
||||||
|
fun saveSong(song: Song) {
|
||||||
|
prefs.edit {
|
||||||
|
putLong("song_id", song.id)
|
||||||
|
putString("song_title", song.title)
|
||||||
|
putString("song_artist", song.artistName)
|
||||||
|
putString("song_cover", song.albumArtUri.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun recentSong(): MediaBrowserCompat.MediaItem {
|
||||||
|
return MediaBrowserCompat.MediaItem(
|
||||||
|
MediaDescriptionCompat.Builder()
|
||||||
|
.setMediaId(prefs.getLong("song_id", 0L).toString())
|
||||||
|
.setTitle(prefs.getString("song_title", ""))
|
||||||
|
.setSubtitle(prefs.getString("song_artist", ""))
|
||||||
|
.setIconUri(prefs.getString("song_cover", "")?.toUri())
|
||||||
|
.build(), MediaBrowserCompat.MediaItem.FLAG_PLAYABLE
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val PREFERENCES_NAME = "retro_recent"
|
||||||
|
|
||||||
|
@Volatile
|
||||||
|
private var instance: PersistentStorage? = null
|
||||||
|
|
||||||
|
fun getInstance(context: Context) =
|
||||||
|
instance ?: synchronized(this) {
|
||||||
|
instance ?: PersistentStorage(context).also { instance = it }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -75,6 +75,7 @@ class PlayingNotificationClassic(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun updateMetadata(song: Song, onUpdate: () -> Unit) {
|
override fun updateMetadata(song: Song, onUpdate: () -> Unit) {
|
||||||
|
if (song == Song.emptySong) return
|
||||||
val notificationLayout = getCombinedRemoteViews(true, song)
|
val notificationLayout = getCombinedRemoteViews(true, song)
|
||||||
val notificationLayoutBig = getCombinedRemoteViews(false, song)
|
val notificationLayoutBig = getCombinedRemoteViews(false, song)
|
||||||
|
|
||||||
|
|
|
@ -110,6 +110,7 @@ class PlayingNotificationImpl24(
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun updateMetadata(song: Song, onUpdate: () -> Unit) {
|
override fun updateMetadata(song: Song, onUpdate: () -> Unit) {
|
||||||
|
if (song == Song.emptySong) return
|
||||||
setContentTitle(song.title)
|
setContentTitle(song.title)
|
||||||
setContentText(song.artistName)
|
setContentText(song.artistName)
|
||||||
setSubText(song.albumName)
|
setSubText(song.albumName)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue