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