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.BOOLEAN_VALIDATOR; 20 import static android.provider.settings.validators.SettingsValidators.COMPONENT_NAME_VALIDATOR; 21 import static android.provider.settings.validators.SettingsValidators.DATE_FORMAT_VALIDATOR; 22 import static android.provider.settings.validators.SettingsValidators.LENIENT_IP_ADDRESS_VALIDATOR; 23 import static android.provider.settings.validators.SettingsValidators.NON_NEGATIVE_INTEGER_VALIDATOR; 24 import static android.provider.settings.validators.SettingsValidators.URI_VALIDATOR; 25 import static android.provider.settings.validators.SettingsValidators.VIBRATION_INTENSITY_VALIDATOR; 26 27 import android.annotation.Nullable; 28 import android.compat.annotation.UnsupportedAppUsage; 29 import android.content.ComponentName; 30 import android.hardware.display.ColorDisplayManager; 31 import android.os.BatteryManager; 32 import android.provider.Settings.System; 33 import android.util.ArrayMap; 34 35 import java.util.Map; 36 37 /** 38 * Validators for System settings 39 */ 40 public class SystemSettingsValidators { 41 /** 42 * These are all public system settings 43 * 44 * <p>All settings in {@link System.SETTINGS_TO_BACKUP} array *must* have a non-null validator, 45 * otherwise they won't be restored. 46 */ 47 @UnsupportedAppUsage 48 public static final Map<String, Validator> VALIDATORS = new ArrayMap<>(); 49 50 static { VALIDATORS.put( System.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; } })51 VALIDATORS.put( 52 System.STAY_ON_WHILE_PLUGGED_IN, 53 value -> { 54 try { 55 int val = Integer.parseInt(value); 56 return (val == 0) 57 || (val == BatteryManager.BATTERY_PLUGGED_AC) 58 || (val == BatteryManager.BATTERY_PLUGGED_USB) 59 || (val == BatteryManager.BATTERY_PLUGGED_WIRELESS) 60 || (val 61 == (BatteryManager.BATTERY_PLUGGED_AC 62 | BatteryManager.BATTERY_PLUGGED_USB)) 63 || (val 64 == (BatteryManager.BATTERY_PLUGGED_AC 65 | BatteryManager.BATTERY_PLUGGED_WIRELESS)) 66 || (val 67 == (BatteryManager.BATTERY_PLUGGED_USB 68 | BatteryManager.BATTERY_PLUGGED_WIRELESS)) 69 || (val 70 == (BatteryManager.BATTERY_PLUGGED_AC 71 | BatteryManager.BATTERY_PLUGGED_USB 72 | BatteryManager.BATTERY_PLUGGED_WIRELESS)); 73 } catch (NumberFormatException e) { 74 return false; 75 } 76 }); VALIDATORS.put(System.END_BUTTON_BEHAVIOR, new InclusiveIntegerRangeValidator(0, 3))77 VALIDATORS.put(System.END_BUTTON_BEHAVIOR, new InclusiveIntegerRangeValidator(0, 3)); VALIDATORS.put(System.WIFI_USE_STATIC_IP, BOOLEAN_VALIDATOR)78 VALIDATORS.put(System.WIFI_USE_STATIC_IP, BOOLEAN_VALIDATOR); VALIDATORS.put(System.BLUETOOTH_DISCOVERABILITY, new InclusiveIntegerRangeValidator(0, 2))79 VALIDATORS.put(System.BLUETOOTH_DISCOVERABILITY, new InclusiveIntegerRangeValidator(0, 2)); VALIDATORS.put(System.BLUETOOTH_DISCOVERABILITY_TIMEOUT, NON_NEGATIVE_INTEGER_VALIDATOR)80 VALIDATORS.put(System.BLUETOOTH_DISCOVERABILITY_TIMEOUT, NON_NEGATIVE_INTEGER_VALIDATOR); VALIDATORS.put( System.NEXT_ALARM_FORMATTED, new Validator() { private static final int MAX_LENGTH = 1000; @Override public boolean validate(String value) { return value == null || value.length() < MAX_LENGTH; } })81 VALIDATORS.put( 82 System.NEXT_ALARM_FORMATTED, 83 new Validator() { 84 private static final int MAX_LENGTH = 1000; 85 86 @Override 87 public boolean validate(String value) { 88 // TODO: No idea what the correct format is. 89 return value == null || value.length() < MAX_LENGTH; 90 } 91 }); VALIDATORS.put( System.FONT_SCALE, value -> { try { return Float.parseFloat(value) >= 0; } catch (NumberFormatException | NullPointerException e) { return false; } })92 VALIDATORS.put( 93 System.FONT_SCALE, 94 value -> { 95 try { 96 return Float.parseFloat(value) >= 0; 97 } catch (NumberFormatException | NullPointerException e) { 98 return false; 99 } 100 }); VALIDATORS.put(System.DIM_SCREEN, BOOLEAN_VALIDATOR)101 VALIDATORS.put(System.DIM_SCREEN, BOOLEAN_VALIDATOR); VALIDATORS.put( System.DISPLAY_COLOR_MODE, new Validator() { @Override public boolean validate(@Nullable String value) { try { final int setting = Integer.parseInt(value); final boolean isInFrameworkRange = setting >= ColorDisplayManager.COLOR_MODE_NATURAL && setting <= ColorDisplayManager.COLOR_MODE_AUTOMATIC; final boolean isInVendorRange = setting >= ColorDisplayManager.VENDOR_COLOR_MODE_RANGE_MIN && setting <= ColorDisplayManager .VENDOR_COLOR_MODE_RANGE_MAX; return isInFrameworkRange || isInVendorRange; } catch (NumberFormatException | NullPointerException e) { return false; } } })102 VALIDATORS.put( 103 System.DISPLAY_COLOR_MODE, 104 new Validator() { 105 @Override 106 public boolean validate(@Nullable String value) { 107 // Assume the actual validation that this device can properly handle this 108 // kind of 109 // color mode further down in ColorDisplayManager / ColorDisplayService. 110 try { 111 final int setting = Integer.parseInt(value); 112 final boolean isInFrameworkRange = 113 setting >= ColorDisplayManager.COLOR_MODE_NATURAL 114 && setting <= ColorDisplayManager.COLOR_MODE_AUTOMATIC; 115 final boolean isInVendorRange = 116 setting >= ColorDisplayManager.VENDOR_COLOR_MODE_RANGE_MIN 117 && setting 118 <= ColorDisplayManager 119 .VENDOR_COLOR_MODE_RANGE_MAX; 120 return isInFrameworkRange || isInVendorRange; 121 } catch (NumberFormatException | NullPointerException e) { 122 return false; 123 } 124 } 125 }); VALIDATORS.put(System.SCREEN_OFF_TIMEOUT, NON_NEGATIVE_INTEGER_VALIDATOR)126 VALIDATORS.put(System.SCREEN_OFF_TIMEOUT, NON_NEGATIVE_INTEGER_VALIDATOR); VALIDATORS.put(System.SCREEN_BRIGHTNESS_FOR_VR, new InclusiveIntegerRangeValidator(0, 255))127 VALIDATORS.put(System.SCREEN_BRIGHTNESS_FOR_VR, new InclusiveIntegerRangeValidator(0, 255)); VALIDATORS.put(System.SCREEN_BRIGHTNESS_MODE, BOOLEAN_VALIDATOR)128 VALIDATORS.put(System.SCREEN_BRIGHTNESS_MODE, BOOLEAN_VALIDATOR); VALIDATORS.put(System.ADAPTIVE_SLEEP, BOOLEAN_VALIDATOR)129 VALIDATORS.put(System.ADAPTIVE_SLEEP, BOOLEAN_VALIDATOR); VALIDATORS.put(System.MODE_RINGER_STREAMS_AFFECTED, NON_NEGATIVE_INTEGER_VALIDATOR)130 VALIDATORS.put(System.MODE_RINGER_STREAMS_AFFECTED, NON_NEGATIVE_INTEGER_VALIDATOR); VALIDATORS.put(System.MUTE_STREAMS_AFFECTED, NON_NEGATIVE_INTEGER_VALIDATOR)131 VALIDATORS.put(System.MUTE_STREAMS_AFFECTED, NON_NEGATIVE_INTEGER_VALIDATOR); VALIDATORS.put(System.VIBRATE_ON, BOOLEAN_VALIDATOR)132 VALIDATORS.put(System.VIBRATE_ON, BOOLEAN_VALIDATOR); VALIDATORS.put(System.NOTIFICATION_VIBRATION_INTENSITY, VIBRATION_INTENSITY_VALIDATOR)133 VALIDATORS.put(System.NOTIFICATION_VIBRATION_INTENSITY, VIBRATION_INTENSITY_VALIDATOR); VALIDATORS.put(System.RING_VIBRATION_INTENSITY, VIBRATION_INTENSITY_VALIDATOR)134 VALIDATORS.put(System.RING_VIBRATION_INTENSITY, VIBRATION_INTENSITY_VALIDATOR); VALIDATORS.put(System.HAPTIC_FEEDBACK_INTENSITY, VIBRATION_INTENSITY_VALIDATOR)135 VALIDATORS.put(System.HAPTIC_FEEDBACK_INTENSITY, VIBRATION_INTENSITY_VALIDATOR); VALIDATORS.put(System.RINGTONE, URI_VALIDATOR)136 VALIDATORS.put(System.RINGTONE, URI_VALIDATOR); VALIDATORS.put(System.NOTIFICATION_SOUND, URI_VALIDATOR)137 VALIDATORS.put(System.NOTIFICATION_SOUND, URI_VALIDATOR); VALIDATORS.put(System.ALARM_ALERT, URI_VALIDATOR)138 VALIDATORS.put(System.ALARM_ALERT, URI_VALIDATOR); VALIDATORS.put(System.TEXT_AUTO_REPLACE, BOOLEAN_VALIDATOR)139 VALIDATORS.put(System.TEXT_AUTO_REPLACE, BOOLEAN_VALIDATOR); VALIDATORS.put(System.TEXT_AUTO_CAPS, BOOLEAN_VALIDATOR)140 VALIDATORS.put(System.TEXT_AUTO_CAPS, BOOLEAN_VALIDATOR); VALIDATORS.put(System.TEXT_AUTO_PUNCTUATE, BOOLEAN_VALIDATOR)141 VALIDATORS.put(System.TEXT_AUTO_PUNCTUATE, BOOLEAN_VALIDATOR); VALIDATORS.put(System.TEXT_SHOW_PASSWORD, BOOLEAN_VALIDATOR)142 VALIDATORS.put(System.TEXT_SHOW_PASSWORD, BOOLEAN_VALIDATOR); VALIDATORS.put(System.AUTO_TIME, BOOLEAN_VALIDATOR)143 VALIDATORS.put(System.AUTO_TIME, BOOLEAN_VALIDATOR); VALIDATORS.put(System.AUTO_TIME_ZONE, BOOLEAN_VALIDATOR)144 VALIDATORS.put(System.AUTO_TIME_ZONE, BOOLEAN_VALIDATOR); VALIDATORS.put(System.SHOW_GTALK_SERVICE_STATUS, BOOLEAN_VALIDATOR)145 VALIDATORS.put(System.SHOW_GTALK_SERVICE_STATUS, BOOLEAN_VALIDATOR); VALIDATORS.put( System.WALLPAPER_ACTIVITY, new Validator() { private static final int MAX_LENGTH = 1000; @Override public boolean validate(String value) { if (value != null && value.length() > MAX_LENGTH) { return false; } return ComponentName.unflattenFromString(value) != null; } })146 VALIDATORS.put( 147 System.WALLPAPER_ACTIVITY, 148 new Validator() { 149 private static final int MAX_LENGTH = 1000; 150 151 @Override 152 public boolean validate(String value) { 153 if (value != null && value.length() > MAX_LENGTH) { 154 return false; 155 } 156 return ComponentName.unflattenFromString(value) != null; 157 } 158 }); VALIDATORS.put( System.TIME_12_24, new DiscreteValueValidator(new String[] {"12", "24", null}))159 VALIDATORS.put( 160 System.TIME_12_24, new DiscreteValueValidator(new String[] {"12", "24", null})); VALIDATORS.put(System.DATE_FORMAT, DATE_FORMAT_VALIDATOR)161 VALIDATORS.put(System.DATE_FORMAT, DATE_FORMAT_VALIDATOR); VALIDATORS.put(System.SETUP_WIZARD_HAS_RUN, BOOLEAN_VALIDATOR)162 VALIDATORS.put(System.SETUP_WIZARD_HAS_RUN, BOOLEAN_VALIDATOR); VALIDATORS.put(System.ACCELEROMETER_ROTATION, BOOLEAN_VALIDATOR)163 VALIDATORS.put(System.ACCELEROMETER_ROTATION, BOOLEAN_VALIDATOR); VALIDATORS.put(System.USER_ROTATION, new InclusiveIntegerRangeValidator(0, 3))164 VALIDATORS.put(System.USER_ROTATION, new InclusiveIntegerRangeValidator(0, 3)); VALIDATORS.put(System.DTMF_TONE_WHEN_DIALING, BOOLEAN_VALIDATOR)165 VALIDATORS.put(System.DTMF_TONE_WHEN_DIALING, BOOLEAN_VALIDATOR); VALIDATORS.put(System.SOUND_EFFECTS_ENABLED, BOOLEAN_VALIDATOR)166 VALIDATORS.put(System.SOUND_EFFECTS_ENABLED, BOOLEAN_VALIDATOR); VALIDATORS.put(System.HAPTIC_FEEDBACK_ENABLED, BOOLEAN_VALIDATOR)167 VALIDATORS.put(System.HAPTIC_FEEDBACK_ENABLED, BOOLEAN_VALIDATOR); VALIDATORS.put(System.POWER_SOUNDS_ENABLED, BOOLEAN_VALIDATOR)168 VALIDATORS.put(System.POWER_SOUNDS_ENABLED, BOOLEAN_VALIDATOR); VALIDATORS.put(System.DOCK_SOUNDS_ENABLED, BOOLEAN_VALIDATOR)169 VALIDATORS.put(System.DOCK_SOUNDS_ENABLED, BOOLEAN_VALIDATOR); VALIDATORS.put(System.SHOW_WEB_SUGGESTIONS, BOOLEAN_VALIDATOR)170 VALIDATORS.put(System.SHOW_WEB_SUGGESTIONS, BOOLEAN_VALIDATOR); VALIDATORS.put(System.WIFI_USE_STATIC_IP, BOOLEAN_VALIDATOR)171 VALIDATORS.put(System.WIFI_USE_STATIC_IP, BOOLEAN_VALIDATOR); VALIDATORS.put(System.ADVANCED_SETTINGS, BOOLEAN_VALIDATOR)172 VALIDATORS.put(System.ADVANCED_SETTINGS, BOOLEAN_VALIDATOR); VALIDATORS.put(System.SCREEN_AUTO_BRIGHTNESS_ADJ, new InclusiveFloatRangeValidator(-1, 1))173 VALIDATORS.put(System.SCREEN_AUTO_BRIGHTNESS_ADJ, new InclusiveFloatRangeValidator(-1, 1)); VALIDATORS.put(System.VIBRATE_INPUT_DEVICES, BOOLEAN_VALIDATOR)174 VALIDATORS.put(System.VIBRATE_INPUT_DEVICES, BOOLEAN_VALIDATOR); VALIDATORS.put(System.MASTER_MONO, BOOLEAN_VALIDATOR)175 VALIDATORS.put(System.MASTER_MONO, BOOLEAN_VALIDATOR); VALIDATORS.put(System.MASTER_BALANCE, new InclusiveFloatRangeValidator(-1.f, 1.f))176 VALIDATORS.put(System.MASTER_BALANCE, new InclusiveFloatRangeValidator(-1.f, 1.f)); VALIDATORS.put(System.NOTIFICATIONS_USE_RING_VOLUME, BOOLEAN_VALIDATOR)177 VALIDATORS.put(System.NOTIFICATIONS_USE_RING_VOLUME, BOOLEAN_VALIDATOR); VALIDATORS.put(System.VIBRATE_IN_SILENT, BOOLEAN_VALIDATOR)178 VALIDATORS.put(System.VIBRATE_IN_SILENT, BOOLEAN_VALIDATOR); VALIDATORS.put(System.MEDIA_BUTTON_RECEIVER, COMPONENT_NAME_VALIDATOR)179 VALIDATORS.put(System.MEDIA_BUTTON_RECEIVER, COMPONENT_NAME_VALIDATOR); VALIDATORS.put(System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, BOOLEAN_VALIDATOR)180 VALIDATORS.put(System.HIDE_ROTATION_LOCK_TOGGLE_FOR_ACCESSIBILITY, BOOLEAN_VALIDATOR); VALIDATORS.put(System.VIBRATE_WHEN_RINGING, BOOLEAN_VALIDATOR)181 VALIDATORS.put(System.VIBRATE_WHEN_RINGING, BOOLEAN_VALIDATOR); VALIDATORS.put(System.DTMF_TONE_TYPE_WHEN_DIALING, BOOLEAN_VALIDATOR)182 VALIDATORS.put(System.DTMF_TONE_TYPE_WHEN_DIALING, BOOLEAN_VALIDATOR); VALIDATORS.put(System.HEARING_AID, BOOLEAN_VALIDATOR)183 VALIDATORS.put(System.HEARING_AID, BOOLEAN_VALIDATOR); VALIDATORS.put(System.TTY_MODE, new InclusiveIntegerRangeValidator(0, 3))184 VALIDATORS.put(System.TTY_MODE, new InclusiveIntegerRangeValidator(0, 3)); VALIDATORS.put(System.NOTIFICATION_LIGHT_PULSE, BOOLEAN_VALIDATOR)185 VALIDATORS.put(System.NOTIFICATION_LIGHT_PULSE, BOOLEAN_VALIDATOR); VALIDATORS.put(System.POINTER_LOCATION, BOOLEAN_VALIDATOR)186 VALIDATORS.put(System.POINTER_LOCATION, BOOLEAN_VALIDATOR); VALIDATORS.put(System.SHOW_TOUCHES, BOOLEAN_VALIDATOR)187 VALIDATORS.put(System.SHOW_TOUCHES, BOOLEAN_VALIDATOR); VALIDATORS.put(System.WINDOW_ORIENTATION_LISTENER_LOG, BOOLEAN_VALIDATOR)188 VALIDATORS.put(System.WINDOW_ORIENTATION_LISTENER_LOG, BOOLEAN_VALIDATOR); VALIDATORS.put(System.LOCKSCREEN_SOUNDS_ENABLED, BOOLEAN_VALIDATOR)189 VALIDATORS.put(System.LOCKSCREEN_SOUNDS_ENABLED, BOOLEAN_VALIDATOR); VALIDATORS.put(System.LOCKSCREEN_DISABLED, BOOLEAN_VALIDATOR)190 VALIDATORS.put(System.LOCKSCREEN_DISABLED, BOOLEAN_VALIDATOR); VALIDATORS.put(System.SIP_RECEIVE_CALLS, BOOLEAN_VALIDATOR)191 VALIDATORS.put(System.SIP_RECEIVE_CALLS, BOOLEAN_VALIDATOR); VALIDATORS.put( System.SIP_CALL_OPTIONS, new DiscreteValueValidator(new String[] {"SIP_ALWAYS", "SIP_ADDRESS_ONLY"}))192 VALIDATORS.put( 193 System.SIP_CALL_OPTIONS, 194 new DiscreteValueValidator(new String[] {"SIP_ALWAYS", "SIP_ADDRESS_ONLY"})); VALIDATORS.put(System.SIP_ALWAYS, BOOLEAN_VALIDATOR)195 VALIDATORS.put(System.SIP_ALWAYS, BOOLEAN_VALIDATOR); VALIDATORS.put(System.SIP_ADDRESS_ONLY, BOOLEAN_VALIDATOR)196 VALIDATORS.put(System.SIP_ADDRESS_ONLY, BOOLEAN_VALIDATOR); VALIDATORS.put(System.SIP_ASK_ME_EACH_TIME, BOOLEAN_VALIDATOR)197 VALIDATORS.put(System.SIP_ASK_ME_EACH_TIME, BOOLEAN_VALIDATOR); VALIDATORS.put(System.POINTER_SPEED, new InclusiveFloatRangeValidator(-7, 7))198 VALIDATORS.put(System.POINTER_SPEED, new InclusiveFloatRangeValidator(-7, 7)); VALIDATORS.put(System.LOCK_TO_APP_ENABLED, BOOLEAN_VALIDATOR)199 VALIDATORS.put(System.LOCK_TO_APP_ENABLED, BOOLEAN_VALIDATOR); VALIDATORS.put( System.EGG_MODE, new Validator() { @Override public boolean validate(@Nullable String value) { try { return Long.parseLong(value) >= 0; } catch (NumberFormatException e) { return false; } } })200 VALIDATORS.put( 201 System.EGG_MODE, 202 new Validator() { 203 @Override 204 public boolean validate(@Nullable String value) { 205 try { 206 return Long.parseLong(value) >= 0; 207 } catch (NumberFormatException e) { 208 return false; 209 } 210 } 211 }); VALIDATORS.put(System.WIFI_STATIC_IP, LENIENT_IP_ADDRESS_VALIDATOR)212 VALIDATORS.put(System.WIFI_STATIC_IP, LENIENT_IP_ADDRESS_VALIDATOR); VALIDATORS.put(System.WIFI_STATIC_GATEWAY, LENIENT_IP_ADDRESS_VALIDATOR)213 VALIDATORS.put(System.WIFI_STATIC_GATEWAY, LENIENT_IP_ADDRESS_VALIDATOR); VALIDATORS.put(System.WIFI_STATIC_NETMASK, LENIENT_IP_ADDRESS_VALIDATOR)214 VALIDATORS.put(System.WIFI_STATIC_NETMASK, LENIENT_IP_ADDRESS_VALIDATOR); VALIDATORS.put(System.WIFI_STATIC_DNS1, LENIENT_IP_ADDRESS_VALIDATOR)215 VALIDATORS.put(System.WIFI_STATIC_DNS1, LENIENT_IP_ADDRESS_VALIDATOR); VALIDATORS.put(System.WIFI_STATIC_DNS2, LENIENT_IP_ADDRESS_VALIDATOR)216 VALIDATORS.put(System.WIFI_STATIC_DNS2, LENIENT_IP_ADDRESS_VALIDATOR); VALIDATORS.put(System.SHOW_BATTERY_PERCENT, BOOLEAN_VALIDATOR)217 VALIDATORS.put(System.SHOW_BATTERY_PERCENT, BOOLEAN_VALIDATOR); VALIDATORS.put(System.NOTIFICATION_LIGHT_PULSE, BOOLEAN_VALIDATOR)218 VALIDATORS.put(System.NOTIFICATION_LIGHT_PULSE, BOOLEAN_VALIDATOR); 219 } 220 } 221