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

Commit bcdcae5f authored by Calvin Pan's avatar Calvin Pan
Browse files

Filter duplicate language code and script in system locale restoration

The new system language design restricts multiple country/region locales
for the same language code. Therefore, the system language restoration
flow must apply the same restriction.

Bug: 403442408
Test: run the test
Flag: EXEMPT bugfix
Change-Id: I1287483fdb56a00029ae1b03c86425361d4cdd1d
parent 42557184
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
@@ -54,6 +54,7 @@ import com.android.settingslib.devicestate.DeviceStateRotationLockSettingsManage
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
@@ -650,6 +651,10 @@ public class SettingsHelper {
     *   e.g. current locale "en-US,zh-CN" and backup locale "ja-JP,zh-Hans-CN,en-US" are merged to
     *   "en-US,zh-CN,ja-JP".
     *
     * - Same language codes and scripts are dropped.
     *   e.g. current locale "en-US, zh-Hans-TW" and backup locale "en-UK, en-GB, zh-Hans-HK" are
     *   merged to "en-US, zh-Hans-TW".
     *
     * - Unsupported locales are dropped.
     *   e.g. current locale "en-US" and backup locale "ja-JP,zh-CN" but the supported locales
     *   are "en-US,zh-CN", the merged locale list is "en-US,zh-CN".
@@ -683,13 +688,23 @@ public class SettingsHelper {
            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();

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

        if (filtered.size() == current.size()) {
            return current;  // Nothing added to current locale list.
        }
+5 −1
Original line number Diff line number Diff line
@@ -388,7 +388,11 @@ 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"),
                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
    }

    @Test