blob: 984a960b5308e9ddf781f66c034f766b59392987 [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.os.Parcel;
21import android.os.Parcelable;
22import android.text.TextUtils;
23
24/**
25 * Information you can retrieve about a particular security permission
26 * known to the system. This corresponds to information collected from the
27 * AndroidManifest.xml's <permission> tags.
28 */
29public class PermissionInfo extends PackageItemInfo implements Parcelable {
30 /**
31 * A normal application value for {@link #protectionLevel}, corresponding
32 * to the <code>normal</code> value of
33 * {@link android.R.attr#protectionLevel}.
34 */
35 public static final int PROTECTION_NORMAL = 0;
36
37 /**
38 * Dangerous value for {@link #protectionLevel}, corresponding
39 * to the <code>dangerous</code> value of
40 * {@link android.R.attr#protectionLevel}.
41 */
42 public static final int PROTECTION_DANGEROUS = 1;
43
44 /**
45 * System-level value for {@link #protectionLevel}, corresponding
46 * to the <code>signature</code> value of
47 * {@link android.R.attr#protectionLevel}.
48 */
49 public static final int PROTECTION_SIGNATURE = 2;
50
51 /**
Dianne Hackborna90c8de2015-07-07 17:25:25 -070052 * @deprecated Use {@link #PROTECTION_SIGNATURE}|{@link #PROTECTION_FLAG_PRIVILEGED}
53 * instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 */
Dianne Hackborna90c8de2015-07-07 17:25:25 -070055 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 public static final int PROTECTION_SIGNATURE_OR_SYSTEM = 3;
57
58 /**
Dianne Hackborne639da72012-02-21 15:11:13 -080059 * Additional flag for {@link #protectionLevel}, corresponding
Dianne Hackborna90c8de2015-07-07 17:25:25 -070060 * to the <code>privileged</code> value of
Dianne Hackborne639da72012-02-21 15:11:13 -080061 * {@link android.R.attr#protectionLevel}.
62 */
Dianne Hackborna90c8de2015-07-07 17:25:25 -070063 public static final int PROTECTION_FLAG_PRIVILEGED = 0x10;
64
65 /**
66 * @deprecated Old name for {@link #PROTECTION_FLAG_PRIVILEGED}, which
67 * is now very confusing because it only applies to privileged apps, not all
68 * apps on the system image.
69 */
70 @Deprecated
Dianne Hackborne639da72012-02-21 15:11:13 -080071 public static final int PROTECTION_FLAG_SYSTEM = 0x10;
72
73 /**
74 * Additional flag for {@link #protectionLevel}, corresponding
75 * to the <code>development</code> value of
76 * {@link android.R.attr#protectionLevel}.
77 */
78 public static final int PROTECTION_FLAG_DEVELOPMENT = 0x20;
79
80 /**
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -070081 * Additional flag for {@link #protectionLevel}, corresponding
Jeff Sharkeye9b78fd2014-08-13 14:16:50 -070082 * to the <code>appop</code> value of
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -070083 * {@link android.R.attr#protectionLevel}.
84 */
85 public static final int PROTECTION_FLAG_APPOP = 0x40;
86
87 /**
Dianne Hackbornde15eda2015-07-01 12:30:54 -070088 * Additional flag for {@link #protectionLevel}, corresponding
89 * to the <code>pre23</code> value of
90 * {@link android.R.attr#protectionLevel}.
91 */
92 public static final int PROTECTION_FLAG_PRE23 = 0x80;
93
94 /**
Svetoslav3e7d9772015-07-06 18:31:23 -070095 * Additional flag for {@link #protectionLevel}, corresponding
96 * to the <code>installer</code> value of
97 * {@link android.R.attr#protectionLevel}.
98 */
99 public static final int PROTECTION_FLAG_INSTALLER = 0x100;
100
101 /**
102 * Additional flag for {@link #protectionLevel}, corresponding
103 * to the <code>verifier</code> value of
104 * {@link android.R.attr#protectionLevel}.
105 */
106 public static final int PROTECTION_FLAG_VERIFIER = 0x200;
107
108 /**
Dianne Hackborna90c8de2015-07-07 17:25:25 -0700109 * Additional flag for {@link #protectionLevel}, corresponding
110 * to the <code>preinstalled</code> value of
111 * {@link android.R.attr#protectionLevel}.
112 */
113 public static final int PROTECTION_FLAG_PREINSTALLED = 0x400;
114
115 /**
Dianne Hackborne639da72012-02-21 15:11:13 -0800116 * Mask for {@link #protectionLevel}: the basic protection type.
117 */
118 public static final int PROTECTION_MASK_BASE = 0xf;
119
120 /**
121 * Mask for {@link #protectionLevel}: additional flag bits.
122 */
Svetoslav3e7d9772015-07-06 18:31:23 -0700123 public static final int PROTECTION_MASK_FLAGS = 0xff0;
Dianne Hackborne639da72012-02-21 15:11:13 -0800124
125 /**
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700126 * The level of access this permission is protecting, as per
127 * {@link android.R.attr#protectionLevel}. Values may be
128 * {@link #PROTECTION_NORMAL}, {@link #PROTECTION_DANGEROUS}, or
129 * {@link #PROTECTION_SIGNATURE}. May also include the additional
130 * flags {@link #PROTECTION_FLAG_SYSTEM} or {@link #PROTECTION_FLAG_DEVELOPMENT}
131 * (which only make sense in combination with the base
132 * {@link #PROTECTION_SIGNATURE}.
133 */
134 public int protectionLevel;
135
136 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 * The group this permission is a part of, as per
138 * {@link android.R.attr#permissionGroup}.
139 */
140 public String group;
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700141
142 /**
143 * Flag for {@link #flags}, corresponding to <code>costsMoney</code>
144 * value of {@link android.R.attr#permissionFlags}.
145 */
146 public static final int FLAG_COSTS_MONEY = 1<<0;
147
148 /**
Svet Ganov2a1376d2016-02-22 17:20:35 -0800149 * Flag for {@link #flags}, corresponding to <code>removed</code>
Svet Ganov3e0be742015-08-07 23:06:00 -0700150 * value of {@link android.R.attr#permissionFlags}.
151 * @hide
152 */
Svet Ganov2a1376d2016-02-22 17:20:35 -0800153 @SystemApi
154 public static final int FLAG_REMOVED = 1<<1;
Svet Ganov3e0be742015-08-07 23:06:00 -0700155
156 /**
Dianne Hackborncfbfafe2015-07-21 16:57:51 -0700157 * Flag for {@link #flags}, indicating that this permission has been
158 * installed into the system's globally defined permissions.
Svetoslav Ganov6d2c0e52015-06-23 16:33:36 +0000159 */
Dianne Hackborncfbfafe2015-07-21 16:57:51 -0700160 public static final int FLAG_INSTALLED = 1<<30;
Svetoslav Ganov6d2c0e52015-06-23 16:33:36 +0000161
162 /**
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700163 * Additional flags about this permission as given by
164 * {@link android.R.attr#permissionFlags}.
165 */
166 public int flags;
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /**
169 * A string resource identifier (in the package's resources) of this
170 * permission's description. From the "description" attribute or,
171 * if not set, 0.
172 */
173 public int descriptionRes;
174
175 /**
176 * The description string provided in the AndroidManifest file, if any. You
177 * probably don't want to use this, since it will be null if the description
178 * is in a resource. You probably want
179 * {@link PermissionInfo#loadDescription} instead.
180 */
181 public CharSequence nonLocalizedDescription;
182
Dianne Hackborne639da72012-02-21 15:11:13 -0800183 /** @hide */
184 public static int fixProtectionLevel(int level) {
185 if (level == PROTECTION_SIGNATURE_OR_SYSTEM) {
Dianne Hackborna90c8de2015-07-07 17:25:25 -0700186 level = PROTECTION_SIGNATURE | PROTECTION_FLAG_PRIVILEGED;
Dianne Hackborne639da72012-02-21 15:11:13 -0800187 }
188 return level;
189 }
190
191 /** @hide */
192 public static String protectionToString(int level) {
193 String protLevel = "????";
194 switch (level&PROTECTION_MASK_BASE) {
195 case PermissionInfo.PROTECTION_DANGEROUS:
196 protLevel = "dangerous";
197 break;
198 case PermissionInfo.PROTECTION_NORMAL:
199 protLevel = "normal";
200 break;
201 case PermissionInfo.PROTECTION_SIGNATURE:
202 protLevel = "signature";
203 break;
204 case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM:
205 protLevel = "signatureOrSystem";
206 break;
207 }
Dianne Hackborna90c8de2015-07-07 17:25:25 -0700208 if ((level&PermissionInfo.PROTECTION_FLAG_PRIVILEGED) != 0) {
209 protLevel += "|privileged";
Dianne Hackborne639da72012-02-21 15:11:13 -0800210 }
211 if ((level&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) {
212 protLevel += "|development";
213 }
Dianne Hackborn33f5ddd2014-07-21 15:35:45 -0700214 if ((level&PermissionInfo.PROTECTION_FLAG_APPOP) != 0) {
215 protLevel += "|appop";
216 }
Dianne Hackbornde15eda2015-07-01 12:30:54 -0700217 if ((level&PermissionInfo.PROTECTION_FLAG_PRE23) != 0) {
218 protLevel += "|pre23";
219 }
Dianne Hackborncfbfafe2015-07-21 16:57:51 -0700220 if ((level&PermissionInfo.PROTECTION_FLAG_INSTALLER) != 0) {
221 protLevel += "|installer";
222 }
223 if ((level&PermissionInfo.PROTECTION_FLAG_VERIFIER) != 0) {
224 protLevel += "|verifier";
225 }
226 if ((level&PermissionInfo.PROTECTION_FLAG_PREINSTALLED) != 0) {
227 protLevel += "|preinstalled";
228 }
Dianne Hackborne639da72012-02-21 15:11:13 -0800229 return protLevel;
230 }
231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 public PermissionInfo() {
233 }
234
235 public PermissionInfo(PermissionInfo orig) {
236 super(orig);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700237 protectionLevel = orig.protectionLevel;
238 flags = orig.flags;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 group = orig.group;
240 descriptionRes = orig.descriptionRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 nonLocalizedDescription = orig.nonLocalizedDescription;
242 }
243
244 /**
245 * Retrieve the textual description of this permission. This
246 * will call back on the given PackageManager to load the description from
247 * the application.
248 *
249 * @param pm A PackageManager from which the label can be loaded; usually
250 * the PackageManager from which you originally retrieved this item.
251 *
252 * @return Returns a CharSequence containing the permission's description.
253 * If there is no description, null is returned.
254 */
255 public CharSequence loadDescription(PackageManager pm) {
256 if (nonLocalizedDescription != null) {
257 return nonLocalizedDescription;
258 }
259 if (descriptionRes != 0) {
260 CharSequence label = pm.getText(packageName, descriptionRes, null);
261 if (label != null) {
262 return label;
263 }
264 }
265 return null;
266 }
267
268 public String toString() {
269 return "PermissionInfo{"
270 + Integer.toHexString(System.identityHashCode(this))
271 + " " + name + "}";
272 }
273
274 public int describeContents() {
275 return 0;
276 }
277
278 public void writeToParcel(Parcel dest, int parcelableFlags) {
279 super.writeToParcel(dest, parcelableFlags);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700280 dest.writeInt(protectionLevel);
281 dest.writeInt(flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 dest.writeString(group);
283 dest.writeInt(descriptionRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags);
285 }
286
287 public static final Creator<PermissionInfo> CREATOR =
288 new Creator<PermissionInfo>() {
289 public PermissionInfo createFromParcel(Parcel source) {
290 return new PermissionInfo(source);
291 }
292 public PermissionInfo[] newArray(int size) {
293 return new PermissionInfo[size];
294 }
295 };
296
297 private PermissionInfo(Parcel source) {
298 super(source);
Dianne Hackborn2ca2c872012-09-16 16:03:36 -0700299 protectionLevel = source.readInt();
300 flags = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 group = source.readString();
302 descriptionRes = source.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
304 }
305}