@@ -18,7 +18,6 @@ import androidx.core.app.PendingIntentCompat
1818import androidx.core.app.ServiceCompat
1919import androidx.core.content.getSystemService
2020import androidx.core.util.keyIterator
21- import androidx.core.util.remove
2221import androidx.core.util.set
2322import androidx.core.util.valueIterator
2423import androidx.lifecycle.LifecycleService
@@ -59,7 +58,6 @@ import kotlinx.coroutines.SupervisorJob
5958import kotlinx.coroutines.asCoroutineDispatcher
6059import kotlinx.coroutines.flow.MutableSharedFlow
6160import kotlinx.coroutines.flow.SharedFlow
62- import kotlinx.coroutines.flow.firstOrNull
6361import kotlinx.coroutines.launch
6462import kotlinx.coroutines.withContext
6563import okhttp3.OkHttpClient
@@ -252,18 +250,20 @@ class DownloadService : LifecycleService() {
252250
253251 downloadQueue[item.id] = false
254252
255- if (downloadFlow.firstOrNull { it.first == item.id }?.second == DownloadStatus .Stopped ) {
256- downloadQueue.remove(item.id, false )
257- }
258-
259253 // start the next download if there are any remaining ones enqueued
260- val nextDownload =
261- downloadFlow.firstOrNull { (_, status) -> status == DownloadStatus .Paused }
262- if (nextDownload != null ) {
263- resume(nextDownload.first)
264- } else {
265- stopServiceIfDone()
254+ for (id in downloadQueue.keyIterator()) {
255+ if (downloadQueue[id]) continue
256+
257+ val dbItem = Database .downloadDao().findDownloadItemById(id)
258+ if (dbItem != null && (dbItem.downloadSize <= 0L || dbItem.path.fileSize() < dbItem.downloadSize)) {
259+ resume(id)
260+ return
261+ }
266262 }
263+
264+ // if no new download was enqueued (i.e. there's no paused/stopped download left),
265+ // look if any downloads are still running, and if not, stop the service
266+ stopServiceIfDone()
267267 }
268268
269269 private suspend fun progressDownload (
0 commit comments