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 set to be revoked upon being requested */ 82 val isRevokeWhenRequested = flags and PackageManager.FLAG_PERMISSION_REVOKE_WHEN_REQUESTED != 0 83 /** Whether this permission is user sensitive in its current grant state */ 84 val isUserSensitive = !isRuntimePlatformPermission(permInfo.name) || 85 (isGrantedIncludingAppOp && 86 (flags and PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED) != 0) || 87 (!isGrantedIncludingAppOp && 88 (flags and PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_DENIED) != 0) 89 /** Whether the permission is restricted */ 90 val isRestricted = when { 91 (permInfo.flags and PermissionInfo.FLAG_HARD_RESTRICTED) != 0 -> { 92 flags and Utils.FLAGS_PERMISSION_RESTRICTION_ANY_EXEMPT == 0 93 } 94 (permInfo.flags and PermissionInfo.FLAG_SOFT_RESTRICTED) != 0 -> { 95 !SoftRestrictedPermissionPolicy.shouldShow(pkgInfo, permInfo.name, flags) 96 } 97 else -> { 98 false 99 } 100 } 101 /** Whether the permission is auto revoked */ 102 val isAutoRevoked = flags and PackageManager.FLAG_PERMISSION_AUTO_REVOKED != 0 103 /** 104 * Whether the location permission is selected as the level of granularity of location accuracy 105 */ 106 val isSelectedLocationAccuracy = 107 flags and PackageManager.FLAG_PERMISSION_SELECTED_LOCATION_ACCURACY != 0 108 109 override fun toString() = buildString { 110 append(name) 111 if (isGrantedIncludingAppOp) append(", Granted") else append(", NotGranted") 112 if (isPolicyFixed) append(", PolicyFixed") 113 if (isSystemFixed) append(", SystemFixed") 114 if (isUserFixed) append(", UserFixed") 115 if (isUserSet) append(", UserSet") 116 if (isCompatRevoked) append(", CompatRevoked") 117 if (isReviewRequired) append(", ReviewRequired") 118 if (isOneTime) append(", OneTime") 119 if (isGrantedByDefault) append(", GrantedByDefault") 120 if (isGrantedByRole) append(", GrantedByRole") 121 if (isUserSensitive) append(", UserSensitive") 122 if (isRestricted) append(", Restricted") 123 if (isAutoRevoked) append(", AutoRevoked") 124 if (isSelectedLocationAccuracy) append(", SelectedLocationAccuracy") 125 } 126 }