From 19d014709f3e5407800edbb124313b0f45967b99 Mon Sep 17 00:00:00 2001 From: tzugen Date: Sun, 14 Nov 2021 23:21:53 +0100 Subject: [PATCH] Don't create DownloadFile instances unnecessarily --- .../moire/ultrasonic/service/DownloadFile.kt | 20 ++++++------- .../moire/ultrasonic/service/Downloader.kt | 30 +++++++++++++------ .../src/main/res/layout/song_details.xml | 4 +-- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadFile.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadFile.kt index 3a1833f2..271bda33 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadFile.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadFile.kt @@ -35,8 +35,9 @@ import timber.log.Timber */ class DownloadFile( val song: MusicDirectory.Entry, - private val save: Boolean + save: Boolean ) : KoinComponent, Identifiable { + var shouldSave = save val partialFile: File val completeFile: File private val saveFile: File = FileUtil.getSongFile(song) @@ -114,7 +115,7 @@ class DownloadFile( @get:Synchronized val isWorkDone: Boolean - get() = saveFile.exists() || completeFile.exists() && !save || + get() = saveFile.exists() || completeFile.exists() && !shouldSave || saveWhenDone || completeWhenDone @get:Synchronized @@ -125,10 +126,6 @@ class DownloadFile( val isDownloadCancelled: Boolean get() = downloadTask != null && downloadTask!!.isCancelled - fun shouldSave(): Boolean { - return save - } - fun shouldRetry(): Boolean { return (retryCount > 0) } @@ -188,7 +185,7 @@ class DownloadFile( Util.renameFile(completeFile, saveFile) saveWhenDone = false } else if (completeWhenDone) { - if (save) { + if (shouldSave) { Util.renameFile(partialFile, saveFile) Util.scanMedia(saveFile) } else { @@ -216,11 +213,12 @@ class DownloadFile( if (saveFile.exists()) { Timber.i("%s already exists. Skipping.", saveFile) status.postValue(DownloadStatus.DONE) + Timber.i("UPDATING STATUS") return } if (completeFile.exists()) { - if (save) { + if (shouldSave) { if (isPlaying) { saveWhenDone = true } else { @@ -230,6 +228,7 @@ class DownloadFile( Timber.i("%s already exists. Skipping.", completeFile) } status.postValue(DownloadStatus.DONE) + Timber.i("UPDATING STATUS") return } @@ -252,7 +251,7 @@ class DownloadFile( if (needsDownloading) { // Attempt partial HTTP GET, appending to the file if it exists. val (inStream, partial) = musicService.getDownloadInputStream( - song, partialFile.length(), desiredBitRate, save + song, partialFile.length(), desiredBitRate, shouldSave ) inputStream = inStream @@ -293,7 +292,7 @@ class DownloadFile( if (isPlaying) { completeWhenDone = true } else { - if (save) { + if (shouldSave) { Util.renameFile(partialFile, saveFile) Util.scanMedia(saveFile) } else { @@ -379,6 +378,7 @@ class DownloadFile( private fun setProgress(totalBytesCopied: Long) { if (song.size != null) { progress.postValue((totalBytesCopied * 100 / song.size!!).toInt()) + Timber.i("UPDATING PROGESS") } } diff --git a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt index 51db86d1..9c8db2a1 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/Downloader.kt @@ -55,6 +55,8 @@ class Downloader( RxBus.playlistPublisher.onNext(playlist) } + var backgroundPriorityCounter = 100 + val downloadChecker = Runnable { try { Timber.w("Checking Downloads") @@ -303,6 +305,8 @@ class Downloader( activelyDownloading.remove(download) } } + + backgroundPriorityCounter = 100 } @Synchronized @@ -327,7 +331,7 @@ class Downloader( @Synchronized fun addToPlaylist( - songs: List, + songs: List, save: Boolean, autoPlay: Boolean, playNext: Boolean, @@ -346,13 +350,13 @@ class Downloader( offset = 0 } for (song in songs) { - val downloadFile = DownloadFile(song!!, save) + val downloadFile = song.getDownloadFile(save) playlist.add(currentPlayingIndex + offset, downloadFile) offset++ } } else { for (song in songs) { - val downloadFile = DownloadFile(song!!, save) + val downloadFile = song.getDownloadFile(save) playlist.add(downloadFile) } } @@ -380,7 +384,10 @@ class Downloader( // Because of the priority handling we add the songs in the reverse order they // were requested, then it is correct in the end. for (song in songs.asReversed()) { - downloadQueue.add(DownloadFile(song, save)) + val file = song.getDownloadFile() + file.shouldSave = save + file.priority = backgroundPriorityCounter++ + downloadQueue.add(file) } checkDownloads() @@ -436,7 +443,7 @@ class Downloader( val size = playlist.size if (size < listSize) { for (song in shufflePlayBuffer[listSize - size]) { - val downloadFile = DownloadFile(song, false) + val downloadFile = song.getDownloadFile(false) playlist.add(downloadFile) playlistUpdateRevision++ } @@ -448,7 +455,7 @@ class Downloader( if (currIndex > SHUFFLE_BUFFER_LIMIT) { val songsToShift = currIndex - 2 for (song in shufflePlayBuffer[songsToShift]) { - playlist.add(DownloadFile(song, false)) + playlist.add(song.getDownloadFile(false)) playlist[0].cancelDownload() playlist.removeAt(0) playlistUpdateRevision++ @@ -475,9 +482,14 @@ class Downloader( const val SHUFFLE_BUFFER_LIMIT = 4 } - // Extension function - fun MusicDirectory.Entry.downloadFile(): DownloadFile { - return getDownloadFileForSong(this) + /** + * Extension function + * Gathers the donwload file for a given song, and modifies shouldSave if provided. + */ + fun MusicDirectory.Entry.getDownloadFile(save: Boolean? = null): DownloadFile { + return getDownloadFileForSong(this).apply { + if (save != null) this.shouldSave = save + } } } diff --git a/ultrasonic/src/main/res/layout/song_details.xml b/ultrasonic/src/main/res/layout/song_details.xml index 4de05f9d..30cb9f5a 100644 --- a/ultrasonic/src/main/res/layout/song_details.xml +++ b/ultrasonic/src/main/res/layout/song_details.xml @@ -17,18 +17,18 @@ a:id="@+id/song_track" a:layout_width="wrap_content" a:layout_height="wrap_content" + a:paddingEnd="6dip" a:layout_gravity="left|center_vertical" a:textAppearance="?android:attr/textAppearanceMedium"/>