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

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

App Lounge: App install worker updated

parent 2fc70878
Loading
Loading
Loading
Loading
Loading
+1 −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,7 @@ class MainActivityViewModel @Inject constructor(
            val fusedDownload =
                fusedManagerRepository.getFusedDownload(packageName = app.package_name)
            fusedManagerRepository.cancelDownload(fusedDownload)
            InstallWorkManager.cancelWork(app.name)
//            InstallWorkManager.cancelWork(app._id)
        }
    }

+57 −60
Original line number Diff line number Diff line
@@ -44,9 +44,7 @@ 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.lang.RuntimeException
import java.util.concurrent.atomic.AtomicInteger
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
@@ -85,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(
@@ -116,15 +117,36 @@ class InstallAppWorker @AssistedInject constructor(
        fusedDownload: FusedDownload
    ) {
        fusedManagerRepository.downloadApp(fusedDownload)
        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 (download.type == Type.NATIVE && download.status != Status.INSTALLED && download.status != Status.INSTALLATION_ISSUE) {
                        checkDownloadProcess(download)
                    }
                }
            }.launchIn(CoroutineScope(Dispatchers.IO))
            observeDownload(fusedDownload)
        Log.d(
            TAG,
            ">>> ===> doWork: Download started ${fusedDownload.name} ${fusedDownload.status}"
        )
    }

    private suspend fun handleFusedDownloadStatusCheckingException(
        download: FusedDownload
    ) {
        try {
            handleFusedDownloadStatus(download)
        } catch (e: Exception) {
            Log.e(TAG, " >>> observeDownload: ", e)
            isDownloading = false
            unlockMutex()
        }
        Log.d(TAG, ">>> ===> doWork: Download started ${fusedDownload.name} ${fusedDownload.status}")
    }

    private fun tickerFlow(period: Duration, initialDelay: Duration = Duration.ZERO) = flow {
@@ -136,7 +158,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()) {
@@ -157,29 +179,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()
        }
    }

@@ -202,12 +203,8 @@ class InstallAppWorker @AssistedInject constructor(
                    TAG,
                    ">>> ===> doWork: Installed/Failed: ${fusedDownload.name} ${fusedDownload.status}"
                )
                if(fusedDownload.status == Status.INSTALLATION_ISSUE) {
                    throw RuntimeException("App Installation issue!")
                } else {
                unlockMutex()
            }
            }
            else -> {
                isDownloading = false
                unlockMutex()
+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()
        )
    }