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.moire.ultrasonic.service.AudioFocusHandler
import org.moire.ultrasonic.service.DownloadQueueSerializer
import org.moire.ultrasonic.service.Downloader
import org.moire.ultrasonic.service.ExternalStorageMonitor
import org.moire.ultrasonic.service.JukeboxMediaPlayer
import org.moire.ultrasonic.service.LocalMediaPlayer
import org.moire.ultrasonic.service.MediaPlayerController
import org.moire.ultrasonic.service.MediaPlayerLifecycleSupport
import org.moire.ultrasonic.service.PlaybackStateSerializer
import org.moire.ultrasonic.util.ShufflePlayBuffer
/**
@ -17,7 +17,7 @@ import org.moire.ultrasonic.util.ShufflePlayBuffer
val mediaPlayerModule = module {
single { JukeboxMediaPlayer(get()) }
single { MediaPlayerLifecycleSupport() }
single { DownloadQueueSerializer() }
single { PlaybackStateSerializer() }
single { ExternalStorageMonitor() }
single { ShufflePlayBuffer() }
single { Downloader(get(), get(), get()) }

View File

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

View File

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

View File

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

View File

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