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

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

Merge branch '5537-update_all_button' into main

parents bcea3812 62309d7d
Loading
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@

package foundation.e.apps

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.os.Build
@@ -47,7 +46,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.utils.enums.Origin
import foundation.e.apps.utils.enums.Status
import foundation.e.apps.utils.enums.Type
@@ -384,7 +382,6 @@ class MainActivityViewModel @Inject constructor(
            val fusedDownload =
                fusedManagerRepository.getFusedDownload(packageName = app.package_name)
            fusedManagerRepository.cancelDownload(fusedDownload)
            InstallWorkManager.cancelWork(app.name)
        }
    }

+1 −0
Original line number Diff line number Diff line
@@ -132,6 +132,7 @@ class FusedManagerImpl @Inject constructor(
                        pkgManagerModule.installApplication(list, fusedDownload.packageName)
                        Log.d(TAG, "installApp: ENDED ${fusedDownload.name} ${list.size}")
                    } catch (e: Exception) {
                        Log.d(TAG, ">>> installApp app failed ")
                        installationIssue(fusedDownload)
                        throw e
                    }
+6 −13
Original line number Diff line number Diff line
@@ -41,6 +41,10 @@ class InstallerService : Service() {
    @Inject
    lateinit var pkgManagerModule: PkgManagerModule

    companion object {
        const val TAG = "InstallerService"
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        val status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS, -69)
@@ -52,7 +56,7 @@ class InstallerService : Service() {
    }

    private fun postStatus(status: Int, packageName: String?, extra: String?) {
        Log.d("InstallerService", "postStatus: $status $packageName $extra")
        Log.d(TAG, "postStatus: $status $packageName $extra")
        if (status != PackageInstaller.STATUS_SUCCESS) {
            updateInstallationIssue(packageName ?: "")
        }
@@ -62,20 +66,9 @@ class InstallerService : Service() {
        return null
    }

    private fun updateDownloadStatus(pkgName: String) {
        if (pkgName.isEmpty()) {
            Log.d("PkgManagerBR", "updateDownloadStatus: package name should not be empty!")
        }
        GlobalScope.launch {
            val fusedDownload = fusedManagerRepository.getFusedDownload(packageName = pkgName)
            pkgManagerModule.setFakeStoreAsInstallerIfNeeded(fusedDownload)
            fusedManagerRepository.updateDownloadStatus(fusedDownload, Status.INSTALLED)
        }
    }

    private fun updateInstallationIssue(pkgName: String) {
        if (pkgName.isEmpty()) {
            Log.d("PkgManagerBR", "updateDownloadStatus: package name should not be empty!")
            Log.d(TAG, "updateDownloadStatus: package name should not be empty!")
        }
        GlobalScope.launch {
            val fusedDownload = fusedManagerRepository.getFusedDownload(packageName = pkgName)
+63 −57
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()
        )
    }
Loading