mirror of
https://github.com/ultrasonic/ultrasonic
synced 2025-02-18 04:30:48 +01:00
Don't create DownloadFile instances unnecessarily
This commit is contained in:
parent
d0e39efc50
commit
19d014709f
@ -35,8 +35,9 @@ import timber.log.Timber
|
|||||||
*/
|
*/
|
||||||
class DownloadFile(
|
class DownloadFile(
|
||||||
val song: MusicDirectory.Entry,
|
val song: MusicDirectory.Entry,
|
||||||
private val save: Boolean
|
save: Boolean
|
||||||
) : KoinComponent, Identifiable {
|
) : KoinComponent, Identifiable {
|
||||||
|
var shouldSave = save
|
||||||
val partialFile: File
|
val partialFile: File
|
||||||
val completeFile: File
|
val completeFile: File
|
||||||
private val saveFile: File = FileUtil.getSongFile(song)
|
private val saveFile: File = FileUtil.getSongFile(song)
|
||||||
@ -114,7 +115,7 @@ class DownloadFile(
|
|||||||
|
|
||||||
@get:Synchronized
|
@get:Synchronized
|
||||||
val isWorkDone: Boolean
|
val isWorkDone: Boolean
|
||||||
get() = saveFile.exists() || completeFile.exists() && !save ||
|
get() = saveFile.exists() || completeFile.exists() && !shouldSave ||
|
||||||
saveWhenDone || completeWhenDone
|
saveWhenDone || completeWhenDone
|
||||||
|
|
||||||
@get:Synchronized
|
@get:Synchronized
|
||||||
@ -125,10 +126,6 @@ class DownloadFile(
|
|||||||
val isDownloadCancelled: Boolean
|
val isDownloadCancelled: Boolean
|
||||||
get() = downloadTask != null && downloadTask!!.isCancelled
|
get() = downloadTask != null && downloadTask!!.isCancelled
|
||||||
|
|
||||||
fun shouldSave(): Boolean {
|
|
||||||
return save
|
|
||||||
}
|
|
||||||
|
|
||||||
fun shouldRetry(): Boolean {
|
fun shouldRetry(): Boolean {
|
||||||
return (retryCount > 0)
|
return (retryCount > 0)
|
||||||
}
|
}
|
||||||
@ -188,7 +185,7 @@ class DownloadFile(
|
|||||||
Util.renameFile(completeFile, saveFile)
|
Util.renameFile(completeFile, saveFile)
|
||||||
saveWhenDone = false
|
saveWhenDone = false
|
||||||
} else if (completeWhenDone) {
|
} else if (completeWhenDone) {
|
||||||
if (save) {
|
if (shouldSave) {
|
||||||
Util.renameFile(partialFile, saveFile)
|
Util.renameFile(partialFile, saveFile)
|
||||||
Util.scanMedia(saveFile)
|
Util.scanMedia(saveFile)
|
||||||
} else {
|
} else {
|
||||||
@ -216,11 +213,12 @@ class DownloadFile(
|
|||||||
if (saveFile.exists()) {
|
if (saveFile.exists()) {
|
||||||
Timber.i("%s already exists. Skipping.", saveFile)
|
Timber.i("%s already exists. Skipping.", saveFile)
|
||||||
status.postValue(DownloadStatus.DONE)
|
status.postValue(DownloadStatus.DONE)
|
||||||
|
Timber.i("UPDATING STATUS")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (completeFile.exists()) {
|
if (completeFile.exists()) {
|
||||||
if (save) {
|
if (shouldSave) {
|
||||||
if (isPlaying) {
|
if (isPlaying) {
|
||||||
saveWhenDone = true
|
saveWhenDone = true
|
||||||
} else {
|
} else {
|
||||||
@ -230,6 +228,7 @@ class DownloadFile(
|
|||||||
Timber.i("%s already exists. Skipping.", completeFile)
|
Timber.i("%s already exists. Skipping.", completeFile)
|
||||||
}
|
}
|
||||||
status.postValue(DownloadStatus.DONE)
|
status.postValue(DownloadStatus.DONE)
|
||||||
|
Timber.i("UPDATING STATUS")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,7 +251,7 @@ class DownloadFile(
|
|||||||
if (needsDownloading) {
|
if (needsDownloading) {
|
||||||
// Attempt partial HTTP GET, appending to the file if it exists.
|
// Attempt partial HTTP GET, appending to the file if it exists.
|
||||||
val (inStream, partial) = musicService.getDownloadInputStream(
|
val (inStream, partial) = musicService.getDownloadInputStream(
|
||||||
song, partialFile.length(), desiredBitRate, save
|
song, partialFile.length(), desiredBitRate, shouldSave
|
||||||
)
|
)
|
||||||
|
|
||||||
inputStream = inStream
|
inputStream = inStream
|
||||||
@ -293,7 +292,7 @@ class DownloadFile(
|
|||||||
if (isPlaying) {
|
if (isPlaying) {
|
||||||
completeWhenDone = true
|
completeWhenDone = true
|
||||||
} else {
|
} else {
|
||||||
if (save) {
|
if (shouldSave) {
|
||||||
Util.renameFile(partialFile, saveFile)
|
Util.renameFile(partialFile, saveFile)
|
||||||
Util.scanMedia(saveFile)
|
Util.scanMedia(saveFile)
|
||||||
} else {
|
} else {
|
||||||
@ -379,6 +378,7 @@ class DownloadFile(
|
|||||||
private fun setProgress(totalBytesCopied: Long) {
|
private fun setProgress(totalBytesCopied: Long) {
|
||||||
if (song.size != null) {
|
if (song.size != null) {
|
||||||
progress.postValue((totalBytesCopied * 100 / song.size!!).toInt())
|
progress.postValue((totalBytesCopied * 100 / song.size!!).toInt())
|
||||||
|
Timber.i("UPDATING PROGESS")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,6 +55,8 @@ class Downloader(
|
|||||||
RxBus.playlistPublisher.onNext(playlist)
|
RxBus.playlistPublisher.onNext(playlist)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var backgroundPriorityCounter = 100
|
||||||
|
|
||||||
val downloadChecker = Runnable {
|
val downloadChecker = Runnable {
|
||||||
try {
|
try {
|
||||||
Timber.w("Checking Downloads")
|
Timber.w("Checking Downloads")
|
||||||
@ -303,6 +305,8 @@ class Downloader(
|
|||||||
activelyDownloading.remove(download)
|
activelyDownloading.remove(download)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
backgroundPriorityCounter = 100
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
@ -327,7 +331,7 @@ class Downloader(
|
|||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
fun addToPlaylist(
|
fun addToPlaylist(
|
||||||
songs: List<MusicDirectory.Entry?>,
|
songs: List<MusicDirectory.Entry>,
|
||||||
save: Boolean,
|
save: Boolean,
|
||||||
autoPlay: Boolean,
|
autoPlay: Boolean,
|
||||||
playNext: Boolean,
|
playNext: Boolean,
|
||||||
@ -346,13 +350,13 @@ class Downloader(
|
|||||||
offset = 0
|
offset = 0
|
||||||
}
|
}
|
||||||
for (song in songs) {
|
for (song in songs) {
|
||||||
val downloadFile = DownloadFile(song!!, save)
|
val downloadFile = song.getDownloadFile(save)
|
||||||
playlist.add(currentPlayingIndex + offset, downloadFile)
|
playlist.add(currentPlayingIndex + offset, downloadFile)
|
||||||
offset++
|
offset++
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (song in songs) {
|
for (song in songs) {
|
||||||
val downloadFile = DownloadFile(song!!, save)
|
val downloadFile = song.getDownloadFile(save)
|
||||||
playlist.add(downloadFile)
|
playlist.add(downloadFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -380,7 +384,10 @@ class Downloader(
|
|||||||
// Because of the priority handling we add the songs in the reverse order they
|
// Because of the priority handling we add the songs in the reverse order they
|
||||||
// were requested, then it is correct in the end.
|
// were requested, then it is correct in the end.
|
||||||
for (song in songs.asReversed()) {
|
for (song in songs.asReversed()) {
|
||||||
downloadQueue.add(DownloadFile(song, save))
|
val file = song.getDownloadFile()
|
||||||
|
file.shouldSave = save
|
||||||
|
file.priority = backgroundPriorityCounter++
|
||||||
|
downloadQueue.add(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
checkDownloads()
|
checkDownloads()
|
||||||
@ -436,7 +443,7 @@ class Downloader(
|
|||||||
val size = playlist.size
|
val size = playlist.size
|
||||||
if (size < listSize) {
|
if (size < listSize) {
|
||||||
for (song in shufflePlayBuffer[listSize - size]) {
|
for (song in shufflePlayBuffer[listSize - size]) {
|
||||||
val downloadFile = DownloadFile(song, false)
|
val downloadFile = song.getDownloadFile(false)
|
||||||
playlist.add(downloadFile)
|
playlist.add(downloadFile)
|
||||||
playlistUpdateRevision++
|
playlistUpdateRevision++
|
||||||
}
|
}
|
||||||
@ -448,7 +455,7 @@ class Downloader(
|
|||||||
if (currIndex > SHUFFLE_BUFFER_LIMIT) {
|
if (currIndex > SHUFFLE_BUFFER_LIMIT) {
|
||||||
val songsToShift = currIndex - 2
|
val songsToShift = currIndex - 2
|
||||||
for (song in shufflePlayBuffer[songsToShift]) {
|
for (song in shufflePlayBuffer[songsToShift]) {
|
||||||
playlist.add(DownloadFile(song, false))
|
playlist.add(song.getDownloadFile(false))
|
||||||
playlist[0].cancelDownload()
|
playlist[0].cancelDownload()
|
||||||
playlist.removeAt(0)
|
playlist.removeAt(0)
|
||||||
playlistUpdateRevision++
|
playlistUpdateRevision++
|
||||||
@ -475,9 +482,14 @@ class Downloader(
|
|||||||
const val SHUFFLE_BUFFER_LIMIT = 4
|
const val SHUFFLE_BUFFER_LIMIT = 4
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extension function
|
/**
|
||||||
fun MusicDirectory.Entry.downloadFile(): DownloadFile {
|
* Extension function
|
||||||
return getDownloadFileForSong(this)
|
* 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,18 +17,18 @@
|
|||||||
a:id="@+id/song_track"
|
a:id="@+id/song_track"
|
||||||
a:layout_width="wrap_content"
|
a:layout_width="wrap_content"
|
||||||
a:layout_height="wrap_content"
|
a:layout_height="wrap_content"
|
||||||
|
a:paddingEnd="6dip"
|
||||||
a:layout_gravity="left|center_vertical"
|
a:layout_gravity="left|center_vertical"
|
||||||
a:textAppearance="?android:attr/textAppearanceMedium"/>
|
a:textAppearance="?android:attr/textAppearanceMedium"/>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
a:id="@+id/song_title"
|
a:id="@+id/song_title"
|
||||||
a:layout_width="0dip"
|
a:layout_width="0dp"
|
||||||
a:layout_height="wrap_content"
|
a:layout_height="wrap_content"
|
||||||
a:layout_gravity="left|center_vertical"
|
a:layout_gravity="left|center_vertical"
|
||||||
a:layout_weight="1"
|
a:layout_weight="1"
|
||||||
a:drawablePadding="4dip"
|
a:drawablePadding="4dip"
|
||||||
a:ellipsize="end"
|
a:ellipsize="end"
|
||||||
a:paddingStart="6dip"
|
|
||||||
a:paddingEnd="4dip"
|
a:paddingEnd="4dip"
|
||||||
a:singleLine="true"
|
a:singleLine="true"
|
||||||
a:textAppearance="?android:attr/textAppearanceMedium"/>
|
a:textAppearance="?android:attr/textAppearanceMedium"/>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user