blob: 9da2ba9ce59dbea6b50d9ca2ec4143d79838bcf3 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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
17package android.content.pm;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.text.TextUtils;
22
23/**
24 * Information you can retrieve about a particular security permission
25 * known to the system. This corresponds to information collected from the
26 * AndroidManifest.xml's <permission> tags.
27 */
28public class PermissionInfo extends PackageItemInfo implements Parcelable {
29 /**
30 * A normal application value for {@link #protectionLevel}, corresponding
31 * to the <code>normal</code> value of
32 * {@link android.R.attr#protectionLevel}.
33 */
34 public static final int PROTECTION_NORMAL = 0;
35
36 /**
37 * Dangerous value for {@link #protectionLevel}, corresponding
38 * to the <code>dangerous</code> value of
39 * {@link android.R.attr#protectionLevel}.
40 */
41 public static final int PROTECTION_DANGEROUS = 1;
42
43 /**
44 * System-level value for {@link #protectionLevel}, corresponding
45 * to the <code>signature</code> value of
46 * {@link android.R.attr#protectionLevel}.
47 */
48 public static final int PROTECTION_SIGNATURE = 2;
49
50 /**
Dianne Hackborna90c8de2015-07-07 17:25:25 -070051 * @deprecated Use {@link #PROTECTION_SIGNATURE}|{@link #PROTECTION_FLAG_PRIVILEGED}
52 * instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053 */
Dianne Hackborna90c8de2015-07-07 17:25:25 -070054 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 public static final int PROTECTION_SIGNATURE_OR_SYSTEM = 3;
56
57 /**
Dianne Hackborne639da72012-02-21 15:11:13 -080058 * Additional flag for {@link #protectionLevel}, corresponding
Dianne Hackborna90c8de2015-07-07 17:25:25 -070059 * to the <code>privileged</code> value of
Dianne Hackborne639da72012-02-21 15:11:13 -080060 * {@link android.R.attr#protectionLevel}.
61 */
Dianne Hackborna90c8de2015-07-07 17:25:25 -070062 public static final int PROTECTION_FLAG_PRIVILEGED = 0x10;
63
64 /**
65 * @deprecated Old name for {@link #PROTECTION_FLAG_PRIVILEGED}, which
66 * is now very confusing because it only applies to privileged apps, not all
67 * apps on the system image.
68 */
69 @Deprecated
Dianne Hackborne639da72012-02-21 15:11:13 -080070 public static final int PROTECTION_FLAG_SYSTEM = 0x10;
71
72 /**
73 * Additional flag for {@link #protectionLevel}, corresponding
74 * to the <code>development</code> value of
75 * {@link android.R.attr#protectionLevel}.
76 */
77 public static final int PROTECTION_FLAG_DEVELOPMENT = 0x20;
78
79 /**
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -070080 * Additional flag for {@link #protectionLevel}, corresponding
Jeff Sharkeye9b78fd2014-08-13 14:16:50 -070081 * to the <code>appop</code> value of
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -070082 * {@link android.R.attr#protectionLevel}.
83 */
84 public static final int PROTECTION_FLAG_APPOP = 0x40;
85
86 /**
Dianne Hackbornde15eda2015-07-01 12:30:54 -070087 * Additional flag for {@link #protectionLevel}, corresponding
88 * to the <code>pre23</code> value of
89 * {@link android.R.attr#protectionLevel}.
90 */
91 public static final int PROTECTION_FLAG_PRE23 = 0x80;
92
93 /**
Svetoslav3e7d9772015-07-06 18:31:23 -070094 * Additional flag for {@link #protectionLevel}, corresponding
95 * to the <code>installer</code> value of
96 * {@link android.R.attr#protectionLevel}.
97 */
98 public static final int PROTECTION_FLAG_INSTALLER = 0x100;
99
100 /**
101 * Additional flag for {@link #protectionLevel}, corresponding
102 * to the <code>verifier</code> value of
103 * {@link android.R.attr#protectionLevel}.
104 */
105 public static final int PROTECTION_FLAG_VERIFIER = 0x200;
106
107 /**
Dianne Hackborna90c8de2015-07-07 17:25:25 -0700108 * Additional flag for {@link #protectionLevel}, corresponding
109 * to the <code>preinstalled</code> value of
110 * {@link android.R.attr#protectionLevel}.
111 */
112 public static final int PROTECTION_FLAG_PREINSTALLED = 0x400;
113
114 /**
Dianne Hackborne639da72012-02-21 15:11:13 -0800115 * Mask for {@link #protectionLevel}: the basic protection type.
116 */
117 public static final int PROTECTION_MASK_BASE = 0xf;
118
119 /**
120 * Mask for {@link #protectionLevel}: additional flag bits.
121 */
Svetoslav3e7d9772015-07-06 18:31:23 -0700122 public static final int PROTECTION_MASK_FLAGS = 0xff0;
Dianne Hackborne639da72012-02-21 15:11:13 -0800123
124 /**
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700125 * The level of access this permission is protecting, as per
126 * {@link android.R.attr#protectionLevel}. Values may be
127 * {@link #PROTECTION_NORMAL}, {@link #PROTECTION_DANGEROUS}, or
128 * {@link #PROTECTION_SIGNATURE}. May also include the additional
129 * flags {@link #PROTECTION_FLAG_SYSTEM} or {@link #PROTECTION_FLAG_DEVELOPMENT}
130 * (which only make sense in combination with the base
131 * {@link #PROTECTION_SIGNATURE}.
132 */
133 public int protectionLevel;
134
135 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 * The group this permission is a part of, as per
137 * {@link android.R.attr#permissionGroup}.
138 */
139 public String group;
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700140
141 /**
142 * Flag for {@link #flags}, corresponding to <code>costsMoney</code>
143 * value of {@link android.R.attr#permissionFlags}.
144 */
145 public static final int FLAG_COSTS_MONEY = 1<<0;
146
147 /**
Svet Ganov3e0be742015-08-07 23:06:00 -0700148 * Flag for {@link #flags}, corresponding to <code>hidden</code>
149 * value of {@link android.R.attr#permissionFlags}.
150 * @hide
151 */
152 public static final int FLAG_HIDDEN = 1<<1;
153
154 /**
Dianne Hackborncfbfafe2015-07-21 16:57:51 -0700155 * Flag for {@link #flags}, indicating that this permission has been
156 * installed into the system's globally defined permissions.
Svetoslav Ganov6d2c0e52015-06-23 16:33:36 +0000157 */
Dianne Hackborncfbfafe2015-07-21 16:57:51 -0700158 public static final int FLAG_INSTALLED = 1<<30;
Svetoslav Ganov6d2c0e52015-06-23 16:33:36 +0000159
160 /**
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700161 * Additional flags about this permission as given by
162 * {@link android.R.attr#permissionFlags}.
163 */
164 public int flags;
165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 /**
167 * A string resource identifier (in the package's resources) of this
168 * permission's description. From the "description" attribute or,
169 * if not set, 0.
170 */
171 public int descriptionRes;
172
173 /**
174 * The description string provided in the AndroidManifest file, if any. You
175 * probably don't want to use this, since it will be null if the description
176 * is in a resource. You probably want
177 * {@link PermissionInfo#loadDescription} instead.
178 */
179 public CharSequence nonLocalizedDescription;
180
Dianne Hackborne639da72012-02-21 15:11:13 -0800181 /** @hide */
182 public static int fixProtectionLevel(int level) {
183 if (level == PROTECTION_SIGNATURE_OR_SYSTEM) {
Dianne Hackborna90c8de2015-07-07 17:25:25 -0700184 level = PROTECTION_SIGNATURE | PROTECTION_FLAG_PRIVILEGED;
Dianne Hackborne639da72012-02-21 15:11:13 -0800185 }
186 return level;
187 }
188
189 /** @hide */
190 public static String protectionToString(int level) {
191 String protLevel = "????";
192 switch (level&PROTECTION_MASK_BASE) {
193 case PermissionInfo.PROTECTION_DANGEROUS:
194 protLevel = "dangerous";
195 break;
196 case PermissionInfo.PROTECTION_NORMAL:
197 protLevel = "normal";
198 break;
199 case PermissionInfo.PROTECTION_SIGNATURE:
200 protLevel = "signature";
201 break;
202 case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM:
203 protLevel = "signatureOrSystem";
204 break;
205 }
Dianne Hackborna90c8de2015-07-07 17:25:25 -0700206 if ((level&PermissionInfo.PROTECTION_FLAG_PRIVILEGED) != 0) {
207 protLevel += "|privileged";
Dianne Hackborne639da72012-02-21 15:11:13 -0800208 }
209 if ((level&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) {
210 protLevel += "|development";
211 }
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -0700212 if ((level&PermissionInfo.PROTECTION_FLAG_APPOP) != 0) {
213 protLevel += "|appop";
214 }
Dianne Hackbornde15eda2015-07-01 12:30:54 -0700215 if ((level&PermissionInfo.PROTECTION_FLAG_PRE23) != 0) {
216 protLevel += "|pre23";
217 }
Dianne Hackborncfbfafe2015-07-21 16:57:51 -0700218 if ((level&PermissionInfo.PROTECTION_FLAG_INSTALLER) != 0) {
219 protLevel += "|installer";
220 }
221 if ((level&PermissionInfo.PROTECTION_FLAG_VERIFIER) != 0) {
222 protLevel += "|verifier";
223 }
224 if ((level&PermissionInfo.PROTECTION_FLAG_PREINSTALLED) != 0) {
225 protLevel += "|preinstalled";
226 }
Dianne Hackborne639da72012-02-21 15:11:13 -0800227 return protLevel;
228 }
229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 public PermissionInfo() {
231 }
232
233 public PermissionInfo(PermissionInfo orig) {
234 super(orig);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700235 protectionLevel = orig.protectionLevel;
236 flags = orig.flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 group = orig.group;
238 descriptionRes = orig.descriptionRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 nonLocalizedDescription = orig.nonLocalizedDescription;
240 }
241
242 /**
243 * Retrieve the textual description of this permission. This
244 * will call back on the given PackageManager to load the description from
245 * the application.
246 *
247 * @param pm A PackageManager from which the label can be loaded; usually
248 * the PackageManager from which you originally retrieved this item.
249 *
250 * @return Returns a CharSequence containing the permission's description.
251 * If there is no description, null is returned.
252 */
253 public CharSequence loadDescription(PackageManager pm) {
254 if (nonLocalizedDescription != null) {
255 return nonLocalizedDescription;
256 }
257 if (descriptionRes != 0) {
258 CharSequence label = pm.getText(packageName, descriptionRes, null);
259 if (label != null) {
260 return label;
261 }
262 }
263 return null;
264 }
265
266 public String toString() {
267 return "PermissionInfo{"
268 + Integer.toHexString(System.identityHashCode(this))
269 + " " + name + "}";
270 }
271
272 public int describeContents() {
273 return 0;
274 }
275
276 public void writeToParcel(Parcel dest, int parcelableFlags) {
277 super.writeToParcel(dest, parcelableFlags);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700278 dest.writeInt(protectionLevel);
279 dest.writeInt(flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 dest.writeString(group);
281 dest.writeInt(descriptionRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags);
283 }
284
285 public static final Creator<PermissionInfo> CREATOR =
286 new Creator<PermissionInfo>() {
287 public PermissionInfo createFromParcel(Parcel source) {
288 return new PermissionInfo(source);
289 }
290 public PermissionInfo[] newArray(int size) {
291 return new PermissionInfo[size];
292 }
293 };
294
295 private PermissionInfo(Parcel source) {
296 super(source);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700297 protectionLevel = source.readInt();
298 flags = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 group = source.readString();
300 descriptionRes = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
302 }
303}