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

Commit 87c2ac42 authored by Abhishek Aggarwal's avatar Abhishek Aggarwal
Browse files

App Lounge: lets cross check status from download manager

for some reasons for few apps broadcast was getting missed so manually
loop through ids and determine correct status
parent 3c5da6f4
Loading
Loading
Loading
Loading
+1 −3
Original line number Diff line number Diff line
@@ -45,9 +45,7 @@ class DownloadManagerBR : BroadcastReceiver() {
            Log.d(TAG, "onReceive: $action")
            when (action) {
                DownloadManager.ACTION_DOWNLOAD_COMPLETE -> {
                    if (downloadManagerUtils.downloadStatus(id) == DownloadManager.STATUS_SUCCESSFUL) {
                        downloadManagerUtils.checkAndUpdateStatus(id)
                    }
                    downloadManagerUtils.updateDownloadStatus(id)
                }
                DownloadManager.ACTION_NOTIFICATION_CLICKED -> {
                    if (id != 0L) downloadManagerUtils.cancelDownload(id)
+21 −37
Original line number Diff line number Diff line
@@ -19,43 +19,24 @@
package foundation.e.apps.manager.download

import android.app.DownloadManager
import android.util.Log
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
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

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

    fun downloadStatus(id: Long): Int {
        downloadManager.query(downloadManagerQuery.setFilterById(id)).use { cursor ->
            if (cursor.moveToFirst()) {
                val statusIndex = cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS)
                return cursor.getInt(statusIndex)
            }
            return DownloadManager.STATUS_FAILED
        }
    }

    @DelicateCoroutinesApi
    fun checkAndUpdateStatus(downloadId: Long) {
        val file = File(downloadedFile(downloadId))
        if (file.exists()) {
            updateDownloadStatus(downloadId)
        } else {
            Log.d(TAG, "Given file doesn't exists, exiting!")
        }
    }

    @DelicateCoroutinesApi
    fun cancelDownload(downloadId: Long) {
        GlobalScope.launch {
@@ -64,23 +45,26 @@ class DownloadManagerUtils @Inject constructor(
        }
    }

    private fun downloadedFile(id: Long): String {
        downloadManager.query(downloadManagerQuery.setFilterById(id)).use { cursor ->
            var fileUri = ""
    @DelicateCoroutinesApi
    fun updateDownloadStatus(downloadId: Long) {
        var downloaded = false
        DownloadProgressLD.downloadId.forEach {
            downloadManager.query(downloadManagerQuery.setFilterById(it)).use { cursor ->
                if (cursor.moveToFirst()) {
                val index = cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LOCAL_URI)
                fileUri = cursor.getString(index)
                    val status = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS))
                    downloaded = status == DownloadManager.STATUS_SUCCESSFUL
                }
            return fileUri.removePrefix("file://")
            }
        }

    @DelicateCoroutinesApi
    private fun updateDownloadStatus(downloadId: Long) {
        if (downloaded) {
            GlobalScope.launch {
                delay(100)
                val fusedDownload = fusedManagerRepository.getFusedDownload(downloadId)
            fusedManagerRepository.updateDownloadStatus(fusedDownload, Status.INSTALLING, downloadId)
                if (!pkgManagerModule.isInstalled(fusedDownload.package_name)) {
                    fusedManagerRepository.updateDownloadStatus(fusedDownload, Status.INSTALLING)
                }
            }
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -71,7 +71,7 @@ class DownloadProgressLD @Inject constructor(
    }

    companion object {
        private var downloadId = mutableListOf<Long>()
        var downloadId = mutableListOf<Long>()

        fun setDownloadId(id: Long) {
            if (id == -1L) {
+6 −21
Original line number Diff line number Diff line
@@ -54,21 +54,7 @@ class FusedManagerImpl @Inject constructor(
        return databaseRepository.getDownloadLiveList()
    }

    suspend fun updateDownloadStatus(
        fusedDownload: FusedDownload,
        status: Status,
        downloadId: Long = 0
    ) {
        if (fusedDownload.id.isNotBlank()) {
            if (downloadId != 0L && fusedDownload.downloadIdMap.containsKey(downloadId)) {
                fusedDownload.downloadIdMap[downloadId] = true
                databaseRepository.updateDownload(fusedDownload)
                delay(100)
            }
        } else {
            Log.d(TAG, "Unable to update download status!")
        }

    suspend fun updateDownloadStatus(fusedDownload: FusedDownload, status: Status) {
        if (status == Status.INSTALLED) {
            fusedDownload.status = status
            databaseRepository.updateDownload(fusedDownload)
@@ -76,14 +62,13 @@ class FusedManagerImpl @Inject constructor(
            flushOldDownload(fusedDownload.package_name)
            databaseRepository.deleteDownload(fusedDownload)
        } else if (status == Status.INSTALLING) {
            if (fusedDownload.downloadIdMap.all { it.value }) {
            fusedDownload.downloadIdMap.all { true }
            fusedDownload.status = status
            databaseRepository.updateDownload(fusedDownload)
            delay(100)
            installApp(fusedDownload)
        }
    }
    }

    suspend fun downloadApp(fusedDownload: FusedDownload) {
        when (fusedDownload.type) {
+2 −2
Original line number Diff line number Diff line
@@ -48,8 +48,8 @@ class FusedManagerRepository @Inject constructor(
        return fusedManagerImpl.getFusedDownload(downloadId, packageName)
    }

    suspend fun updateDownloadStatus(fusedDownload: FusedDownload, status: Status, downloadId: Long = 0) {
        return fusedManagerImpl.updateDownloadStatus(fusedDownload, status, downloadId)
    suspend fun updateDownloadStatus(fusedDownload: FusedDownload, status: Status) {
        return fusedManagerImpl.updateDownloadStatus(fusedDownload, status)
    }

    suspend fun cancelDownload(fusedDownload: FusedDownload) {
Loading