• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /**
24  * A LiveData which tracks the PackageInfos of all of the packages in the system, for all users.
25  */
26 object AllPackageInfosLiveData :
27     SmartUpdateMediatorLiveData<Map<UserHandle, List<LightPackageInfo>>>() {
28 
29     private val userPackageInfosLiveDatas = mutableMapOf<UserHandle, UserPackageInfosLiveData>()
30     private val userPackageInfos = mutableMapOf<UserHandle, List<LightPackageInfo>>()
31 
32     init {
33         addSource(UsersLiveData) {
34             update()
35         }
36     }
37 
38     override fun onUpdate() {
39         UsersLiveData.value?.let { users ->
40             val getLiveData = { user: UserHandle -> UserPackageInfosLiveData[user] }
41             setSourcesToDifference(users, userPackageInfosLiveDatas, getLiveData)
42         }
43         if (!userPackageInfosLiveDatas.all { it.value.isInitialized }) {
44             return
45         }
46 
47         for ((user, userPackageInfosLiveData) in userPackageInfosLiveDatas) {
48             val packageInfos = userPackageInfosLiveData.value
49             if (packageInfos == null) {
50                 userPackageInfos.remove(user)
51             } else {
52                 userPackageInfos[user] = packageInfos
53             }
54         }
55         value = userPackageInfos.toMap()
56     }
57 }
58