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

Commit 58d19e71 authored by Hasib Prince's avatar Hasib Prince
Browse files

Resolved: conflicts cherypicking ab56b073=>install worker udpated

parent 7ffd2e79
Loading
Loading
Loading
Loading
+0 −2
Original line number Diff line number Diff line
@@ -49,7 +49,6 @@ import foundation.e.apps.api.fused.data.FusedApp
import foundation.e.apps.manager.database.fusedDownload.FusedDownload
import foundation.e.apps.manager.fused.FusedManagerRepository
import foundation.e.apps.manager.pkg.PkgManagerModule
import foundation.e.apps.manager.workmanager.InstallWorkManager
import foundation.e.apps.settings.SettingsFragment
import foundation.e.apps.utils.enums.Origin
import foundation.e.apps.utils.enums.Status
@@ -474,7 +473,6 @@ class MainActivityViewModel @Inject constructor(
            val fusedDownload =
                fusedManagerRepository.getFusedDownload(packageName = app.package_name)
            fusedManagerRepository.cancelDownload(fusedDownload)
            InstallWorkManager.cancelWork(app.name)
        }
    }

+64 −58
Original line number Diff line number Diff line
@@ -44,7 +44,6 @@ import kotlinx.coroutines.delay
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
@@ -84,11 +83,14 @@ class InstallAppWorker @AssistedInject constructor(
        var fusedDownload: FusedDownload? = null
        try {
            val fusedDownloadString = params.inputData.getString(INPUT_DATA_FUSED_DOWNLOAD) ?: ""
            Log.d(TAG, "Fused download name $fusedDownloadString")
            fusedDownload = databaseRepository.getDownloadById(fusedDownloadString)
            Log.d(
                TAG,
                ">>> dowork started for Fused download name ${fusedDownload?.name} $fusedDownloadString"
            )
            fusedDownload?.let {
                if (fusedDownload.status != Status.AWAITING) {
                    return@let
                    return Result.success()
                }
                setForeground(
                    createForegroundInfo(
@@ -99,12 +101,12 @@ class InstallAppWorker @AssistedInject constructor(
                mutex.lock()
            }
        } catch (e: Exception) {
            Log.e(TAG, "doWork: Failed: ${e.stackTraceToString()}")
            Log.e(TAG, ">>> doWork: Failed: ${e.stackTraceToString()}")
            fusedDownload?.let {
                fusedManagerRepository.installationIssue(it)
            }
        } finally {
            Log.d(TAG, "doWork: RESULT SUCCESS: ${fusedDownload?.name}")
            Log.d(TAG, ">>> doWork: RESULT SUCCESS: ${fusedDownload?.name}")
            return Result.success()
        }
    }
@@ -113,14 +115,39 @@ class InstallAppWorker @AssistedInject constructor(
        fusedDownload: FusedDownload
    ) {
        fusedManagerRepository.downloadApp(fusedDownload)
        Log.d(TAG, "===> doWork: Download started ${fusedDownload.name} ${fusedDownload.status}")
        if (fusedDownload.type == Type.NATIVE) {
        isDownloading = true
            tickerFlow(1.seconds)
        tickerFlow(3.seconds)
            .onEach {
                    checkDownloadProcess(fusedDownload)
                val download = databaseRepository.getDownloadById(fusedDownload.id)
                if (download == null) {
                    isDownloading = false
                    unlockMutex()
                } else {
                    handleFusedDownloadStatusCheckingException(download)
                    if (isAppDownloading(download)) {
                        checkDownloadProcess(download)
                    }
                }
            }.launchIn(CoroutineScope(Dispatchers.IO))
            observeDownload(fusedDownload)
        Log.d(
            TAG,
            ">>> ===> doWork: Download started ${fusedDownload.name} ${fusedDownload.status}"
        )
    }

    private fun isAppDownloading(download: FusedDownload): Boolean {
        return download.type == Type.NATIVE && download.status != Status.INSTALLED && download.status != Status.INSTALLATION_ISSUE
    }

    private suspend fun handleFusedDownloadStatusCheckingException(
        download: FusedDownload
    ) {
        try {
            handleFusedDownloadStatus(download)
        } catch (e: Exception) {
            Log.e(TAG, "observeDownload: ", e)
            isDownloading = false
            unlockMutex()
        }
    }

@@ -133,7 +160,7 @@ class InstallAppWorker @AssistedInject constructor(
    }

    private suspend fun checkDownloadProcess(fusedDownload: FusedDownload) {

        try {
            downloadManager.query(downloadManagerQuery.setFilterById(*fusedDownload.downloadIdMap.keys.toLongArray()))
                .use { cursor ->
                    if (cursor.moveToFirst()) {
@@ -154,29 +181,8 @@ class InstallAppWorker @AssistedInject constructor(
                        }
                    }
                }
    }

    private suspend fun observeDownload(
        it: FusedDownload,
    ) {
        databaseRepository.getDownloadFlowById(it.id).takeWhile { isDownloading }
            .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()
                }
            e.printStackTrace()
        }
    }

@@ -195,11 +201,11 @@ class InstallAppWorker @AssistedInject constructor(
            }
            Status.INSTALLED, Status.INSTALLATION_ISSUE -> {
                isDownloading = false
                unlockMutex()
                Log.d(
                    TAG,
                    "===> doWork: Installed/Failed: ${fusedDownload.name} ${fusedDownload.status}"
                )
                unlockMutex()
            }
            else -> {
                isDownloading = false
+1 −1
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ object InstallWorkManager {
                Data.Builder()
                    .putString(InstallAppWorker.INPUT_DATA_FUSED_DOWNLOAD, fusedDownload.id)
                    .build()
            ).addTag(fusedDownload.name)
            ).addTag(fusedDownload.id)
                .build()
        )
    }
+1 −1
Original line number Diff line number Diff line
@@ -35,7 +35,7 @@ object UpdatesWorkManager {
    fun startUpdateAllWork(context: Context) {
        WorkManager.getInstance(context).enqueueUniqueWork(
            UPDATES_WORK_NAME,
            ExistingWorkPolicy.KEEP,
            ExistingWorkPolicy.REPLACE,
            buildOneTimeWorkRequest()
        )
    }