Merge "Fix logic in validators"

This commit is contained in:
Al Sutton
2019-08-13 08:22:21 +00:00
committed by Android (Google) Code Review
3 changed files with 102 additions and 48 deletions

View File

@@ -7112,24 +7112,8 @@ public final class Settings {
*/
public static final String TTS_DEFAULT_LOCALE = "tts_default_locale";
private static final Validator TTS_DEFAULT_LOCALE_VALIDATOR = new Validator() {
@Override
public boolean validate(@Nullable String value) {
if (value == null || value.length() == 0) {
return false;
}
String[] ttsLocales = value.split(",");
boolean valid = true;
for (String ttsLocale : ttsLocales) {
String[] parts = ttsLocale.split(":");
valid |= ((parts.length == 2)
&& (parts[0].length() > 0)
&& ANY_STRING_VALIDATOR.validate(parts[0])
&& LOCALE_VALIDATOR.validate(parts[1]));
}
return valid;
}
};
private static final Validator TTS_DEFAULT_LOCALE_VALIDATOR =
new SettingsValidators.TTSListValidator();
/**
* Space delimited list of plugin packages that are enabled.
@@ -8649,25 +8633,13 @@ public final class Settings {
/**
* Holds comma separated list of ordering of QS tiles.
*
* @hide
*/
public static final String QS_TILES = "sysui_qs_tiles";
private static final Validator QS_TILES_VALIDATOR = new Validator() {
@Override
public boolean validate(@Nullable String value) {
if (value == null) {
return false;
}
String[] tiles = value.split(",");
boolean valid = true;
for (String tile : tiles) {
// tile can be any non-empty string as specified by OEM
valid |= ((tile.length() > 0) && ANY_STRING_VALIDATOR.validate(tile));
}
return valid;
}
};
private static final Validator QS_TILES_VALIDATOR =
new SettingsValidators.TileListValidator();
/**
* Specifies whether the web action API is enabled.
@@ -8733,21 +8705,8 @@ public final class Settings {
*/
public static final String QS_AUTO_ADDED_TILES = "qs_auto_tiles";
private static final Validator QS_AUTO_ADDED_TILES_VALIDATOR = new Validator() {
@Override
public boolean validate(@Nullable String value) {
if (value == null) {
return false;
}
String[] tiles = value.split(",");
boolean valid = true;
for (String tile : tiles) {
// tile can be any non-empty string as specified by OEM
valid |= ((tile.length() > 0) && ANY_STRING_VALIDATOR.validate(tile));
}
return valid;
}
};
private static final Validator QS_AUTO_ADDED_TILES_VALIDATOR =
new SettingsValidators.TileListValidator();
/**
* Whether the Lockdown button should be shown in the power menu.

View File

@@ -283,4 +283,50 @@ public class SettingsValidators {
return true;
}
}
private abstract static class ListValidator implements Validator {
public boolean validate(@Nullable String value) {
if (!isEntryValid(value)) {
return false;
}
String[] items = value.split(",");
for (String item : items) {
if (!isItemValid(item)) {
return false;
}
}
return true;
}
protected abstract boolean isEntryValid(String entry);
protected abstract boolean isItemValid(String item);
}
/** Ensure a restored value is a string in the format the text-to-speech system handles */
public static final class TTSListValidator extends ListValidator {
protected boolean isEntryValid(String entry) {
return entry != null && entry.length() > 0;
}
protected boolean isItemValid(String item) {
String[] parts = item.split(":");
// Replaces any old language separator (-) with the new one (_)
return ((parts.length == 2)
&& (parts[0].length() > 0)
&& ANY_STRING_VALIDATOR.validate(parts[0])
&& LOCALE_VALIDATOR.validate(parts[1].replace('-', '_')));
}
}
/** Ensure a restored value is suitable to be used as a tile name */
public static final class TileListValidator extends ListValidator {
protected boolean isEntryValid(String entry) {
return entry != null;
}
protected boolean isItemValid(String item) {
return item.length() > 0 && ANY_STRING_VALIDATOR.validate(item);
}
}
}