2022-04-03 23:57:50 +02:00
|
|
|
/*
|
|
|
|
* LegacyPlaylist.kt
|
|
|
|
* Copyright (C) 2009-2022 Ultrasonic developers
|
|
|
|
*
|
|
|
|
* Distributed under terms of the GNU GPLv3 license.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.moire.ultrasonic.playback
|
|
|
|
|
|
|
|
import androidx.media3.common.MediaItem
|
|
|
|
import androidx.media3.session.MediaController
|
|
|
|
import org.koin.core.component.KoinComponent
|
|
|
|
import org.koin.core.component.inject
|
|
|
|
import org.moire.ultrasonic.domain.Track
|
|
|
|
import org.moire.ultrasonic.service.DownloadFile
|
|
|
|
import org.moire.ultrasonic.service.Downloader
|
|
|
|
import org.moire.ultrasonic.service.JukeboxMediaPlayer
|
|
|
|
import org.moire.ultrasonic.service.RxBus
|
|
|
|
import org.moire.ultrasonic.util.LRUCache
|
|
|
|
import timber.log.Timber
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class keeps a legacy playlist maintained which
|
|
|
|
* reflects the internal timeline of the Media3.Player
|
|
|
|
*/
|
|
|
|
class LegacyPlaylistManager : KoinComponent {
|
|
|
|
|
|
|
|
private val _playlist = mutableListOf<DownloadFile>()
|
|
|
|
|
|
|
|
@JvmField
|
|
|
|
var currentPlaying: DownloadFile? = null
|
|
|
|
|
2022-04-24 08:44:36 +02:00
|
|
|
// TODO This limits the maximum size of the playlist.
|
|
|
|
// This will be fixed when this class is refactored and removed
|
|
|
|
private val mediaItemCache = LRUCache<String, DownloadFile>(2000)
|
2022-04-03 23:57:50 +02:00
|
|
|
|
|
|
|
val jukeboxMediaPlayer: JukeboxMediaPlayer by inject()
|
|
|
|
val downloader: Downloader by inject()
|
|
|
|
|
|
|
|
private var playlistUpdateRevision: Long = 0
|
|
|
|
private set(value) {
|
|
|
|
field = value
|
|
|
|
RxBus.playlistPublisher.onNext(_playlist)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun rebuildPlaylist(controller: MediaController) {
|
|
|
|
_playlist.clear()
|
|
|
|
|
|
|
|
val n = controller.mediaItemCount
|
|
|
|
|
|
|
|
for (i in 0 until n) {
|
|
|
|
val item = controller.getMediaItemAt(i)
|
2022-06-19 18:21:33 +02:00
|
|
|
val file = mediaItemCache[item.requestMetadata.toString()]
|
2022-04-03 23:57:50 +02:00
|
|
|
if (file != null)
|
|
|
|
_playlist.add(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
playlistUpdateRevision++
|
|
|
|
}
|
|
|
|
|
|
|
|
fun addToCache(item: MediaItem, file: DownloadFile) {
|
2022-06-19 18:21:33 +02:00
|
|
|
mediaItemCache.put(item.requestMetadata.toString(), file)
|
2022-04-03 23:57:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun updateCurrentPlaying(item: MediaItem?) {
|
2022-06-19 18:21:33 +02:00
|
|
|
currentPlaying = mediaItemCache[item?.requestMetadata.toString()]
|
2022-04-03 23:57:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Synchronized
|
|
|
|
fun clearPlaylist() {
|
|
|
|
_playlist.clear()
|
|
|
|
playlistUpdateRevision++
|
|
|
|
}
|
|
|
|
|
|
|
|
fun onDestroy() {
|
|
|
|
clearPlaylist()
|
|
|
|
Timber.i("PlaylistManager destroyed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Public facing playlist (immutable)
|
|
|
|
val playlist: List<DownloadFile>
|
2022-04-22 21:03:57 +02:00
|
|
|
get() = _playlist
|
2022-04-03 23:57:50 +02:00
|
|
|
|
|
|
|
@get:Synchronized
|
|
|
|
val playlistDuration: Long
|
|
|
|
get() {
|
|
|
|
var totalDuration: Long = 0
|
|
|
|
for (downloadFile in _playlist) {
|
|
|
|
val song = downloadFile.track
|
|
|
|
if (!song.isDirectory) {
|
|
|
|
if (song.artist != null) {
|
|
|
|
if (song.duration != null) {
|
|
|
|
totalDuration += song.duration!!.toLong()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return totalDuration
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extension function
|
|
|
|
* Gathers the download file for a given song, and modifies shouldSave if provided.
|
|
|
|
*/
|
|
|
|
fun Track.getDownloadFile(save: Boolean? = null): DownloadFile {
|
|
|
|
return downloader.getDownloadFileForSong(this).apply {
|
|
|
|
if (save != null) this.shouldSave = save
|
|
|
|
}
|
|
|
|
}
|
2022-04-04 17:59:12 +02:00
|
|
|
}
|