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 * group known to the system.  This corresponds to information collected from the
26 * AndroidManifest.xml's <permission-group> tags.
27 */
28public class PermissionGroupInfo extends PackageItemInfo implements Parcelable {
29    /**
30     * A string resource identifier (in the package's resources) of this
31     * permission's description.  From the "description" attribute or,
32     * if not set, 0.
33     */
34    public int descriptionRes;
35
36    /**
37     * The description string provided in the AndroidManifest file, if any.  You
38     * probably don't want to use this, since it will be null if the description
39     * is in a resource.  You probably want
40     * {@link PermissionInfo#loadDescription} instead.
41     */
42    public CharSequence nonLocalizedDescription;
43
44    /**
45     * Flag for {@link #flags}, corresponding to <code>personalInfo</code>
46     * value of {@link android.R.attr#permissionGroupFlags}.
47     */
48    public static final int FLAG_PERSONAL_INFO = 1<<0;
49
50    /**
51     * Additional flags about this group as given by
52     * {@link android.R.attr#permissionGroupFlags}.
53     */
54    public int flags;
55
56    /**
57     * Prioritization of this group, for visually sorting with other groups.
58     */
59    public int priority;
60
61    public PermissionGroupInfo() {
62    }
63
64    public PermissionGroupInfo(PermissionGroupInfo orig) {
65        super(orig);
66        descriptionRes = orig.descriptionRes;
67        nonLocalizedDescription = orig.nonLocalizedDescription;
68        flags = orig.flags;
69        priority = orig.priority;
70    }
71
72    /**
73     * Retrieve the textual description of this permission.  This
74     * will call back on the given PackageManager to load the description from
75     * the application.
76     *
77     * @param pm A PackageManager from which the label can be loaded; usually
78     * the PackageManager from which you originally retrieved this item.
79     *
80     * @return Returns a CharSequence containing the permission's description.
81     * If there is no description, null is returned.
82     */
83    public CharSequence loadDescription(PackageManager pm) {
84        if (nonLocalizedDescription != null) {
85            return nonLocalizedDescription;
86        }
87        if (descriptionRes != 0) {
88            CharSequence label = pm.getText(packageName, descriptionRes, null);
89            if (label != null) {
90                return label;
91            }
92        }
93        return null;
94    }
95
96    public String toString() {
97        return "PermissionGroupInfo{"
98            + Integer.toHexString(System.identityHashCode(this))
99            + " " + name + " flgs=0x" + Integer.toHexString(flags) + "}";
100    }
101
102    public int describeContents() {
103        return 0;
104    }
105
106    public void writeToParcel(Parcel dest, int parcelableFlags) {
107        super.writeToParcel(dest, parcelableFlags);
108        dest.writeInt(descriptionRes);
109        TextUtils.writeToParcel(nonLocalizedDescription, dest, parcelableFlags);
110        dest.writeInt(flags);
111        dest.writeInt(priority);
112    }
113
114    public static final Creator<PermissionGroupInfo> CREATOR =
115            new Creator<PermissionGroupInfo>() {
116        public PermissionGroupInfo createFromParcel(Parcel source) {
117            return new PermissionGroupInfo(source);
118        }
119        public PermissionGroupInfo[] newArray(int size) {
120            return new PermissionGroupInfo[size];
121        }
122    };
123
124    private PermissionGroupInfo(Parcel source) {
125        super(source);
126        descriptionRes = source.readInt();
127        nonLocalizedDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
128        flags = source.readInt();
129        priority = source.readInt();
130    }
131}
132