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      * System-level value for {@link #protectionLevel}, corresponding
52      * to the <code>signatureOrSystem</code> value of
53      * {@link android.R.attr#protectionLevel}.
54      */
55     public static final int PROTECTION_SIGNATURE_OR_SYSTEM = 3;
56 
57     /**
58      * Additional flag for {@link #protectionLevel}, corresponding
59      * to the <code>system</code> value of
60      * {@link android.R.attr#protectionLevel}.
61      */
62     public static final int PROTECTION_FLAG_SYSTEM = 0x10;
63 
64     /**
65      * Additional flag for {@link #protectionLevel}, corresponding
66      * to the <code>development</code> value of
67      * {@link android.R.attr#protectionLevel}.
68      */
69     public static final int PROTECTION_FLAG_DEVELOPMENT = 0x20;
70 
71     /**
72      * Additional flag for {@link #protectionLevel}, corresponding
73      * to the <code>appop</code> value of
74      * {@link android.R.attr#protectionLevel}.
75      */
76     public static final int PROTECTION_FLAG_APPOP = 0x40;
77 
78     /**
79      * Mask for {@link #protectionLevel}: the basic protection type.
80      */
81     public static final int PROTECTION_MASK_BASE = 0xf;
82 
83     /**
84      * Mask for {@link #protectionLevel}: additional flag bits.
85      */
86     public static final int PROTECTION_MASK_FLAGS = 0xf0;
87 
88     /**
89      * The level of access this permission is protecting, as per
90      * {@link android.R.attr#protectionLevel}.  Values may be
91      * {@link #PROTECTION_NORMAL}, {@link #PROTECTION_DANGEROUS}, or
92      * {@link #PROTECTION_SIGNATURE}.  May also include the additional
93      * flags {@link #PROTECTION_FLAG_SYSTEM} or {@link #PROTECTION_FLAG_DEVELOPMENT}
94      * (which only make sense in combination with the base
95      * {@link #PROTECTION_SIGNATURE}.
96      */
97     public int protectionLevel;
98 
99     /**
100      * The group this permission is a part of, as per
101      * {@link android.R.attr#permissionGroup}.
102      */
103     public String group;
104 
105     /**
106      * Flag for {@link #flags}, corresponding to <code>costsMoney</code>
107      * value of {@link android.R.attr#permissionFlags}.
108      */
109     public static final int FLAG_COSTS_MONEY = 1<<0;
110 
111     /**
112      * Additional flags about this permission as given by
113      * {@link android.R.attr#permissionFlags}.
114      */
115     public int flags;
116 
117     /**
118      * A string resource identifier (in the package's resources) of this
119      * permission's description.  From the "description" attribute or,
120      * if not set, 0.
121      */
122     public int descriptionRes;
123 
124     /**
125      * The description string provided in the AndroidManifest file, if any.  You
126      * probably don't want to use this, since it will be null if the description
127      * is in a resource.  You probably want
128      * {@link PermissionInfo#loadDescription} instead.
129      */
130     public CharSequence nonLocalizedDescription;
131 
132     /** @hide */
fixProtectionLevel(int level)133     public static int fixProtectionLevel(int level) {
134         if (level == PROTECTION_SIGNATURE_OR_SYSTEM) {
135             level = PROTECTION_SIGNATURE | PROTECTION_FLAG_SYSTEM;
136         }
137         return level;
138     }
139 
140     /** @hide */
protectionToString(int level)141     public static String protectionToString(int level) {
142         String protLevel = "????";
143         switch (level&PROTECTION_MASK_BASE) {
144             case PermissionInfo.PROTECTION_DANGEROUS:
145                 protLevel = "dangerous";
146                 break;
147             case PermissionInfo.PROTECTION_NORMAL:
148                 protLevel = "normal";
149                 break;
150             case PermissionInfo.PROTECTION_SIGNATURE:
151                 protLevel = "signature";
152                 break;
153             case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM:
154                 protLevel = "signatureOrSystem";
155                 break;
156         }
157         if ((level&PermissionInfo.PROTECTION_FLAG_SYSTEM) != 0) {
158             protLevel += "|system";
159         }
160         if ((level&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) {
161             protLevel += "|development";
162         }
163         if ((level&PermissionInfo.PROTECTION_FLAG_APPOP) != 0) {
164             protLevel += "|appop";
165         }
166         return protLevel;
167     }
168 
PermissionInfo()169     public PermissionInfo() {
170     }
171 
PermissionInfo(PermissionInfo orig)172     public PermissionInfo(PermissionInfo orig) {
173         super(orig);
174         protectionLevel = orig.protectionLevel;
175         flags = orig.flags;
176         group = orig.group;
177         descriptionRes = orig.descriptionRes;
178         nonLocalizedDescription = orig.nonLocalizedDescription;
179     }
180 
181     /**
182      * Retrieve the textual description of this permission.  This
183      * will call back on the given PackageManager to load the description from
184      * the application.
185      *
186      * @param pm A PackageManager from which the label can be loaded; usually
187      * the PackageManager from which you originally retrieved this item.
188      *
189      * @return Returns a CharSequence containing the permission's description.
190      * If there is no description, null is returned.
191      */
loadDescription(PackageManager pm)192     public CharSequence loadDescription(PackageManager pm) {
193         if (nonLocalizedDescription != null) {
194             return nonLocalizedDescription;
195         }
196         if (descriptionRes != 0) {
197             CharSequence label = pm.getText(packageName, descriptionRes, null);
198             if (label != null) {
199                 return label;
200             }
201         }
202         return null;
203     }
204 
toString()205     public String toString() {
206         return "PermissionInfo{"
207             + Integer.toHexString(System.identityHashCode(this))
208             + " " + name + "}";
209     }
210 
describeContents()211     public int describeContents() {
212         return 0;
213     }
214 
writeToParcel(Parcel dest, int parcelableFlags)215     public void writeToParcel(Parcel dest, int parcelableFlags) {
216         super.writeToParcel(dest, parcelableFlags);
217         dest.writeInt(protectionLevel);
218         dest.writeInt(flags);
219         dest.writeString(group);
220         dest.writeInt(descriptionRes);
221         TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags);
222     }
223 
224     public static final Creator<PermissionInfo> CREATOR =
225         new Creator<PermissionInfo>() {
226         public PermissionInfo createFromParcel(Parcel source) {
227             return new PermissionInfo(source);
228         }
229         public PermissionInfo[] newArray(int size) {
230             return new PermissionInfo[size];
231         }
232     };
233 
PermissionInfo(Parcel source)234     private PermissionInfo(Parcel source) {
235         super(source);
236         protectionLevel = source.readInt();
237         flags = source.readInt();
238         group = source.readString();
239         descriptionRes = source.readInt();
240         nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
241     }
242 }
243