Revert "Remove usage of deprecated LocalBroadcastManager (#3215)"
This reverts commit b201ed163e.
This commit is contained in:
@@ -1,17 +1,15 @@
|
|||||||
package eu.kanade.tachiyomi.extension.installer
|
package eu.kanade.tachiyomi.extension.installer
|
||||||
|
|
||||||
import android.app.Service
|
import android.app.Service
|
||||||
|
import android.content.BroadcastReceiver
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.IntentFilter
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import androidx.annotation.CallSuper
|
import androidx.annotation.CallSuper
|
||||||
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||||
import eu.kanade.tachiyomi.extension.ExtensionManager
|
import eu.kanade.tachiyomi.extension.ExtensionManager
|
||||||
import eu.kanade.tachiyomi.extension.model.InstallStep
|
import eu.kanade.tachiyomi.extension.model.InstallStep
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.cancel
|
|
||||||
import kotlinx.coroutines.channels.Channel
|
|
||||||
import kotlinx.coroutines.flow.launchIn
|
|
||||||
import kotlinx.coroutines.flow.onEach
|
|
||||||
import kotlinx.coroutines.flow.receiveAsFlow
|
|
||||||
import uy.kohesive.injekt.injectLazy
|
import uy.kohesive.injekt.injectLazy
|
||||||
import java.util.Collections
|
import java.util.Collections
|
||||||
import kotlin.concurrent.atomics.AtomicReference
|
import kotlin.concurrent.atomics.AtomicReference
|
||||||
@@ -28,6 +26,13 @@ abstract class Installer(private val service: Service) {
|
|||||||
private var waitingInstall = AtomicReference<Entry?>(null)
|
private var waitingInstall = AtomicReference<Entry?>(null)
|
||||||
private val queue = Collections.synchronizedList(mutableListOf<Entry>())
|
private val queue = Collections.synchronizedList(mutableListOf<Entry>())
|
||||||
|
|
||||||
|
private val cancelReceiver = object : BroadcastReceiver() {
|
||||||
|
override fun onReceive(context: Context, intent: Intent) {
|
||||||
|
val downloadId = intent.getLongExtra(EXTRA_DOWNLOAD_ID, -1).takeIf { it >= 0 } ?: return
|
||||||
|
cancelQueue(downloadId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Installer readiness. If false, queue check will not run.
|
* Installer readiness. If false, queue check will not run.
|
||||||
*
|
*
|
||||||
@@ -109,7 +114,7 @@ abstract class Installer(private val service: Service) {
|
|||||||
*/
|
*/
|
||||||
@CallSuper
|
@CallSuper
|
||||||
open fun onDestroy() {
|
open fun onDestroy() {
|
||||||
scope.cancel()
|
LocalBroadcastManager.getInstance(service).unregisterReceiver(cancelReceiver)
|
||||||
queue.forEach { extensionManager.updateInstallStep(it.downloadId, InstallStep.Error) }
|
queue.forEach { extensionManager.updateInstallStep(it.downloadId, InstallStep.Error) }
|
||||||
queue.clear()
|
queue.clear()
|
||||||
waitingInstall.store(null)
|
waitingInstall.store(null)
|
||||||
@@ -144,24 +149,24 @@ abstract class Installer(private val service: Service) {
|
|||||||
*/
|
*/
|
||||||
data class Entry(val downloadId: Long, val uri: Uri)
|
data class Entry(val downloadId: Long, val uri: Uri)
|
||||||
|
|
||||||
private val scope = CoroutineScope(Dispatchers.Main)
|
|
||||||
|
|
||||||
init {
|
init {
|
||||||
cancelChannel.receiveAsFlow()
|
val filter = IntentFilter(ACTION_CANCEL_QUEUE)
|
||||||
.onEach(::cancelQueue)
|
LocalBroadcastManager.getInstance(service).registerReceiver(cancelReceiver, filter)
|
||||||
.launchIn(scope)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val cancelChannel = Channel<Long>(Channel.UNLIMITED)
|
private const val ACTION_CANCEL_QUEUE = "Installer.action.CANCEL_QUEUE"
|
||||||
|
private const val EXTRA_DOWNLOAD_ID = "Installer.extra.DOWNLOAD_ID"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Attempts to cancel the installation entry for the provided download ID.
|
* Attempts to cancel the installation entry for the provided download ID.
|
||||||
*
|
*
|
||||||
* @param downloadId Download ID as known by [ExtensionManager]
|
* @param downloadId Download ID as known by [ExtensionManager]
|
||||||
*/
|
*/
|
||||||
suspend fun cancelInstallQueue(downloadId: Long) {
|
fun cancelInstallQueue(context: Context, downloadId: Long) {
|
||||||
cancelChannel.send(downloadId)
|
val intent = Intent(ACTION_CANCEL_QUEUE)
|
||||||
|
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId)
|
||||||
|
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -140,10 +140,8 @@ internal class ExtensionInstaller(
|
|||||||
* Cancels extension install and remove from download manager and installer.
|
* Cancels extension install and remove from download manager and installer.
|
||||||
*/
|
*/
|
||||||
fun cancelInstall(pkgName: String) {
|
fun cancelInstall(pkgName: String) {
|
||||||
scope.launch {
|
activeJobs.remove(pkgName)?.cancel()
|
||||||
activeJobs.remove(pkgName)?.cancel()
|
Installer.cancelInstallQueue(context, pkgName.hashCode().toLong())
|
||||||
Installer.cancelInstallQueue(pkgName.hashCode().toLong())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user