Fix de(serilization)

This commit is contained in:
tzugen 2022-04-20 21:40:22 +02:00
parent 5d4aff1f21
commit 2aaa3c2119
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
4 changed files with 54 additions and 61 deletions

View File

@ -109,7 +109,6 @@ class TrackViewHolder(val view: View) : RecyclerView.ViewHolder(view), Checkable
} }
rxSubscription = RxBus.playerStateObservable.subscribe { rxSubscription = RxBus.playerStateObservable.subscribe {
Timber.i("NEW PLAY STATE")
setPlayIcon(it.index == bindingAdapterPosition && it.track == downloadFile) setPlayIcon(it.index == bindingAdapterPosition && it.track == downloadFile)
} }
} }

View File

@ -3,6 +3,9 @@ package org.moire.ultrasonic.app
import android.content.Context import android.content.Context
import android.os.StrictMode import android.os.StrictMode
import androidx.multidex.MultiDexApplication import androidx.multidex.MultiDexApplication
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.koin.android.ext.koin.androidContext import org.koin.android.ext.koin.androidContext
import org.koin.core.context.startKoin import org.koin.core.context.startKoin
import org.koin.core.logger.Level import org.koin.core.logger.Level
@ -24,6 +27,8 @@ import timber.log.Timber.DebugTree
class UApp : MultiDexApplication() { class UApp : MultiDexApplication() {
private var ioScope = CoroutineScope(Dispatchers.IO)
init { init {
instance = this instance = this
if (BuildConfig.DEBUG) if (BuildConfig.DEBUG)
@ -36,9 +41,13 @@ class UApp : MultiDexApplication() {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Timber.plant(DebugTree()) Timber.plant(DebugTree())
} }
// In general we should not access the settings from the main thread to avoid blocking...
ioScope.launch {
if (Settings.debugLogToFile) { if (Settings.debugLogToFile) {
FileLoggerTree.plantToTimberForest() FileLoggerTree.plantToTimberForest()
} }
}
startKoin { startKoin {
// TODO Currently there is a bug in Koin which makes necessary to set the loglevel to ERROR // TODO Currently there is a bug in Koin which makes necessary to set the loglevel to ERROR

View File

@ -29,7 +29,6 @@ import org.moire.ultrasonic.provider.UltrasonicAppWidgetProvider4X1
import org.moire.ultrasonic.provider.UltrasonicAppWidgetProvider4X2 import org.moire.ultrasonic.provider.UltrasonicAppWidgetProvider4X2
import org.moire.ultrasonic.provider.UltrasonicAppWidgetProvider4X3 import org.moire.ultrasonic.provider.UltrasonicAppWidgetProvider4X3
import org.moire.ultrasonic.provider.UltrasonicAppWidgetProvider4X4 import org.moire.ultrasonic.provider.UltrasonicAppWidgetProvider4X4
import org.moire.ultrasonic.service.DownloadService.Companion.getInstance
import org.moire.ultrasonic.service.MusicServiceFactory.getMusicService import org.moire.ultrasonic.service.MusicServiceFactory.getMusicService
import org.moire.ultrasonic.util.FileUtil import org.moire.ultrasonic.util.FileUtil
import org.moire.ultrasonic.util.Settings import org.moire.ultrasonic.util.Settings
@ -72,7 +71,7 @@ class MediaPlayerController(
var controller: MediaController? = null var controller: MediaController? = null
fun onCreate() { fun onCreate(onCreated: () -> Unit) {
if (created) return if (created) return
externalStorageMonitor.onCreate { reset() } externalStorageMonitor.onCreate { reset() }
isJukeboxEnabled = activeServerProvider.getActiveServer().jukeboxByDefault isJukeboxEnabled = activeServerProvider.getActiveServer().jukeboxByDefault
@ -80,32 +79,25 @@ class MediaPlayerController(
mediaControllerFuture.addListener({ mediaControllerFuture.addListener({
controller = mediaControllerFuture.get() controller = mediaControllerFuture.get()
Timber.i("MediaController Instance received")
controller?.addListener(object : Player.Listener { controller?.addListener(object : Player.Listener {
/* /*
* Log all events * Log all events
*/ */
// override fun onEvents(player: Player, events: Player.Events) { override fun onEvents(player: Player, events: Player.Events) {
// //Timber.i("Media3 Event: %s", events) for (i in 0 until events.size()) {
// } Timber.i("Media3 Event, event type: %s", events[i])
}
}
// override fun onIsLoadingChanged(isLoading: Boolean) {
// super.onIsLoadingChanged(isLoading)
// }
//
// override fun onPlayWhenReadyChanged(playWhenReady: Boolean, reason: Int) {
// super.onPlayWhenReadyChanged(playWhenReady, reason)
// }
//
// override fun onPlaylistMetadataChanged(mediaMetadata: MediaMetadata) {
// super.onPlaylistMetadataChanged(mediaMetadata)
// }
//
/* /*
* This will be called everytime the playlist has changed. * This will be called everytime the playlist has changed.
*/ */
override fun onTimelineChanged(timeline: Timeline, reason: Int) { override fun onTimelineChanged(timeline: Timeline, reason: Int) {
legacyPlaylistManager.rebuildPlaylist(controller!!) legacyPlaylistManager.rebuildPlaylist(controller!!)
serializeCurrentSession()
} }
override fun onPlaybackStateChanged(playbackState: Int) { override fun onPlaybackStateChanged(playbackState: Int) {
@ -125,6 +117,10 @@ class MediaPlayerController(
} }
}) })
onCreated()
Timber.i("MediaPlayerController creation complete")
// controller?.play() // controller?.play()
}, MoreExecutors.directExecutor()) }, MoreExecutors.directExecutor())
@ -134,7 +130,7 @@ class MediaPlayerController(
} }
created = true created = true
Timber.i("MediaPlayerController created") Timber.i("MediaPlayerController started")
} }
private fun playerStateChangedHandler() { private fun playerStateChangedHandler() {
@ -145,10 +141,6 @@ class MediaPlayerController(
Player.STATE_READY -> { Player.STATE_READY -> {
if (isPlaying) { if (isPlaying) {
scrobbler.scrobble(currentPlaying, false) scrobbler.scrobble(currentPlaying, false)
} else {
playbackStateSerializer.serialize(
playList, currentMediaItemIndex, playerPosition
)
} }
} }
Player.STATE_ENDED -> { Player.STATE_ENDED -> {
@ -156,6 +148,11 @@ class MediaPlayerController(
} }
} }
// Save playback state
playbackStateSerializer.serialize(
playList, currentMediaItemIndex, playerPosition
)
// Update widget // Update widget
if (currentPlaying != null) { if (currentPlaying != null) {
updateWidget(currentPlaying.track) updateWidget(currentPlaying.track)
@ -237,8 +234,9 @@ class MediaPlayerController(
seekTo(currentPlayingIndex, currentPlayingPosition) seekTo(currentPlayingIndex, currentPlayingPosition)
} }
if (autoPlay) {
prepare() prepare()
if (autoPlay) {
play() play()
} }
@ -246,11 +244,6 @@ class MediaPlayerController(
} }
} }
@Synchronized
fun preload() {
getInstance()
}
@Synchronized @Synchronized
fun play(index: Int) { fun play(index: Int) {
controller?.seekTo(index, 0L) controller?.seekTo(index, 0L)
@ -356,12 +349,6 @@ class MediaPlayerController(
} else { } else {
downloader.checkDownloads() downloader.checkDownloads()
} }
playbackStateSerializer.serialize(
legacyPlaylistManager.playlist,
currentMediaItemIndex,
playerPosition
)
} }
@Synchronized @Synchronized
@ -370,11 +357,7 @@ class MediaPlayerController(
val filteredSongs = songs.filterNotNull() val filteredSongs = songs.filterNotNull()
downloader.downloadBackground(filteredSongs, save) downloader.downloadBackground(filteredSongs, save)
playbackStateSerializer.serialize( serializeCurrentSession()
legacyPlaylistManager.playlist,
currentMediaItemIndex,
playerPosition
)
} }
fun stopJukeboxService() { fun stopJukeboxService() {
@ -439,12 +422,6 @@ class MediaPlayerController(
downloader.clearActiveDownloads() downloader.clearActiveDownloads()
downloader.clearBackground() downloader.clearBackground()
playbackStateSerializer.serialize(
legacyPlaylistManager.playlist,
currentMediaItemIndex,
playerPosition
)
jukeboxMediaPlayer.updatePlaylist() jukeboxMediaPlayer.updatePlaylist()
} }
@ -453,13 +430,16 @@ class MediaPlayerController(
controller?.removeMediaItem(position) controller?.removeMediaItem(position)
jukeboxMediaPlayer.updatePlaylist()
}
@Synchronized
private fun serializeCurrentSession() {
playbackStateSerializer.serialize( playbackStateSerializer.serialize(
legacyPlaylistManager.playlist, legacyPlaylistManager.playlist,
currentMediaItemIndex, currentMediaItemIndex,
playerPosition playerPosition
) )
jukeboxMediaPlayer.updatePlaylist()
} }
@Synchronized @Synchronized
@ -625,7 +605,7 @@ class MediaPlayerController(
} }
init { init {
Timber.i("MediaPlayerController constructed") Timber.i("MediaPlayerController instance initiated")
} }
enum class InsertionMode { enum class InsertionMode {

View File

@ -29,32 +29,37 @@ class MediaPlayerLifecycleSupport : KoinComponent {
onCreate(false, null) onCreate(false, null)
} }
private fun onCreate(autoPlay: Boolean, afterCreated: Runnable?) { private fun onCreate(autoPlay: Boolean, afterRestore: Runnable?) {
if (created) { if (created) {
afterCreated?.run() afterRestore?.run()
return return
} }
mediaPlayerController.onCreate() mediaPlayerController.onCreate {
if (autoPlay) mediaPlayerController.preload() restoreLastSession(autoPlay, afterRestore)
}
CacheCleaner().clean()
created = true
Timber.i("LifecycleSupport created")
}
private fun restoreLastSession(autoPlay: Boolean, afterRestore: Runnable?) {
playbackStateSerializer.deserialize { playbackStateSerializer.deserialize {
Timber.i("Restoring %s songs", it!!.songs.size)
mediaPlayerController.restore( mediaPlayerController.restore(
it!!.songs, it.songs,
it.currentPlayingIndex, it.currentPlayingIndex,
it.currentPlayingPosition, it.currentPlayingPosition,
autoPlay, autoPlay,
false false
) )
afterCreated?.run() afterRestore?.run()
} }
CacheCleaner().clean()
created = true
Timber.i("LifecycleSupport created")
} }
fun onDestroy() { fun onDestroy() {