blob: 25e77867b757fdf12d9c70848e1dad93848db82c [file] [log] [blame]
Michal Karpinski2c37b082018-01-18 16:14:27 +00001/*
2 * Copyright (C) 2018 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.provider;
18
Annie Mengeed1b812018-05-21 16:34:52 +010019import android.annotation.Nullable;
Michal Karpinski2c37b082018-01-18 16:14:27 +000020import android.content.ComponentName;
21import android.net.Uri;
22
23import com.android.internal.util.ArrayUtils;
24
Michal Karpinski964943a2018-01-19 16:28:26 +000025import java.util.Locale;
26
Michal Karpinski2c37b082018-01-18 16:14:27 +000027/**
28 * This class provides both interface for validation and common validators
29 * used to ensure Settings have meaningful values.
30 *
31 * @hide
32 */
33public class SettingsValidators {
34
35 public static final Validator BOOLEAN_VALIDATOR =
36 new DiscreteValueValidator(new String[] {"0", "1"});
37
Michal Karpinski5db1e432018-01-18 20:10:24 +000038 public static final Validator ANY_STRING_VALIDATOR = new Validator() {
39 @Override
Annie Mengeed1b812018-05-21 16:34:52 +010040 public boolean validate(@Nullable String value) {
Michal Karpinski5db1e432018-01-18 20:10:24 +000041 return true;
42 }
43 };
44
Michal Karpinski2c37b082018-01-18 16:14:27 +000045 public static final Validator NON_NEGATIVE_INTEGER_VALIDATOR = new Validator() {
46 @Override
Annie Mengeed1b812018-05-21 16:34:52 +010047 public boolean validate(@Nullable String value) {
Michal Karpinski2c37b082018-01-18 16:14:27 +000048 try {
49 return Integer.parseInt(value) >= 0;
50 } catch (NumberFormatException e) {
51 return false;
52 }
53 }
54 };
55
Michal Karpinski964943a2018-01-19 16:28:26 +000056 public static final Validator ANY_INTEGER_VALIDATOR = new Validator() {
57 @Override
Annie Mengeed1b812018-05-21 16:34:52 +010058 public boolean validate(@Nullable String value) {
Michal Karpinski964943a2018-01-19 16:28:26 +000059 try {
60 Integer.parseInt(value);
61 return true;
62 } catch (NumberFormatException e) {
63 return false;
64 }
65 }
66 };
67
Michal Karpinski2c37b082018-01-18 16:14:27 +000068 public static final Validator URI_VALIDATOR = new Validator() {
69 @Override
Annie Mengeed1b812018-05-21 16:34:52 +010070 public boolean validate(@Nullable String value) {
Michal Karpinski2c37b082018-01-18 16:14:27 +000071 try {
72 Uri.decode(value);
73 return true;
74 } catch (IllegalArgumentException e) {
75 return false;
76 }
77 }
78 };
79
Annie Meng44511792018-05-17 18:49:24 +010080 /**
81 * Does not allow a setting to have a null {@link ComponentName}. Use {@link
82 * SettingsValidators#NULLABLE_COMPONENT_NAME_VALIDATOR} instead if a setting can have a
83 * nullable {@link ComponentName}.
84 */
Michal Karpinski2c37b082018-01-18 16:14:27 +000085 public static final Validator COMPONENT_NAME_VALIDATOR = new Validator() {
86 @Override
Annie Mengeed1b812018-05-21 16:34:52 +010087 public boolean validate(@Nullable String value) {
Annie Meng5d26b8c2018-05-17 16:00:16 +010088 return value != null && ComponentName.unflattenFromString(value) != null;
Michal Karpinski2c37b082018-01-18 16:14:27 +000089 }
90 };
91
Annie Meng44511792018-05-17 18:49:24 +010092 /**
93 * Allows a setting to have a null {@link ComponentName}.
94 */
95 public static final Validator NULLABLE_COMPONENT_NAME_VALIDATOR = new Validator() {
96 @Override
97 public boolean validate(@Nullable String value) {
98 return value == null || COMPONENT_NAME_VALIDATOR.validate(value);
99 }
100 };
101
Michal Karpinski5db1e432018-01-18 20:10:24 +0000102 public static final Validator PACKAGE_NAME_VALIDATOR = new Validator() {
103 @Override
Annie Mengeed1b812018-05-21 16:34:52 +0100104 public boolean validate(@Nullable String value) {
Michal Karpinski5db1e432018-01-18 20:10:24 +0000105 return value != null && isStringPackageName(value);
106 }
107
108 private boolean isStringPackageName(String value) {
109 // The name may contain uppercase or lowercase letters ('A' through 'Z'), numbers,
110 // and underscores ('_'). However, individual package name parts may only
111 // start with letters.
112 // (https://developer.android.com/guide/topics/manifest/manifest-element.html#package)
Michal Karpinski964943a2018-01-19 16:28:26 +0000113 if (value == null) {
114 return false;
115 }
Michal Karpinski29a74722018-01-20 14:31:25 +0000116 String[] subparts = value.split("\\.");
Michal Karpinski5db1e432018-01-18 20:10:24 +0000117 boolean isValidPackageName = true;
118 for (String subpart : subparts) {
Michal Karpinski6fe6c8e2018-01-24 15:40:09 +0000119 isValidPackageName &= isSubpartValidForPackageName(subpart);
Michal Karpinski5db1e432018-01-18 20:10:24 +0000120 if (!isValidPackageName) break;
121 }
122 return isValidPackageName;
123 }
124
125 private boolean isSubpartValidForPackageName(String subpart) {
126 if (subpart.length() == 0) return false;
127 boolean isValidSubpart = Character.isLetter(subpart.charAt(0));
128 for (int i = 1; i < subpart.length(); i++) {
Michal Karpinski6fe6c8e2018-01-24 15:40:09 +0000129 isValidSubpart &= (Character.isLetterOrDigit(subpart.charAt(i))
Michal Karpinski5db1e432018-01-18 20:10:24 +0000130 || (subpart.charAt(i) == '_'));
131 if (!isValidSubpart) break;
132 }
133 return isValidSubpart;
134 }
135 };
136
Michal Karpinski2c37b082018-01-18 16:14:27 +0000137 public static final Validator LENIENT_IP_ADDRESS_VALIDATOR = new Validator() {
138 private static final int MAX_IPV6_LENGTH = 45;
139
140 @Override
Annie Mengeed1b812018-05-21 16:34:52 +0100141 public boolean validate(@Nullable String value) {
Michal Karpinski964943a2018-01-19 16:28:26 +0000142 if (value == null) {
143 return false;
144 }
Michal Karpinski2c37b082018-01-18 16:14:27 +0000145 return value.length() <= MAX_IPV6_LENGTH;
146 }
147 };
148
Michal Karpinski964943a2018-01-19 16:28:26 +0000149 public static final Validator LOCALE_VALIDATOR = new Validator() {
150 @Override
Annie Mengeed1b812018-05-21 16:34:52 +0100151 public boolean validate(@Nullable String value) {
Michal Karpinski964943a2018-01-19 16:28:26 +0000152 if (value == null) {
153 return false;
154 }
155 Locale[] validLocales = Locale.getAvailableLocales();
156 for (Locale locale : validLocales) {
157 if (value.equals(locale.toString())) {
158 return true;
159 }
160 }
161 return false;
162 }
163 };
164
Michal Karpinski2c37b082018-01-18 16:14:27 +0000165 public interface Validator {
Annie Mengeed1b812018-05-21 16:34:52 +0100166 /**
167 * Returns whether the input value is valid. Subclasses should handle the case where the
168 * input value is {@code null}.
169 */
170 boolean validate(@Nullable String value);
Michal Karpinski2c37b082018-01-18 16:14:27 +0000171 }
172
173 public static final class DiscreteValueValidator implements Validator {
174 private final String[] mValues;
175
176 public DiscreteValueValidator(String[] values) {
177 mValues = values;
178 }
179
180 @Override
Annie Mengeed1b812018-05-21 16:34:52 +0100181 public boolean validate(@Nullable String value) {
Michal Karpinski2c37b082018-01-18 16:14:27 +0000182 return ArrayUtils.contains(mValues, value);
183 }
184 }
185
186 public static final class InclusiveIntegerRangeValidator implements Validator {
187 private final int mMin;
188 private final int mMax;
189
190 public InclusiveIntegerRangeValidator(int min, int max) {
191 mMin = min;
192 mMax = max;
193 }
194
195 @Override
Annie Mengeed1b812018-05-21 16:34:52 +0100196 public boolean validate(@Nullable String value) {
Michal Karpinski2c37b082018-01-18 16:14:27 +0000197 try {
198 final int intValue = Integer.parseInt(value);
199 return intValue >= mMin && intValue <= mMax;
200 } catch (NumberFormatException e) {
201 return false;
202 }
203 }
204 }
205
206 public static final class InclusiveFloatRangeValidator implements Validator {
207 private final float mMin;
208 private final float mMax;
209
210 public InclusiveFloatRangeValidator(float min, float max) {
211 mMin = min;
212 mMax = max;
213 }
214
215 @Override
Annie Mengeed1b812018-05-21 16:34:52 +0100216 public boolean validate(@Nullable String value) {
Michal Karpinski2c37b082018-01-18 16:14:27 +0000217 try {
218 final float floatValue = Float.parseFloat(value);
219 return floatValue >= mMin && floatValue <= mMax;
Annie Mengeed1b812018-05-21 16:34:52 +0100220 } catch (NumberFormatException | NullPointerException e) {
Michal Karpinski2c37b082018-01-18 16:14:27 +0000221 return false;
222 }
223 }
224 }
Michal Karpinski964943a2018-01-19 16:28:26 +0000225
226 public static final class ComponentNameListValidator implements Validator {
227 private final String mSeparator;
228
229 public ComponentNameListValidator(String separator) {
230 mSeparator = separator;
231 }
232
233 @Override
Annie Mengeed1b812018-05-21 16:34:52 +0100234 public boolean validate(@Nullable String value) {
Michal Karpinski964943a2018-01-19 16:28:26 +0000235 if (value == null) {
236 return false;
237 }
238 String[] elements = value.split(mSeparator);
239 for (String element : elements) {
240 if (!COMPONENT_NAME_VALIDATOR.validate(element)) {
241 return false;
242 }
243 }
244 return true;
245 }
246 }
247
248 public static final class PackageNameListValidator implements Validator {
249 private final String mSeparator;
250
251 public PackageNameListValidator(String separator) {
252 mSeparator = separator;
253 }
254
255 @Override
Annie Mengeed1b812018-05-21 16:34:52 +0100256 public boolean validate(@Nullable String value) {
Michal Karpinski964943a2018-01-19 16:28:26 +0000257 if (value == null) {
258 return false;
259 }
260 String[] elements = value.split(mSeparator);
261 for (String element : elements) {
262 if (!PACKAGE_NAME_VALIDATOR.validate(element)) {
263 return false;
264 }
265 }
266 return true;
267 }
268 }
Michal Karpinski2c37b082018-01-18 16:14:27 +0000269}