1 /*
<lambda>null2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.permissioncontroller.permission.data
18 
19 import android.os.UserHandle
20 import com.android.permissioncontroller.permission.data.AllPackageInfosLiveData.addSource
21 import com.android.permissioncontroller.permission.model.livedatatypes.LightPackageInfo
22 
23 /** A LiveData which tracks the PackageInfos of all of the packages in the system, for all users. */
24 object AllPackageInfosLiveData :
25     SmartUpdateMediatorLiveData<Map<UserHandle, List<LightPackageInfo>>>() {
26 
27     private val userPackageInfosLiveDatas = mutableMapOf<UserHandle, UserPackageInfosLiveData>()
28     private val userPackageInfos = mutableMapOf<UserHandle, List<LightPackageInfo>>()
29 
30     init {
31         addSource(UsersLiveData) { update() }
32     }
33 
34     override fun onUpdate() {
35         UsersLiveData.value?.let { users ->
36             val getLiveData = { user: UserHandle -> UserPackageInfosLiveData[user] }
37             setSourcesToDifference(users, userPackageInfosLiveDatas, getLiveData)
38         }
39         if (!userPackageInfosLiveDatas.all { it.value.isInitialized }) {
40             return
41         }
42 
43         for ((user, userPackageInfosLiveData) in userPackageInfosLiveDatas) {
44             val packageInfos = userPackageInfosLiveData.value
45             if (packageInfos == null) {
46                 userPackageInfos.remove(user)
47             } else {
48                 userPackageInfos[user] = packageInfos
49             }
50         }
51         value = userPackageInfos.toMap()
52     }
53 }
54