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

Commit c7d7ca87 authored by Nate Myren's avatar Nate Myren Committed by Android (Google) Code Review
Browse files

Merge "Make add/removeSource blocking, make source lists sets" into rvc-dev

parents dd6e7458 ffbb2fe0
Loading
Loading
Loading
Loading
+6 −3
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.app.Application
import android.content.pm.PackageManager
import android.os.UserHandle
import android.util.Log
import androidx.lifecycle.Observer
import com.android.permissioncontroller.PermissionControllerApplication
import com.android.permissioncontroller.permission.model.livedatatypes.LightPackageInfo
import com.android.permissioncontroller.permission.utils.Utils
@@ -108,9 +109,7 @@ class LightPackageInfoLiveData private constructor(
        }
        if (userPackagesLiveData.hasActiveObservers() && !watchingUserPackagesLiveData) {
            watchingUserPackagesLiveData = true
            addSource(userPackagesLiveData) {
                updateFromUserPackageInfosLiveData()
            }
            addSource(userPackagesLiveData, userPackageInfosObserver)
            if (userPackagesLiveData.isInitialized) {
                // Set our value, but listen for new updates.
                updateFromUserPackageInfosLiveData()
@@ -120,6 +119,10 @@ class LightPackageInfoLiveData private constructor(
        }
    }

    val userPackageInfosObserver = Observer<List<LightPackageInfo>> {
        updateFromUserPackageInfosLiveData()
    }

    private fun updateFromUserPackageInfosLiveData() {
        val packageInfo = userPackagesLiveData.value!!.find { it.packageName == packageName }
        if (packageInfo != null) {
+1 −1
Original line number Diff line number Diff line
@@ -78,7 +78,7 @@ class PermGroupsPackagesUiInfoLiveData(
    private fun addPermGroupPackagesUiInfoLiveDatas(
        groupNames: Collection<String>
    ) {
        val groupsAdded = mutableListOf<String>()
        val groupsAdded = mutableSetOf<String>()
        for (groupName in groupNames) {
            if (!permGroupPackagesLiveDatas.containsKey(groupName)) {
                groupsAdded.add(groupName)
+16 −11
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import com.android.permissioncontroller.permission.utils.shortStackTrace
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

/**
 * A MediatorLiveData which tracks how long it has been inactive, compares new values before setting
@@ -146,6 +147,7 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
    }

    override fun <S : Any?> addSource(source: LiveData<S>, onChanged: Observer<in S>) {
        runBlocking {
            GlobalScope.launch(Main.immediate) {
                if (source is SmartUpdateMediatorLiveData) {
                    source.addChild(this@SmartUpdateMediatorLiveData, onChanged,
@@ -155,8 +157,10 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
                super.addSource(source, onChanged)
            }
        }
    }

    override fun <S : Any?> removeSource(toRemote: LiveData<S>) {
        runBlocking {
            GlobalScope.launch(Main.immediate) {
                if (toRemote is SmartUpdateMediatorLiveData) {
                    toRemote.removeChild(this@SmartUpdateMediatorLiveData)
@@ -165,6 +169,7 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
                super.removeSource(toRemote)
            }
        }
    }

    @MainThread
    private fun <S : Any?> removeChild(liveData: LiveData<S>) {
+3 −3
Original line number Diff line number Diff line
@@ -99,9 +99,9 @@ object KotlinUtils {
    fun <K> getMapAndListDifferences(
        newValues: List<K>,
        oldValues: Map<K, *>
    ): Pair<List<K>, List<K>> {
        val mapHas = oldValues.keys.toMutableList()
        val listHas = newValues.toMutableList()
    ): Pair<Set<K>, Set<K>> {
        val mapHas = oldValues.keys.toMutableSet()
        val listHas = newValues.toMutableSet()
        for (newVal in newValues) {
            if (oldValues.containsKey(newVal)) {
                mapHas.remove(newVal)