Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3f21bec6 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Iterate over copy of packageBroadcastReceiver receivers" into rvc-dev

parents 2ef30800 5f0aeff6
Loading
Loading
Loading
Loading
+36 −28
Original line number Diff line number Diff line
@@ -22,6 +22,9 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.android.permissioncontroller.PermissionControllerApplication
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * Listens for package additions, replacements, and removals, and notifies listeners.
@@ -49,12 +52,14 @@ object PackageBroadcastReceiver : BroadcastReceiver() {
     * Add a callback which will be notified when the specified packaged is changed or removed.
     */
    fun addChangeCallback(packageName: String, listener: PackageBroadcastListener) {
        GlobalScope.launch(Main.immediate) {
            val wasEmpty = hasNoListeners()

            changeCallbacks.getOrPut(packageName, { mutableSetOf() }).add(listener)

            if (wasEmpty) {
            app.registerReceiver(this, intentFilter)
                app.registerReceiver(this@PackageBroadcastReceiver, intentFilter)
            }
        }
    }

@@ -65,12 +70,14 @@ object PackageBroadcastReceiver : BroadcastReceiver() {
     * @return returns the integer ID assigned to the
     */
    fun addAllCallback(listener: PackageBroadcastListener) {
        GlobalScope.launch(Main.immediate) {
            val wasEmpty = hasNoListeners()

            allCallbacks.add(listener)

            if (wasEmpty) {
            app.registerReceiver(this, intentFilter)
                app.registerReceiver(this@PackageBroadcastReceiver, intentFilter)
            }
        }
    }

@@ -80,13 +87,12 @@ object PackageBroadcastReceiver : BroadcastReceiver() {
     * @param listener the listener we wish to remove
     */
    fun removeAllCallback(listener: PackageBroadcastListener) {
        GlobalScope.launch(Main.immediate) {
            val wasEmpty = hasNoListeners()

        if (!allCallbacks.remove(listener)) {
            return
            if (allCallbacks.remove(listener) && hasNoListeners() && !wasEmpty) {
                app.unregisterReceiver(this@PackageBroadcastReceiver)
            }
        if (hasNoListeners() && !wasEmpty) {
            app.unregisterReceiver(this)
        }
    }

@@ -97,17 +103,17 @@ object PackageBroadcastReceiver : BroadcastReceiver() {
     * @param listener the listener we wish to remove
     */
    fun removeChangeCallback(packageName: String?, listener: PackageBroadcastListener) {
        GlobalScope.launch(Main.immediate) {
            val wasEmpty = hasNoListeners()

            changeCallbacks[packageName]?.let { callbackSet ->
            if (!callbackSet.remove(listener)) {
                return
            }
                callbackSet.remove(listener)
                if (callbackSet.isEmpty()) {
                    changeCallbacks.remove(packageName)
                }
                if (hasNoListeners() && !wasEmpty) {
                app.unregisterReceiver(this)
                    app.unregisterReceiver(this@PackageBroadcastReceiver)
                }
            }
        }
    }
@@ -133,15 +139,17 @@ object PackageBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val packageName = intent.data?.schemeSpecificPart ?: return

        for (callback in allCallbacks) {
        for (callback in allCallbacks.toList()) {
            callback.onPackageUpdate(packageName)
        }

        if (intent.action != Intent.ACTION_PACKAGE_ADDED) {
            changeCallbacks[packageName]?.forEach { callback ->
            changeCallbacks[packageName]?.toList()?.let { callbacks ->
                for (callback in callbacks) {
                    callback.onPackageUpdate(packageName)
                }
            }
        }

        if (intent.action == Intent.ACTION_PACKAGE_REMOVED) {
            // Invalidate all livedatas associated with this package