blob: b84c1b93068d1bc1ed8c4d003ea988c0442eeb81 [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
Svet Ganov2a1376d2016-02-22 17:20:35 -080019import android.annotation.SystemApi;
Chad Brubakerc19706a2016-10-13 15:44:59 -070020import android.annotation.TestApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.os.Parcel;
22import android.os.Parcelable;
23import android.text.TextUtils;
24
25/**
26 * Information you can retrieve about a particular security permission
27 * known to the system. This corresponds to information collected from the
28 * AndroidManifest.xml's <permission> tags.
29 */
30public class PermissionInfo extends PackageItemInfo implements Parcelable {
31 /**
32 * A normal application value for {@link #protectionLevel}, corresponding
33 * to the <code>normal</code> value of
34 * {@link android.R.attr#protectionLevel}.
35 */
36 public static final int PROTECTION_NORMAL = 0;
37
38 /**
39 * Dangerous value for {@link #protectionLevel}, corresponding
40 * to the <code>dangerous</code> value of
41 * {@link android.R.attr#protectionLevel}.
42 */
43 public static final int PROTECTION_DANGEROUS = 1;
44
45 /**
46 * System-level value for {@link #protectionLevel}, corresponding
47 * to the <code>signature</code> value of
48 * {@link android.R.attr#protectionLevel}.
49 */
50 public static final int PROTECTION_SIGNATURE = 2;
51
52 /**
Dianne Hackborna90c8de2015-07-07 17:25:25 -070053 * @deprecated Use {@link #PROTECTION_SIGNATURE}|{@link #PROTECTION_FLAG_PRIVILEGED}
54 * instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 */
Dianne Hackborna90c8de2015-07-07 17:25:25 -070056 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 public static final int PROTECTION_SIGNATURE_OR_SYSTEM = 3;
58
59 /**
Dianne Hackborne639da72012-02-21 15:11:13 -080060 * Additional flag for {@link #protectionLevel}, corresponding
Dianne Hackborna90c8de2015-07-07 17:25:25 -070061 * to the <code>privileged</code> value of
Dianne Hackborne639da72012-02-21 15:11:13 -080062 * {@link android.R.attr#protectionLevel}.
63 */
Dianne Hackborna90c8de2015-07-07 17:25:25 -070064 public static final int PROTECTION_FLAG_PRIVILEGED = 0x10;
65
66 /**
67 * @deprecated Old name for {@link #PROTECTION_FLAG_PRIVILEGED}, which
68 * is now very confusing because it only applies to privileged apps, not all
69 * apps on the system image.
70 */
71 @Deprecated
Dianne Hackborne639da72012-02-21 15:11:13 -080072 public static final int PROTECTION_FLAG_SYSTEM = 0x10;
73
74 /**
75 * Additional flag for {@link #protectionLevel}, corresponding
76 * to the <code>development</code> value of
77 * {@link android.R.attr#protectionLevel}.
78 */
79 public static final int PROTECTION_FLAG_DEVELOPMENT = 0x20;
80
81 /**
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -070082 * Additional flag for {@link #protectionLevel}, corresponding
Jeff Sharkeye9b78fd2014-08-13 14:16:50 -070083 * to the <code>appop</code> value of
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -070084 * {@link android.R.attr#protectionLevel}.
85 */
86 public static final int PROTECTION_FLAG_APPOP = 0x40;
87
88 /**
Dianne Hackbornde15eda2015-07-01 12:30:54 -070089 * Additional flag for {@link #protectionLevel}, corresponding
90 * to the <code>pre23</code> value of
91 * {@link android.R.attr#protectionLevel}.
92 */
93 public static final int PROTECTION_FLAG_PRE23 = 0x80;
94
95 /**
Svetoslav3e7d9772015-07-06 18:31:23 -070096 * Additional flag for {@link #protectionLevel}, corresponding
97 * to the <code>installer</code> value of
98 * {@link android.R.attr#protectionLevel}.
99 */
100 public static final int PROTECTION_FLAG_INSTALLER = 0x100;
101
102 /**
103 * Additional flag for {@link #protectionLevel}, corresponding
104 * to the <code>verifier</code> value of
105 * {@link android.R.attr#protectionLevel}.
106 */
107 public static final int PROTECTION_FLAG_VERIFIER = 0x200;
108
109 /**
Dianne Hackborna90c8de2015-07-07 17:25:25 -0700110 * Additional flag for {@link #protectionLevel}, corresponding
111 * to the <code>preinstalled</code> value of
112 * {@link android.R.attr#protectionLevel}.
113 */
114 public static final int PROTECTION_FLAG_PREINSTALLED = 0x400;
115
116 /**
Russell Brennerb2334662016-03-23 10:16:39 -0700117 * Additional flag for {@link #protectionLevel}, corresponding
118 * to the <code>setup</code> value of
119 * {@link android.R.attr#protectionLevel}.
120 */
121 public static final int PROTECTION_FLAG_SETUP = 0x800;
122
Chad Brubakerc19706a2016-10-13 15:44:59 -0700123 /**
124 * Additional flag for {@link #protectionLevel}, corresponding
Todd Kennedyc247fa12017-06-02 10:29:22 -0700125 * to the <code>instant</code> value of
Chad Brubakerc19706a2016-10-13 15:44:59 -0700126 * {@link android.R.attr#protectionLevel}.
Chad Brubakerc19706a2016-10-13 15:44:59 -0700127 */
Todd Kennedyc247fa12017-06-02 10:29:22 -0700128 public static final int PROTECTION_FLAG_INSTANT = 0x1000;
Chad Brubakerc19706a2016-10-13 15:44:59 -0700129
Russell Brennerb2334662016-03-23 10:16:39 -0700130 /**
Chad Brubakera5d70a12017-03-23 11:04:50 -0700131 * Additional flag for {@link #protectionLevel}, corresponding
132 * to the <code>runtime</code> value of
133 * {@link android.R.attr#protectionLevel}.
134 */
135 public static final int PROTECTION_FLAG_RUNTIME_ONLY = 0x2000;
136
137 /**
Dianne Hackborne639da72012-02-21 15:11:13 -0800138 * Mask for {@link #protectionLevel}: the basic protection type.
139 */
140 public static final int PROTECTION_MASK_BASE = 0xf;
141
142 /**
143 * Mask for {@link #protectionLevel}: additional flag bits.
144 */
Chad Brubakerc19706a2016-10-13 15:44:59 -0700145 public static final int PROTECTION_MASK_FLAGS = 0xfff0;
Dianne Hackborne639da72012-02-21 15:11:13 -0800146
147 /**
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700148 * The level of access this permission is protecting, as per
149 * {@link android.R.attr#protectionLevel}. Values may be
150 * {@link #PROTECTION_NORMAL}, {@link #PROTECTION_DANGEROUS}, or
151 * {@link #PROTECTION_SIGNATURE}. May also include the additional
152 * flags {@link #PROTECTION_FLAG_SYSTEM} or {@link #PROTECTION_FLAG_DEVELOPMENT}
153 * (which only make sense in combination with the base
154 * {@link #PROTECTION_SIGNATURE}.
155 */
156 public int protectionLevel;
157
158 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 * The group this permission is a part of, as per
160 * {@link android.R.attr#permissionGroup}.
161 */
162 public String group;
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700163
164 /**
165 * Flag for {@link #flags}, corresponding to <code>costsMoney</code>
166 * value of {@link android.R.attr#permissionFlags}.
167 */
168 public static final int FLAG_COSTS_MONEY = 1<<0;
169
170 /**
Svet Ganov2a1376d2016-02-22 17:20:35 -0800171 * Flag for {@link #flags}, corresponding to <code>removed</code>
Svet Ganov3e0be742015-08-07 23:06:00 -0700172 * value of {@link android.R.attr#permissionFlags}.
173 * @hide
174 */
Svet Ganov2a1376d2016-02-22 17:20:35 -0800175 @SystemApi
176 public static final int FLAG_REMOVED = 1<<1;
Svet Ganov3e0be742015-08-07 23:06:00 -0700177
178 /**
Dianne Hackborncfbfafe2015-07-21 16:57:51 -0700179 * Flag for {@link #flags}, indicating that this permission has been
180 * installed into the system's globally defined permissions.
Svetoslav Ganov6d2c0e52015-06-23 16:33:36 +0000181 */
Dianne Hackborncfbfafe2015-07-21 16:57:51 -0700182 public static final int FLAG_INSTALLED = 1<<30;
Svetoslav Ganov6d2c0e52015-06-23 16:33:36 +0000183
184 /**
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700185 * Additional flags about this permission as given by
186 * {@link android.R.attr#permissionFlags}.
187 */
188 public int flags;
189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 /**
191 * A string resource identifier (in the package's resources) of this
192 * permission's description. From the "description" attribute or,
193 * if not set, 0.
194 */
195 public int descriptionRes;
196
197 /**
Philip P. Moltmann27ffeb32017-07-06 13:54:46 -0700198 * A string resource identifier (in the package's resources) used to request the permissions.
199 * From the "request" attribute or, if not set, 0.
200 *
201 * @hide
202 */
203 @SystemApi
204 public int requestRes;
205
206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * The description string provided in the AndroidManifest file, if any. You
208 * probably don't want to use this, since it will be null if the description
209 * is in a resource. You probably want
210 * {@link PermissionInfo#loadDescription} instead.
211 */
212 public CharSequence nonLocalizedDescription;
213
Dianne Hackborne639da72012-02-21 15:11:13 -0800214 /** @hide */
215 public static int fixProtectionLevel(int level) {
216 if (level == PROTECTION_SIGNATURE_OR_SYSTEM) {
Dianne Hackborna90c8de2015-07-07 17:25:25 -0700217 level = PROTECTION_SIGNATURE | PROTECTION_FLAG_PRIVILEGED;
Dianne Hackborne639da72012-02-21 15:11:13 -0800218 }
219 return level;
220 }
221
222 /** @hide */
223 public static String protectionToString(int level) {
224 String protLevel = "????";
225 switch (level&PROTECTION_MASK_BASE) {
226 case PermissionInfo.PROTECTION_DANGEROUS:
227 protLevel = "dangerous";
228 break;
229 case PermissionInfo.PROTECTION_NORMAL:
230 protLevel = "normal";
231 break;
232 case PermissionInfo.PROTECTION_SIGNATURE:
233 protLevel = "signature";
234 break;
235 case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM:
236 protLevel = "signatureOrSystem";
237 break;
238 }
Dianne Hackborna90c8de2015-07-07 17:25:25 -0700239 if ((level&PermissionInfo.PROTECTION_FLAG_PRIVILEGED) != 0) {
240 protLevel += "|privileged";
Dianne Hackborne639da72012-02-21 15:11:13 -0800241 }
242 if ((level&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) {
243 protLevel += "|development";
244 }
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -0700245 if ((level&PermissionInfo.PROTECTION_FLAG_APPOP) != 0) {
246 protLevel += "|appop";
247 }
Dianne Hackbornde15eda2015-07-01 12:30:54 -0700248 if ((level&PermissionInfo.PROTECTION_FLAG_PRE23) != 0) {
249 protLevel += "|pre23";
250 }
Dianne Hackborncfbfafe2015-07-21 16:57:51 -0700251 if ((level&PermissionInfo.PROTECTION_FLAG_INSTALLER) != 0) {
252 protLevel += "|installer";
253 }
254 if ((level&PermissionInfo.PROTECTION_FLAG_VERIFIER) != 0) {
255 protLevel += "|verifier";
256 }
257 if ((level&PermissionInfo.PROTECTION_FLAG_PREINSTALLED) != 0) {
258 protLevel += "|preinstalled";
259 }
Russell Brennerb2334662016-03-23 10:16:39 -0700260 if ((level&PermissionInfo.PROTECTION_FLAG_SETUP) != 0) {
261 protLevel += "|setup";
262 }
Todd Kennedyc247fa12017-06-02 10:29:22 -0700263 if ((level&PermissionInfo.PROTECTION_FLAG_INSTANT) != 0) {
264 protLevel += "|instant";
Chad Brubakerc19706a2016-10-13 15:44:59 -0700265 }
Chad Brubakera5d70a12017-03-23 11:04:50 -0700266 if ((level&PermissionInfo.PROTECTION_FLAG_RUNTIME_ONLY) != 0) {
267 protLevel += "|runtime";
268 }
Dianne Hackborne639da72012-02-21 15:11:13 -0800269 return protLevel;
270 }
271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 public PermissionInfo() {
273 }
274
275 public PermissionInfo(PermissionInfo orig) {
276 super(orig);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700277 protectionLevel = orig.protectionLevel;
278 flags = orig.flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 group = orig.group;
280 descriptionRes = orig.descriptionRes;
Philip P. Moltmann27ffeb32017-07-06 13:54:46 -0700281 requestRes = orig.requestRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 nonLocalizedDescription = orig.nonLocalizedDescription;
283 }
284
285 /**
286 * Retrieve the textual description of this permission. This
287 * will call back on the given PackageManager to load the description from
288 * the application.
289 *
290 * @param pm A PackageManager from which the label can be loaded; usually
291 * the PackageManager from which you originally retrieved this item.
292 *
293 * @return Returns a CharSequence containing the permission's description.
294 * If there is no description, null is returned.
295 */
296 public CharSequence loadDescription(PackageManager pm) {
297 if (nonLocalizedDescription != null) {
298 return nonLocalizedDescription;
299 }
300 if (descriptionRes != 0) {
301 CharSequence label = pm.getText(packageName, descriptionRes, null);
302 if (label != null) {
303 return label;
304 }
305 }
306 return null;
307 }
308
309 public String toString() {
310 return "PermissionInfo{"
311 + Integer.toHexString(System.identityHashCode(this))
312 + " " + name + "}";
313 }
314
315 public int describeContents() {
316 return 0;
317 }
318
319 public void writeToParcel(Parcel dest, int parcelableFlags) {
320 super.writeToParcel(dest, parcelableFlags);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700321 dest.writeInt(protectionLevel);
322 dest.writeInt(flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 dest.writeString(group);
324 dest.writeInt(descriptionRes);
Philip P. Moltmann27ffeb32017-07-06 13:54:46 -0700325 dest.writeInt(requestRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags);
327 }
328
329 public static final Creator<PermissionInfo> CREATOR =
330 new Creator<PermissionInfo>() {
331 public PermissionInfo createFromParcel(Parcel source) {
332 return new PermissionInfo(source);
333 }
334 public PermissionInfo[] newArray(int size) {
335 return new PermissionInfo[size];
336 }
337 };
338
339 private PermissionInfo(Parcel source) {
340 super(source);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700341 protectionLevel = source.readInt();
342 flags = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 group = source.readString();
344 descriptionRes = source.readInt();
Philip P. Moltmann27ffeb32017-07-06 13:54:46 -0700345 requestRes = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
347 }
348}