• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Utils
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  */
29 object CustomPermGroupNamesLiveData : SmartUpdateMediatorLiveData<List<String>>() {
30 
31     private val app: Application = PermissionControllerApplication.get()
32     private val packagesLiveData = AllPackageInfosLiveData
33 
34     init {
<lambda>null35         addSource(packagesLiveData) {
36             update()
37         }
38     }
39 
onUpdatenull40     override fun onUpdate() {
41         val platformGroupNames = Utils.getPlatformPermissionGroups()
42         val groupNames = mutableListOf<String>()
43 
44         val allPackages = packagesLiveData.value ?: return
45 
46         for ((_, packageInfos) in allPackages) {
47             for (packageInfo in packageInfos) {
48                 // Look for possible lone runtime permissions or custom groups
49                 packageInfo.permissions.let {
50                     for (permission in it) {
51                         // We care only about installed runtime permissions.
52                         if (permission.protection != PermissionInfo.PROTECTION_DANGEROUS ||
53                             permission.flags and PermissionInfo.FLAG_INSTALLED == 0) {
54                             continue
55                         }
56 
57                         // If this permission is already in a group, no more work to do
58                         if (groupNames.contains(permission.group) ||
59                             platformGroupNames.contains(permission.group) ||
60                             groupNames.contains(permission.name)) {
61                             continue
62                         }
63 
64                         if (permission.group != null) {
65                             groupNames.add(permission.group)
66                         } else {
67                             groupNames.add(permission.name)
68                         }
69                     }
70                 }
71             }
72         }
73         value = groupNames
74     }
75 }
76