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

Commit 3c7fe2cc authored by Calvin Pan's avatar Calvin Pan Committed by Android (Google) Code Review
Browse files

Merge "Fix missing to check the existing locale in system language restoration" into main

parents d6d06458 71b36a04
Loading
Loading
Loading
Loading
+23 −16
Original line number Diff line number Diff line
@@ -672,6 +672,7 @@ public class SettingsHelper {
    public static LocaleList resolveLocales(LocaleList restore, LocaleList current,
            String[] supportedLocales) {
        final HashMap<Locale, Locale> allLocales = new HashMap<>(supportedLocales.length);
        final HashSet<String> existingLanguageAndScript = new HashSet<>();
        for (String supportedLocaleStr : supportedLocales) {
            final Locale locale = Locale.forLanguageTag(supportedLocaleStr);
            allLocales.put(toFullLocale(locale), locale);
@@ -679,32 +680,28 @@ public class SettingsHelper {

        // After restoring to reset locales, need to get extensions from restored locale. Get the
        // first restored locale to check its extension.
        final Locale restoredLocale = restore.isEmpty()
        final Locale firstRestoredLocale = restore.isEmpty()
                ? Locale.ROOT
                : restore.get(0);
        final ArrayList<Locale> filtered = new ArrayList<>(current.size());
        for (int i = 0; i < current.size(); i++) {
            Locale locale = copyExtensionToTargetLocale(restoredLocale, current.get(i));
            Locale locale = copyExtensionToTargetLocale(firstRestoredLocale, current.get(i));

            if (locale != null && existingLanguageAndScript.add(getLanguageAndScript(locale))) {
                allLocales.remove(toFullLocale(locale));
                filtered.add(locale);
            }
        }

        final HashSet<String> existingLanguageAndScript = new HashSet<>();
        for (int i = 0; i < restore.size(); i++) {
            final Locale restoredLocaleWithExtension = copyExtensionToTargetLocale(restoredLocale,
                    getFilteredLocale(restore.get(i), allLocales));

            if (restoredLocaleWithExtension != null) {
                String language = restoredLocaleWithExtension.getLanguage();
                String script = restoredLocaleWithExtension.getScript();
            final Locale restoredLocaleWithExtension = copyExtensionToTargetLocale(
                    firstRestoredLocale, getFilteredLocale(restore.get(i), allLocales));

                String restoredLanguageAndScript =
                        script == null ? language : language + "-" + script;
                if (existingLanguageAndScript.add(restoredLanguageAndScript)) {
            if (restoredLocaleWithExtension != null && existingLanguageAndScript.add(
                    getLanguageAndScript(restoredLocaleWithExtension))) {
                filtered.add(restoredLocaleWithExtension);
            }
        }
        }

        if (filtered.size() == current.size()) {
            return current;  // Nothing added to current locale list.
@@ -713,6 +710,16 @@ public class SettingsHelper {
        return new LocaleList(filtered.toArray(new Locale[filtered.size()]));
    }

    private static String getLanguageAndScript(Locale locale) {
        if (locale == null) {
            return "";
        }

        String language = locale.getLanguage();
        String script = locale.getScript();
        return script == null ? language : String.join("-", language, script);
    }

    private static Locale copyExtensionToTargetLocale(Locale restoredLocale,
            Locale targetLocale) {
        if (!restoredLocale.hasExtensions()) {
+11 −4
Original line number Diff line number Diff line
@@ -388,11 +388,18 @@ public class SettingsHelperTest {
                        LocaleList.forLanguageTags("zh-Hant-TW"),  // current
                        new String[] { "fa-Arab-AF-u-nu-latn", "zh-Hant-TW" }));  // supported

        assertEquals(LocaleList.forLanguageTags("en-US,zh-Hans-TW"),
        assertEquals(LocaleList.forLanguageTags("en-US,zh-Hans-TW,fr-FR"),
                SettingsHelper.resolveLocales(
                        LocaleList.forLanguageTags("en-UK,en-GB,zh-Hans-HK"),  // restore
                        LocaleList.forLanguageTags("en-US,zh-Hans-TW"),  // current
                        new String[] { "en-US,zh-Hans-TW,en-UK,en-GB,zh-Hans-HK" }));  // supported
                        // restore
                        LocaleList.forLanguageTags("en-UK,en-GB,zh-Hans-HK,fr-FR"),

                        // current
                        LocaleList.forLanguageTags("en-US,zh-Hans-TW"),

                        // supported
                        new String[] {
                                "en-US" , "zh-Hans-TW" , "en-UK", "en-GB", "zh-Hans-HK", "fr-FR"
                        }));
    }

    @Test