Improved the resuming of downloads.

* Improved the resuming download logic.
This commit is contained in:
MohitMaliFtechiz 2024-12-18 13:16:26 +05:30 committed by Kelson
parent 9b8abe5ab2
commit 1f55d95386

View File

@ -20,7 +20,6 @@ package org.kiwix.kiwixmobile.core.downloader.downloadManager
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.DownloadManager import android.app.DownloadManager
import android.content.ContentUris
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
@ -562,9 +561,13 @@ class DownloadManagerMonitor @Inject constructor(
put(COLUMN_CONTROL, control) put(COLUMN_CONTROL, control)
put(DownloadManager.COLUMN_STATUS, status) put(DownloadManager.COLUMN_STATUS, status)
} }
val uri = ContentUris.withAppendedId(downloadBaseUri, downloadId)
context.contentResolver context.contentResolver
.update(uri, contentValues, null, null) .update(
downloadBaseUri,
contentValues,
getWhereClauseForIds(longArrayOf(downloadId)),
getWhereArgsForIds(longArrayOf(downloadId))
)
true true
} catch (ignore: Exception) { } catch (ignore: Exception) {
Log.e("DOWNLOAD_MONITOR", "Couldn't pause/resume the download. Original exception = $ignore") Log.e("DOWNLOAD_MONITOR", "Couldn't pause/resume the download. Original exception = $ignore")
@ -572,6 +575,33 @@ class DownloadManagerMonitor @Inject constructor(
} }
} }
private fun getWhereArgsForIds(ids: LongArray): Array<String?> {
val whereArgs = arrayOfNulls<String>(ids.size)
return getWhereArgsForIds(ids, whereArgs)
}
private fun getWhereArgsForIds(ids: LongArray, args: Array<String?>): Array<String?> {
assert(args.size >= ids.size)
for (i in ids.indices) {
args[i] = "${ids[i]}"
}
return args
}
private fun getWhereClauseForIds(ids: LongArray): String {
val whereClause = StringBuilder()
whereClause.append("(")
for (i in ids.indices) {
if (i > 0) {
whereClause.append("OR ")
}
whereClause.append("_id")
whereClause.append(" = ? ")
}
whereClause.append(")")
return "$whereClause"
}
private fun shouldUpdateDownloadStatus(downloadRoomEntity: DownloadRoomEntity) = private fun shouldUpdateDownloadStatus(downloadRoomEntity: DownloadRoomEntity) =
downloadRoomEntity.status != Status.COMPLETED downloadRoomEntity.status != Status.COMPLETED