Merge pull request #2980 from Isira-Seneviratne/SparseBooleanArray

Use SparseBooleanArray in DownloadService.
This commit is contained in:
Bnyro 2023-02-06 15:33:08 +01:00 committed by GitHub
commit 8e737afc72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,7 +6,10 @@ import android.app.Service
import android.content.Intent import android.content.Intent
import android.os.Binder import android.os.Binder
import android.os.IBinder import android.os.IBinder
import android.util.SparseBooleanArray
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.util.set
import androidx.core.util.valueIterator
import androidx.core.app.ServiceCompat import androidx.core.app.ServiceCompat
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.CronetHelper import com.github.libretube.api.CronetHelper
@ -66,8 +69,7 @@ class DownloadService : Service() {
private lateinit var notificationManager: NotificationManager private lateinit var notificationManager: NotificationManager
private lateinit var summaryNotificationBuilder: NotificationCompat.Builder private lateinit var summaryNotificationBuilder: NotificationCompat.Builder
private val jobs = mutableMapOf<Int, Job>() private val downloadQueue = SparseBooleanArray()
private val downloadQueue = mutableMapOf<Int, Boolean>()
private val _downloadFlow = MutableSharedFlow<Pair<Int, DownloadStatus>>() private val _downloadFlow = MutableSharedFlow<Pair<Int, DownloadStatus>>()
val downloadFlow: SharedFlow<Pair<Int, DownloadStatus>> = _downloadFlow val downloadFlow: SharedFlow<Pair<Int, DownloadStatus>> = _downloadFlow
@ -151,7 +153,7 @@ class DownloadService : Service() {
Database.downloadDao().insertDownloadItem(item) Database.downloadDao().insertDownloadItem(item)
}.toInt() }.toInt()
jobs[item.id] = scope.launch { scope.launch {
downloadFile(item) downloadFile(item)
} }
} }
@ -222,8 +224,7 @@ class DownloadService : Service() {
try { try {
// Check if downloading is still active and read next bytes. // Check if downloading is still active and read next bytes.
while (downloadQueue[item.id] == true && while (downloadQueue[item.id] && sourceByte
sourceByte
.read(sink.buffer, DownloadHelper.DOWNLOAD_CHUNK_SIZE) .read(sink.buffer, DownloadHelper.DOWNLOAD_CHUNK_SIZE)
.also { lastRead = it } != -1L .also { lastRead = it } != -1L
) { ) {
@ -289,9 +290,12 @@ class DownloadService : Service() {
*/ */
fun resume(id: Int) { fun resume(id: Int) {
// If file is already downloading then avoid new download job. // If file is already downloading then avoid new download job.
if (downloadQueue[id] == true) return if (downloadQueue[id]) {
return
}
if (downloadQueue.values.count { it } >= DownloadHelper.getMaxConcurrentDownloads()) { val downloadCount = downloadQueue.valueIterator().asSequence().count { it }
if (downloadCount >= DownloadHelper.getMaxConcurrentDownloads()) {
toastFromMainThread(getString(R.string.concurrent_downloads_limit_reached)) toastFromMainThread(getString(R.string.concurrent_downloads_limit_reached))
scope.launch { scope.launch {
_downloadFlow.emit(id to DownloadStatus.Paused) _downloadFlow.emit(id to DownloadStatus.Paused)
@ -314,7 +318,7 @@ class DownloadService : Service() {
downloadQueue[id] = false downloadQueue[id] = false
// Stop the service if no downloads are active. // Stop the service if no downloads are active.
if (downloadQueue.none { it.value }) { if (downloadQueue.valueIterator().asSequence().none { it }) {
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_DETACH) ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_DETACH)
sendBroadcast(Intent(ACTION_SERVICE_STOPPED)) sendBroadcast(Intent(ACTION_SERVICE_STOPPED))
stopSelf() stopSelf()
@ -343,7 +347,7 @@ class DownloadService : Service() {
* Check whether the file downloading or not. * Check whether the file downloading or not.
*/ */
fun isDownloading(id: Int): Boolean { fun isDownloading(id: Int): Boolean {
return downloadQueue[id] ?: false return downloadQueue[id]
} }
private fun notifyForeground() { private fun notifyForeground() {