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 0cd910a68676c1b608d1ba0df8a38a710cdf9431..7cf086c690c8b67ee4d9bced757b623b0de4c836 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 8460149c6116e39738332c6e279cd0f5614d6fa4..c44d097d681f9f464973b1521b442c60adf03603 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) {