1 /*
2  * Copyright (C) 2019 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.app.Application
20 import android.content.pm.PermissionInfo
21 import com.android.permissioncontroller.PermissionControllerApplication
22 import com.android.permissioncontroller.permission.utils.PermissionMapping
23 
24 /**
25  * A class which tracks the names of all custom permission groups in the system, including
26  * non-grouped runtime permissions, the UNDEFINED group, and any group not defined by the system.
27  */
28 object CustomPermGroupNamesLiveData : SmartUpdateMediatorLiveData<List<String>>() {
29 
30     private val app: Application = PermissionControllerApplication.get()
31     private val packagesLiveData = AllPackageInfosLiveData
32 
33     init {
<lambda>null34         addSource(packagesLiveData) { update() }
35     }
36 
onUpdatenull37     override fun onUpdate() {
38         val platformGroupNames = PermissionMapping.getPlatformPermissionGroups()
39         val groupNames = mutableListOf<String>()
40 
41         val allPackages = packagesLiveData.value ?: return
42 
43         for ((_, packageInfos) in allPackages) {
44             for (packageInfo in packageInfos) {
45                 // Look for possible lone runtime permissions or custom groups
46                 packageInfo.permissions.let {
47                     for (permission in it) {
48                         // We care only about installed runtime permissions.
49                         if (
50                             permission.protection != PermissionInfo.PROTECTION_DANGEROUS ||
51                                 permission.flags and PermissionInfo.FLAG_INSTALLED == 0
52                         ) {
53                             continue
54                         }
55 
56                         // If this permission is already in a group, no more work to do
57                         if (
58                             groupNames.contains(permission.group) ||
59                                 platformGroupNames.contains(permission.group) ||
60                                 groupNames.contains(permission.name)
61                         ) {
62                             continue
63                         }
64 
65                         if (permission.group != null) {
66                             groupNames.add(permission.group)
67                         } else {
68                             groupNames.add(permission.name)
69                         }
70                     }
71                 }
72             }
73         }
74         value = groupNames
75     }
76 }
77