Remove usage of deprecated LocalBroadcastManager (#3215)

This commit is contained in:
AntsyLich
2026-04-09 23:24:13 +06:00
committed by GitHub
parent 3521ccfb4f
commit b201ed163e
2 changed files with 20 additions and 23 deletions
@@ -1,15 +1,17 @@
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
@@ -26,13 +28,6 @@ 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.
* *
@@ -114,7 +109,7 @@ abstract class Installer(private val service: Service) {
*/ */
@CallSuper @CallSuper
open fun onDestroy() { open fun onDestroy() {
LocalBroadcastManager.getInstance(service).unregisterReceiver(cancelReceiver) scope.cancel()
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)
@@ -149,24 +144,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 {
val filter = IntentFilter(ACTION_CANCEL_QUEUE) cancelChannel.receiveAsFlow()
LocalBroadcastManager.getInstance(service).registerReceiver(cancelReceiver, filter) .onEach(::cancelQueue)
.launchIn(scope)
} }
companion object { companion object {
private const val ACTION_CANCEL_QUEUE = "Installer.action.CANCEL_QUEUE" private val cancelChannel = Channel<Long>(Channel.UNLIMITED)
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]
*/ */
fun cancelInstallQueue(context: Context, downloadId: Long) { suspend fun cancelInstallQueue(downloadId: Long) {
val intent = Intent(ACTION_CANCEL_QUEUE) cancelChannel.send(downloadId)
intent.putExtra(EXTRA_DOWNLOAD_ID, downloadId)
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
} }
} }
} }
@@ -140,8 +140,10 @@ 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())
}
} }
/** /**