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