summaryrefslogtreecommitdiff
path: root/PermissionController/src/com/android/permissioncontroller/permission/model/livedatatypes/LightPermission.kt
blob: 745c5c2fdedf8a5822af745691c3d2b728dd3c64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.permissioncontroller.permission.model.livedatatypes

import android.content.pm.PackageManager
import android.content.pm.PermissionInfo
import com.android.permissioncontroller.permission.utils.SoftRestrictedPermissionPolicy
import com.android.permissioncontroller.permission.utils.Utils
import com.android.permissioncontroller.permission.utils.Utils.isRuntimePlatformPermission

/**
 * Represents a single permission, and its state
 *
 * @param pkgInfo The package requesting the permission
 * @param permInfo The permissionInfo this represents
 * @param isGrantedIncludingAppOp Whether or not this permission is functionally granted.
 * A non-granted app op but granted permission is counted as not granted
 * @param flags The PermissionController flags for this permission
 * @param foregroundPerms The foreground permission names corresponding to this permission, if this
 * permission is a background permission
 */
data class LightPermission(
    val pkgInfo: LightPackageInfo,
    val permInfo: LightPermInfo,
    val isGrantedIncludingAppOp: Boolean,
    val flags: Int,
    val foregroundPerms: List<String>?
) {

    constructor(
        pkgInfo: LightPackageInfo,
        permInfo: LightPermInfo,
        permState: PermState,
        foregroundPerms: List<String>?
    ) :
        this(pkgInfo, permInfo, permState.granted, permState.permFlags, foregroundPerms)

    /** The name of this permission */
    val name = permInfo.name
    /** The background permission name of this permission, if it exists */
    val backgroundPermission: String? = permInfo.backgroundPermission
    /** If this is a background permission **/
    val isBackgroundPermission = foregroundPerms?.isNotEmpty() ?: false
    /** Whether this permission is fixed by policy */
    val isPolicyFixed = flags and PackageManager.FLAG_PERMISSION_POLICY_FIXED != 0
    /** Whether this permission is fixed by the system */
    val isSystemFixed = flags and PackageManager.FLAG_PERMISSION_SYSTEM_FIXED != 0
    /** Whether this permission is fixed by the system */
    val isUserFixed = flags and PackageManager.FLAG_PERMISSION_USER_FIXED != 0
    /** Whether this permission is user set */
    val isUserSet = flags and PackageManager.FLAG_PERMISSION_USER_SET != 0
    /** Whether this permission is granted, but its app op is revoked */
    val isCompatRevoked = flags and PackageManager.FLAG_PERMISSION_REVOKED_COMPAT != 0
    /** Whether this permission requires review (only relevant for pre-M apps) */
    val isReviewRequired = flags and PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED != 0
    /** Whether this permission is one-time */
    val isOneTime = flags and PackageManager.FLAG_PERMISSION_ONE_TIME != 0
    /** Whether this permission is an instant app permission */
    val isInstantPerm = permInfo.protectionFlags and PermissionInfo.PROTECTION_FLAG_INSTANT != 0
    /** Whether this permission is a runtime only permission */
    val isRuntimeOnly =
        permInfo.protectionFlags and PermissionInfo.PROTECTION_FLAG_RUNTIME_ONLY != 0
    /** Whether this permission is granted by default */
    val isGrantedByDefault = flags and PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT != 0
    /** Whether this permission is granted by role */
    val isGrantedByRole = flags and PackageManager.FLAG_PERMISSION_GRANTED_BY_ROLE != 0
    /** Whether this permission is set to be revoked upon being requested */
    val isRevokeWhenRequested = flags and PackageManager.FLAG_PERMISSION_REVOKE_WHEN_REQUESTED != 0
    /** Whether this permission is user sensitive in its current grant state */
    val isUserSensitive = !isRuntimePlatformPermission(permInfo.name) ||
            (isGrantedIncludingAppOp &&
                    (flags and PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED) != 0) ||
            (!isGrantedIncludingAppOp &&
                    (flags and PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_DENIED) != 0)
    /** Whether the permission is restricted */
    val isRestricted = when {
        (permInfo.flags and PermissionInfo.FLAG_HARD_RESTRICTED) != 0 -> {
            flags and Utils.FLAGS_PERMISSION_RESTRICTION_ANY_EXEMPT == 0
        }
        (permInfo.flags and PermissionInfo.FLAG_SOFT_RESTRICTED) != 0 -> {
            !SoftRestrictedPermissionPolicy.shouldShow(pkgInfo, permInfo.name, flags)
        }
        else -> {
            false
        }
    }
    /** Whether the permission is auto revoked */
    val isAutoRevoked = flags and PackageManager.FLAG_PERMISSION_AUTO_REVOKED != 0
    /**
     * Whether the location permission is selected as the level of granularity of location accuracy
     */
    val isSelectedLocationAccuracy =
        flags and PackageManager.FLAG_PERMISSION_SELECTED_LOCATION_ACCURACY != 0

    override fun toString() = buildString {
        append(name)
        if (isGrantedIncludingAppOp) append(", Granted") else append(", NotGranted")
        if (isPolicyFixed) append(", PolicyFixed")
        if (isSystemFixed) append(", SystemFixed")
        if (isUserFixed) append(", UserFixed")
        if (isUserSet) append(", UserSet")
        if (isCompatRevoked) append(", CompatRevoked")
        if (isReviewRequired) append(", ReviewRequired")
        if (isOneTime) append(", OneTime")
        if (isGrantedByDefault) append(", GrantedByDefault")
        if (isGrantedByRole) append(", GrantedByRole")
        if (isUserSensitive) append(", UserSensitive")
        if (isRestricted) append(", Restricted")
        if (isAutoRevoked) append(", AutoRevoked")
        if (isSelectedLocationAccuracy) append(", SelectedLocationAccuracy")
    }
}