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.model.livedatatypes
18 
19 import android.content.pm.PackageManager
20 import android.content.pm.PermissionInfo
21 import com.android.permissioncontroller.permission.utils.SoftRestrictedPermissionPolicy
22 import com.android.permissioncontroller.permission.utils.Utils
23 import com.android.permissioncontroller.permission.utils.Utils.isRuntimePlatformPermission
24 
25 /**
26  * Represents a single permission, and its state
27  *
28  * @param pkgInfo The package requesting the permission
29  * @param permInfo The permissionInfo this represents
30  * @param isGrantedIncludingAppOp Whether or not this permission is functionally granted.
31  * A non-granted app op but granted permission is counted as not granted
32  * @param flags The PermissionController flags for this permission
33  * @param foregroundPerms The foreground permission names corresponding to this permission, if this
34  * permission is a background permission
35  */
36 data class LightPermission(
37     val pkgInfo: LightPackageInfo,
38     val permInfo: LightPermInfo,
39     val isGrantedIncludingAppOp: Boolean,
40     val flags: Int,
41     val foregroundPerms: List<String>?
42 ) {
43 
44     constructor(
45         pkgInfo: LightPackageInfo,
46         permInfo: LightPermInfo,
47         permState: PermState,
48         foregroundPerms: List<String>?
49     ) :
50         this(pkgInfo, permInfo, permState.granted, permState.permFlags, foregroundPerms)
51 
52     /** The name of this permission */
53     val name = permInfo.name
54     /** The background permission name of this permission, if it exists */
55     val backgroundPermission: String? = permInfo.backgroundPermission
56     /** If this is a background permission **/
57     val isBackgroundPermission = foregroundPerms?.isNotEmpty() ?: false
58     /** Whether this permission is fixed by policy */
59     val isPolicyFixed = flags and PackageManager.FLAG_PERMISSION_POLICY_FIXED != 0
60     /** Whether this permission is fixed by the system */
61     val isSystemFixed = flags and PackageManager.FLAG_PERMISSION_SYSTEM_FIXED != 0
62     /** Whether this permission is fixed by the system */
63     val isUserFixed = flags and PackageManager.FLAG_PERMISSION_USER_FIXED != 0
64     /** Whether this permission is user set */
65     val isUserSet = flags and PackageManager.FLAG_PERMISSION_USER_SET != 0
66     /** Whether this permission is granted, but its app op is revoked */
67     val isCompatRevoked = flags and PackageManager.FLAG_PERMISSION_REVOKED_COMPAT != 0
68     /** Whether this permission requires review (only relevant for pre-M apps) */
69     val isReviewRequired = flags and PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED != 0
70     /** Whether this permission is one-time */
71     val isOneTime = flags and PackageManager.FLAG_PERMISSION_ONE_TIME != 0
72     /** Whether this permission is an instant app permission */
73     val isInstantPerm = permInfo.protectionFlags and PermissionInfo.PROTECTION_FLAG_INSTANT != 0
74     /** Whether this permission is a runtime only permission */
75     val isRuntimeOnly =
76         permInfo.protectionFlags and PermissionInfo.PROTECTION_FLAG_RUNTIME_ONLY != 0
77     /** Whether this permission is granted by default */
78     val isGrantedByDefault = flags and PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT != 0
79     /** Whether this permission is granted by role */
80     val isGrantedByRole = flags and PackageManager.FLAG_PERMISSION_GRANTED_BY_ROLE != 0
81     /** Whether this permission is user sensitive in its current grant state */
82     val isUserSensitive = !isRuntimePlatformPermission(permInfo.name) ||
83             (isGrantedIncludingAppOp &&
84                     (flags and PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED) != 0) ||
85             (!isGrantedIncludingAppOp &&
86                     (flags and PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_DENIED) != 0)
87     /** Whether the permission is restricted */
88     val isRestricted = when {
89         (permInfo.flags and PermissionInfo.FLAG_HARD_RESTRICTED) != 0 -> {
90             flags and Utils.FLAGS_PERMISSION_RESTRICTION_ANY_EXEMPT == 0
91         }
92         (permInfo.flags and PermissionInfo.FLAG_SOFT_RESTRICTED) != 0 -> {
93             !SoftRestrictedPermissionPolicy.shouldShow(pkgInfo, permInfo.name, flags)
94         }
95         else -> {
96             false
97         }
98     }
99     /** Whether the permission is auto revoked */
100     val isAutoRevoked = flags and PackageManager.FLAG_PERMISSION_AUTO_REVOKED != 0
101 
<lambda>null102     override fun toString() = buildString {
103         append(name)
104         if (isGrantedIncludingAppOp) append(", Granted") else append(", NotGranted")
105         if (isPolicyFixed) append(", PolicyFixed")
106         if (isSystemFixed) append(", SystemFixed")
107         if (isUserFixed) append(", UserFixed")
108         if (isUserSet) append(", UserSet")
109         if (isCompatRevoked) append(", CompatRevoked")
110         if (isReviewRequired) append(", ReviewRequired")
111         if (isOneTime) append(", OneTime")
112         if (isGrantedByDefault) append(", GrantedByDefault")
113         if (isGrantedByRole) append(", GrantedByRole")
114         if (isUserSensitive) append(", UserSensitive")
115         if (isRestricted) append(", Restricted")
116         if (isAutoRevoked) append(", AutoRevoked")
117     }
118 }