RestrictionEntry.java revision 1eab5f26939748dea5e94bf019804a68d2a2b161
1/*
2 * Copyright (C) 2013 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;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.lang.annotation.Inherited;
23
24/**
25 * Applications can expose restrictions for a restricted user on a
26 * multiuser device. The administrator can configure these restrictions that will then be
27 * applied to the restricted user. Each RestrictionsEntry is one configurable restriction.
28 * <p/>
29 * Any application that chooses to expose such restrictions does so by implementing a
30 * receiver that handles the {@link Intent#ACTION_GET_RESTRICTION_ENTRIES} action.
31 * The receiver then returns a result bundle that contains an entry called "restrictions", whose
32 * value is an ArrayList<RestrictionsEntry>.
33 */
34public class RestrictionEntry implements Parcelable {
35
36    /**
37     * A type of restriction. Use this one for information that needs to be transferred across
38     * but shouldn't be presented to the user in the UI.
39     */
40    public static final int TYPE_NULL         = 0;
41    /**
42     * A type of restriction. Use this for storing true/false values, typically presented as
43     * a checkbox in the UI.
44     */
45    public static final int TYPE_BOOLEAN      = 1;
46    /**
47     * A type of restriction. Use this for storing a string value, typically presented as
48     * a single-select list. The {@link #values} and {@link #choices} need to have the list of
49     * possible values and the corresponding localized strings, respectively, to present in the UI.
50     */
51    public static final int TYPE_CHOICE       = 2;
52    /**
53     * A type of restriction. Use this for storing a string value, typically presented as
54     * a single-select list. The {@link #values} and {@link #choices} need to have the list of
55     * possible values and the corresponding localized strings, respectively, to present in the UI.
56     * The presentation could imply that values in lower array indices are included when a
57     * particular value is chosen.
58     */
59    public static final int TYPE_CHOICE_LEVEL = 3;
60    /**
61     * A type of restriction. Use this for presenting a multi-select list where more than one
62     * entry can be selected, such as for choosing specific titles to white-list.
63     * The {@link #values} and {@link #choices} need to have the list of
64     * possible values and the corresponding localized strings, respectively, to present in the UI.
65     * Use {@link #getMultipleValues()} and {@link #setMultipleValues(String[])} to manipulate
66     * the selections.
67     */
68    public static final int TYPE_MULTI_SELECT = 4;
69
70    /** The type of restriction. */
71    public int type;
72
73    /** The unique key that identifies the restriction. */
74    public String key;
75
76    /** The user-visible title of the restriction. */
77    public String title;
78
79    /** The user-visible secondary description of the restriction. */
80    public String description;
81
82    /** The user-visible set of choices used for single-select and multi-select lists. */
83    public String [] choices;
84
85    /** The values corresponding to the user-visible choices. The value(s) of this entry will
86     * one or more of these, returned by {@link #getMultipleValues()} and
87     * {@link #getStringValue()}.
88     */
89    public String [] values;
90
91    /* The chosen value, whose content depends on the type of the restriction. */
92    private String currentValue;
93    /* List of selected choices in the multi-select case. */
94    private String[] currentValues;
95
96    /**
97     * Constructor for {@link #TYPE_CHOICE} and {@link #TYPE_CHOICE_LEVEL} types.
98     * @param key the unique key for this restriction
99     * @param value the current value
100     */
101    public RestrictionEntry(String key, String value) {
102        this.key = key;
103        this.currentValue = value;
104    }
105
106    /**
107     * Constructor for {@link #TYPE_BOOLEAN} type.
108     * @param key the unique key for this restriction
109     * @param value the current value
110     */
111    public RestrictionEntry(String key, boolean value) {
112        this.key = key;
113        setValue(value);
114    }
115
116    /**
117     * Constructor for {@link #TYPE_MULTI_SELECT} type.
118     * @param key the unique key for this restriction
119     * @param multipleValues the list of values that are currently selected
120     */
121    public RestrictionEntry(String key, String[] multipleValues) {
122        this.key = key;
123        this.currentValues = multipleValues;
124    }
125
126    /**
127     * Returns the current value. Null for {@link #TYPE_MULTI_SELECT} type.
128     * @return the current value
129     */
130    public String getStringValue() {
131        return currentValue;
132    }
133
134    /**
135     * Returns the list of current selections. Null if the type is not {@link #TYPE_MULTI_SELECT}.
136     * @return the list of current selections.
137     */
138    public String[] getMultipleValues() {
139        return currentValues;
140    }
141
142    /**
143     * Returns the current boolean value for entries of type {@link #TYPE_BOOLEAN}.
144     * @return the current value
145     */
146    public boolean getBooleanValue() {
147        return Boolean.parseBoolean(currentValue);
148    }
149
150    /**
151     * Set the current string value.
152     * @param s the current value
153     */
154    public void setValue(String s) {
155        currentValue = s;
156    }
157
158    /**
159     * Sets the current boolean value.
160     * @param b the current value
161     */
162    public void setValue(boolean b) {
163        currentValue = Boolean.toString(b);
164    }
165
166    /**
167     * Sets the current list of selected values.
168     * @param values the current list of selected values
169     */
170    public void setMultipleValues(String[] values) {
171        currentValues = values;
172    }
173
174    private boolean equalArrays(String[] one, String[] other) {
175        if (one.length != other.length) return false;
176        for (int i = 0; i < one.length; i++) {
177            if (!one[i].equals(other[i])) return false;
178        }
179        return true;
180    }
181
182    @Override
183    public boolean equals(Object o) {
184        if (o == this) return true;
185        if (!(o instanceof RestrictionEntry)) return false;
186        final RestrictionEntry other = (RestrictionEntry) o;
187        // Make sure that either currentValue matches or currentValues matches.
188        return type == other.type && key.equals(other.key)
189                &&
190                ((currentValues == null && other.currentValues == null
191                  && currentValue != null && currentValue.equals(other.currentValue))
192                 ||
193                 (currentValue == null && other.currentValue == null
194                  && currentValues != null && equalArrays(currentValues, other.currentValues)));
195    }
196
197    @Override
198    public int hashCode() {
199        int result = 17;
200        result = 31 * result + key.hashCode();
201        if (currentValue != null) {
202            result = 31 * result + currentValue.hashCode();
203        } else if (currentValues != null) {
204            for (String value : currentValues) {
205                if (value != null) {
206                    result = 31 * result + value.hashCode();
207                }
208            }
209        }
210        return result;
211    }
212
213    private String[] readArray(Parcel in) {
214        int count = in.readInt();
215        String[] values = new String[count];
216        for (int i = 0; i < count; i++) {
217            values[i] = in.readString();
218        }
219        return values;
220    }
221
222    public RestrictionEntry(Parcel in) {
223        type = in.readInt();
224        key = in.readString();
225        title = in.readString();
226        description = in.readString();
227        choices = readArray(in);
228        values = readArray(in);
229        currentValue = in.readString();
230        currentValues = readArray(in);
231    }
232
233    @Override
234    public int describeContents() {
235        return 0;
236    }
237
238    private void writeArray(Parcel dest, String[] values) {
239        if (values == null) {
240            dest.writeInt(0);
241        } else {
242            dest.writeInt(values.length);
243            for (int i = 0; i < values.length; i++) {
244                dest.writeString(values[i]);
245            }
246        }
247    }
248
249    @Override
250    public void writeToParcel(Parcel dest, int flags) {
251        dest.writeInt(type);
252        dest.writeString(key);
253        dest.writeString(title);
254        dest.writeString(description);
255        writeArray(dest, choices);
256        writeArray(dest, values);
257        dest.writeString(currentValue);
258        writeArray(dest, currentValues);
259    }
260
261    public static final Creator<RestrictionEntry> CREATOR = new Creator<RestrictionEntry>() {
262        public RestrictionEntry createFromParcel(Parcel source) {
263            return new RestrictionEntry(source);
264        }
265
266        public RestrictionEntry[] newArray(int size) {
267            return new RestrictionEntry[size];
268        }
269    };
270
271    @Override
272    public String toString() {
273        return "RestrictionsEntry {type=" + type + ", key=" + key + ", value=" + currentValue + "}";
274    }
275}
276