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:
@@ -193,7 +193,7 @@ class DownloadWorker @AssistedInject constructor(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun onProgress(downloadInfo: DownloadInfo) {
|
private suspend fun onProgress(downloadInfo: DownloadInfo) {
|
||||||
if (!isStopped) {
|
if (!isStopped && !download.isFinished) {
|
||||||
val progress = ((downloadedBytes + downloadInfo.bytesCopied) * 100 / totalBytes).toInt()
|
val progress = ((downloadedBytes + downloadInfo.bytesCopied) * 100 / totalBytes).toInt()
|
||||||
|
|
||||||
// Individual file progress can be negligible in contrast to total progress
|
// 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) {
|
private suspend fun notifyStatus(status: DownloadStatus, dID: Int = -1) {
|
||||||
// Update database for all status except downloading which is handled onProgress
|
// Update database for all status except downloading which is handled onProgress
|
||||||
if (status != DownloadStatus.DOWNLOADING) {
|
if (status != DownloadStatus.DOWNLOADING) {
|
||||||
download.status = status
|
download.apply {
|
||||||
|
this.status = status
|
||||||
|
if (download.status == DownloadStatus.COMPLETED) this.progress = 100
|
||||||
|
}
|
||||||
downloadDao.update(download)
|
downloadDao.update(download)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -247,7 +247,7 @@ object NotificationUtil {
|
|||||||
builder.setCategory(Notification.CATEGORY_ERROR)
|
builder.setCategory(Notification.CATEGORY_ERROR)
|
||||||
}
|
}
|
||||||
|
|
||||||
DownloadStatus.COMPLETED -> if (download.progress == 100) {
|
DownloadStatus.COMPLETED -> {
|
||||||
builder.setSmallIcon(android.R.drawable.stat_sys_download_done)
|
builder.setSmallIcon(android.R.drawable.stat_sys_download_done)
|
||||||
builder.setContentText(context.getString(R.string.download_completed))
|
builder.setContentText(context.getString(R.string.download_completed))
|
||||||
builder.setAutoCancel(true)
|
builder.setAutoCancel(true)
|
||||||
|
|||||||
Reference in New Issue
Block a user