Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 89ba77bc authored by Hasib Prince's avatar Hasib Prince
Browse files

App Lounge: fixed work manager stucks if app install faces exception

parent 6150be6d
Loading
Loading
Loading
Loading
Loading
+15 −13
Original line number Diff line number Diff line
@@ -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)