Merge pull request #611 from ultrasonic/downloader-ld

Rename DownloadQueueSerializer to PlaybackstateSerializer
This commit is contained in:
Nite 2021-10-28 17:16:24 +02:00 committed by GitHub
commit f0c02f5551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 50 deletions

View File

@ -2,13 +2,13 @@ package org.moire.ultrasonic.di
import org.koin.dsl.module import org.koin.dsl.module
import org.moire.ultrasonic.service.AudioFocusHandler import org.moire.ultrasonic.service.AudioFocusHandler
import org.moire.ultrasonic.service.DownloadQueueSerializer
import org.moire.ultrasonic.service.Downloader import org.moire.ultrasonic.service.Downloader
import org.moire.ultrasonic.service.ExternalStorageMonitor import org.moire.ultrasonic.service.ExternalStorageMonitor
import org.moire.ultrasonic.service.JukeboxMediaPlayer import org.moire.ultrasonic.service.JukeboxMediaPlayer
import org.moire.ultrasonic.service.LocalMediaPlayer import org.moire.ultrasonic.service.LocalMediaPlayer
import org.moire.ultrasonic.service.MediaPlayerController import org.moire.ultrasonic.service.MediaPlayerController
import org.moire.ultrasonic.service.MediaPlayerLifecycleSupport import org.moire.ultrasonic.service.MediaPlayerLifecycleSupport
import org.moire.ultrasonic.service.PlaybackStateSerializer
import org.moire.ultrasonic.util.ShufflePlayBuffer import org.moire.ultrasonic.util.ShufflePlayBuffer
/** /**
@ -17,7 +17,7 @@ import org.moire.ultrasonic.util.ShufflePlayBuffer
val mediaPlayerModule = module { val mediaPlayerModule = module {
single { JukeboxMediaPlayer(get()) } single { JukeboxMediaPlayer(get()) }
single { MediaPlayerLifecycleSupport() } single { MediaPlayerLifecycleSupport() }
single { DownloadQueueSerializer() } single { PlaybackStateSerializer() }
single { ExternalStorageMonitor() } single { ExternalStorageMonitor() }
single { ShufflePlayBuffer() } single { ShufflePlayBuffer() }
single { Downloader(get(), get(), get()) } single { Downloader(get(), get(), get()) }

View File

@ -32,7 +32,7 @@ import timber.log.Timber
*/ */
@Suppress("TooManyFunctions") @Suppress("TooManyFunctions")
class MediaPlayerController( class MediaPlayerController(
private val downloadQueueSerializer: DownloadQueueSerializer, private val playbackStateSerializer: PlaybackStateSerializer,
private val externalStorageMonitor: ExternalStorageMonitor, private val externalStorageMonitor: ExternalStorageMonitor,
private val downloader: Downloader, private val downloader: Downloader,
private val shufflePlayBuffer: ShufflePlayBuffer, private val shufflePlayBuffer: ShufflePlayBuffer,
@ -197,7 +197,7 @@ class MediaPlayerController(
downloader.checkDownloads() downloader.checkDownloads()
} }
downloadQueueSerializer.serializeDownloadQueue( playbackStateSerializer.serialize(
downloader.playlist, downloader.playlist,
downloader.currentPlayingIndex, downloader.currentPlayingIndex,
playerPosition playerPosition
@ -209,7 +209,7 @@ class MediaPlayerController(
if (songs == null) return if (songs == null) return
val filteredSongs = songs.filterNotNull() val filteredSongs = songs.filterNotNull()
downloader.downloadBackground(filteredSongs, save) downloader.downloadBackground(filteredSongs, save)
downloadQueueSerializer.serializeDownloadQueue( playbackStateSerializer.serialize(
downloader.playlist, downloader.playlist,
downloader.currentPlayingIndex, downloader.currentPlayingIndex,
playerPosition playerPosition
@ -240,7 +240,7 @@ class MediaPlayerController(
@Synchronized @Synchronized
fun shuffle() { fun shuffle() {
downloader.shuffle() downloader.shuffle()
downloadQueueSerializer.serializeDownloadQueue( playbackStateSerializer.serialize(
downloader.playlist, downloader.playlist,
downloader.currentPlayingIndex, downloader.currentPlayingIndex,
playerPosition playerPosition
@ -273,7 +273,7 @@ class MediaPlayerController(
// If no MediaPlayerService is available, just empty the playlist // If no MediaPlayerService is available, just empty the playlist
downloader.clearPlaylist() downloader.clearPlaylist()
if (serialize) { if (serialize) {
downloadQueueSerializer.serializeDownloadQueue( playbackStateSerializer.serialize(
downloader.playlist, downloader.playlist,
downloader.currentPlayingIndex, playerPosition downloader.currentPlayingIndex, playerPosition
) )
@ -293,7 +293,7 @@ class MediaPlayerController(
} }
} }
downloadQueueSerializer.serializeDownloadQueue( playbackStateSerializer.serialize(
downloader.playlist, downloader.playlist,
downloader.currentPlayingIndex, downloader.currentPlayingIndex,
playerPosition playerPosition
@ -310,7 +310,7 @@ class MediaPlayerController(
} }
downloader.removeFromPlaylist(downloadFile) downloader.removeFromPlaylist(downloadFile)
downloadQueueSerializer.serializeDownloadQueue( playbackStateSerializer.serialize(
downloader.playlist, downloader.playlist,
downloader.currentPlayingIndex, downloader.currentPlayingIndex,
playerPosition playerPosition

View File

@ -32,7 +32,7 @@ import timber.log.Timber
* @author Sindre Mehus * @author Sindre Mehus
*/ */
class MediaPlayerLifecycleSupport : KoinComponent { class MediaPlayerLifecycleSupport : KoinComponent {
private val downloadQueueSerializer by inject<DownloadQueueSerializer>() private val playbackStateSerializer by inject<PlaybackStateSerializer>()
private val mediaPlayerController by inject<MediaPlayerController>() private val mediaPlayerController by inject<MediaPlayerController>()
private val downloader by inject<Downloader>() private val downloader by inject<Downloader>()
private val mediaSessionEventDistributor by inject<MediaSessionEventDistributor>() private val mediaSessionEventDistributor by inject<MediaSessionEventDistributor>()
@ -63,26 +63,25 @@ class MediaPlayerLifecycleSupport : KoinComponent {
mediaPlayerController.onCreate() mediaPlayerController.onCreate()
if (autoPlay) mediaPlayerController.preload() if (autoPlay) mediaPlayerController.preload()
downloadQueueSerializer.deserializeDownloadQueue(object : Consumer<State?>() { playbackStateSerializer.deserialize {
override fun accept(state: State?) {
mediaPlayerController.restore(
state!!.songs,
state.currentPlayingIndex,
state.currentPlayingPosition,
autoPlay,
false
)
// Work-around: Serialize again, as the restore() method creates a mediaPlayerController.restore(
// serialization without current playing info. it!!.songs,
downloadQueueSerializer.serializeDownloadQueue( it.currentPlayingIndex,
downloader.playlist, it.currentPlayingPosition,
downloader.currentPlayingIndex, autoPlay,
mediaPlayerController.playerPosition false
) )
afterCreated?.run()
} // Work-around: Serialize again, as the restore() method creates a
}) // serialization without current playing info.
playbackStateSerializer.serialize(
downloader.playlist,
downloader.currentPlayingIndex,
mediaPlayerController.playerPosition
)
afterCreated?.run()
}
CacheCleaner().clean() CacheCleaner().clean()
created = true created = true
@ -93,7 +92,7 @@ class MediaPlayerLifecycleSupport : KoinComponent {
if (!created) return if (!created) return
downloadQueueSerializer.serializeDownloadQueueNow( playbackStateSerializer.serializeNow(
downloader.playlist, downloader.playlist,
downloader.currentPlayingIndex, downloader.currentPlayingIndex,
mediaPlayerController.playerPosition mediaPlayerController.playerPosition

View File

@ -60,7 +60,7 @@ class MediaPlayerService : Service() {
private val scrobbler = Scrobbler() private val scrobbler = Scrobbler()
private val jukeboxMediaPlayer by inject<JukeboxMediaPlayer>() private val jukeboxMediaPlayer by inject<JukeboxMediaPlayer>()
private val downloadQueueSerializer by inject<DownloadQueueSerializer>() private val playbackStateSerializer by inject<PlaybackStateSerializer>()
private val shufflePlayBuffer by inject<ShufflePlayBuffer>() private val shufflePlayBuffer by inject<ShufflePlayBuffer>()
private val downloader by inject<Downloader>() private val downloader by inject<Downloader>()
private val localMediaPlayer by inject<LocalMediaPlayer>() private val localMediaPlayer by inject<LocalMediaPlayer>()
@ -92,7 +92,7 @@ class MediaPlayerService : Service() {
setupOnSongCompletedHandler() setupOnSongCompletedHandler()
localMediaPlayer.onPrepared = { localMediaPlayer.onPrepared = {
downloadQueueSerializer.serializeDownloadQueue( playbackStateSerializer.serialize(
downloader.playlist, downloader.playlist,
downloader.currentPlayingIndex, downloader.currentPlayingIndex,
playerPosition playerPosition
@ -304,7 +304,7 @@ class MediaPlayerService : Service() {
private fun resetPlayback() { private fun resetPlayback() {
localMediaPlayer.reset() localMediaPlayer.reset()
localMediaPlayer.setCurrentPlaying(null) localMediaPlayer.setCurrentPlaying(null)
downloadQueueSerializer.serializeDownloadQueue( playbackStateSerializer.serialize(
downloader.playlist, downloader.playlist,
downloader.currentPlayingIndex, playerPosition downloader.currentPlayingIndex, playerPosition
) )
@ -406,7 +406,7 @@ class MediaPlayerService : Service() {
) )
if (playerState === PlayerState.PAUSED) { if (playerState === PlayerState.PAUSED) {
downloadQueueSerializer.serializeDownloadQueue( playbackStateSerializer.serialize(
downloader.playlist, downloader.currentPlayingIndex, playerPosition downloader.playlist, downloader.currentPlayingIndex, playerPosition
) )
} }
@ -496,7 +496,7 @@ class MediaPlayerService : Service() {
localMediaPlayer.setCurrentPlaying(null) localMediaPlayer.setCurrentPlaying(null)
setNextPlaying() setNextPlaying()
if (serialize) { if (serialize) {
downloadQueueSerializer.serializeDownloadQueue( playbackStateSerializer.serialize(
downloader.playlist, downloader.playlist,
downloader.currentPlayingIndex, playerPosition downloader.currentPlayingIndex, playerPosition
) )

View File

@ -1,5 +1,5 @@
/* /*
* DownloadQueueSerializer.kt * PlaybackStateSerializer.kt
* Copyright (C) 2009-2021 Ultrasonic developers * Copyright (C) 2009-2021 Ultrasonic developers
* *
* Distributed under terms of the GNU GPLv3 license. * Distributed under terms of the GNU GPLv3 license.
@ -24,20 +24,20 @@ import timber.log.Timber
/** /**
* This class is responsible for the serialization / deserialization * This class is responsible for the serialization / deserialization
* of the DownloadQueue (playlist) to the filesystem. * of the playlist and the player state (e.g. current playing number and play position)
* It also serializes the player state e.g. current playing number and play position. * to the filesystem.
*/ */
class DownloadQueueSerializer : KoinComponent { class PlaybackStateSerializer : KoinComponent {
private val context by inject<Context>() private val context by inject<Context>()
private val mediaSessionHandler by inject<MediaSessionHandler>() private val mediaSessionHandler by inject<MediaSessionHandler>()
val lock: Lock = ReentrantLock() val lock: Lock = ReentrantLock()
val setup = AtomicBoolean(false) private val setup = AtomicBoolean(false)
private val appScope = CoroutineScope(SupervisorJob() + Dispatchers.IO) private val appScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
fun serializeDownloadQueue( fun serialize(
songs: Iterable<DownloadFile>, songs: Iterable<DownloadFile>,
currentPlayingIndex: Int, currentPlayingIndex: Int,
currentPlayingPosition: Int currentPlayingPosition: Int
@ -47,7 +47,7 @@ class DownloadQueueSerializer : KoinComponent {
appScope.launch { appScope.launch {
if (lock.tryLock()) { if (lock.tryLock()) {
try { try {
serializeDownloadQueueNow(songs, currentPlayingIndex, currentPlayingPosition) serializeNow(songs, currentPlayingIndex, currentPlayingPosition)
} finally { } finally {
lock.unlock() lock.unlock()
} }
@ -55,7 +55,7 @@ class DownloadQueueSerializer : KoinComponent {
} }
} }
fun serializeDownloadQueueNow( fun serializeNow(
songs: Iterable<DownloadFile>, songs: Iterable<DownloadFile>,
currentPlayingIndex: Int, currentPlayingIndex: Int,
currentPlayingPosition: Int currentPlayingPosition: Int
@ -75,18 +75,18 @@ class DownloadQueueSerializer : KoinComponent {
state.currentPlayingPosition state.currentPlayingPosition
) )
FileUtil.serialize(context, state, Constants.FILENAME_DOWNLOADS_SER) FileUtil.serialize(context, state, Constants.FILENAME_PLAYLIST_SER)
// This is called here because the queue is usually serialized after a change // This is called here because the queue is usually serialized after a change
mediaSessionHandler.updateMediaSessionQueue(state.songs) mediaSessionHandler.updateMediaSessionQueue(state.songs)
} }
fun deserializeDownloadQueue(afterDeserialized: Consumer<State?>) { fun deserialize(afterDeserialized: (State?) -> Unit?) {
appScope.launch { appScope.launch {
try { try {
lock.lock() lock.lock()
deserializeDownloadQueueNow(afterDeserialized) deserializeNow(afterDeserialized)
setup.set(true) setup.set(true)
} finally { } finally {
lock.unlock() lock.unlock()
@ -94,10 +94,10 @@ class DownloadQueueSerializer : KoinComponent {
} }
} }
private fun deserializeDownloadQueueNow(afterDeserialized: Consumer<State?>) { private fun deserializeNow(afterDeserialized: (State?) -> Unit?) {
val state = FileUtil.deserialize<State>( val state = FileUtil.deserialize<State>(
context, Constants.FILENAME_DOWNLOADS_SER context, Constants.FILENAME_PLAYLIST_SER
) ?: return ) ?: return
Timber.i( Timber.i(
@ -107,6 +107,6 @@ class DownloadQueueSerializer : KoinComponent {
) )
mediaSessionHandler.updateMediaSessionQueue(state.songs) mediaSessionHandler.updateMediaSessionQueue(state.songs)
afterDeserialized.accept(state) afterDeserialized(state)
} }
} }

View File

@ -117,7 +117,7 @@ object Constants {
const val PREFERENCE_VALUE_ALL = 0 const val PREFERENCE_VALUE_ALL = 0
const val PREFERENCE_VALUE_A2DP = 1 const val PREFERENCE_VALUE_A2DP = 1
const val PREFERENCE_VALUE_DISABLED = 2 const val PREFERENCE_VALUE_DISABLED = 2
const val FILENAME_DOWNLOADS_SER = "downloadstate.ser" const val FILENAME_PLAYLIST_SER = "downloadstate.ser"
const val ALBUM_ART_FILE = "folder.jpeg" const val ALBUM_ART_FILE = "folder.jpeg"
const val STARRED = "starred" const val STARRED = "starred"
const val ALPHABETICAL_BY_NAME = "alphabeticalByName" const val ALPHABETICAL_BY_NAME = "alphabeticalByName"