1 /*
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 
17 package android.content.pm;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import 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  */
28 public 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     /**
51      * @deprecated Use {@link #PROTECTION_SIGNATURE}|{@link #PROTECTION_FLAG_PRIVILEGED}
52      * instead.
53      */
54     @Deprecated
55     public static final int PROTECTION_SIGNATURE_OR_SYSTEM = 3;
56 
57     /**
58      * Additional flag for {@link #protectionLevel}, corresponding
59      * to the <code>privileged</code> value of
60      * {@link android.R.attr#protectionLevel}.
61      */
62     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
70     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     /**
80      * Additional flag for {@link #protectionLevel}, corresponding
81      * to the <code>appop</code> value of
82      * {@link android.R.attr#protectionLevel}.
83      */
84     public static final int PROTECTION_FLAG_APPOP = 0x40;
85 
86     /**
87      * 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     /**
94      * 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     /**
108      * 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     /**
115      * 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      */
122     public static final int PROTECTION_MASK_FLAGS = 0xff0;
123 
124     /**
125      * 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     /**
136      * The group this permission is a part of, as per
137      * {@link android.R.attr#permissionGroup}.
138      */
139     public String group;
140 
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     /**
148      * 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     /**
155      * Flag for {@link #flags}, indicating that this permission has been
156      * installed into the system's globally defined permissions.
157      */
158     public static final int FLAG_INSTALLED = 1<<30;
159 
160     /**
161      * Additional flags about this permission as given by
162      * {@link android.R.attr#permissionFlags}.
163      */
164     public int flags;
165 
166     /**
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 
181     /** @hide */
fixProtectionLevel(int level)182     public static int fixProtectionLevel(int level) {
183         if (level == PROTECTION_SIGNATURE_OR_SYSTEM) {
184             level = PROTECTION_SIGNATURE | PROTECTION_FLAG_PRIVILEGED;
185         }
186         return level;
187     }
188 
189     /** @hide */
protectionToString(int level)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         }
206         if ((level&PermissionInfo.PROTECTION_FLAG_PRIVILEGED) != 0) {
207             protLevel += "|privileged";
208         }
209         if ((level&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) {
210             protLevel += "|development";
211         }
212         if ((level&PermissionInfo.PROTECTION_FLAG_APPOP) != 0) {
213             protLevel += "|appop";
214         }
215         if ((level&PermissionInfo.PROTECTION_FLAG_PRE23) != 0) {
216             protLevel += "|pre23";
217         }
218         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         }
227         return protLevel;
228     }
229 
PermissionInfo()230     public PermissionInfo() {
231     }
232 
PermissionInfo(PermissionInfo orig)233     public PermissionInfo(PermissionInfo orig) {
234         super(orig);
235         protectionLevel = orig.protectionLevel;
236         flags = orig.flags;
237         group = orig.group;
238         descriptionRes = orig.descriptionRes;
239         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      */
loadDescription(PackageManager pm)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 
toString()266     public String toString() {
267         return "PermissionInfo{"
268             + Integer.toHexString(System.identityHashCode(this))
269             + " " + name + "}";
270     }
271 
describeContents()272     public int describeContents() {
273         return 0;
274     }
275 
writeToParcel(Parcel dest, int parcelableFlags)276     public void writeToParcel(Parcel dest, int parcelableFlags) {
277         super.writeToParcel(dest, parcelableFlags);
278         dest.writeInt(protectionLevel);
279         dest.writeInt(flags);
280         dest.writeString(group);
281         dest.writeInt(descriptionRes);
282         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 
PermissionInfo(Parcel source)295     private PermissionInfo(Parcel source) {
296         super(source);
297         protectionLevel = source.readInt();
298         flags = source.readInt();
299         group = source.readString();
300         descriptionRes = source.readInt();
301         nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
302     }
303 }
304