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