diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index 8f04bf6a6..70fee7ad5 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -7,7 +7,10 @@ import android.content.Intent import android.os.Binder import android.os.Build import android.os.IBinder +import android.util.SparseBooleanArray import androidx.core.app.NotificationCompat +import androidx.core.util.set +import androidx.core.util.valueIterator import com.github.libretube.R import com.github.libretube.api.CronetHelper import com.github.libretube.api.RetrofitInstance @@ -66,8 +69,7 @@ class DownloadService : Service() { private lateinit var notificationManager: NotificationManager private lateinit var summaryNotificationBuilder: NotificationCompat.Builder - private val jobs = mutableMapOf() - private val downloadQueue = mutableMapOf() + private val downloadQueue = SparseBooleanArray() private val _downloadFlow = MutableSharedFlow>() val downloadFlow: SharedFlow> = _downloadFlow @@ -151,7 +153,7 @@ class DownloadService : Service() { Database.downloadDao().insertDownloadItem(item) }.toInt() - jobs[item.id] = scope.launch { + scope.launch { downloadFile(item) } } @@ -222,8 +224,7 @@ class DownloadService : Service() { try { // Check if downloading is still active and read next bytes. - while (downloadQueue[item.id] == true && - sourceByte + while (downloadQueue[item.id] && sourceByte .read(sink.buffer, DownloadHelper.DOWNLOAD_CHUNK_SIZE) .also { lastRead = it } != -1L ) { @@ -289,9 +290,12 @@ class DownloadService : Service() { */ fun resume(id: Int) { // If file is already downloading then avoid new download job. - if (downloadQueue[id] == true) return + if (downloadQueue[id]) { + return + } - if (downloadQueue.values.count { it } >= DownloadHelper.getMaxConcurrentDownloads()) { + val downloadCount = downloadQueue.valueIterator().asSequence().count { it } + if (downloadCount >= DownloadHelper.getMaxConcurrentDownloads()) { toastFromMainThread(getString(R.string.concurrent_downloads_limit_reached)) scope.launch { _downloadFlow.emit(id to DownloadStatus.Paused) @@ -314,7 +318,7 @@ class DownloadService : Service() { downloadQueue[id] = false // Stop the service if no downloads are active. - if (downloadQueue.none { it.value }) { + if (downloadQueue.valueIterator().asSequence().none { it }) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { stopForeground(STOP_FOREGROUND_DETACH) } @@ -345,7 +349,7 @@ class DownloadService : Service() { * Check whether the file downloading or not. */ fun isDownloading(id: Int): Boolean { - return downloadQueue[id] ?: false + return downloadQueue[id] } private fun notifyForeground() {