From e915c5060b2365e0e8cc35bb6633b59ab54ff5b6 Mon Sep 17 00:00:00 2001 From: Aayush Gupta Date: Wed, 20 Dec 2023 21:07:54 +0530 Subject: [PATCH] DownloadWorker: Address race condition on download complete Sometimes when download gets completed, the progress hasn't been updated which results in no notification data and downloads showing status as completed with a progress less than 100%. Set progress manually to 100% and avoid checking progress in notificationUtil if download has succeded. Also avoid calling onProgress again if download has finished. Signed-off-by: Aayush Gupta --- .../main/java/com/aurora/store/data/work/DownloadWorker.kt | 7 +++++-- .../main/java/com/aurora/store/util/NotificationUtil.kt | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt b/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt index 4d0af1959..2ba64aee4 100644 --- a/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt +++ b/app/src/main/java/com/aurora/store/data/work/DownloadWorker.kt @@ -193,7 +193,7 @@ class DownloadWorker @AssistedInject constructor( } private suspend fun onProgress(downloadInfo: DownloadInfo) { - if (!isStopped) { + if (!isStopped && !download.isFinished) { val progress = ((downloadedBytes + downloadInfo.bytesCopied) * 100 / totalBytes).toInt() // Individual file progress can be negligible in contrast to total progress @@ -236,7 +236,10 @@ class DownloadWorker @AssistedInject constructor( private suspend fun notifyStatus(status: DownloadStatus, dID: Int = -1) { // Update database for all status except downloading which is handled onProgress if (status != DownloadStatus.DOWNLOADING) { - download.status = status + download.apply { + this.status = status + if (download.status == DownloadStatus.COMPLETED) this.progress = 100 + } downloadDao.update(download) } diff --git a/app/src/main/java/com/aurora/store/util/NotificationUtil.kt b/app/src/main/java/com/aurora/store/util/NotificationUtil.kt index a08d97b46..2583cca3b 100644 --- a/app/src/main/java/com/aurora/store/util/NotificationUtil.kt +++ b/app/src/main/java/com/aurora/store/util/NotificationUtil.kt @@ -247,7 +247,7 @@ object NotificationUtil { builder.setCategory(Notification.CATEGORY_ERROR) } - DownloadStatus.COMPLETED -> if (download.progress == 100) { + DownloadStatus.COMPLETED -> { builder.setSmallIcon(android.R.drawable.stat_sys_download_done) builder.setContentText(context.getString(R.string.download_completed)) builder.setAutoCancel(true)