1 /* 2 * 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) { user -> 42 onUserPackageUpdates(user, userPackageInfosLiveDatas[user]?.value) 43 } 44 } 45 } 46 47 private fun onUserPackageUpdates(user: UserHandle, packageInfos: List<LightPackageInfo>?) { 48 if (packageInfos == null) { 49 userPackageInfos.remove(user) 50 } else { 51 userPackageInfos[user] = packageInfos 52 } 53 if (userPackageInfosLiveDatas.all { it.value.isInitialized }) { 54 value = userPackageInfos.toMap() 55 } 56 } 57 } 58