• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.content.pm.PackageManager
20 import com.android.permissioncontroller.PermissionControllerApplication
21 import com.android.permissioncontroller.permission.utils.PermissionMapping
22 import com.android.permissioncontroller.permission.utils.Utils
23 import kotlinx.coroutines.Job
24 
25 /**
26  * LiveData for a map of background permission name -> list of foreground permission names for every
27  * installed, runtime permission in every platform permission group. This LiveData's value is
28  * static, since the background/foreground permission relationships are defined by the system.
29  */
30 object ForegroundPermNamesLiveData : SmartAsyncMediatorLiveData<Map<String, List<String>>>(true) {
31     private val app = PermissionControllerApplication.get()
32 
33     // Since the value will be static, initialize the value upon creating the LiveData.
34     init {
35         onUpdate()
36     }
37 
loadDataAndPostValuenull38     override suspend fun loadDataAndPostValue(job: Job) {
39         val systemGroups = PermissionMapping.getPlatformPermissionGroups()
40         val permMap = mutableMapOf<String, MutableList<String>>()
41         for (groupName in systemGroups) {
42             val permInfos = try {
43                 Utils.getInstalledRuntimePermissionInfosForGroup(app.packageManager, groupName)
44             } catch (e: PackageManager.NameNotFoundException) {
45                 continue
46             }
47             for (permInfo in permInfos) {
48                 val backgroundPerm: String? = permInfo.backgroundPermission
49                 if (backgroundPerm != null) {
50                     val foregroundPerms = permMap.getOrPut(backgroundPerm) { mutableListOf() }
51                     foregroundPerms.add(permInfo.name)
52                 }
53             }
54         }
55         postValue(permMap)
56     }
57 }