From 8f5f6cb2941df94e828c6957a5f9af98d48d1f11 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Mon, 16 Dec 2024 15:19:27 +0530 Subject: [PATCH] Improved the query to DownloadManager. * 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. --- .../downloadManager/DownloadManagerMonitor.kt | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/DownloadManagerMonitor.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/DownloadManagerMonitor.kt index 8e0964c1fe..3191c0d79f 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/DownloadManagerMonitor.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/downloadManager/DownloadManagerMonitor.kt @@ -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() } @@ -128,21 +129,10 @@ class DownloadManagerMonitor @Inject constructor( } @SuppressLint("Range") - private fun checkDownloads() { + private fun checkDownloads(downloadingList: List) { 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) } } } @@ -150,11 +140,13 @@ class DownloadManagerMonitor @Inject constructor( @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) + } } } }