blob: 3ff53bf279d62f4dc381c5cebf74c16a307ed185 [file] [log] [blame]
Amith Yamasanidf2e92a2013-03-01 17:04:38 -08001/*
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
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080022/**
23 * Applications can expose restrictions for a restricted user on a
24 * multiuser device. The administrator can configure these restrictions that will then be
25 * applied to the restricted user. Each RestrictionsEntry is one configurable restriction.
26 * <p/>
27 * Any application that chooses to expose such restrictions does so by implementing a
Amith Yamasani1eab5f22013-03-20 23:54:07 -070028 * receiver that handles the {@link Intent#ACTION_GET_RESTRICTION_ENTRIES} action.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080029 * The receiver then returns a result bundle that contains an entry called "restrictions", whose
30 * value is an ArrayList<RestrictionsEntry>.
31 */
32public class RestrictionEntry implements Parcelable {
33
34 /**
Amith Yamasani86118ba2013-03-28 14:33:16 -070035 * A type of restriction. Use this type for information that needs to be transferred across
36 * but shouldn't be presented to the user in the UI. Stores a single String value.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080037 */
38 public static final int TYPE_NULL = 0;
Amith Yamasani86118ba2013-03-28 14:33:16 -070039
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080040 /**
Amith Yamasani86118ba2013-03-28 14:33:16 -070041 * A type of restriction. Use this for storing a boolean value, typically presented as
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080042 * a checkbox in the UI.
43 */
44 public static final int TYPE_BOOLEAN = 1;
Amith Yamasani86118ba2013-03-28 14:33:16 -070045
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080046 /**
47 * A type of restriction. Use this for storing a string value, typically presented as
Amith Yamasani86118ba2013-03-28 14:33:16 -070048 * a single-select list. Call {@link #setChoiceEntries(String[])} and
49 * {@link #setChoiceValues(String[])} to set the localized list entries to present to the user
50 * and the corresponding values, respectively.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080051 */
52 public static final int TYPE_CHOICE = 2;
Amith Yamasani86118ba2013-03-28 14:33:16 -070053
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080054 /**
55 * A type of restriction. Use this for storing a string value, typically presented as
Amith Yamasani86118ba2013-03-28 14:33:16 -070056 * a single-select list. Call {@link #setChoiceEntries(String[])} and
57 * {@link #setChoiceValues(String[])} to set the localized list entries to present to the user
58 * and the corresponding values, respectively.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080059 * The presentation could imply that values in lower array indices are included when a
60 * particular value is chosen.
Amith Yamasanid5e946a2013-04-24 21:37:36 -070061 * @hide
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080062 */
63 public static final int TYPE_CHOICE_LEVEL = 3;
Amith Yamasani86118ba2013-03-28 14:33:16 -070064
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080065 /**
66 * A type of restriction. Use this for presenting a multi-select list where more than one
67 * entry can be selected, such as for choosing specific titles to white-list.
Amith Yamasani86118ba2013-03-28 14:33:16 -070068 * Call {@link #setChoiceEntries(String[])} and
69 * {@link #setChoiceValues(String[])} to set the localized list entries to present to the user
70 * and the corresponding values, respectively.
71 * Use {@link #getAllSelectedStrings()} and {@link #setAllSelectedStrings(String[])} to
72 * manipulate the selections.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080073 */
74 public static final int TYPE_MULTI_SELECT = 4;
75
76 /** The type of restriction. */
Amith Yamasani86118ba2013-03-28 14:33:16 -070077 private int type;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080078
79 /** The unique key that identifies the restriction. */
Amith Yamasani86118ba2013-03-28 14:33:16 -070080 private String key;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080081
82 /** The user-visible title of the restriction. */
Amith Yamasani86118ba2013-03-28 14:33:16 -070083 private String title;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080084
85 /** The user-visible secondary description of the restriction. */
Amith Yamasani86118ba2013-03-28 14:33:16 -070086 private String description;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080087
88 /** The user-visible set of choices used for single-select and multi-select lists. */
Amith Yamasani86118ba2013-03-28 14:33:16 -070089 private String [] choices;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080090
91 /** The values corresponding to the user-visible choices. The value(s) of this entry will
Amith Yamasani86118ba2013-03-28 14:33:16 -070092 * one or more of these, returned by {@link #getAllSelectedStrings()} and
93 * {@link #getSelectedString()}.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080094 */
Amith Yamasani86118ba2013-03-28 14:33:16 -070095 private String [] values;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -080096
97 /* The chosen value, whose content depends on the type of the restriction. */
98 private String currentValue;
Amith Yamasani86118ba2013-03-28 14:33:16 -070099
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800100 /* List of selected choices in the multi-select case. */
101 private String[] currentValues;
102
103 /**
Amith Yamasanid5e946a2013-04-24 21:37:36 -0700104 * Constructor for {@link #TYPE_CHOICE} type.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800105 * @param key the unique key for this restriction
Amith Yamasani86118ba2013-03-28 14:33:16 -0700106 * @param selectedString the current value
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800107 */
Amith Yamasani86118ba2013-03-28 14:33:16 -0700108 public RestrictionEntry(String key, String selectedString) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800109 this.key = key;
Amith Yamasani0d8750d2013-05-01 15:25:28 -0700110 this.type = TYPE_CHOICE;
Amith Yamasani86118ba2013-03-28 14:33:16 -0700111 this.currentValue = selectedString;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800112 }
113
114 /**
115 * Constructor for {@link #TYPE_BOOLEAN} type.
116 * @param key the unique key for this restriction
Amith Yamasani86118ba2013-03-28 14:33:16 -0700117 * @param selectedState whether this restriction is selected or not
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800118 */
Amith Yamasani86118ba2013-03-28 14:33:16 -0700119 public RestrictionEntry(String key, boolean selectedState) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800120 this.key = key;
Amith Yamasani0d8750d2013-05-01 15:25:28 -0700121 this.type = TYPE_BOOLEAN;
Amith Yamasani86118ba2013-03-28 14:33:16 -0700122 setSelectedState(selectedState);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800123 }
124
125 /**
126 * Constructor for {@link #TYPE_MULTI_SELECT} type.
127 * @param key the unique key for this restriction
Amith Yamasani86118ba2013-03-28 14:33:16 -0700128 * @param selectedStrings the list of values that are currently selected
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800129 */
Amith Yamasani86118ba2013-03-28 14:33:16 -0700130 public RestrictionEntry(String key, String[] selectedStrings) {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800131 this.key = key;
Amith Yamasani0d8750d2013-05-01 15:25:28 -0700132 this.type = TYPE_MULTI_SELECT;
Amith Yamasani86118ba2013-03-28 14:33:16 -0700133 this.currentValues = selectedStrings;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800134 }
135
136 /**
Amith Yamasani86118ba2013-03-28 14:33:16 -0700137 * Sets the type for this restriction.
138 * @param type the type for this restriction.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800139 */
Amith Yamasani86118ba2013-03-28 14:33:16 -0700140 public void setType(int type) {
141 this.type = type;
142 }
143
144 /**
145 * Returns the type for this restriction.
146 * @return the type for this restriction
147 */
148 public int getType() {
149 return type;
150 }
151
152 /**
153 * Returns the currently selected string value.
154 * @return the currently selected value, which can be null for types that aren't for holding
155 * single string values.
156 */
157 public String getSelectedString() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800158 return currentValue;
159 }
160
161 /**
Amith Yamasani86118ba2013-03-28 14:33:16 -0700162 * Returns the list of currently selected values.
163 * @return the list of current selections, if type is {@link #TYPE_MULTI_SELECT},
164 * null otherwise.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800165 */
Amith Yamasani86118ba2013-03-28 14:33:16 -0700166 public String[] getAllSelectedStrings() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800167 return currentValues;
168 }
169
170 /**
Amith Yamasani86118ba2013-03-28 14:33:16 -0700171 * Returns the current selected state for an entry of type {@link #TYPE_BOOLEAN}.
172 * @return the current selected state of the entry.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800173 */
Amith Yamasani86118ba2013-03-28 14:33:16 -0700174 public boolean getSelectedState() {
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800175 return Boolean.parseBoolean(currentValue);
176 }
177
178 /**
Amith Yamasani86118ba2013-03-28 14:33:16 -0700179 * Sets the string value to use as the selected value for this restriction. This value will
180 * be persisted by the system for later use by the application.
181 * @param selectedString the string value to select.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800182 */
Amith Yamasani86118ba2013-03-28 14:33:16 -0700183 public void setSelectedString(String selectedString) {
184 currentValue = selectedString;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800185 }
186
187 /**
Amith Yamasani86118ba2013-03-28 14:33:16 -0700188 * Sets the current selected state for an entry of type {@link #TYPE_BOOLEAN}. This value will
189 * be persisted by the system for later use by the application.
190 * @param state the current selected state
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800191 */
Amith Yamasani86118ba2013-03-28 14:33:16 -0700192 public void setSelectedState(boolean state) {
193 currentValue = Boolean.toString(state);
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800194 }
195
196 /**
Amith Yamasani86118ba2013-03-28 14:33:16 -0700197 * Sets the current list of selected values for an entry of type {@link #TYPE_MULTI_SELECT}.
198 * These values will be persisted by the system for later use by the application.
199 * @param allSelectedStrings the current list of selected values.
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800200 */
Amith Yamasani86118ba2013-03-28 14:33:16 -0700201 public void setAllSelectedStrings(String[] allSelectedStrings) {
202 currentValues = allSelectedStrings;
203 }
204
205 /**
206 * Sets a list of string values that can be selected by the user. If no user-visible entries
207 * are set by a call to {@link #setChoiceEntries(String[])}, these values will be the ones
208 * shown to the user. Values will be chosen from this list as the user's selection and the
209 * selected values can be retrieved by a call to {@link #getAllSelectedStrings()}, or
210 * {@link #getSelectedString()}, depending on whether it is a multi-select type or choice type.
Amith Yamasanid5e946a2013-04-24 21:37:36 -0700211 * This method is not relevant for types other than
Amith Yamasani86118ba2013-03-28 14:33:16 -0700212 * {@link #TYPE_CHOICE}, and {@link #TYPE_MULTI_SELECT}.
213 * @param choiceValues an array of Strings which will be the selected values for the user's
214 * selections.
215 * @see #getChoiceValues()
216 * @see #getAllSelectedStrings()
217 */
218 public void setChoiceValues(String[] choiceValues) {
219 values = choiceValues;
220 }
221
222 /**
223 * Sets a list of string values that can be selected by the user, similar to
224 * {@link #setChoiceValues(String[])}.
225 * @param context the application context for retrieving the resources.
226 * @param stringArrayResId the resource id for a string array containing the possible values.
227 * @see #setChoiceValues(String[])
228 */
229 public void setChoiceValues(Context context, int stringArrayResId) {
230 values = context.getResources().getStringArray(stringArrayResId);
231 }
232
233 /**
234 * Returns the list of possible string values set earlier.
235 * @return the list of possible values.
236 */
237 public String[] getChoiceValues() {
238 return values;
239 }
240
241 /**
242 * Sets a list of strings that will be presented as choices to the user. When the
243 * user selects one or more of these choices, the corresponding value from the possible values
244 * are stored as the selected strings. The size of this array must match the size of the array
245 * set in {@link #setChoiceValues(String[])}. This method is not relevant for types other
Amith Yamasanid5e946a2013-04-24 21:37:36 -0700246 * than {@link #TYPE_CHOICE}, and {@link #TYPE_MULTI_SELECT}.
Amith Yamasani86118ba2013-03-28 14:33:16 -0700247 * @param choiceEntries the list of user-visible choices.
248 * @see #setChoiceValues(String[])
249 */
250 public void setChoiceEntries(String[] choiceEntries) {
251 choices = choiceEntries;
252 }
253
254 /** Sets a list of strings that will be presented as choices to the user. This is similar to
255 * {@link #setChoiceEntries(String[])}.
256 * @param context the application context, used for retrieving the resources.
257 * @param stringArrayResId the resource id of a string array containing the possible entries.
258 */
259 public void setChoiceEntries(Context context, int stringArrayResId) {
260 choices = context.getResources().getStringArray(stringArrayResId);
261 }
262
263 /**
264 * Returns the list of strings, set earlier, that will be presented as choices to the user.
265 * @return the list of choices presented to the user.
266 */
267 public String[] getChoiceEntries() {
268 return choices;
269 }
270
271 /**
272 * Returns the provided user-visible description of the entry, if any.
273 * @return the user-visible description, null if none was set earlier.
274 */
275 public String getDescription() {
276 return description;
277 }
278
279 /**
280 * Sets the user-visible description of the entry, as a possible sub-text for the title.
281 * You can use this to describe the entry in more detail or to display the current state of
282 * the restriction.
283 * @param description the user-visible description string.
284 */
285 public void setDescription(String description) {
286 this.description = description;
287 }
288
289 /**
290 * This is the unique key for the restriction entry.
291 * @return the key for the restriction.
292 */
293 public String getKey() {
294 return key;
295 }
296
297 /**
298 * Returns the user-visible title for the entry, if any.
299 * @return the user-visible title for the entry, null if none was set earlier.
300 */
301 public String getTitle() {
302 return title;
303 }
304
305 /**
306 * Sets the user-visible title for the entry.
307 * @param title the user-visible title for the entry.
308 */
309 public void setTitle(String title) {
310 this.title = title;
Amith Yamasanidf2e92a2013-03-01 17:04:38 -0800311 }
312
313 private boolean equalArrays(String[] one, String[] other) {
314 if (one.length != other.length) return false;
315 for (int i = 0; i < one.length; i++) {
316 if (!one[i].equals(other[i])) return false;
317 }
318 return true;
319 }
320
321 @Override
322 public boolean equals(Object o) {
323 if (o == this) return true;
324 if (!(o instanceof RestrictionEntry)) return false;
325 final RestrictionEntry other = (RestrictionEntry) o;
326 // Make sure that either currentValue matches or currentValues matches.
327 return type == other.type && key.equals(other.key)
328 &&
329 ((currentValues == null && other.currentValues == null
330 && currentValue != null && currentValue.equals(other.currentValue))
331 ||
332 (currentValue == null && other.currentValue == null
333 && currentValues != null && equalArrays(currentValues, other.currentValues)));
334 }
335
336 @Override
337 public int hashCode() {
338 int result = 17;
339 result = 31 * result + key.hashCode();
340 if (currentValue != null) {
341 result = 31 * result + currentValue.hashCode();
342 } else if (currentValues != null) {
343 for (String value : currentValues) {
344 if (value != null) {
345 result = 31 * result + value.hashCode();
346 }
347 }
348 }
349 return result;
350 }
351
352 private String[] readArray(Parcel in) {
353 int count = in.readInt();
354 String[] values = new String[count];
355 for (int i = 0; i < count; i++) {
356 values[i] = in.readString();
357 }
358 return values;
359 }
360
361 public RestrictionEntry(Parcel in) {
362 type = in.readInt();
363 key = in.readString();
364 title = in.readString();
365 description = in.readString();
366 choices = readArray(in);
367 values = readArray(in);
368 currentValue = in.readString();
369 currentValues = readArray(in);
370 }
371
372 @Override
373 public int describeContents() {
374 return 0;
375 }
376
377 private void writeArray(Parcel dest, String[] values) {
378 if (values == null) {
379 dest.writeInt(0);
380 } else {
381 dest.writeInt(values.length);
382 for (int i = 0; i < values.length; i++) {
383 dest.writeString(values[i]);
384 }
385 }
386 }
387
388 @Override
389 public void writeToParcel(Parcel dest, int flags) {
390 dest.writeInt(type);
391 dest.writeString(key);
392 dest.writeString(title);
393 dest.writeString(description);
394 writeArray(dest, choices);
395 writeArray(dest, values);
396 dest.writeString(currentValue);
397 writeArray(dest, currentValues);
398 }
399
400 public static final Creator<RestrictionEntry> CREATOR = new Creator<RestrictionEntry>() {
401 public RestrictionEntry createFromParcel(Parcel source) {
402 return new RestrictionEntry(source);
403 }
404
405 public RestrictionEntry[] newArray(int size) {
406 return new RestrictionEntry[size];
407 }
408 };
409
410 @Override
411 public String toString() {
412 return "RestrictionsEntry {type=" + type + ", key=" + key + ", value=" + currentValue + "}";
413 }
414}