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

Commit b9be8f1a authored by Abhishek Aggarwal's avatar Abhishek Aggarwal
Browse files

App Lounge: Another try to make installation working

parent 87c2ac42
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -143,7 +143,7 @@ class MainActivity : AppCompatActivity() {
        // Observe and handle downloads
        viewModel.downloadList.observe(this) { list ->
            val shouldDownload = list.any {
                it.status == Status.INSTALLING || it.status == Status.DOWNLOADING
                it.status == Status.INSTALLING || it.status == Status.DOWNLOADING || it.status == Status.INSTALLED
            }
            if (!shouldDownload && list.isNotEmpty()) {
                for (item in list) {
+4 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import foundation.e.apps.api.fused.FusedAPIRepository
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.utils.enums.Status
import foundation.e.apps.utils.enums.Type
import foundation.e.apps.utils.modules.DataStoreModule
import kotlinx.coroutines.Dispatchers
@@ -134,6 +135,9 @@ class MainActivityViewModel @Inject constructor(
                app.type,
                appIcon
            )
            if (fusedDownload.status == Status.INSTALLATION_ISSUE) {
                fusedManagerRepository.clearInstallationIssue(fusedDownload)
            }
            fusedManagerRepository.addDownload(fusedDownload)
        }
    }
+2 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ class DownloadManagerBR : BroadcastReceiver() {

    companion object {
        private const val TAG = "DownloadManagerBR"
        val downloadedList = mutableListOf<Long>()
    }

    override fun onReceive(context: Context?, intent: Intent?) {
@@ -45,6 +46,7 @@ class DownloadManagerBR : BroadcastReceiver() {
            Log.d(TAG, "onReceive: $action")
            when (action) {
                DownloadManager.ACTION_DOWNLOAD_COMPLETE -> {
                    downloadedList.add(id)
                    downloadManagerUtils.updateDownloadStatus(id)
                }
                DownloadManager.ACTION_NOTIFICATION_CLICKED -> {
+6 −24
Original line number Diff line number Diff line
@@ -18,10 +18,7 @@

package foundation.e.apps.manager.download

import android.app.DownloadManager
import foundation.e.apps.manager.download.data.DownloadProgressLD
import foundation.e.apps.manager.fused.FusedManagerRepository
import foundation.e.apps.manager.pkg.PkgManagerModule
import foundation.e.apps.utils.enums.Status
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
@@ -30,10 +27,7 @@ import kotlinx.coroutines.launch
import javax.inject.Inject

class DownloadManagerUtils @Inject constructor(
    private val downloadManager: DownloadManager,
    private val downloadManagerQuery: DownloadManager.Query,
    private val fusedManagerRepository: FusedManagerRepository,
    private val pkgManagerModule: PkgManagerModule,
    private val fusedManagerRepository: FusedManagerRepository
) {
    private val TAG = DownloadManagerUtils::class.java.simpleName

@@ -47,24 +41,12 @@ class DownloadManagerUtils @Inject constructor(

    @DelicateCoroutinesApi
    fun updateDownloadStatus(downloadId: Long) {
        var downloaded = false
        DownloadProgressLD.downloadId.forEach {
            downloadManager.query(downloadManagerQuery.setFilterById(it)).use { cursor ->
                if (cursor.moveToFirst()) {
                    val status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS))
                    downloaded = status == DownloadManager.STATUS_SUCCESSFUL
                }
            }
        }

        if (downloaded) {
        GlobalScope.launch {
                delay(100)
            val fusedDownload = fusedManagerRepository.getFusedDownload(downloadId)
                if (!pkgManagerModule.isInstalled(fusedDownload.package_name)) {
            delay(100)
            if (DownloadManagerBR.downloadedList.size == fusedDownload.downloadIdMap.size) {
                fusedManagerRepository.updateDownloadStatus(fusedDownload, Status.INSTALLING)
            }
        }
    }
}
}
+27 −8
Original line number Diff line number Diff line
@@ -10,11 +10,13 @@ import androidx.annotation.RequiresApi
import androidx.lifecycle.LiveData
import foundation.e.apps.manager.database.DatabaseRepository
import foundation.e.apps.manager.database.fusedDownload.FusedDownload
import foundation.e.apps.manager.download.DownloadManagerBR
import foundation.e.apps.manager.download.data.DownloadProgressLD
import foundation.e.apps.manager.pkg.PkgManagerModule
import foundation.e.apps.utils.enums.Status
import foundation.e.apps.utils.enums.Type
import foundation.e.apps.utils.modules.PWAManagerModule
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.delay
import java.io.File
import javax.inject.Inject
@@ -54,10 +56,17 @@ class FusedManagerImpl @Inject constructor(
        return databaseRepository.getDownloadLiveList()
    }

    suspend fun clearInstallationIssue(fusedDownload: FusedDownload) {
        flushOldDownload(fusedDownload.package_name)
        databaseRepository.deleteDownload(fusedDownload)
    }

    @OptIn(DelicateCoroutinesApi::class)
    suspend fun updateDownloadStatus(fusedDownload: FusedDownload, status: Status) {
        if (status == Status.INSTALLED) {
            fusedDownload.status = status
            databaseRepository.updateDownload(fusedDownload)
            DownloadManagerBR.downloadedList.clear()
            delay(100)
            flushOldDownload(fusedDownload.package_name)
            databaseRepository.deleteDownload(fusedDownload)
@@ -67,6 +76,7 @@ class FusedManagerImpl @Inject constructor(
            databaseRepository.updateDownload(fusedDownload)
            delay(100)
            installApp(fusedDownload)
            delay(100)
        }
    }

@@ -77,27 +87,34 @@ class FusedManagerImpl @Inject constructor(
        }
    }

    fun installApp(fusedDownload: FusedDownload) {
    suspend fun installApp(fusedDownload: FusedDownload) {
        val list = mutableListOf<File>()
        when (fusedDownload.type) {
            Type.NATIVE -> {
                val parentPathFile = File("$cacheDir/${fusedDownload.package_name}")
                parentPathFile.listFiles()?.let { list.addAll(it) }
                list.sort()
                if (list.size != 0) pkgManagerModule.installApplication(
                    list,
                    fusedDownload.package_name
                )
                if (list.size != 0) {
                    pkgManagerModule.installApplication(list, fusedDownload.package_name)
                }
            }
            else -> {
                Log.d(TAG, "Unsupported application type!")
                fusedDownload.status = Status.INSTALLATION_ISSUE
                databaseRepository.updateDownload(fusedDownload)
                delay(100)
            }
            else -> Log.d(TAG, "Unsupported application type!")
        }
    }

    @OptIn(DelicateCoroutinesApi::class)
    suspend fun cancelDownload(fusedDownload: FusedDownload) {
        if (fusedDownload.id.isNotBlank()) {
            fusedDownload.downloadIdMap.forEach { (key, _) ->
                downloadManager.remove(key)
            }
            DownloadProgressLD.setDownloadId(-1)
            DownloadManagerBR.downloadedList.clear()

            // Reset the status before deleting download
            updateDownloadStatus(fusedDownload, fusedDownload.orgStatus)
@@ -138,10 +155,12 @@ class FusedManagerImpl @Inject constructor(

        // Clean old downloads and re-create download dir
        flushOldDownload(fusedDownload.package_name)
        File(parentPath).mkdir()
        DownloadProgressLD.setDownloadId(-1L)
        File(parentPath).mkdirs()

        fusedDownload.status = Status.DOWNLOADING
        databaseRepository.updateDownload(fusedDownload)
        DownloadProgressLD.setDownloadId(-1)
        delay(100)

        fusedDownload.downloadURLList.forEach {
            count += 1
Loading