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