Al Sutton | 91f89d0 | 2019-08-16 12:56:57 +0100 | [diff] [blame^] | 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 { |
| 44 | VALIDATORS.put(Global.APPLY_RAMPING_RINGER, BOOLEAN_VALIDATOR); |
| 45 | VALIDATORS.put(Global.BUGREPORT_IN_POWER_MENU, BOOLEAN_VALIDATOR); |
| 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 | }); |
| 72 | VALIDATORS.put(Global.AUTO_TIME, BOOLEAN_VALIDATOR); |
| 73 | VALIDATORS.put(Global.AUTO_TIME_ZONE, BOOLEAN_VALIDATOR); |
| 74 | VALIDATORS.put(Global.POWER_SOUNDS_ENABLED, BOOLEAN_VALIDATOR); |
| 75 | VALIDATORS.put(Global.DOCK_SOUNDS_ENABLED, BOOLEAN_VALIDATOR); |
| 76 | VALIDATORS.put(Global.CHARGING_SOUNDS_ENABLED, BOOLEAN_VALIDATOR); |
| 77 | VALIDATORS.put(Global.USB_MASS_STORAGE_ENABLED, BOOLEAN_VALIDATOR); |
| 78 | VALIDATORS.put( |
| 79 | Global.NETWORK_RECOMMENDATIONS_ENABLED, |
| 80 | new DiscreteValueValidator(new String[] {"-1", "0", "1"})); |
| 81 | VALIDATORS.put(Global.WIFI_WAKEUP_ENABLED, BOOLEAN_VALIDATOR); |
| 82 | VALIDATORS.put(Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, BOOLEAN_VALIDATOR); |
| 83 | VALIDATORS.put( |
| 84 | Global.USE_OPEN_WIFI_PACKAGE, |
| 85 | value -> (value == null) || PACKAGE_NAME_VALIDATOR.validate(value)); |
| 86 | VALIDATORS.put(Global.WIFI_WATCHDOG_POOR_NETWORK_TEST_ENABLED, ANY_STRING_VALIDATOR); |
| 87 | VALIDATORS.put( |
| 88 | Global.EMERGENCY_TONE, new DiscreteValueValidator(new String[] {"0", "1", "2"})); |
| 89 | VALIDATORS.put(Global.CALL_AUTO_RETRY, BOOLEAN_VALIDATOR); |
| 90 | VALIDATORS.put(Global.DOCK_AUDIO_MEDIA_ENABLED, BOOLEAN_VALIDATOR); |
| 91 | VALIDATORS.put( |
| 92 | Global.ENABLE_AUTOMATIC_SYSTEM_SERVER_HEAP_DUMPS, |
| 93 | new DiscreteValueValidator(new String[] {"0", "1"})); |
| 94 | VALIDATORS.put( |
| 95 | Global.ENCODED_SURROUND_OUTPUT, |
| 96 | new DiscreteValueValidator(new String[] {"0", "1", "2", "3"})); |
| 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 | }); |
| 120 | VALIDATORS.put( |
| 121 | Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_LEVEL, |
| 122 | new InclusiveIntegerRangeValidator(0, 100)); |
| 123 | VALIDATORS.put( |
| 124 | Global.LOW_POWER_MODE_STICKY_AUTO_DISABLE_ENABLED, |
| 125 | new DiscreteValueValidator(new String[] {"0", "1"})); |
| 126 | VALIDATORS.put(Global.LOW_POWER_MODE_TRIGGER_LEVEL, PERCENTAGE_INTEGER_VALIDATOR); |
| 127 | VALIDATORS.put(Global.LOW_POWER_MODE_TRIGGER_LEVEL_MAX, PERCENTAGE_INTEGER_VALIDATOR); |
| 128 | VALIDATORS.put( |
| 129 | Global.AUTOMATIC_POWER_SAVE_MODE, |
| 130 | new DiscreteValueValidator(new String[] {"0", "1"})); |
| 131 | VALIDATORS.put( |
| 132 | Global.DYNAMIC_POWER_SAVINGS_DISABLE_THRESHOLD, PERCENTAGE_INTEGER_VALIDATOR); |
| 133 | VALIDATORS.put(Global.BLUETOOTH_ON, BOOLEAN_VALIDATOR); |
| 134 | VALIDATORS.put(Global.PRIVATE_DNS_MODE, ANY_STRING_VALIDATOR); |
| 135 | VALIDATORS.put(Global.PRIVATE_DNS_SPECIFIER, ANY_STRING_VALIDATOR); |
| 136 | VALIDATORS.put(Global.SOFT_AP_TIMEOUT_ENABLED, BOOLEAN_VALIDATOR); |
| 137 | VALIDATORS.put(Global.WIFI_SCAN_THROTTLE_ENABLED, BOOLEAN_VALIDATOR); |
| 138 | VALIDATORS.put(Global.APP_AUTO_RESTRICTION_ENABLED, BOOLEAN_VALIDATOR); |
| 139 | VALIDATORS.put(Global.ZEN_DURATION, ANY_INTEGER_VALIDATOR); |
| 140 | VALIDATORS.put(Global.CHARGING_VIBRATION_ENABLED, BOOLEAN_VALIDATOR); |
| 141 | VALIDATORS.put(Global.DEVICE_PROVISIONING_MOBILE_DATA_ENABLED, BOOLEAN_VALIDATOR); |
| 142 | VALIDATORS.put(Global.REQUIRE_PASSWORD_TO_DECRYPT, BOOLEAN_VALIDATOR); |
| 143 | VALIDATORS.put(Global.DEVICE_DEMO_MODE, BOOLEAN_VALIDATOR); |
| 144 | VALIDATORS.put(Global.WIFI_PNO_FREQUENCY_CULLING_ENABLED, BOOLEAN_VALIDATOR); |
| 145 | VALIDATORS.put(Global.WIFI_PNO_RECENCY_SORTING_ENABLED, BOOLEAN_VALIDATOR); |
| 146 | VALIDATORS.put(Global.WIFI_LINK_PROBING_ENABLED, BOOLEAN_VALIDATOR); |
| 147 | VALIDATORS.put(Global.AWARE_ALLOWED, BOOLEAN_VALIDATOR); |
| 148 | VALIDATORS.put(Global.POWER_BUTTON_LONG_PRESS, new InclusiveIntegerRangeValidator(0, 5)); |
| 149 | VALIDATORS.put( |
| 150 | Global.POWER_BUTTON_VERY_LONG_PRESS, new InclusiveIntegerRangeValidator(0, 1)); |
| 151 | } |
| 152 | } |