mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
Restore coroutine context originally used in DownloadService.
This commit is contained in:
parent
5e6d171675
commit
2321ed22f1
@ -44,6 +44,8 @@ import java.net.URL
|
|||||||
import kotlinx.coroutines.CancellationException
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
import kotlinx.coroutines.asCoroutineDispatcher
|
||||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||||
import kotlinx.coroutines.flow.SharedFlow
|
import kotlinx.coroutines.flow.SharedFlow
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -52,12 +54,15 @@ import okio.BufferedSink
|
|||||||
import okio.buffer
|
import okio.buffer
|
||||||
import okio.sink
|
import okio.sink
|
||||||
import okio.source
|
import okio.source
|
||||||
|
import java.util.concurrent.Executors
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Download service with custom implementation of downloading using [HttpURLConnection].
|
* Download service with custom implementation of downloading using [HttpURLConnection].
|
||||||
*/
|
*/
|
||||||
class DownloadService : LifecycleService() {
|
class DownloadService : LifecycleService() {
|
||||||
private val binder = LocalBinder()
|
private val binder = LocalBinder()
|
||||||
|
private val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
|
||||||
|
private val coroutineContext = dispatcher + SupervisorJob()
|
||||||
|
|
||||||
private lateinit var notificationManager: NotificationManager
|
private lateinit var notificationManager: NotificationManager
|
||||||
private lateinit var summaryNotificationBuilder: NotificationCompat.Builder
|
private lateinit var summaryNotificationBuilder: NotificationCompat.Builder
|
||||||
@ -88,7 +93,7 @@ class DownloadService : LifecycleService() {
|
|||||||
val audioQuality = intent.getStringExtra(IntentData.audioQuality)
|
val audioQuality = intent.getStringExtra(IntentData.audioQuality)
|
||||||
val subtitleCode = intent.getStringExtra(IntentData.subtitleCode)
|
val subtitleCode = intent.getStringExtra(IntentData.subtitleCode)
|
||||||
|
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch(coroutineContext) {
|
||||||
try {
|
try {
|
||||||
val streams = withContext(Dispatchers.IO) {
|
val streams = withContext(Dispatchers.IO) {
|
||||||
RetrofitInstance.api.getStreams(videoId)
|
RetrofitInstance.api.getStreams(videoId)
|
||||||
@ -149,7 +154,7 @@ class DownloadService : LifecycleService() {
|
|||||||
Database.downloadDao().insertDownloadItem(item)
|
Database.downloadDao().insertDownloadItem(item)
|
||||||
}.toInt()
|
}.toInt()
|
||||||
|
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch(coroutineContext) {
|
||||||
downloadFile(item)
|
downloadFile(item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -239,7 +244,7 @@ class DownloadService : LifecycleService() {
|
|||||||
notificationBuilder
|
notificationBuilder
|
||||||
.setContentText(
|
.setContentText(
|
||||||
totalRead.formatAsFileSize() + " / " +
|
totalRead.formatAsFileSize() + " / " +
|
||||||
item.downloadSize.formatAsFileSize()
|
item.downloadSize.formatAsFileSize()
|
||||||
)
|
)
|
||||||
.setProgress(
|
.setProgress(
|
||||||
item.downloadSize.toInt(),
|
item.downloadSize.toInt(),
|
||||||
@ -265,7 +270,8 @@ class DownloadService : LifecycleService() {
|
|||||||
sourceByte.close()
|
sourceByte.close()
|
||||||
con.disconnect()
|
con.disconnect()
|
||||||
}
|
}
|
||||||
} catch (_: Exception) { }
|
} catch (_: Exception) {
|
||||||
|
}
|
||||||
|
|
||||||
val completed = when {
|
val completed = when {
|
||||||
totalRead < item.downloadSize -> {
|
totalRead < item.downloadSize -> {
|
||||||
@ -293,7 +299,7 @@ class DownloadService : LifecycleService() {
|
|||||||
val downloadCount = downloadQueue.valueIterator().asSequence().count { it }
|
val downloadCount = downloadQueue.valueIterator().asSequence().count { it }
|
||||||
if (downloadCount >= DownloadHelper.getMaxConcurrentDownloads()) {
|
if (downloadCount >= DownloadHelper.getMaxConcurrentDownloads()) {
|
||||||
toastFromMainThread(getString(R.string.concurrent_downloads_limit_reached))
|
toastFromMainThread(getString(R.string.concurrent_downloads_limit_reached))
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch(coroutineContext) {
|
||||||
_downloadFlow.emit(id to DownloadStatus.Paused)
|
_downloadFlow.emit(id to DownloadStatus.Paused)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -302,7 +308,7 @@ class DownloadService : LifecycleService() {
|
|||||||
val downloadItem = awaitQuery {
|
val downloadItem = awaitQuery {
|
||||||
Database.downloadDao().findDownloadItemById(id)
|
Database.downloadDao().findDownloadItemById(id)
|
||||||
}
|
}
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch(coroutineContext) {
|
||||||
downloadFile(downloadItem)
|
downloadFile(downloadItem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user