From 6150be6d030d9c96258e73efdb4fb3f80eb80188 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Sun, 22 May 2022 23:53:40 +0600 Subject: [PATCH 1/3] App Lounge: draft fix for update stuck --- .../manager/download/DownloadManagerUtils.kt | 10 +++-- .../e/apps/manager/fused/FusedManagerImpl.kt | 1 + .../manager/workmanager/InstallAppWorker.kt | 43 ++++++++++++++----- .../foundation/e/apps/utils/enums/Status.kt | 1 + 4 files changed, 40 insertions(+), 15 deletions(-) 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 e85f97965..0f074bdeb 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 @@ -64,10 +64,12 @@ class DownloadManagerUtils @Inject constructor( ) if (downloaded == fusedDownload.downloadIdMap.size) { fusedManagerRepository.moveOBBFileToOBBDirectory(fusedDownload) - fusedManagerRepository.updateDownloadStatus( - fusedDownload, - Status.INSTALLING - ) + fusedDownload.status = Status.DOWNLOADED + fusedManagerRepository.updateFusedDownload(fusedDownload) +// fusedManagerRepository.updateDownloadStatus( +// fusedDownload, +// Status.INSTALLING +// ) } } } 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 cc62ccae9..fa23c24d4 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 @@ -115,6 +115,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 61febf65e..18c2b21ac 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 @@ -27,6 +27,8 @@ 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.lang.RuntimeException import java.util.concurrent.atomic.AtomicInteger import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds @@ -59,6 +61,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 +79,19 @@ class InstallAppWorker @AssistedInject constructor( ) ) startAppInstallationProcess(it) + mutex.lock() } - Log.d(TAG, "doWork: RESULT SUCCESS: ${fusedDownload?.name}") - return Result.success() +// 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() +// return Result.failure() + } finally { + Log.d(TAG, "doWork: RESULT SUCCESS: ${fusedDownload?.name}") + return Result.success() } } @@ -94,10 +102,10 @@ class InstallAppWorker @AssistedInject constructor( Log.d(TAG, "===> doWork: Download started ${fusedDownload.name} ${fusedDownload.status}") if (fusedDownload.type == Type.NATIVE) { isDownloading = true - tickerFlow(1.seconds) - .onEach { - checkDownloadProcess(fusedDownload) - }.launchIn(CoroutineScope(Dispatchers.IO)) +// tickerFlow(1.seconds) +// .onEach { +// checkDownloadProcess(fusedDownload) +// }.launchIn(CoroutineScope(Dispatchers.IO)) observeDownload(fusedDownload) } } @@ -141,20 +149,29 @@ class InstallAppWorker @AssistedInject constructor( .collect { fusedDownload -> if (fusedDownload == null) { isDownloading = false + mutex.unlock() 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) + mutex.unlock() + } } } - 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, @@ -165,8 +182,12 @@ class InstallAppWorker @AssistedInject constructor( isDownloading = false Log.d( TAG, - "===> doWork: Installed/Failed started ${fusedDownload.name} ${fusedDownload.status}" + "===> doWork: Installed/Failed: ${fusedDownload.name} ${fusedDownload.status}" ) +// if(fusedDownload.status == Status.INSTALLATION_ISSUE) { +// throw RuntimeException("Installation error occurred") +// } + mutex.unlock() } else -> { isDownloading = false @@ -187,7 +208,7 @@ class InstallAppWorker @AssistedInject constructor( val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as - NotificationManager + NotificationManager // Create a Notification channel if necessary if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val mChannel = NotificationChannel( 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 2b2eaad97..3af256f0d 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 @@ -23,6 +23,7 @@ enum class Status { UPDATABLE, INSTALLING, DOWNLOADING, + DOWNLOADED, UNAVAILABLE, UNINSTALLING, QUEUED, -- GitLab From 89ba77bc9f5e6b45af7d55af053afc950d348343 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Mon, 23 May 2022 17:12:01 +0600 Subject: [PATCH 2/3] App Lounge: fixed work manager stucks if app install faces exception --- .../manager/workmanager/InstallAppWorker.kt | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) 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 18c2b21ac..290921bad 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 @@ -81,14 +81,11 @@ 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() @@ -102,10 +99,10 @@ class InstallAppWorker @AssistedInject constructor( Log.d(TAG, "===> doWork: Download started ${fusedDownload.name} ${fusedDownload.status}") if (fusedDownload.type == Type.NATIVE) { isDownloading = true -// tickerFlow(1.seconds) -// .onEach { -// checkDownloadProcess(fusedDownload) -// }.launchIn(CoroutineScope(Dispatchers.IO)) + tickerFlow(1.seconds) + .onEach { + checkDownloadProcess(fusedDownload) + }.launchIn(CoroutineScope(Dispatchers.IO)) observeDownload(fusedDownload) } } @@ -149,7 +146,7 @@ class InstallAppWorker @AssistedInject constructor( .collect { fusedDownload -> if (fusedDownload == null) { isDownloading = false - mutex.unlock() + unlockMutex() return@collect } Log.d( @@ -160,7 +157,8 @@ class InstallAppWorker @AssistedInject constructor( handleFusedDownloadStatus(fusedDownload) } catch (e: Exception) { Log.e(TAG, "observeDownload: ", e) - mutex.unlock() + isDownloading = false + unlockMutex() } } } @@ -180,17 +178,15 @@ class InstallAppWorker @AssistedInject constructor( } Status.INSTALLED, Status.INSTALLATION_ISSUE -> { isDownloading = false + unlockMutex() Log.d( TAG, "===> doWork: Installed/Failed: ${fusedDownload.name} ${fusedDownload.status}" ) -// if(fusedDownload.status == Status.INSTALLATION_ISSUE) { -// throw RuntimeException("Installation error occurred") -// } - mutex.unlock() } else -> { isDownloading = false + unlockMutex() Log.wtf( TAG, "===> ${fusedDownload.name} is in wrong state ${fusedDownload.status}" @@ -199,6 +195,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) -- GitLab From eb006b470b04fd99249974e6b646430c02678091 Mon Sep 17 00:00:00 2001 From: Hasib Prince Date: Mon, 23 May 2022 17:44:54 +0600 Subject: [PATCH 3/3] App Lounge: License added, code clean up --- .../manager/download/DownloadManagerUtils.kt | 6 +---- .../e/apps/manager/fused/FusedManagerImpl.kt | 18 +++++++++++++++ .../manager/workmanager/InstallAppWorker.kt | 23 ++++++++++++++++--- .../foundation/e/apps/utils/enums/Status.kt | 2 +- 4 files changed, 40 insertions(+), 9 deletions(-) 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 0f074bdeb..2b27368aa 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 @@ -66,10 +66,6 @@ class DownloadManagerUtils @Inject constructor( fusedManagerRepository.moveOBBFileToOBBDirectory(fusedDownload) fusedDownload.status = Status.DOWNLOADED fusedManagerRepository.updateFusedDownload(fusedDownload) -// fusedManagerRepository.updateDownloadStatus( -// fusedDownload, -// Status.INSTALLING -// ) } } } 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 fa23c24d4..b1a26f2ca 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 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 290921bad..ca25722bf 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 @@ -28,7 +46,6 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.sync.Mutex -import java.lang.RuntimeException import java.util.concurrent.atomic.AtomicInteger import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds @@ -196,7 +213,7 @@ class InstallAppWorker @AssistedInject constructor( } private fun unlockMutex() { - if(mutex.isLocked) { + if (mutex.isLocked) { mutex.unlock() } } @@ -210,7 +227,7 @@ class InstallAppWorker @AssistedInject constructor( val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as - NotificationManager + NotificationManager // Create a Notification channel if necessary if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val mChannel = NotificationChannel( 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 3af256f0d..3fd339775 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 -- GitLab