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

Commit fe5ea47a authored by Hasib Prince's avatar Hasib Prince
Browse files

Merge branch '5371-update_all_stuck' into main

parents 80535f4a eb006b47
Loading
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
/*
 * 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)
                    }
                }
            }
+19 −0
Original line number Diff line number Diff line
/*
 * 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 <https://www.gnu.org/licenses/>.
 */

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
                    }
                }
            }
+46 −6
Original line number Diff line number Diff line
/*
 * 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 <https://www.gnu.org/licenses/>.
 */

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}"
                )
                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)
+2 −1
Original line number Diff line number Diff line
/*
 * 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,