diff --git a/app/src/main/java/foundation/e/apps/manager/download/DownloadManagerUtils.kt b/app/src/main/java/foundation/e/apps/manager/download/DownloadManagerUtils.kt index e85f97965e20b79bf20961c08c275808e504cd83..2b27368aa13235e40a293651abe287fb6c3bd380 100644 --- a/app/src/main/java/foundation/e/apps/manager/download/DownloadManagerUtils.kt +++ b/app/src/main/java/foundation/e/apps/manager/download/DownloadManagerUtils.kt @@ -1,6 +1,6 @@ /* + * Copyright ECORP SAS 2022 * Apps Quickly and easily install Android apps onto your device! - * Copyright (C) 2021 E FOUNDATION * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -64,10 +64,8 @@ class DownloadManagerUtils @Inject constructor( ) if (downloaded == fusedDownload.downloadIdMap.size) { fusedManagerRepository.moveOBBFileToOBBDirectory(fusedDownload) - fusedManagerRepository.updateDownloadStatus( - fusedDownload, - Status.INSTALLING - ) + fusedDownload.status = Status.DOWNLOADED + fusedManagerRepository.updateFusedDownload(fusedDownload) } } } diff --git a/app/src/main/java/foundation/e/apps/manager/fused/FusedManagerImpl.kt b/app/src/main/java/foundation/e/apps/manager/fused/FusedManagerImpl.kt index cc62ccae9735382a68ccf77b9b27fdc16af3df8c..b1a26f2caa873467e1ed1c6f4ac1357377f400cb 100644 --- a/app/src/main/java/foundation/e/apps/manager/fused/FusedManagerImpl.kt +++ b/app/src/main/java/foundation/e/apps/manager/fused/FusedManagerImpl.kt @@ -1,3 +1,21 @@ +/* + * Copyright ECORP SAS 2022 + * Apps Quickly and easily install Android apps onto your device! + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + package foundation.e.apps.manager.fused import android.app.DownloadManager @@ -115,6 +133,7 @@ class FusedManagerImpl @Inject constructor( Log.d(TAG, "installApp: ENDED ${fusedDownload.name} ${list.size}") } catch (e: Exception) { installationIssue(fusedDownload) + throw e } } } 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 61febf65ecfbcb7c83960afc988b67fa1a123aad..ca25722bf9186c410adfd87e9cb124e0e594d50c 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 @@ -1,3 +1,21 @@ +/* + * Copyright ECORP SAS 2022 + * Apps Quickly and easily install Android apps onto your device! + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + package foundation.e.apps.manager.workmanager import android.app.DownloadManager @@ -27,6 +45,7 @@ import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.takeWhile +import kotlinx.coroutines.sync.Mutex import java.util.concurrent.atomic.AtomicInteger import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds @@ -59,6 +78,8 @@ class InstallAppWorker @AssistedInject constructor( private val atomicInteger = AtomicInteger(100) } + private val mutex = Mutex(true) + override suspend fun doWork(): Result { var fusedDownload: FusedDownload? = null try { @@ -75,15 +96,16 @@ class InstallAppWorker @AssistedInject constructor( ) ) startAppInstallationProcess(it) + mutex.lock() } - Log.d(TAG, "doWork: RESULT SUCCESS: ${fusedDownload?.name}") - return Result.success() } catch (e: Exception) { Log.e(TAG, "doWork: Failed: ${e.stackTraceToString()}") fusedDownload?.let { fusedManagerRepository.installationIssue(it) } - return Result.failure() + } finally { + Log.d(TAG, "doWork: RESULT SUCCESS: ${fusedDownload?.name}") + return Result.success() } } @@ -141,20 +163,30 @@ class InstallAppWorker @AssistedInject constructor( .collect { fusedDownload -> if (fusedDownload == null) { isDownloading = false + unlockMutex() return@collect } Log.d( TAG, "doWork: flow collect ===> ${fusedDownload.name} ${fusedDownload.status}" ) - handleFusedDownloadStatus(fusedDownload) + try { + handleFusedDownloadStatus(fusedDownload) + } catch (e: Exception) { + Log.e(TAG, "observeDownload: ", e) + isDownloading = false + unlockMutex() + } } } - private fun handleFusedDownloadStatus(fusedDownload: FusedDownload) { + private suspend fun handleFusedDownloadStatus(fusedDownload: FusedDownload) { when (fusedDownload.status) { Status.AWAITING, Status.DOWNLOADING -> { } + Status.DOWNLOADED -> { + fusedManagerRepository.updateDownloadStatus(fusedDownload, Status.INSTALLING) + } Status.INSTALLING -> { Log.d( TAG, @@ -163,13 +195,15 @@ class InstallAppWorker @AssistedInject constructor( } Status.INSTALLED, Status.INSTALLATION_ISSUE -> { isDownloading = false + unlockMutex() Log.d( TAG, - "===> doWork: Installed/Failed started ${fusedDownload.name} ${fusedDownload.status}" + "===> doWork: Installed/Failed: ${fusedDownload.name} ${fusedDownload.status}" ) } else -> { isDownloading = false + unlockMutex() Log.wtf( TAG, "===> ${fusedDownload.name} is in wrong state ${fusedDownload.status}" @@ -178,6 +212,12 @@ class InstallAppWorker @AssistedInject constructor( } } + private fun unlockMutex() { + if (mutex.isLocked) { + mutex.unlock() + } + } + private fun createForegroundInfo(progress: String): ForegroundInfo { val title = applicationContext.getString(R.string.app_name) val cancel = applicationContext.getString(R.string.cancel) diff --git a/app/src/main/java/foundation/e/apps/utils/enums/Status.kt b/app/src/main/java/foundation/e/apps/utils/enums/Status.kt index 2b2eaad97b6afbc7697f92bdcb437344075aba59..3fd33977566e8ea25d0e30a790b4add4d433dca4 100644 --- a/app/src/main/java/foundation/e/apps/utils/enums/Status.kt +++ b/app/src/main/java/foundation/e/apps/utils/enums/Status.kt @@ -1,6 +1,6 @@ /* + * Copyright ECORP SAS 2022 * Apps Quickly and easily install Android apps onto your device! - * Copyright (C) 2021 E FOUNDATION * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,6 +23,7 @@ enum class Status { UPDATABLE, INSTALLING, DOWNLOADING, + DOWNLOADED, UNAVAILABLE, UNINSTALLING, QUEUED,