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.annotation.SystemApi;
20 import android.annotation.TestApi;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import 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  */
30 public 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     /**
53      * @deprecated Use {@link #PROTECTION_SIGNATURE}|{@link #PROTECTION_FLAG_PRIVILEGED}
54      * instead.
55      */
56     @Deprecated
57     public static final int PROTECTION_SIGNATURE_OR_SYSTEM = 3;
58 
59     /**
60      * Additional flag for {@link #protectionLevel}, corresponding
61      * to the <code>privileged</code> value of
62      * {@link android.R.attr#protectionLevel}.
63      */
64     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
72     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     /**
82      * Additional flag for {@link #protectionLevel}, corresponding
83      * to the <code>appop</code> value of
84      * {@link android.R.attr#protectionLevel}.
85      */
86     public static final int PROTECTION_FLAG_APPOP = 0x40;
87 
88     /**
89      * 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     /**
96      * 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     /**
110      * 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     /**
117      * 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 
123     /**
124      * Additional flag for {@link #protectionLevel}, corresponding
125      * to the <code>ephemeral</code> value of
126      * {@link android.R.attr#protectionLevel}.
127      * @hide
128      */
129     @SystemApi
130     @TestApi
131     public static final int PROTECTION_FLAG_EPHEMERAL = 0x1000;
132 
133     /**
134      * Additional flag for {@link #protectionLevel}, corresponding
135      * to the <code>runtime</code> value of
136      * {@link android.R.attr#protectionLevel}.
137      */
138     public static final int PROTECTION_FLAG_RUNTIME_ONLY = 0x2000;
139 
140     /**
141      * Mask for {@link #protectionLevel}: the basic protection type.
142      */
143     public static final int PROTECTION_MASK_BASE = 0xf;
144 
145     /**
146      * Mask for {@link #protectionLevel}: additional flag bits.
147      */
148     public static final int PROTECTION_MASK_FLAGS = 0xfff0;
149 
150     /**
151      * The level of access this permission is protecting, as per
152      * {@link android.R.attr#protectionLevel}.  Values may be
153      * {@link #PROTECTION_NORMAL}, {@link #PROTECTION_DANGEROUS}, or
154      * {@link #PROTECTION_SIGNATURE}.  May also include the additional
155      * flags {@link #PROTECTION_FLAG_SYSTEM} or {@link #PROTECTION_FLAG_DEVELOPMENT}
156      * (which only make sense in combination with the base
157      * {@link #PROTECTION_SIGNATURE}.
158      */
159     public int protectionLevel;
160 
161     /**
162      * The group this permission is a part of, as per
163      * {@link android.R.attr#permissionGroup}.
164      */
165     public String group;
166 
167     /**
168      * Flag for {@link #flags}, corresponding to <code>costsMoney</code>
169      * value of {@link android.R.attr#permissionFlags}.
170      */
171     public static final int FLAG_COSTS_MONEY = 1<<0;
172 
173     /**
174      * Flag for {@link #flags}, corresponding to <code>removed</code>
175      * value of {@link android.R.attr#permissionFlags}.
176      * @hide
177      */
178     @SystemApi
179     public static final int FLAG_REMOVED = 1<<1;
180 
181     /**
182      * Flag for {@link #flags}, indicating that this permission has been
183      * installed into the system's globally defined permissions.
184      */
185     public static final int FLAG_INSTALLED = 1<<30;
186 
187     /**
188      * Additional flags about this permission as given by
189      * {@link android.R.attr#permissionFlags}.
190      */
191     public int flags;
192 
193     /**
194      * A string resource identifier (in the package's resources) of this
195      * permission's description.  From the "description" attribute or,
196      * if not set, 0.
197      */
198     public int descriptionRes;
199 
200     /**
201      * The description string provided in the AndroidManifest file, if any.  You
202      * probably don't want to use this, since it will be null if the description
203      * is in a resource.  You probably want
204      * {@link PermissionInfo#loadDescription} instead.
205      */
206     public CharSequence nonLocalizedDescription;
207 
208     /** @hide */
fixProtectionLevel(int level)209     public static int fixProtectionLevel(int level) {
210         if (level == PROTECTION_SIGNATURE_OR_SYSTEM) {
211             level = PROTECTION_SIGNATURE | PROTECTION_FLAG_PRIVILEGED;
212         }
213         return level;
214     }
215 
216     /** @hide */
protectionToString(int level)217     public static String protectionToString(int level) {
218         String protLevel = "????";
219         switch (level&PROTECTION_MASK_BASE) {
220             case PermissionInfo.PROTECTION_DANGEROUS:
221                 protLevel = "dangerous";
222                 break;
223             case PermissionInfo.PROTECTION_NORMAL:
224                 protLevel = "normal";
225                 break;
226             case PermissionInfo.PROTECTION_SIGNATURE:
227                 protLevel = "signature";
228                 break;
229             case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM:
230                 protLevel = "signatureOrSystem";
231                 break;
232         }
233         if ((level&PermissionInfo.PROTECTION_FLAG_PRIVILEGED) != 0) {
234             protLevel += "|privileged";
235         }
236         if ((level&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) {
237             protLevel += "|development";
238         }
239         if ((level&PermissionInfo.PROTECTION_FLAG_APPOP) != 0) {
240             protLevel += "|appop";
241         }
242         if ((level&PermissionInfo.PROTECTION_FLAG_PRE23) != 0) {
243             protLevel += "|pre23";
244         }
245         if ((level&PermissionInfo.PROTECTION_FLAG_INSTALLER) != 0) {
246             protLevel += "|installer";
247         }
248         if ((level&PermissionInfo.PROTECTION_FLAG_VERIFIER) != 0) {
249             protLevel += "|verifier";
250         }
251         if ((level&PermissionInfo.PROTECTION_FLAG_PREINSTALLED) != 0) {
252             protLevel += "|preinstalled";
253         }
254         if ((level&PermissionInfo.PROTECTION_FLAG_SETUP) != 0) {
255             protLevel += "|setup";
256         }
257         if ((level&PermissionInfo.PROTECTION_FLAG_EPHEMERAL) != 0) {
258             protLevel += "|ephemeral";
259         }
260         if ((level&PermissionInfo.PROTECTION_FLAG_RUNTIME_ONLY) != 0) {
261             protLevel += "|runtime";
262         }
263         return protLevel;
264     }
265 
PermissionInfo()266     public PermissionInfo() {
267     }
268 
PermissionInfo(PermissionInfo orig)269     public PermissionInfo(PermissionInfo orig) {
270         super(orig);
271         protectionLevel = orig.protectionLevel;
272         flags = orig.flags;
273         group = orig.group;
274         descriptionRes = orig.descriptionRes;
275         nonLocalizedDescription = orig.nonLocalizedDescription;
276     }
277 
278     /**
279      * Retrieve the textual description of this permission.  This
280      * will call back on the given PackageManager to load the description from
281      * the application.
282      *
283      * @param pm A PackageManager from which the label can be loaded; usually
284      * the PackageManager from which you originally retrieved this item.
285      *
286      * @return Returns a CharSequence containing the permission's description.
287      * If there is no description, null is returned.
288      */
loadDescription(PackageManager pm)289     public CharSequence loadDescription(PackageManager pm) {
290         if (nonLocalizedDescription != null) {
291             return nonLocalizedDescription;
292         }
293         if (descriptionRes != 0) {
294             CharSequence label = pm.getText(packageName, descriptionRes, null);
295             if (label != null) {
296                 return label;
297             }
298         }
299         return null;
300     }
301 
toString()302     public String toString() {
303         return "PermissionInfo{"
304             + Integer.toHexString(System.identityHashCode(this))
305             + " " + name + "}";
306     }
307 
describeContents()308     public int describeContents() {
309         return 0;
310     }
311 
writeToParcel(Parcel dest, int parcelableFlags)312     public void writeToParcel(Parcel dest, int parcelableFlags) {
313         super.writeToParcel(dest, parcelableFlags);
314         dest.writeInt(protectionLevel);
315         dest.writeInt(flags);
316         dest.writeString(group);
317         dest.writeInt(descriptionRes);
318         TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags);
319     }
320 
321     public static final Creator<PermissionInfo> CREATOR =
322         new Creator<PermissionInfo>() {
323         public PermissionInfo createFromParcel(Parcel source) {
324             return new PermissionInfo(source);
325         }
326         public PermissionInfo[] newArray(int size) {
327             return new PermissionInfo[size];
328         }
329     };
330 
PermissionInfo(Parcel source)331     private PermissionInfo(Parcel source) {
332         super(source);
333         protectionLevel = source.readInt();
334         flags = source.readInt();
335         group = source.readString();
336         descriptionRes = source.readInt();
337         nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
338     }
339 }
340