1 /*
2  * Copyright (C) 2015 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.packageinstaller.permission.model;
18 
19 import android.content.pm.PackageManager;
20 import android.content.pm.PermissionInfo;
21 
22 public final class Permission {
23     private final String mName;
24     private final String mAppOp;
25 
26     private boolean mGranted;
27     private boolean mAppOpAllowed;
28     private int mFlags;
29     private boolean mIsEphemeral;
30     private boolean mIsRuntimeOnly;
31 
Permission(String name, boolean granted, String appOp, boolean appOpAllowed, int flags, int protectionLevel)32     public Permission(String name, boolean granted,
33             String appOp, boolean appOpAllowed, int flags, int protectionLevel) {
34         mName = name;
35         mGranted = granted;
36         mAppOp = appOp;
37         mAppOpAllowed = appOpAllowed;
38         mFlags = flags;
39         mIsEphemeral = (protectionLevel & PermissionInfo.PROTECTION_FLAG_EPHEMERAL) != 0;
40         mIsRuntimeOnly = (protectionLevel & PermissionInfo.PROTECTION_FLAG_RUNTIME_ONLY) != 0;
41     }
42 
getName()43     public String getName() {
44         return mName;
45     }
46 
getAppOp()47     public String getAppOp() {
48         return mAppOp;
49     }
50 
getFlags()51     public int getFlags() {
52         return mFlags;
53     }
54 
hasAppOp()55     public boolean hasAppOp() {
56         return mAppOp != null;
57     }
58 
isGranted()59     public boolean isGranted() {
60         return mGranted;
61     }
62 
isReviewRequired()63     public boolean isReviewRequired() {
64         return (mFlags & PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED) != 0;
65     }
66 
resetReviewRequired()67     public void resetReviewRequired() {
68         mFlags &= ~PackageManager.FLAG_PERMISSION_REVIEW_REQUIRED;
69     }
70 
setGranted(boolean mGranted)71     public void setGranted(boolean mGranted) {
72         this.mGranted = mGranted;
73     }
74 
isAppOpAllowed()75     public boolean isAppOpAllowed() {
76         return mAppOpAllowed;
77     }
78 
isUserFixed()79     public boolean isUserFixed() {
80         return (mFlags & PackageManager.FLAG_PERMISSION_USER_FIXED) != 0;
81     }
82 
setUserFixed(boolean userFixed)83     public void setUserFixed(boolean userFixed) {
84         if (userFixed) {
85             mFlags |= PackageManager.FLAG_PERMISSION_USER_FIXED;
86         } else {
87             mFlags &= ~PackageManager.FLAG_PERMISSION_USER_FIXED;
88         }
89     }
90 
isSystemFixed()91     public boolean isSystemFixed() {
92         return (mFlags & PackageManager.FLAG_PERMISSION_SYSTEM_FIXED) != 0;
93     }
94 
isPolicyFixed()95     public boolean isPolicyFixed() {
96         return (mFlags & PackageManager.FLAG_PERMISSION_POLICY_FIXED) != 0;
97     }
98 
isUserSet()99     public boolean isUserSet() {
100         return (mFlags & PackageManager.FLAG_PERMISSION_USER_SET) != 0;
101     }
102 
isGrantedByDefault()103     public boolean isGrantedByDefault() {
104         return (mFlags & PackageManager.FLAG_PERMISSION_GRANTED_BY_DEFAULT) != 0;
105     }
106 
setUserSet(boolean userSet)107     public void setUserSet(boolean userSet) {
108         if (userSet) {
109             mFlags |= PackageManager.FLAG_PERMISSION_USER_SET;
110         } else {
111             mFlags &= ~PackageManager.FLAG_PERMISSION_USER_SET;
112         }
113     }
114 
setPolicyFixed(boolean policyFixed)115     public void setPolicyFixed(boolean policyFixed) {
116         if (policyFixed) {
117             mFlags |= PackageManager.FLAG_PERMISSION_POLICY_FIXED;
118         } else {
119             mFlags &= ~PackageManager.FLAG_PERMISSION_POLICY_FIXED;
120         }
121     }
122 
shouldRevokeOnUpgrade()123     public boolean shouldRevokeOnUpgrade() {
124         return (mFlags & PackageManager.FLAG_PERMISSION_REVOKE_ON_UPGRADE) != 0;
125     }
126 
setRevokeOnUpgrade(boolean revokeOnUpgrade)127     public void setRevokeOnUpgrade(boolean revokeOnUpgrade) {
128         if (revokeOnUpgrade) {
129             mFlags |= PackageManager.FLAG_PERMISSION_REVOKE_ON_UPGRADE;
130         } else {
131             mFlags &= ~PackageManager.FLAG_PERMISSION_REVOKE_ON_UPGRADE;
132         }
133     }
134 
setAppOpAllowed(boolean mAppOpAllowed)135     public void setAppOpAllowed(boolean mAppOpAllowed) {
136         this.mAppOpAllowed = mAppOpAllowed;
137     }
138 
isEphemeral()139     public boolean isEphemeral() {
140         return mIsEphemeral;
141     }
142 
isRuntimeOnly()143     public boolean isRuntimeOnly() {
144         return mIsRuntimeOnly;
145     }
146 
isGrantingAllowed(boolean isEphemeralApp, boolean supportsRuntimePermissions)147     public boolean isGrantingAllowed(boolean isEphemeralApp, boolean supportsRuntimePermissions) {
148         return (!isEphemeralApp || isEphemeral())
149                 && (supportsRuntimePermissions || !isRuntimeOnly());
150     }
151 }
152