Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fd8b3611 authored by Daniel Norman's avatar Daniel Norman
Browse files

Fixes a11y captioning B&R validators for font scale and locale.

- Font scale: can go down to 0.25, so update the validator range
- Locale: Use a "loose" locale validator which allows subset
          matching against the installed locale set, since this
          setting may not include locale details like Script
          while still representing a valid locale for the device.

Fix: 384862163
Change-Id: I05f14666a7013ac9fadee86787bf688c8b19c945
Test: atest SettingsValidatorsTest
Test: perform B&R of these settings; see b/384862163  comment 10
Flag: EXEMPT low-risk bugfix
parent 0af50eb3
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ import static android.provider.settings.validators.SettingsValidators.COMMA_SEPA
import static android.provider.settings.validators.SettingsValidators.COMPONENT_NAME_VALIDATOR;
import static android.provider.settings.validators.SettingsValidators.CREDENTIAL_SERVICE_VALIDATOR;
import static android.provider.settings.validators.SettingsValidators.JSON_OBJECT_VALIDATOR;
import static android.provider.settings.validators.SettingsValidators.LOCALE_VALIDATOR;
import static android.provider.settings.validators.SettingsValidators.LOCALE_LOOSE_VALIDATOR;
import static android.provider.settings.validators.SettingsValidators.NONE_NEGATIVE_LONG_VALIDATOR;
import static android.provider.settings.validators.SettingsValidators.NON_NEGATIVE_INTEGER_VALIDATOR;
import static android.provider.settings.validators.SettingsValidators.NULLABLE_COMPONENT_NAME_VALIDATOR;
@@ -96,7 +96,7 @@ public class SecureSettingsValidators {
                Secure.ACCESSIBILITY_CAPTIONING_PRESET,
                new DiscreteValueValidator(new String[] {"-1", "0", "1", "2", "3", "4"}));
        VALIDATORS.put(Secure.ACCESSIBILITY_CAPTIONING_ENABLED, BOOLEAN_VALIDATOR);
        VALIDATORS.put(Secure.ACCESSIBILITY_CAPTIONING_LOCALE, LOCALE_VALIDATOR);
        VALIDATORS.put(Secure.ACCESSIBILITY_CAPTIONING_LOCALE, LOCALE_LOOSE_VALIDATOR);
        VALIDATORS.put(Secure.ACCESSIBILITY_CAPTIONING_BACKGROUND_COLOR, ANY_INTEGER_VALIDATOR);
        VALIDATORS.put(Secure.ACCESSIBILITY_CAPTIONING_FOREGROUND_COLOR, ANY_INTEGER_VALIDATOR);
        VALIDATORS.put(
@@ -109,7 +109,7 @@ public class SecureSettingsValidators {
                        new String[] {"DEFAULT", "MONOSPACE", "SANS_SERIF", "SERIF"}));
        VALIDATORS.put(
                Secure.ACCESSIBILITY_CAPTIONING_FONT_SCALE,
                new InclusiveFloatRangeValidator(0.5f, 2.0f));
                new InclusiveFloatRangeValidator(0.25f, 2.0f));
        VALIDATORS.put(Secure.ACCESSIBILITY_CAPTIONING_WINDOW_COLOR, ANY_INTEGER_VALIDATOR);
        VALIDATORS.put(Secure.FONT_WEIGHT_ADJUSTMENT, ANY_INTEGER_VALIDATOR);
        VALIDATORS.put(Secure.REDUCE_BRIGHT_COLORS_LEVEL, PERCENTAGE_INTEGER_VALIDATOR);
+32 −0
Original line number Diff line number Diff line
@@ -188,6 +188,38 @@ public class SettingsValidators {
        }
    };

    /**
     * Similar to {@link #LOCALE_VALIDATOR} but allows loose/subset matches against the list
     * from {@link Locale#getAvailableLocales()}.
     *
     * <p>Expects that the string is '_'-separated with 1 to 3 parts. Then checks the parts against
     * the locale list, returning true if any available locale matches the parts provided using
     * case insensitive string comparison.
     * <li>Part 1: match against {@link Locale#getLanguage()}</li>
     * <li>Part 2, if present: match against {@link Locale#getCountry()}</li>
     * <li>Part 3, if present: match against {@link Locale#getVariant()}</li>
     */
    public static final Validator LOCALE_LOOSE_VALIDATOR = value -> {
        if (value == null) {
            return true;
        }
        String[] parts = value.split("_", 3);
        Locale[] validLocales = Locale.getAvailableLocales();
        for (Locale locale : validLocales) {
            if (!parts[0].equalsIgnoreCase(locale.getLanguage())) {
                continue;
            }
            if (parts.length >= 2 && !parts[1].equalsIgnoreCase(locale.getCountry())) {
                continue;
            }
            if (parts.length == 3 && !parts[2].equalsIgnoreCase(locale.getVariant())) {
                continue;
            }
            return true;
        }
        return false;
    };

    /** {@link Validator} that checks whether a value is a valid {@link JSONObject}. */
    public static final Validator JSON_OBJECT_VALIDATOR = (value) -> {
        if (value == null) {
+13 −0
Original line number Diff line number Diff line
@@ -124,6 +124,19 @@ public class SettingsValidatorsTest {
        assertTrue(SettingsValidators.LOCALE_VALIDATOR.validate(null));
    }

    @Test
    public void testLocaleLooseValidator() {
        assertTrue(SettingsValidators.LOCALE_LOOSE_VALIDATOR.validate(null));
        assertTrue(SettingsValidators.LOCALE_LOOSE_VALIDATOR.validate("en_US"));
        assertTrue(SettingsValidators.LOCALE_LOOSE_VALIDATOR.validate("en_us"));
        assertTrue(SettingsValidators.LOCALE_LOOSE_VALIDATOR.validate("es"));
        assertTrue(SettingsValidators.LOCALE_LOOSE_VALIDATOR.validate("zh_TW"));
        assertTrue(SettingsValidators.LOCALE_LOOSE_VALIDATOR.validate("en_US_POSIX"));
        assertFalse(SettingsValidators.LOCALE_LOOSE_VALIDATOR.validate("en_FAKE"));
        assertFalse(SettingsValidators.LOCALE_LOOSE_VALIDATOR.validate("en_US_FAKE"));
        assertFalse(SettingsValidators.LOCALE_LOOSE_VALIDATOR.validate("rectangle"));
    }

    @Test
    public void testPackageNameValidator() {
        assertTrue(SettingsValidators.PACKAGE_NAME_VALIDATOR.validate(