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 <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2023-12-20 21:07:54 +05:30
parent 010dde2896
commit e915c5060b
2 changed files with 6 additions and 3 deletions

View File

@@ -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)
}

View File

@@ -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)