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