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

Commit 67260eb9 authored by Eugene Susla's avatar Eugene Susla Committed by Michael Bestas
Browse files

[DO NOT MERGE] Deep-update live datas and update on isStale in getInitializedValue

Test: on branch aosp-android11-gsi: $ atest AutoRevokeTest
Fixes: 171371510
Change-Id: Ie0e2d036f1877c157f3d0c21ed3361c5feffa4ce
parent f4b38f29
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -19,12 +19,16 @@ package com.android.permissioncontroller.permission.data
import android.os.UserHandle
import com.android.permissioncontroller.permission.data.AllPackageInfosLiveData.addSource
import com.android.permissioncontroller.permission.model.livedatatypes.LightPackageInfo
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * A LiveData which tracks the PackageInfos of all of the packages in the system, for all users.
 */
object AllPackageInfosLiveData :
    SmartUpdateMediatorLiveData<Map<UserHandle, List<LightPackageInfo>>>() {
    SmartUpdateMediatorLiveData<Map<UserHandle, List<LightPackageInfo>>>(),
    DeepUpdateable {

    private val userPackageInfosLiveDatas = mutableMapOf<UserHandle, UserPackageInfosLiveData>()
    private val userPackageInfos = mutableMapOf<UserHandle, List<LightPackageInfo>>()
@@ -35,6 +39,15 @@ object AllPackageInfosLiveData :
        }
    }

    override suspend fun onDeepUpdate() {
        updateIfActive()
        GlobalScope.launch(Main.immediate) {
            for ((_, liveData) in userPackageInfosLiveDatas) {
                liveData.updateAsync()
            }
        }
    }

    override fun onUpdate() {
        UsersLiveData.value?.let { users ->
            val getLiveData = { user: UserHandle -> UserPackageInfosLiveData[user] }
+40 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.permissioncontroller.permission.data

/**
 * Marks a [SmartUpdateMediatorLiveData] that supports deep updating, that is triggering updates on
 * its dependencies (also deep where supported)
 */
interface DeepUpdateable {
    /**
     * Trigger a deep update
     */
    suspend fun onDeepUpdate()
}

/**
 * Trigger deep update, falling back to a regular update if deep update not supported by this
 * [SmartUpdateMediatorLiveData]
 */
suspend fun SmartUpdateMediatorLiveData<*>.deepUpdate() {
    if (this is DeepUpdateable) {
        onDeepUpdate()
    } else {
        updateIfActive()
    }
}
 No newline at end of file
+4 −2
Original line number Diff line number Diff line
@@ -328,8 +328,10 @@ abstract class SmartUpdateMediatorLiveData<T> : MediatorLiveData<T>(),
        return getInitializedValue(
            observe = { observer ->
                observeStale(ForeverActiveLifecycle, observer)
                if (forceUpdate) {
                    updateIfActive()
                if (forceUpdate || (!staleOk && isStale)) {
                    GlobalScope.launch(Main) {
                        deepUpdate()
                    }
                }
            },
            isInitialized = { isInitialized && (staleOk || !isStale) })