From d084270c4bdf81e2421ddffa51054e3c9eb7b0c6 Mon Sep 17 00:00:00 2001 From: James Wells Date: Sun, 25 Apr 2021 23:08:12 -0400 Subject: [PATCH] Add a retry count to stop infinite downloads Signed-off-by: James Wells --- .../java/org/moire/ultrasonic/service/Downloader.java | 6 ++++++ .../kotlin/org/moire/ultrasonic/service/DownloadFile.kt | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/service/Downloader.java b/ultrasonic/src/main/java/org/moire/ultrasonic/service/Downloader.java index 6aa31584..fa2eaa6e 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/service/Downloader.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/service/Downloader.java @@ -194,6 +194,12 @@ public class Downloader revision++; i--; } + else if (downloadFile.isFailed() && !downloadFile.shouldRetry()) { + // Don't continue to attempt to download forever + backgroundDownloadList.remove(i); + revision++; + i--; + } else { currentDownloading = downloadFile; 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 08b081c6..2f486196 100644 --- a/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadFile.kt +++ b/ultrasonic/src/main/kotlin/org/moire/ultrasonic/service/DownloadFile.kt @@ -45,6 +45,7 @@ class DownloadFile( private val mediaStoreService: MediaStoreService private var downloadTask: CancellableTask? = null var isFailed = false + private var retryCount = 5 private val desiredBitRate: Int = Util.getMaxBitRate(context) @@ -127,6 +128,10 @@ class DownloadFile( return save } + fun shouldRetry(): Boolean { + return (retryCount > 0) + } + fun delete() { cancelDownload() Util.delete(partialFile) @@ -288,6 +293,9 @@ class DownloadFile( Util.delete(saveFile) if (!isCancelled) { isFailed = true + if (retryCount > 0) { + --retryCount + } Timber.w(x, "Failed to download '%s'.", song) } } finally {