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     * The group this permission is a part of, as per
59     * {@link android.R.attr#permissionGroup}.
60     */
61    public String group;
62
63    /**
64     * A string resource identifier (in the package's resources) of this
65     * permission's description.  From the "description" attribute or,
66     * if not set, 0.
67     */
68    public int descriptionRes;
69
70    /**
71     * The description string provided in the AndroidManifest file, if any.  You
72     * probably don't want to use this, since it will be null if the description
73     * is in a resource.  You probably want
74     * {@link PermissionInfo#loadDescription} instead.
75     */
76    public CharSequence nonLocalizedDescription;
77
78    /**
79     * The level of access this permission is protecting, as per
80     * {@link android.R.attr#protectionLevel}.  Values may be
81     * {@link #PROTECTION_NORMAL}, {@link #PROTECTION_DANGEROUS}, or
82     * {@link #PROTECTION_SIGNATURE}.
83     */
84    public int protectionLevel;
85
86    public PermissionInfo() {
87    }
88
89    public PermissionInfo(PermissionInfo orig) {
90        super(orig);
91        group = orig.group;
92        descriptionRes = orig.descriptionRes;
93        protectionLevel = orig.protectionLevel;
94        nonLocalizedDescription = orig.nonLocalizedDescription;
95    }
96
97    /**
98     * Retrieve the textual description of this permission.  This
99     * will call back on the given PackageManager to load the description from
100     * the application.
101     *
102     * @param pm A PackageManager from which the label can be loaded; usually
103     * the PackageManager from which you originally retrieved this item.
104     *
105     * @return Returns a CharSequence containing the permission's description.
106     * If there is no description, null is returned.
107     */
108    public CharSequence loadDescription(PackageManager pm) {
109        if (nonLocalizedDescription != null) {
110            return nonLocalizedDescription;
111        }
112        if (descriptionRes != 0) {
113            CharSequence label = pm.getText(packageName, descriptionRes, null);
114            if (label != null) {
115                return label;
116            }
117        }
118        return null;
119    }
120
121    public String toString() {
122        return "PermissionInfo{"
123            + Integer.toHexString(System.identityHashCode(this))
124            + " " + name + "}";
125    }
126
127    public int describeContents() {
128        return 0;
129    }
130
131    public void writeToParcel(Parcel dest, int parcelableFlags) {
132        super.writeToParcel(dest, parcelableFlags);
133        dest.writeString(group);
134        dest.writeInt(descriptionRes);
135        dest.writeInt(protectionLevel);
136        TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags);
137    }
138
139    public static final Creator<PermissionInfo> CREATOR =
140        new Creator<PermissionInfo>() {
141        public PermissionInfo createFromParcel(Parcel source) {
142            return new PermissionInfo(source);
143        }
144        public PermissionInfo[] newArray(int size) {
145            return new PermissionInfo[size];
146        }
147    };
148
149    private PermissionInfo(Parcel source) {
150        super(source);
151        group = source.readString();
152        descriptionRes = source.readInt();
153        protectionLevel = source.readInt();
154        nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
155    }
156}
157