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

Commit 0528c6fb authored by Nate Myren's avatar Nate Myren
Browse files

Post AddSource and RemoveSource to main thread

Ensure addSource and removeSource are called on the main thread

Test: Manual
Fixes: 147831048
Change-Id: I94ccb90deb009c3104c0fe2d1e539a8ba7ea52de
parent def1ca31
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -119,13 +119,13 @@ class AutoRevokeStateLiveData private constructor(
        }

        for (groupToAdd in toAdd) {
            postAddSource(permStateLiveDatas[groupToAdd]!!, Observer {
            addSource(permStateLiveDatas[groupToAdd]!!, Observer {
                updateIfActive()
            })
        }

        for (groupToRemove in toRemove) {
            postRemoveSource(permStateLiveDatas[groupToRemove]!!)
            removeSource(permStateLiveDatas[groupToRemove]!!)
            permStateLiveDatas.remove(groupToRemove)
        }
    }
+18 −14
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ class PermGroupLiveData private constructor(
    /**
     * Map<packageName, LiveData<PackageInfo>>
     */
    private val pkgsUsingGroup = mutableMapOf<String, LightPackageInfoLiveData>()
    private val packageLiveDatas = mutableMapOf<String, LightPackageInfoLiveData>()

    private lateinit var groupInfo: PackageItemInfo

@@ -70,11 +70,8 @@ class PermGroupLiveData private constructor(
     * @param liveData the PackageInfoLiveData to be inserted
     */
    private fun addPackageLiveData(packageName: String, liveData: LightPackageInfoLiveData) {
        if (!pkgsUsingGroup.contains(packageName)) {
            pkgsUsingGroup[packageName] = liveData
            addSource(liveData) {
                updateIfActive()
            }
        if (!packageLiveDatas.contains(packageName)) {
            packageLiveDatas[packageName] = liveData
        }
    }

@@ -124,18 +121,25 @@ class PermGroupLiveData private constructor(
        addPackageLiveData(groupInfo.packageName,
            LightPackageInfoLiveData[groupInfo.packageName, UserHandle.SYSTEM])

        val (toAdd, toRemove) = KotlinUtils.getMapAndListDifferences(packageNames, pkgsUsingGroup)
        for (packageName in toAdd) {
            if (!packageNames.contains(packageName)) {
                addPackageLiveData(groupInfo.packageName,
                    LightPackageInfoLiveData[packageName, UserHandle.SYSTEM])
            }
        val (toAdd, toRemove) = KotlinUtils.getMapAndListDifferences(packageNames, packageLiveDatas)
        val toAddWithGroupPkg = toAdd.toMutableSet()
        if (!packageLiveDatas.contains(groupInfo.packageName)) {
                toAddWithGroupPkg.add(groupInfo.packageName)
            }

        for (packageName in toRemove) {
            pkgsUsingGroup[packageName]?.let { liveData ->
            packageLiveDatas[packageName]?.let { liveData ->
                packageLiveDatas.remove(packageName)
                removeSource(liveData)
                pkgsUsingGroup.remove(packageName)
            }
        }
        for (packageName in toAddWithGroupPkg) {
            packageLiveDatas[packageName] = LightPackageInfoLiveData[packageName, UserHandle.SYSTEM]
        }

        for (packageName in toAddWithGroupPkg) {
            addSource(packageLiveDatas[packageName]!!) {
                onUpdate()
            }
        }
    }
+20 −20
Original line number Diff line number Diff line
@@ -133,6 +133,7 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
        return valOne != valTwo
    }

    @MainThread
    fun observeStale(owner: LifecycleOwner, observer: Observer<in T>) {
        val oldStaleObserver = hasStaleObserver()
        staleObservers.add(owner to observer)
@@ -144,38 +145,33 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
        }
    }

    @MainThread
    override fun <S : Any?> addSource(source: LiveData<S>, onChanged: Observer<in S>) {
        // ensureMainThread() //TODO (b/148458939): violated in PackagePermissionsLiveData
        GlobalScope.launch(Main.immediate) {
            if (source is SmartUpdateMediatorLiveData) {
            source.addChild(this, onChanged,
                source.addChild(this@SmartUpdateMediatorLiveData, onChanged,
                    staleObservers.isNotEmpty() || children.any { it.third })
                sources.add(source)
            }
            super.addSource(source, onChanged)
        }
    }

    @MainThread
    override fun <S : Any?> removeSource(toRemote: LiveData<S>) {
        GlobalScope.launch(Main.immediate) {
            if (toRemote is SmartUpdateMediatorLiveData) {
            toRemote.removeChild(this)
                toRemote.removeChild(this@SmartUpdateMediatorLiveData)
                sources.remove(toRemote)
            }
            super.removeSource(toRemote)
        }

    fun <S : Any?> postAddSource(source: LiveData<S>, onChanged: Observer<in S>) {
        GlobalScope.launch(Main) { addSource(source, onChanged) }
    }

    fun <S : Any?> postRemoveSource(toRemote: LiveData<S>) {
        GlobalScope.launch(Main) { removeSource(toRemote) }
    }

    @MainThread
    private fun <S : Any?> removeChild(liveData: LiveData<S>) {
        children.removeIf { it.first == liveData }
    }

    @MainThread
    private fun <S : Any?> addChild(
        liveData: SmartUpdateMediatorLiveData<S>,
        onChanged: Observer<in T>,
@@ -184,6 +180,7 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
        children.add(Triple(liveData, onChanged, sendStaleUpdates))
    }

    @MainThread
    private fun <S : Any?> updateStaleChildNotify(
        liveData: SmartUpdateMediatorLiveData<S>,
        sendStaleUpdates: Boolean
@@ -195,6 +192,7 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
        }
    }

    @MainThread
    override fun removeObserver(observer: Observer<in T>) {
        val oldStaleObserver = hasStaleObserver()
        staleObservers.removeIf { it.second == observer }
@@ -202,6 +200,7 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
        super.removeObserver(observer)
    }

    @MainThread
    override fun removeObservers(owner: LifecycleOwner) {
        val oldStaleObserver = hasStaleObserver()
        staleObservers.removeIf { it.first == owner }
@@ -209,6 +208,7 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
        super.removeObservers(owner)
    }

    @MainThread
    private fun notifySourcesOnStaleUpdates(oldHasStale: Boolean, newHasStale: Boolean) {
        if (oldHasStale == newHasStale) {
            return