Fix: Micro-crash on download cancel

Improve: Use provided function
This commit is contained in:
LooKeR 2021-12-24 12:37:21 +05:30
parent dbd43b1a2b
commit 66075c9e64
2 changed files with 16 additions and 13 deletions

View File

@ -23,9 +23,7 @@ import com.looker.droidify.utility.extension.text.*
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.Disposable import io.reactivex.rxjava3.disposables.Disposable
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.collect
import java.io.File import java.io.File
import java.security.MessageDigest import java.security.MessageDigest
import kotlin.math.* import kotlin.math.*
@ -42,7 +40,8 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
private val downloadState = mutableDownloadState.asSharedFlow() private val downloadState = mutableDownloadState.asSharedFlow()
} }
val scope = CoroutineScope(Dispatchers.Default) private val scope = CoroutineScope(Dispatchers.Default)
private val mainDispatcher = Dispatchers.Main
class Receiver : BroadcastReceiver() { class Receiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
@ -150,9 +149,7 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
.let(notificationManager::createNotificationChannel) .let(notificationManager::createNotificationChannel)
} }
scope.launch { downloadState.onEach { publishForegroundState(false, it) }.launchIn(scope)
downloadState.collect { publishForegroundState(false, it) }
}
} }
override fun onDestroy() { override fun onDestroy() {
@ -173,7 +170,14 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
private fun cancelTasks(packageName: String?) { private fun cancelTasks(packageName: String?) {
tasks.removeAll { tasks.removeAll {
(packageName == null || it.packageName == packageName) && run { (packageName == null || it.packageName == packageName) && run {
scope.launch { mutableStateSubject.emit(State.Cancel(it.packageName, it.name)) } scope.launch(mainDispatcher) {
mutableStateSubject.emit(
State.Cancel(
it.packageName,
it.name
)
)
}
true true
} }
} }
@ -183,7 +187,7 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
currentTask?.let { currentTask?.let {
if (packageName == null || it.task.packageName == packageName) { if (packageName == null || it.task.packageName == packageName) {
currentTask = null currentTask = null
scope.launch { scope.launch(mainDispatcher) {
mutableStateSubject.emit( mutableStateSubject.emit(
State.Cancel( State.Cancel(
it.task.packageName, it.task.packageName,
@ -300,7 +304,7 @@ class DownloadService : ConnectionService<DownloadService.Binder>() {
private fun publishSuccess(task: Task) { private fun publishSuccess(task: Task) {
var consumed = false var consumed = false
scope.launch { scope.launch(mainDispatcher) {
mutableStateSubject.emit(State.Success(task.packageName, task.name, task.release)) mutableStateSubject.emit(State.Success(task.packageName, task.name, task.release))
consumed = true consumed = true
} }

View File

@ -7,7 +7,6 @@ import android.app.job.JobParameters
import android.app.job.JobService import android.app.job.JobService
import android.content.Intent import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.os.Build
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan import android.text.style.ForegroundColorSpan
import android.view.ContextThemeWrapper import android.view.ContextThemeWrapper
@ -248,7 +247,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
this, this,
0, 0,
Intent(this, this::class.java).setAction(ACTION_CANCEL), Intent(this, this::class.java).setAction(ACTION_CANCEL),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) if (Android.sdk(23))
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
else else
PendingIntent.FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT
@ -438,7 +437,7 @@ class SyncService : ConnectionService<SyncService.Binder>() {
0, 0,
Intent(this, MainActivity::class.java) Intent(this, MainActivity::class.java)
.setAction(MainActivity.ACTION_UPDATES), .setAction(MainActivity.ACTION_UPDATES),
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) if (Android.sdk(23))
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
else else
PendingIntent.FLAG_UPDATE_CURRENT PendingIntent.FLAG_UPDATE_CURRENT