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 com.android.permissioncontroller.permission.utils.Utils
20 
21 /**
22  * LiveData for a map of background permission name -> list of foreground permission names for every
23  * installed, runtime permission in every platform permission group. This LiveData's value is
24  * static, since the background/foreground permission relationships are defined by the system.
25  */
26 object ForegroundPermNamesLiveData : SmartUpdateMediatorLiveData<Map<String, List<String>>>() {
27 
28     // Since the value will be static, initialize the value upon creating the LiveData.
29     init {
30         onUpdate()
31     }
32 
33     override fun onUpdate() {
34         val systemGroups = Utils.getPlatformPermissionGroups()
35         val groupLiveDatas = systemGroups.map { PermGroupLiveData[it] }
36         val permMap = mutableMapOf<String, MutableList<String>>()
37         var numLiveDatasSeen = 0
38         for (groupLiveData in groupLiveDatas) {
39             addSource(groupLiveData) { permGroup ->
40                 if (permGroup == null) {
41                     if (groupLiveData.isInitialized) {
42                         numLiveDatasSeen ++
43                     }
44                     return@addSource
45                 }
46                 for (permInfo in permGroup.permissionInfos.values) {
47                     val backgroundPerm: String? = permInfo.backgroundPermission
48                     if (backgroundPerm != null) {
49                         val foregroundPerms = permMap.getOrPut(backgroundPerm) { mutableListOf() }
50                         foregroundPerms.add(permInfo.name)
51                     }
52                 }
53                 numLiveDatasSeen ++
54                 if (numLiveDatasSeen == groupLiveDatas.size) {
55                     value = permMap
56                 }
57             }
58         }
59     }
60 }