diff --git a/src/main/kotlin/com/looker/droidify/service/DownloadService.kt b/src/main/kotlin/com/looker/droidify/service/DownloadService.kt index 327139d5..256a9e9c 100644 --- a/src/main/kotlin/com/looker/droidify/service/DownloadService.kt +++ b/src/main/kotlin/com/looker/droidify/service/DownloadService.kt @@ -23,9 +23,7 @@ import com.looker.droidify.utility.extension.text.* import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers import io.reactivex.rxjava3.disposables.Disposable import kotlinx.coroutines.* -import kotlinx.coroutines.flow.MutableSharedFlow -import kotlinx.coroutines.flow.asSharedFlow -import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.* import java.io.File import java.security.MessageDigest import kotlin.math.* @@ -42,7 +40,8 @@ class DownloadService : ConnectionService() { private val downloadState = mutableDownloadState.asSharedFlow() } - val scope = CoroutineScope(Dispatchers.Default) + private val scope = CoroutineScope(Dispatchers.Default) + private val mainDispatcher = Dispatchers.Main class Receiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { @@ -150,9 +149,7 @@ class DownloadService : ConnectionService() { .let(notificationManager::createNotificationChannel) } - scope.launch { - downloadState.collect { publishForegroundState(false, it) } - } + downloadState.onEach { publishForegroundState(false, it) }.launchIn(scope) } override fun onDestroy() { @@ -173,7 +170,14 @@ class DownloadService : ConnectionService() { private fun cancelTasks(packageName: String?) { tasks.removeAll { (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 } } @@ -183,7 +187,7 @@ class DownloadService : ConnectionService() { currentTask?.let { if (packageName == null || it.task.packageName == packageName) { currentTask = null - scope.launch { + scope.launch(mainDispatcher) { mutableStateSubject.emit( State.Cancel( it.task.packageName, @@ -300,7 +304,7 @@ class DownloadService : ConnectionService() { private fun publishSuccess(task: Task) { var consumed = false - scope.launch { + scope.launch(mainDispatcher) { mutableStateSubject.emit(State.Success(task.packageName, task.name, task.release)) consumed = true } diff --git a/src/main/kotlin/com/looker/droidify/service/SyncService.kt b/src/main/kotlin/com/looker/droidify/service/SyncService.kt index d8e657f1..227b76b4 100644 --- a/src/main/kotlin/com/looker/droidify/service/SyncService.kt +++ b/src/main/kotlin/com/looker/droidify/service/SyncService.kt @@ -7,7 +7,6 @@ import android.app.job.JobParameters import android.app.job.JobService import android.content.Intent import android.graphics.Color -import android.os.Build import android.text.SpannableStringBuilder import android.text.style.ForegroundColorSpan import android.view.ContextThemeWrapper @@ -248,7 +247,7 @@ class SyncService : ConnectionService() { this, 0, 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 else PendingIntent.FLAG_UPDATE_CURRENT @@ -438,7 +437,7 @@ class SyncService : ConnectionService() { 0, Intent(this, MainActivity::class.java) .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 else PendingIntent.FLAG_UPDATE_CURRENT