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