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

Verified Commit 5ca87a9b authored by Fahim M. Choudhury's avatar Fahim M. Choudhury
Browse files

fix: fix showing progress values (download vs. total size and percentage) on app details screen

Previously, progress was posted only when downloadingIds.size == cursor.count in DownloadProgressLD. When the cursor was partial or null), no progress was emitted, causing the UI to show no progress data, despite available data.

With this fix, removed dependency on cursor row count; always post progress after processing returned rows.
parent 2a12de55
Loading
Loading
Loading
Loading
+54 −39
Original line number Diff line number Diff line
@@ -75,19 +75,37 @@ class DownloadProgressLD @Inject constructor(
    }

    private fun findDownloadProgress(downloadingIds: MutableList<Long>) {
        downloadManager.query(downloadManagerQuery.setFilterById(*downloadingIds.toLongArray()))
            .use { cursor ->
                cursor.moveToFirst()
                while (!cursor.isAfterLast) {
                    val id =
                        cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_ID))
                    val status =
                        cursor.getInt(cursor.getColumnIndexOrThrow(COLUMN_STATUS))
        val idsToQuery = downloadingIds.toLongArray()
        if (idsToQuery.isEmpty()) {
            Timber.i("No pending download ids to query; skipping progress refresh.")
            return
        }

        val cursor = downloadManager.query(downloadManagerQuery.setFilterById(*idsToQuery))
        if (cursor == null) {
            Timber.w("DownloadManager returned null cursor for ids $downloadingIds; posting cached progress to avoid stalling UI.")
            postValue(downloadProgress)
            return
        }

        cursor.use { safeCursor ->
            val foundIds = mutableSetOf<Long>()

            if (!safeCursor.moveToFirst()) {
                Timber.w("Download cursor empty for ids $downloadingIds; posting cached progress to stay responsive.")
                postValue(downloadProgress)
                return
            }

            do {
                val id = safeCursor.getLong(safeCursor.getColumnIndexOrThrow(COLUMN_ID))
                val status = safeCursor.getInt(safeCursor.getColumnIndexOrThrow(COLUMN_STATUS))
                val totalSizeBytes =
                        cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_TOTAL_SIZE_BYTES))
                    safeCursor.getLong(safeCursor.getColumnIndexOrThrow(COLUMN_TOTAL_SIZE_BYTES))
                val bytesDownloadedSoFar =
                        cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_BYTES_DOWNLOADED_SO_FAR))
                    safeCursor.getLong(safeCursor.getColumnIndexOrThrow(COLUMN_BYTES_DOWNLOADED_SO_FAR))

                foundIds.add(id)
                downloadProgress.downloadId = id

                if (!downloadProgress.totalSizeBytes.containsKey(id) ||
@@ -105,15 +123,12 @@ class DownloadProgressLD @Inject constructor(
                downloadProgress.status[id] =
                    status == DownloadManager.STATUS_SUCCESSFUL || status == DownloadManager.STATUS_FAILED

                    if (downloadingIds.size == cursor.count) {
                        postValue(downloadProgress)
                    }

                if (downloadingIds.isEmpty()) {
                    cancel()
                }
                    cursor.moveToNext()
                }
            } while (safeCursor.moveToNext())

            postValue(downloadProgress)
        }
    }