• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 
17 package android.provider.settings.validators;
18 
19 import static android.provider.settings.validators.SettingsValidators.ANY_INTEGER_VALIDATOR;
20 import static android.provider.settings.validators.SettingsValidators.ANY_STRING_VALIDATOR;
21 import static android.provider.settings.validators.SettingsValidators.BOOLEAN_VALIDATOR;
22 import static android.provider.settings.validators.SettingsValidators.PACKAGE_NAME_VALIDATOR;
23 import static android.provider.settings.validators.SettingsValidators.PERCENTAGE_INTEGER_VALIDATOR;
24 
25 import android.media.AudioFormat;
26 import android.os.BatteryManager;
27 import android.provider.Settings.Global;
28 import android.text.TextUtils;
29 import android.util.ArrayMap;
30 
31 import java.util.Map;
32 
33 /**
34  * Validators for Global settings
35  */
36 public class GlobalSettingsValidators {
37     /**
38      * All settings in {@link Global.SETTINGS_TO_BACKUP} array *must* have a non-null validator,
39      * otherwise they won't be restored.
40      */
41     public static final Map<String, Validator> VALIDATORS = new ArrayMap<>();
42 
43     static {
VALIDATORS.put(Global.APPLY_RAMPING_RINGER, BOOLEAN_VALIDATOR)44         VALIDATORS.put(Global.APPLY_RAMPING_RINGER, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.BUGREPORT_IN_POWER_MENU, BOOLEAN_VALIDATOR)45         VALIDATORS.put(Global.BUGREPORT_IN_POWER_MENU, BOOLEAN_VALIDATOR);
VALIDATORS.put( Global.STAY_ON_WHILE_PLUGGED_IN, value -> { try { int val = Integer.parseInt(value); return (val == 0) || (val == BatteryManager.BATTERY_PLUGGED_AC) || (val == BatteryManager.BATTERY_PLUGGED_USB) || (val == BatteryManager.BATTERY_PLUGGED_WIRELESS) || (val == (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB)) || (val == (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_WIRELESS)) || (val == (BatteryManager.BATTERY_PLUGGED_USB | BatteryManager.BATTERY_PLUGGED_WIRELESS)) || (val == (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB | BatteryManager.BATTERY_PLUGGED_WIRELESS)); } catch (NumberFormatException e) { return false; } })46         VALIDATORS.put(
47                 Global.STAY_ON_WHILE_PLUGGED_IN,
48                 value -> {
49                     try {
50                         int val = Integer.parseInt(value);
51                         return (val == 0)
52                                 || (val == BatteryManager.BATTERY_PLUGGED_AC)
53                                 || (val == BatteryManager.BATTERY_PLUGGED_USB)
54                                 || (val == BatteryManager.BATTERY_PLUGGED_WIRELESS)
55                                 || (val
56                                         == (BatteryManager.BATTERY_PLUGGED_AC
57                                                 | BatteryManager.BATTERY_PLUGGED_USB))
58                                 || (val
59                                         == (BatteryManager.BATTERY_PLUGGED_AC
60                                                 | BatteryManager.BATTERY_PLUGGED_WIRELESS))
61                                 || (val
62                                         == (BatteryManager.BATTERY_PLUGGED_USB
63                                                 | BatteryManager.BATTERY_PLUGGED_WIRELESS))
64                                 || (val
65                                         == (BatteryManager.BATTERY_PLUGGED_AC
66                                                 | BatteryManager.BATTERY_PLUGGED_USB
67                                                 | BatteryManager.BATTERY_PLUGGED_WIRELESS));
68                     } catch (NumberFormatException e) {
69                         return false;
70                     }
71                 });
VALIDATORS.put(Global.AUTO_TIME, BOOLEAN_VALIDATOR)72         VALIDATORS.put(Global.AUTO_TIME, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.AUTO_TIME_ZONE, BOOLEAN_VALIDATOR)73         VALIDATORS.put(Global.AUTO_TIME_ZONE, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.POWER_SOUNDS_ENABLED, BOOLEAN_VALIDATOR)74         VALIDATORS.put(Global.POWER_SOUNDS_ENABLED, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.DOCK_SOUNDS_ENABLED, BOOLEAN_VALIDATOR)75         VALIDATORS.put(Global.DOCK_SOUNDS_ENABLED, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.CHARGING_SOUNDS_ENABLED, BOOLEAN_VALIDATOR)76         VALIDATORS.put(Global.CHARGING_SOUNDS_ENABLED, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.USB_MASS_STORAGE_ENABLED, BOOLEAN_VALIDATOR)77         VALIDATORS.put(Global.USB_MASS_STORAGE_ENABLED, BOOLEAN_VALIDATOR);
VALIDATORS.put( Global.NETWORK_RECOMMENDATIONS_ENABLED, new DiscreteValueValidator(new String[] {"-1", "0", "1"}))78         VALIDATORS.put(
79                 Global.NETWORK_RECOMMENDATIONS_ENABLED,
80                 new DiscreteValueValidator(new String[] {"-1", "0", "1"}));
VALIDATORS.put(Global.WIFI_WAKEUP_ENABLED, BOOLEAN_VALIDATOR)81         VALIDATORS.put(Global.WIFI_WAKEUP_ENABLED, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, BOOLEAN_VALIDATOR)82         VALIDATORS.put(Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, BOOLEAN_VALIDATOR);
VALIDATORS.put( Global.USE_OPEN_WIFI_PACKAGE, value -> (value == null) || PACKAGE_NAME_VALIDATOR.validate(value))83         VALIDATORS.put(
84                 Global.USE_OPEN_WIFI_PACKAGE,
85                 value -> (value == null) || PACKAGE_NAME_VALIDATOR.validate(value));
VALIDATORS.put(Global.WIFI_WATCHDOG_POOR_NETWORK_TEST_ENABLED, ANY_STRING_VALIDATOR)86         VALIDATORS.put(Global.WIFI_WATCHDOG_POOR_NETWORK_TEST_ENABLED, ANY_STRING_VALIDATOR);
VALIDATORS.put( Global.EMERGENCY_TONE, new DiscreteValueValidator(new String[] {"0", "1", "2"}))87         VALIDATORS.put(
88                 Global.EMERGENCY_TONE, new DiscreteValueValidator(new String[] {"0", "1", "2"}));
VALIDATORS.put(Global.CALL_AUTO_RETRY, BOOLEAN_VALIDATOR)89         VALIDATORS.put(Global.CALL_AUTO_RETRY, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.DOCK_AUDIO_MEDIA_ENABLED, BOOLEAN_VALIDATOR)90         VALIDATORS.put(Global.DOCK_AUDIO_MEDIA_ENABLED, BOOLEAN_VALIDATOR);
VALIDATORS.put( Global.ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS, new DiscreteValueValidator(new String[] {"0", "1"}))91         VALIDATORS.put(
92                 Global.ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS,
93                 new DiscreteValueValidator(new String[] {"0", "1"}));
VALIDATORS.put( Global.ENCODED_SURROUND_OUTPUT, new DiscreteValueValidator(new String[] {"0", "1", "2", "3"}))94         VALIDATORS.put(
95                 Global.ENCODED_SURROUND_OUTPUT,
96                 new DiscreteValueValidator(new String[] {"0", "1", "2", "3"}));
VALIDATORS.put( Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS, value -> { try { String[] surroundFormats = TextUtils.split(value, ","); for (String format : surroundFormats) { int audioFormat = Integer.valueOf(format); boolean isSurroundFormat = false; for (int sf : AudioFormat.SURROUND_SOUND_ENCODING) { if (sf == audioFormat) { isSurroundFormat = true; break; } } if (!isSurroundFormat) { return false; } } return true; } catch (NumberFormatException e) { return false; } })97         VALIDATORS.put(
98                 Global.ENCODED_SURROUND_OUTPUT_ENABLED_FORMATS,
99                 value -> {
100                     try {
101                         String[] surroundFormats = TextUtils.split(value, ",");
102                         for (String format : surroundFormats) {
103                             int audioFormat = Integer.valueOf(format);
104                             boolean isSurroundFormat = false;
105                             for (int sf : AudioFormat.SURROUND_SOUND_ENCODING) {
106                                 if (sf == audioFormat) {
107                                     isSurroundFormat = true;
108                                     break;
109                                 }
110                             }
111                             if (!isSurroundFormat) {
112                                 return false;
113                             }
114                         }
115                         return true;
116                     } catch (NumberFormatException e) {
117                         return false;
118                     }
119                 });
VALIDATORS.put( Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_LEVEL, new InclusiveIntegerRangeValidator(0, 100))120         VALIDATORS.put(
121                 Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_LEVEL,
122                 new InclusiveIntegerRangeValidator(0, 100));
VALIDATORS.put( Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED, new DiscreteValueValidator(new String[] {"0", "1"}))123         VALIDATORS.put(
124                 Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED,
125                 new DiscreteValueValidator(new String[] {"0", "1"}));
VALIDATORS.put(Global.LOW_POWER_MODE_TRIGGER_LEVEL, PERCENTAGE_INTEGER_VALIDATOR)126         VALIDATORS.put(Global.LOW_POWER_MODE_TRIGGER_LEVEL, PERCENTAGE_INTEGER_VALIDATOR);
VALIDATORS.put(Global.LOW_POWER_MODE_TRIGGER_LEVEL_MAX, PERCENTAGE_INTEGER_VALIDATOR)127         VALIDATORS.put(Global.LOW_POWER_MODE_TRIGGER_LEVEL_MAX, PERCENTAGE_INTEGER_VALIDATOR);
VALIDATORS.put( Global.AUTOMATIC_POWER_SAVE_MODE, new DiscreteValueValidator(new String[] {"0", "1"}))128         VALIDATORS.put(
129                 Global.AUTOMATIC_POWER_SAVE_MODE,
130                 new DiscreteValueValidator(new String[] {"0", "1"}));
VALIDATORS.put( Global.DYNAMIC_POWER_SAVINGS_DISABLE_THRESHOLD, PERCENTAGE_INTEGER_VALIDATOR)131         VALIDATORS.put(
132                 Global.DYNAMIC_POWER_SAVINGS_DISABLE_THRESHOLD, PERCENTAGE_INTEGER_VALIDATOR);
VALIDATORS.put(Global.BLUETOOTH_ON, BOOLEAN_VALIDATOR)133         VALIDATORS.put(Global.BLUETOOTH_ON, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.PRIVATE_DNS_MODE, ANY_STRING_VALIDATOR)134         VALIDATORS.put(Global.PRIVATE_DNS_MODE, ANY_STRING_VALIDATOR);
VALIDATORS.put(Global.PRIVATE_DNS_SPECIFIER, ANY_STRING_VALIDATOR)135         VALIDATORS.put(Global.PRIVATE_DNS_SPECIFIER, ANY_STRING_VALIDATOR);
VALIDATORS.put(Global.SOFT_AP_TIMEOUT_ENABLED, BOOLEAN_VALIDATOR)136         VALIDATORS.put(Global.SOFT_AP_TIMEOUT_ENABLED, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.WIFI_SCAN_THROTTLE_ENABLED, BOOLEAN_VALIDATOR)137         VALIDATORS.put(Global.WIFI_SCAN_THROTTLE_ENABLED, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.APP_AUTO_RESTRICTION_ENABLED, BOOLEAN_VALIDATOR)138         VALIDATORS.put(Global.APP_AUTO_RESTRICTION_ENABLED, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.ZEN_DURATION, ANY_INTEGER_VALIDATOR)139         VALIDATORS.put(Global.ZEN_DURATION, ANY_INTEGER_VALIDATOR);
VALIDATORS.put(Global.CHARGING_VIBRATION_ENABLED, BOOLEAN_VALIDATOR)140         VALIDATORS.put(Global.CHARGING_VIBRATION_ENABLED, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.DEVICE_PROVISIONING_MOBILE_DATA_ENABLED, BOOLEAN_VALIDATOR)141         VALIDATORS.put(Global.DEVICE_PROVISIONING_MOBILE_DATA_ENABLED, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.REQUIRE_PASSWORD_TO_DECRYPT, BOOLEAN_VALIDATOR)142         VALIDATORS.put(Global.REQUIRE_PASSWORD_TO_DECRYPT, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.DEVICE_DEMO_MODE, BOOLEAN_VALIDATOR)143         VALIDATORS.put(Global.DEVICE_DEMO_MODE, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.AWARE_ALLOWED, BOOLEAN_VALIDATOR)144         VALIDATORS.put(Global.AWARE_ALLOWED, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.POWER_BUTTON_LONG_PRESS, new InclusiveIntegerRangeValidator(0, 5))145         VALIDATORS.put(Global.POWER_BUTTON_LONG_PRESS, new InclusiveIntegerRangeValidator(0, 5));
VALIDATORS.put( Global.POWER_BUTTON_VERY_LONG_PRESS, new InclusiveIntegerRangeValidator(0, 1))146         VALIDATORS.put(
147                 Global.POWER_BUTTON_VERY_LONG_PRESS, new InclusiveIntegerRangeValidator(0, 1));
VALIDATORS.put(Global.NOTIFICATION_BUBBLES, BOOLEAN_VALIDATOR)148         VALIDATORS.put(Global.NOTIFICATION_BUBBLES, BOOLEAN_VALIDATOR);
VALIDATORS.put(Global.CUSTOM_BUGREPORT_HANDLER_APP, ANY_STRING_VALIDATOR)149         VALIDATORS.put(Global.CUSTOM_BUGREPORT_HANDLER_APP, ANY_STRING_VALIDATOR);
VALIDATORS.put(Global.CUSTOM_BUGREPORT_HANDLER_USER, ANY_INTEGER_VALIDATOR)150         VALIDATORS.put(Global.CUSTOM_BUGREPORT_HANDLER_USER, ANY_INTEGER_VALIDATOR);
VALIDATORS.put(Global.DEVELOPMENT_SETTINGS_ENABLED, BOOLEAN_VALIDATOR)151         VALIDATORS.put(Global.DEVELOPMENT_SETTINGS_ENABLED, BOOLEAN_VALIDATOR);
152     }
153 }
154