mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
Use SparseBooleanArray in DownloadService.
This commit is contained in:
parent
c75f1dcb85
commit
fd60959827
@ -7,7 +7,10 @@ import android.content.Intent
|
|||||||
import android.os.Binder
|
import android.os.Binder
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
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 com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.CronetHelper
|
import com.github.libretube.api.CronetHelper
|
||||||
import com.github.libretube.api.RetrofitInstance
|
import com.github.libretube.api.RetrofitInstance
|
||||||
@ -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 }) {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
||||||
stopForeground(STOP_FOREGROUND_DETACH)
|
stopForeground(STOP_FOREGROUND_DETACH)
|
||||||
}
|
}
|
||||||
@ -345,7 +349,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() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user