DownloadWorker: Handle cancelling downloads

* Keep results for 7 days atleast
* Cancel download using tag & remove app from download queue
* Tag downloads and updates. This will allow a user to query & cancel all updates or downloads in one click
* Default to cancel all downloads & updates

Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
Aayush Gupta
2023-11-07 20:25:03 +05:30
parent 67bd65be94
commit 1e75bcbf85

View File

@@ -41,6 +41,8 @@ import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.createDirectories
import kotlin.io.path.deleteRecursively
import kotlin.properties.Delegates
import kotlin.time.Duration.Companion.days
import kotlin.time.toJavaDuration
import com.aurora.gplayapi.data.models.File as GPlayFile
class DownloadWorker(private val appContext: Context, workerParams: WorkerParameters) :
@@ -48,14 +50,34 @@ class DownloadWorker(private val appContext: Context, workerParams: WorkerParame
companion object {
const val DOWNLOAD_WORKER = "DOWNLOAD_WORKER"
const val DOWNLOAD_PROGRESS = "DOWNLOAD_PROGRESS"
const val DOWNLOAD_TIME = "DOWNLOAD_TIME"
const val DOWNLOAD_SPEED = "DOWNLOAD_SPEED"
private const val DOWNLOAD_APP = "DOWNLOAD_APP"
private const val DOWNLOAD_UPDATE = "DOWNLOAD_UPDATE"
fun isEnqueued(packageName: String): Boolean {
return AuroraApplication.enqueuedDownloads.value.any { it.packageName == packageName }
}
fun enqueueApp(app: App) {
AuroraApplication.enqueuedDownloads.update { it.copyAndAdd(app) }
}
fun cancelDownload(context: Context, app: App) {
AuroraApplication.enqueuedDownloads.update {it.copyAndRemove(app) }
WorkManager.getInstance(context).cancelAllWorkByTag(app.packageName)
}
fun cancelAll(context: Context, downloads: Boolean = true, updates: Boolean = true) {
val workManager = WorkManager.getInstance(context)
if (downloads) workManager.cancelAllWorkByTag(DOWNLOAD_APP)
if (updates) workManager.cancelAllWorkByTag(DOWNLOAD_UPDATE)
}
/**
* Downloads and install an [App]
*
@@ -64,9 +86,12 @@ class DownloadWorker(private val appContext: Context, workerParams: WorkerParame
*/
fun downloadApp(context: Context, app: App) {
val work = OneTimeWorkRequestBuilder<DownloadWorker>()
.addTag(DOWNLOAD_WORKER)
.addTag(app.packageName)
.addTag(app.versionCode.toString())
.addTag(if (app.isInstalled) DOWNLOAD_UPDATE else DOWNLOAD_APP)
.setExpedited(OutOfQuotaPolicy.DROP_WORK_REQUEST)
.keepResultsForAtLeast(7.days.toJavaDuration())
.build()
WorkManager.getInstance(context).enqueueUniqueWork(DOWNLOAD_WORKER, KEEP, work)