• 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.Utils
22 import kotlinx.coroutines.Job
23 
24 /**
25  * LiveData for a map of background permission name -> list of foreground permission names for every
26  * installed, runtime permission in every platform permission group. This LiveData's value is
27  * static, since the background/foreground permission relationships are defined by the system.
28  */
29 object ForegroundPermNamesLiveData : SmartAsyncMediatorLiveData<Map<String, List<String>>>(true) {
30     private val app = PermissionControllerApplication.get()
31 
32     // Since the value will be static, initialize the value upon creating the LiveData.
33     init {
34         onUpdate()
35     }
36 
loadDataAndPostValuenull37     override suspend fun loadDataAndPostValue(job: Job) {
38         val systemGroups = Utils.getPlatformPermissionGroups()
39         val permMap = mutableMapOf<String, MutableList<String>>()
40         for (groupName in systemGroups) {
41             val permInfos = try {
42                 Utils.getInstalledRuntimePermissionInfosForGroup(app.packageManager, groupName)
43             } catch (e: PackageManager.NameNotFoundException) {
44                 continue
45             }
46             for (permInfo in permInfos) {
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         }
54         postValue(permMap)
55     }
56 }