From 5a8529c677d1b55b8554fe23bf303d9440493bfe Mon Sep 17 00:00:00 2001 From: hasibprince Date: Thu, 15 Dec 2022 22:32:28 +0600 Subject: [PATCH] fixed: app installation if device is rebooted --- .../database/fusedDownload/FusedDownload.kt | 4 ++++ .../manager/workmanager/InstallAppWorker.kt | 21 ++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt b/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt index 0cd910a68..7cf086c69 100644 --- a/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt +++ b/app/src/main/java/foundation/e/apps/manager/database/fusedDownload/FusedDownload.kt @@ -26,3 +26,7 @@ data class FusedDownload( var files: List = mutableListOf(), var signature: String = String() ) + +fun FusedDownload.isAppInstalling() = listOf(Status.AWAITING, Status.DOWNLOADING, Status.DOWNLOADED, Status.INSTALLING).contains(status) + +fun FusedDownload.isAwaiting() = status == Status.AWAITING diff --git a/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt b/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt index 8460149c6..c44d097d6 100644 --- a/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt +++ b/app/src/main/java/foundation/e/apps/manager/workmanager/InstallAppWorker.kt @@ -36,6 +36,8 @@ import foundation.e.apps.R import foundation.e.apps.api.fused.UpdatesDao import foundation.e.apps.manager.database.DatabaseRepository import foundation.e.apps.manager.database.fusedDownload.FusedDownload +import foundation.e.apps.manager.database.fusedDownload.isAppInstalling +import foundation.e.apps.manager.database.fusedDownload.isAwaiting import foundation.e.apps.manager.fused.FusedManagerRepository import foundation.e.apps.manager.pkg.PkgManagerModule import foundation.e.apps.updates.UpdatesNotifier @@ -103,9 +105,10 @@ class InstallAppWorker @AssistedInject constructor( isItUpdateWork = params.inputData.getBoolean(IS_UPDATE_WORK, false) && packageManagerModule.isInstalled(it.packageName) - if (fusedDownload.status != Status.AWAITING) { + if (!fusedDownload.isAppInstalling()) { return Result.success() } + setForeground( createForegroundInfo( "Installing ${it.name}" @@ -186,9 +189,16 @@ class InstallAppWorker @AssistedInject constructor( private suspend fun startAppInstallationProcess( fusedDownload: FusedDownload ) { - fusedManagerRepository.downloadApp(fusedDownload) - Timber.d("===> doWork: Download started ${fusedDownload.name} ${fusedDownload.status}") + if (fusedDownload.isAwaiting()) { + fusedManagerRepository.downloadApp(fusedDownload) + Timber.d("===> doWork: Download started ${fusedDownload.name} ${fusedDownload.status}") + } + isDownloading = true + /** + * observe app download/install process in a separate thread as DownloadManager download artifacts in a separate process + * It checks install status every three seconds + */ tickerFlow(3.seconds) .onEach { val download = databaseRepository.getDownloadById(fusedDownload.id) @@ -220,6 +230,11 @@ class InstallAppWorker @AssistedInject constructor( } } + /** + * Triggers a repetitive event according to the delay passed in the parameter + * @param period delay of each event + * @param initialDelay initial delay to trigger the first event + */ private fun tickerFlow(period: Duration, initialDelay: Duration = Duration.ZERO) = flow { delay(initialDelay) while (isDownloading) { -- GitLab