PermissionInfo.java revision 6d2c0e5ee2f717d4a5c00df08aca21c76eea8278
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.os.Parcel;
20import android.os.Parcelable;
21import 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 */
28public 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     * Flag for {@link #protectionLevel}, corresponding
113     * to the <code>hide</code> value of
114     * {@link android.R.attr#permissionFlags}.
115     * @hide
116     */
117    public static final int PROTECTION_FLAG_HIDE = 1<<1;
118
119    /**
120     * Additional flags about this permission as given by
121     * {@link android.R.attr#permissionFlags}.
122     */
123    public int flags;
124
125    /**
126     * A string resource identifier (in the package's resources) of this
127     * permission's description.  From the "description" attribute or,
128     * if not set, 0.
129     */
130    public int descriptionRes;
131
132    /**
133     * The description string provided in the AndroidManifest file, if any.  You
134     * probably don't want to use this, since it will be null if the description
135     * is in a resource.  You probably want
136     * {@link PermissionInfo#loadDescription} instead.
137     */
138    public CharSequence nonLocalizedDescription;
139
140    /** @hide */
141    public static int fixProtectionLevel(int level) {
142        if (level == PROTECTION_SIGNATURE_OR_SYSTEM) {
143            level = PROTECTION_SIGNATURE | PROTECTION_FLAG_SYSTEM;
144        }
145        return level;
146    }
147
148    /** @hide */
149    public static String protectionToString(int level) {
150        String protLevel = "????";
151        switch (level&PROTECTION_MASK_BASE) {
152            case PermissionInfo.PROTECTION_DANGEROUS:
153                protLevel = "dangerous";
154                break;
155            case PermissionInfo.PROTECTION_NORMAL:
156                protLevel = "normal";
157                break;
158            case PermissionInfo.PROTECTION_SIGNATURE:
159                protLevel = "signature";
160                break;
161            case PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM:
162                protLevel = "signatureOrSystem";
163                break;
164        }
165        if ((level&PermissionInfo.PROTECTION_FLAG_SYSTEM) != 0) {
166            protLevel += "|system";
167        }
168        if ((level&PermissionInfo.PROTECTION_FLAG_DEVELOPMENT) != 0) {
169            protLevel += "|development";
170        }
171        if ((level&PermissionInfo.PROTECTION_FLAG_APPOP) != 0) {
172            protLevel += "|appop";
173        }
174        return protLevel;
175    }
176
177    public PermissionInfo() {
178    }
179
180    public PermissionInfo(PermissionInfo orig) {
181        super(orig);
182        protectionLevel = orig.protectionLevel;
183        flags = orig.flags;
184        group = orig.group;
185        descriptionRes = orig.descriptionRes;
186        nonLocalizedDescription = orig.nonLocalizedDescription;
187    }
188
189    /**
190     * Retrieve the textual description of this permission.  This
191     * will call back on the given PackageManager to load the description from
192     * the application.
193     *
194     * @param pm A PackageManager from which the label can be loaded; usually
195     * the PackageManager from which you originally retrieved this item.
196     *
197     * @return Returns a CharSequence containing the permission's description.
198     * If there is no description, null is returned.
199     */
200    public CharSequence loadDescription(PackageManager pm) {
201        if (nonLocalizedDescription != null) {
202            return nonLocalizedDescription;
203        }
204        if (descriptionRes != 0) {
205            CharSequence label = pm.getText(packageName, descriptionRes, null);
206            if (label != null) {
207                return label;
208            }
209        }
210        return null;
211    }
212
213    public String toString() {
214        return "PermissionInfo{"
215            + Integer.toHexString(System.identityHashCode(this))
216            + " " + name + "}";
217    }
218
219    public int describeContents() {
220        return 0;
221    }
222
223    public void writeToParcel(Parcel dest, int parcelableFlags) {
224        super.writeToParcel(dest, parcelableFlags);
225        dest.writeInt(protectionLevel);
226        dest.writeInt(flags);
227        dest.writeString(group);
228        dest.writeInt(descriptionRes);
229        TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags);
230    }
231
232    public static final Creator<PermissionInfo> CREATOR =
233        new Creator<PermissionInfo>() {
234        public PermissionInfo createFromParcel(Parcel source) {
235            return new PermissionInfo(source);
236        }
237        public PermissionInfo[] newArray(int size) {
238            return new PermissionInfo[size];
239        }
240    };
241
242    private PermissionInfo(Parcel source) {
243        super(source);
244        protectionLevel = source.readInt();
245        flags = source.readInt();
246        group = source.readString();
247        descriptionRes = source.readInt();
248        nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
249    }
250}
251