Skip to content

Commit

Permalink
Improved the query to DownloadManager.
Browse files Browse the repository at this point in the history
* Removed the unnecessary query to download manager for previous downloads. Now we are only making request to active downloads which are in our download DAO.
  • Loading branch information
MohitMaliDeveloper committed Dec 16, 2024
1 parent 352606c commit 8f5f6cb
Showing 1 changed file with 13 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ class DownloadManagerMonitor @Inject constructor(
{
try {
synchronized(lock) {
if (downloadRoomDao.downloads().blockingFirst().isNotEmpty()) {
checkDownloads()
val downloadingList = downloadRoomDao.downloads().blockingFirst()
if (downloadingList.isNotEmpty()) {
checkDownloads(downloadingList)
} else {
monitoringDisposable?.dispose()
}
Expand All @@ -128,33 +129,24 @@ class DownloadManagerMonitor @Inject constructor(
}

@SuppressLint("Range")
private fun checkDownloads() {
private fun checkDownloads(downloadingList: List<DownloadModel>) {
synchronized(lock) {
val query = DownloadManager.Query().setFilterByStatus(
DownloadManager.STATUS_RUNNING or
DownloadManager.STATUS_PAUSED or
DownloadManager.STATUS_PENDING or
DownloadManager.STATUS_SUCCESSFUL
)
downloadManager.query(query).use { cursor ->
if (cursor.moveToFirst()) {
do {
val downloadId = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID))
queryDownloadStatus(downloadId)
} while (cursor.moveToNext())
}
downloadingList.forEach {
queryDownloadStatus(it.downloadId)
}
}
}

@SuppressLint("Range")
fun queryDownloadStatus(downloadId: Long) {
synchronized(lock) {
downloadManager.query(DownloadManager.Query().setFilterById(downloadId)).use { cursor ->
if (cursor.moveToFirst()) {
handleDownloadStatus(cursor, downloadId)
} else {
handleCancelledDownload(downloadId)
updater.onNext {
downloadManager.query(DownloadManager.Query().setFilterById(downloadId)).use { cursor ->
if (cursor.moveToFirst()) {
handleDownloadStatus(cursor, downloadId)
} else {
handleCancelledDownload(downloadId)
}
}
}
}
Expand Down

0 comments on commit 8f5f6cb

Please sign in to comment.