1 /*
<lambda>null2  * Copyright (C) 2024 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.role.data.repository.v31
18 
19 import android.app.Application
20 import android.app.role.RoleManager
21 import kotlin.concurrent.Volatile
22 import kotlinx.coroutines.Dispatchers
23 import kotlinx.coroutines.withContext
24 
25 /**
26  * This repository encapsulates roles data (i.e. querying/adding role holders) exposed by
27  * [RoleManager].
28  */
29 interface RoleRepository {
30     /**
31      * @return Set of package names, the usage of private data by these packages is not shown in the
32      *   privacy dashboard.
33      */
34     suspend fun getExemptedPackages(): Set<String>
35 
36     companion object {
37         @Volatile private var instance: RoleRepository? = null
38 
39         fun getInstance(application: Application): RoleRepository =
40             instance
41                 ?: synchronized(this) { RoleRepositoryImpl(application).also { instance = it } }
42     }
43 }
44 
45 class RoleRepositoryImpl(application: Application) : RoleRepository {
46     private val roleManager = application.getSystemService(RoleManager::class.java)!!
47 
getExemptedPackagesnull48     override suspend fun getExemptedPackages(): Set<String> =
49         withContext(Dispatchers.Default) {
50             return@withContext buildSet {
51                 add(OS_PKG)
52                 addAll(EXEMPTED_ROLES.map { role -> roleManager.getRoleHolders(role) }.flatten())
53             }
54         }
55 
56     companion object {
57         private const val OS_PKG = "android"
58         private const val SYSTEM_AMBIENT_AUDIO_INTELLIGENCE =
59             "android.app.role.SYSTEM_AMBIENT_AUDIO_INTELLIGENCE"
60         private const val SYSTEM_UI_INTELLIGENCE = "android.app.role.SYSTEM_UI_INTELLIGENCE"
61         private const val SYSTEM_AUDIO_INTELLIGENCE = "android.app.role.SYSTEM_AUDIO_INTELLIGENCE"
62         private const val SYSTEM_NOTIFICATION_INTELLIGENCE =
63             "android.app.role.SYSTEM_NOTIFICATION_INTELLIGENCE"
64         private const val SYSTEM_TEXT_INTELLIGENCE = "android.app.role.SYSTEM_TEXT_INTELLIGENCE"
65         private const val SYSTEM_VISUAL_INTELLIGENCE = "android.app.role.SYSTEM_VISUAL_INTELLIGENCE"
66 
67         private val EXEMPTED_ROLES =
68             arrayOf(
69                 SYSTEM_AMBIENT_AUDIO_INTELLIGENCE,
70                 SYSTEM_UI_INTELLIGENCE,
71                 SYSTEM_AUDIO_INTELLIGENCE,
72                 SYSTEM_NOTIFICATION_INTELLIGENCE,
73                 SYSTEM_TEXT_INTELLIGENCE,
74                 SYSTEM_VISUAL_INTELLIGENCE
75             )
76     }
77 }
78