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

Commit 985b050e authored by Allen Su's avatar Allen Su Committed by Automerger Merge Worker
Browse files

Merge "[Panlingual] numbering system and suggestion flag" into udc-dev am: b1cd4937

parents 250c6c8e b1cd4937
Loading
Loading
Loading
Loading
+42 −19
Original line number Original line Diff line number Diff line
@@ -154,12 +154,16 @@ public class AppLocaleCollector implements LocalePickerWithRegion.LocaleCollecto
    }
    }


    /**
    /**
     * Get the locales that system activates.
     * Get a list of system locale that removes all extensions except for the numbering system.
     * @return A set which includes all the locales that system activates.
     */
     */
    @VisibleForTesting
    @VisibleForTesting
    public List<LocaleStore.LocaleInfo> getSystemCurrentLocale() {
    public List<LocaleStore.LocaleInfo> getSystemCurrentLocales() {
        return LocaleStore.getSystemCurrentLocaleInfo();
        List<LocaleStore.LocaleInfo> sysLocales = LocaleStore.getSystemCurrentLocales();
        return sysLocales.stream().filter(
                // For the locale to be added into the suggestion area, its country could not be
                // empty.
                info -> info.getLocale().getCountry().length() > 0).collect(
                Collectors.toList());
    }
    }


    @Override
    @Override
@@ -220,12 +224,15 @@ public class AppLocaleCollector implements LocalePickerWithRegion.LocaleCollecto


        // Add current system language into suggestion list
        // Add current system language into suggestion list
        if (!isForCountryMode) {
        if (!isForCountryMode) {
            boolean isCurrentLocale, isInAppOrIme;
            boolean isCurrentLocale, existsInApp, existsInIme;
            for (LocaleStore.LocaleInfo localeInfo : getSystemCurrentLocale()) {
            for (LocaleStore.LocaleInfo localeInfo : getSystemCurrentLocales()) {
                isCurrentLocale = mAppCurrentLocale != null
                isCurrentLocale = mAppCurrentLocale != null
                        && localeInfo.getLocale().equals(mAppCurrentLocale.getLocale());
                        && localeInfo.getLocale().equals(mAppCurrentLocale.getLocale());
                isInAppOrIme = existsInAppOrIme(localeInfo.getLocale());
                // Add the system suggestion flag if the localeInfo exists in mAllAppActiveLocales
                if (!isCurrentLocale && !isInAppOrIme) {
                // and mImeLocales.
                existsInApp = addSystemSuggestionFlag(localeInfo, mAllAppActiveLocales);
                existsInIme = addSystemSuggestionFlag(localeInfo, mImeLocales);
                if (!isCurrentLocale && !existsInApp && !existsInIme) {
                    appLocaleList.add(localeInfo);
                    appLocaleList.add(localeInfo);
                }
                }
            }
            }
@@ -248,6 +255,8 @@ public class AppLocaleCollector implements LocalePickerWithRegion.LocaleCollecto
                // Filter out the locale with the same language and country
                // Filter out the locale with the same language and country
                // like zh-TW vs zh-Hant-TW.
                // like zh-TW vs zh-Hant-TW.
                localeSet = filterSameLanguageAndCountry(localeSet, suggestedSet);
                localeSet = filterSameLanguageAndCountry(localeSet, suggestedSet);
                // Add IME suggestion flag if the locale is supported by IME.
                localeSet = addImeSuggestionFlag(localeSet);
            }
            }
            appLocaleList.addAll(localeSet);
            appLocaleList.addAll(localeSet);
            suggestedSet.addAll(localeSet);
            suggestedSet.addAll(localeSet);
@@ -284,6 +293,31 @@ public class AppLocaleCollector implements LocalePickerWithRegion.LocaleCollecto
                Collectors.toSet());
                Collectors.toSet());
    }
    }


    private boolean addSystemSuggestionFlag(LocaleStore.LocaleInfo localeInfo,
            Set<LocaleStore.LocaleInfo> appLocaleSet) {
        for (LocaleStore.LocaleInfo info : appLocaleSet) {
            if (info.getLocale().equals(localeInfo.getLocale())) {
                info.extendSuggestionOfType(
                        LocaleStore.LocaleInfo.SUGGESTION_TYPE_SYSTEM_AVAILABLE_LANGUAGE);
                return true;
            }
        }
        return false;
    }

    private Set<LocaleStore.LocaleInfo> addImeSuggestionFlag(
            Set<LocaleStore.LocaleInfo> localeSet) {
        for (LocaleStore.LocaleInfo localeInfo : localeSet) {
            for (LocaleStore.LocaleInfo imeLocale : mImeLocales) {
                if (imeLocale.getLocale().equals(localeInfo.getLocale())) {
                    localeInfo.extendSuggestionOfType(
                            LocaleStore.LocaleInfo.SUGGESTION_TYPE_IME_LANGUAGE);
                }
            }
        }
        return localeSet;
    }

    private Set<LocaleStore.LocaleInfo> filterSameLanguageAndCountry(
    private Set<LocaleStore.LocaleInfo> filterSameLanguageAndCountry(
            Set<LocaleStore.LocaleInfo> newLocaleList,
            Set<LocaleStore.LocaleInfo> newLocaleList,
            Set<LocaleStore.LocaleInfo> existingLocaleList) {
            Set<LocaleStore.LocaleInfo> existingLocaleList) {
@@ -306,17 +340,6 @@ public class AppLocaleCollector implements LocalePickerWithRegion.LocaleCollecto
        return result;
        return result;
    }
    }


    private boolean existsInAppOrIme(Locale locale) {
        boolean existInApp = mAllAppActiveLocales.stream().anyMatch(
                localeInfo -> localeInfo.getLocale().equals(locale));
        if (existInApp) {
            return true;
        } else {
            return mImeLocales.stream().anyMatch(
                    localeInfo -> localeInfo.getLocale().equals(locale));
        }
    }

    private Set<LocaleStore.LocaleInfo> filterSupportedLocales(
    private Set<LocaleStore.LocaleInfo> filterSupportedLocales(
            Set<LocaleStore.LocaleInfo> suggestedLocales,
            Set<LocaleStore.LocaleInfo> suggestedLocales,
            HashSet<Locale> appSupportedLocales) {
            HashSet<Locale> appSupportedLocales) {
+4 −0
Original line number Original line Diff line number Diff line
@@ -270,6 +270,10 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
        boolean mayHaveDifferentNumberingSystem = locale.hasNumberingSystems();
        boolean mayHaveDifferentNumberingSystem = locale.hasNumberingSystems();


        if (isSystemLocale
        if (isSystemLocale
                // The suggeseted locale would contain the country code except an edge case for
                // SUGGESTION_TYPE_CURRENT where the application itself set the preferred locale.
                // In this case, onLocaleSelected() will still set the app locale.
                || locale.isSuggested()
                || (isRegionLocale && !mayHaveDifferentNumberingSystem)
                || (isRegionLocale && !mayHaveDifferentNumberingSystem)
                || mIsNumberingSystem) {
                || mIsNumberingSystem) {
            if (mListener != null) {
            if (mListener != null) {
+119 −35
Original line number Original line Diff line number Diff line
@@ -16,6 +16,7 @@


package com.android.internal.app;
package com.android.internal.app;


import android.annotation.IntDef;
import android.app.LocaleManager;
import android.app.LocaleManager;
import android.compat.annotation.UnsupportedAppUsage;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.Context;
@@ -29,6 +30,8 @@ import android.view.inputmethod.InputMethodSubtype;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.annotations.VisibleForTesting;


import java.io.Serializable;
import java.io.Serializable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashMap;
@@ -47,15 +50,42 @@ public class LocaleStore {
    private static boolean sFullyInitialized = false;
    private static boolean sFullyInitialized = false;


    public static class LocaleInfo implements Serializable {
    public static class LocaleInfo implements Serializable {
        @VisibleForTesting static final int SUGGESTION_TYPE_NONE = 0;
        public static final int SUGGESTION_TYPE_NONE = 0;
        @VisibleForTesting static final int SUGGESTION_TYPE_SIM = 1 << 0;
        // A mask used to identify the suggested locale is from SIM.
        @VisibleForTesting static final int SUGGESTION_TYPE_CFG = 1 << 1;
        public static final int SUGGESTION_TYPE_SIM = 1 << 0;
        // A mask used to identify the suggested locale is from the config.
        public static final int SUGGESTION_TYPE_CFG = 1 << 1;
        // Only for per-app language picker
        // Only for per-app language picker
        @VisibleForTesting static final int SUGGESTION_TYPE_CURRENT = 1 << 2;
        // A mask used to identify the suggested locale is from the same application's current
        // configured locale.
        public static final int SUGGESTION_TYPE_CURRENT = 1 << 2;
        // Only for per-app language picker
        // Only for per-app language picker
        @VisibleForTesting  static final int SUGGESTION_TYPE_SYSTEM_LANGUAGE = 1 << 3;
        // A mask used to identify the suggested locale is the system default language.
        public  static final int SUGGESTION_TYPE_SYSTEM_LANGUAGE = 1 << 3;
        // Only for per-app language picker
        // Only for per-app language picker
        @VisibleForTesting static final int SUGGESTION_TYPE_OTHER_APP_LANGUAGE = 1 << 4;
        // A mask used to identify the suggested locale is from other applications' configured
        // locales.
        public static final int SUGGESTION_TYPE_OTHER_APP_LANGUAGE = 1 << 4;
        // Only for per-app language picker
        // A mask used to identify the suggested locale is what the active IME supports.
        public static final int SUGGESTION_TYPE_IME_LANGUAGE = 1 << 5;
        // Only for per-app language picker
        // A mask used to identify the suggested locale is in the current system languages.
        public static final int SUGGESTION_TYPE_SYSTEM_AVAILABLE_LANGUAGE = 1 << 6;
        /** @hide */
        @IntDef(prefix = { "SUGGESTION_TYPE_" }, value = {
                SUGGESTION_TYPE_NONE,
                SUGGESTION_TYPE_SIM,
                SUGGESTION_TYPE_CFG,
                SUGGESTION_TYPE_CURRENT,
                SUGGESTION_TYPE_SYSTEM_LANGUAGE,
                SUGGESTION_TYPE_OTHER_APP_LANGUAGE,
                SUGGESTION_TYPE_IME_LANGUAGE,
                SUGGESTION_TYPE_SYSTEM_AVAILABLE_LANGUAGE
        })
        @Retention(RetentionPolicy.SOURCE)
        public @interface SuggestionType {}

        private final Locale mLocale;
        private final Locale mLocale;
        private final Locale mParent;
        private final Locale mParent;
        private final String mId;
        private final String mId;
@@ -88,6 +118,17 @@ public class LocaleStore {
            this(Locale.forLanguageTag(localeId));
            this(Locale.forLanguageTag(localeId));
        }
        }


        private LocaleInfo(LocaleInfo localeInfo) {
            this.mLocale = localeInfo.getLocale();
            this.mId = localeInfo.getId();
            this.mParent = localeInfo.getParent();
            this.mHasNumberingSystems = localeInfo.mHasNumberingSystems;
            this.mIsChecked = localeInfo.getChecked();
            this.mSuggestionFlags = localeInfo.mSuggestionFlags;
            this.mIsTranslated = localeInfo.isTranslated();
            this.mIsPseudo = localeInfo.mIsPseudo;
        }

        private static Locale getParent(Locale locale) {
        private static Locale getParent(Locale locale) {
            if (locale.getCountry().isEmpty()) {
            if (locale.getCountry().isEmpty()) {
                return null;
                return null;
@@ -142,13 +183,31 @@ public class LocaleStore {
            return mSuggestionFlags != SUGGESTION_TYPE_NONE;
            return mSuggestionFlags != SUGGESTION_TYPE_NONE;
        }
        }


        private boolean isSuggestionOfType(int suggestionMask) {
        /**
         * Check whether the LocaleInfo is suggested by a specific mask
         *
         * @param suggestionMask The mask which is used to identify the suggestion flag.
         * @return true if the locale is suggested by a specific suggestion flag. Otherwise, false.
         */
        public boolean isSuggestionOfType(int suggestionMask) {
            if (!mIsTranslated) { // Never suggest an untranslated locale
            if (!mIsTranslated) { // Never suggest an untranslated locale
                return false;
                return false;
            }
            }
            return (mSuggestionFlags & suggestionMask) == suggestionMask;
            return (mSuggestionFlags & suggestionMask) == suggestionMask;
        }
        }


        /**
         * Extend the locale's suggestion type
         *
         * @param suggestionMask The mask to extend the suggestion flag
         */
        public void extendSuggestionOfType(@SuggestionType int suggestionMask) {
            if (!mIsTranslated) { // Never suggest an untranslated locale
                return;
            }
            mSuggestionFlags |= suggestionMask;
        }

        @UnsupportedAppUsage
        @UnsupportedAppUsage
        public String getFullNameNative() {
        public String getFullNameNative() {
            if (mFullNameNative == null) {
            if (mFullNameNative == null) {
@@ -233,6 +292,10 @@ public class LocaleStore {
        public boolean isSystemLocale() {
        public boolean isSystemLocale() {
            return (mSuggestionFlags & SUGGESTION_TYPE_SYSTEM_LANGUAGE) > 0;
            return (mSuggestionFlags & SUGGESTION_TYPE_SYSTEM_LANGUAGE) > 0;
        }
        }

        public boolean isInCurrentSystemLocales() {
            return (mSuggestionFlags & SUGGESTION_TYPE_SYSTEM_AVAILABLE_LANGUAGE) > 0;
        }
    }
    }


    private static Set<String> getSimCountries(Context context) {
    private static Set<String> getSimCountries(Context context) {
@@ -304,13 +367,13 @@ public class LocaleStore {
            Locale locale = localeList == null ? null : localeList.get(0);
            Locale locale = localeList == null ? null : localeList.get(0);


            if (locale != null) {
            if (locale != null) {
                LocaleInfo localeInfo = new LocaleInfo(locale);
                LocaleInfo cacheInfo  = getLocaleInfo(locale, sLocaleCache);
                LocaleInfo localeInfo = new LocaleInfo(cacheInfo);
                if (isAppSelected) {
                if (isAppSelected) {
                    localeInfo.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_CURRENT;
                    localeInfo.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_CURRENT;
                } else {
                } else {
                    localeInfo.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_OTHER_APP_LANGUAGE;
                    localeInfo.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_OTHER_APP_LANGUAGE;
                }
                }
                localeInfo.mIsTranslated = true;
                return localeInfo;
                return localeInfo;
            }
            }
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
@@ -329,26 +392,27 @@ public class LocaleStore {
            List<InputMethodSubtype> list) {
            List<InputMethodSubtype> list) {
        Set<LocaleInfo> imeLocales = new HashSet<>();
        Set<LocaleInfo> imeLocales = new HashSet<>();
        for (InputMethodSubtype subtype : list) {
        for (InputMethodSubtype subtype : list) {
            LocaleInfo localeInfo = new LocaleInfo(subtype.getLanguageTag());
            Locale locale = Locale.forLanguageTag(subtype.getLanguageTag());
            localeInfo.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_OTHER_APP_LANGUAGE;
            LocaleInfo cacheInfo  = getLocaleInfo(locale, sLocaleCache);
            localeInfo.mIsTranslated = true;
            LocaleInfo localeInfo = new LocaleInfo(cacheInfo);
            localeInfo.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_IME_LANGUAGE;
            imeLocales.add(localeInfo);
            imeLocales.add(localeInfo);
        }
        }
        return imeLocales;
        return imeLocales;
    }
    }


    /**
    /**
     * Returns a list of system languages with LocaleInfo
     * Returns a list of system locale that removes all extensions except for the numbering system.
     */
     */
    public static List<LocaleInfo> getSystemCurrentLocaleInfo() {
    public static List<LocaleInfo> getSystemCurrentLocales() {
        List<LocaleInfo> localeList = new ArrayList<>();
        List<LocaleInfo> localeList = new ArrayList<>();

        LocaleList systemLangList = LocaleList.getDefault();
        LocaleList systemLangList = LocaleList.getDefault();
        for(int i = 0; i < systemLangList.size(); i++) {
        for(int i = 0; i < systemLangList.size(); i++) {
            LocaleInfo systemLocaleInfo = new LocaleInfo(systemLangList.get(i));
            Locale sysLocale = getLocaleWithOnlyNumberingSystem(systemLangList.get(i));
            systemLocaleInfo.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM;
            LocaleInfo cacheInfo  = getLocaleInfo(sysLocale, sLocaleCache);
            systemLocaleInfo.mIsTranslated = true;
            LocaleInfo localeInfo = new LocaleInfo(cacheInfo);
            localeList.add(systemLocaleInfo);
            localeInfo.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SYSTEM_AVAILABLE_LANGUAGE;
            localeList.add(localeInfo);
        }
        }
        return localeList;
        return localeList;
    }
    }
@@ -542,12 +606,12 @@ public class LocaleStore {
            Set<String> ignorables,
            Set<String> ignorables,
            LocaleInfo parent,
            LocaleInfo parent,
            boolean translatedOnly,
            boolean translatedOnly,
            HashMap<String, LocaleInfo> supportedLcoaleInfos) {
            HashMap<String, LocaleInfo> supportedLocaleInfos) {


        boolean hasTargetParent = parent != null;
        boolean hasTargetParent = parent != null;
        String parentId = hasTargetParent ? parent.getId() : null;
        String parentId = hasTargetParent ? parent.getId() : null;
        HashSet<LocaleInfo> result = new HashSet<>();
        HashSet<LocaleInfo> result = new HashSet<>();
        for (LocaleStore.LocaleInfo li : supportedLcoaleInfos.values()) {
        for (LocaleStore.LocaleInfo li : supportedLocaleInfos.values()) {
            if (isShallIgnore(ignorables, li, translatedOnly)) {
            if (isShallIgnore(ignorables, li, translatedOnly)) {
                continue;
                continue;
            }
            }
@@ -556,13 +620,18 @@ public class LocaleStore {
                    if (li.isSuggestionOfType(LocaleInfo.SUGGESTION_TYPE_SIM)) {
                    if (li.isSuggestionOfType(LocaleInfo.SUGGESTION_TYPE_SIM)) {
                        result.add(li);
                        result.add(li);
                    } else {
                    } else {
                        result.add(getLocaleInfo(li.getParent(), supportedLcoaleInfos));
                        Locale locale = li.getParent();
                        LocaleInfo localeInfo = getLocaleInfo(locale, supportedLocaleInfos);
                        addLocaleInfoToMap(locale, localeInfo, supportedLocaleInfos);
                        result.add(localeInfo);
                    }
                    }
                    break;
                    break;
                case TIER_REGION:
                case TIER_REGION:
                    if (parentId.equals(li.getParent().toLanguageTag())) {
                    if (parentId.equals(li.getParent().toLanguageTag())) {
                        result.add(getLocaleInfo(
                        Locale locale = li.getLocale().stripExtensions();
                                li.getLocale().stripExtensions(), supportedLcoaleInfos));
                        LocaleInfo localeInfo = getLocaleInfo(locale, supportedLocaleInfos);
                        addLocaleInfoToMap(locale, localeInfo, supportedLocaleInfos);
                        result.add(localeInfo);
                    }
                    }
                    break;
                    break;
                case TIER_NUMBERING:
                case TIER_NUMBERING:
@@ -636,7 +705,9 @@ public class LocaleStore {


    @UnsupportedAppUsage
    @UnsupportedAppUsage
    public static LocaleInfo getLocaleInfo(Locale locale) {
    public static LocaleInfo getLocaleInfo(Locale locale) {
        return getLocaleInfo(locale, sLocaleCache);
        LocaleInfo localeInfo = getLocaleInfo(locale, sLocaleCache);
        addLocaleInfoToMap(locale, localeInfo, sLocaleCache);
        return localeInfo;
    }
    }


    private static LocaleInfo getLocaleInfo(
    private static LocaleInfo getLocaleInfo(
@@ -646,10 +717,7 @@ public class LocaleStore {
        if (!localeInfos.containsKey(id)) {
        if (!localeInfos.containsKey(id)) {
            // Locale preferences can modify the language tag to current system languages, so we
            // Locale preferences can modify the language tag to current system languages, so we
            // need to check the input locale without extra u extension except numbering system.
            // need to check the input locale without extra u extension except numbering system.
            Locale filteredLocale = new Locale.Builder()
            Locale filteredLocale = getLocaleWithOnlyNumberingSystem(locale);
                    .setLocale(locale.stripExtensions())
                    .setUnicodeLocaleKeyword("nu", locale.getUnicodeLocaleType("nu"))
                    .build();
            if (localeInfos.containsKey(filteredLocale.toLanguageTag())) {
            if (localeInfos.containsKey(filteredLocale.toLanguageTag())) {
                result = new LocaleInfo(locale);
                result = new LocaleInfo(locale);
                LocaleInfo localeInfo = localeInfos.get(filteredLocale.toLanguageTag());
                LocaleInfo localeInfo = localeInfos.get(filteredLocale.toLanguageTag());
@@ -662,13 +730,29 @@ public class LocaleStore {
                return result;
                return result;
            }
            }
            result = new LocaleInfo(locale);
            result = new LocaleInfo(locale);
            localeInfos.put(id, result);
        } else {
        } else {
            result = localeInfos.get(id);
            result = localeInfos.get(id);
        }
        }
        return result;
        return result;
    }
    }


    private static Locale getLocaleWithOnlyNumberingSystem(Locale locale) {
        return new Locale.Builder()
                .setLocale(locale.stripExtensions())
                .setUnicodeLocaleKeyword("nu", locale.getUnicodeLocaleType("nu"))
                .build();
    }

    private static void addLocaleInfoToMap(Locale locale, LocaleInfo localeInfo,
            HashMap<String, LocaleInfo> map) {
        if (!map.containsKey(locale.toLanguageTag())) {
            Locale localeWithNumberingSystem = getLocaleWithOnlyNumberingSystem(locale);
            if (!map.containsKey(localeWithNumberingSystem.toLanguageTag())) {
                map.put(locale.toLanguageTag(), localeInfo);
            }
        }
    }

    /**
    /**
     * API for testing.
     * API for testing.
     */
     */
+34 −7
Original line number Original line Diff line number Diff line
@@ -19,11 +19,14 @@ package com.android.internal.app;
import static com.android.internal.app.AppLocaleStore.AppLocaleResult.LocaleStatus.GET_SUPPORTED_LANGUAGE_FROM_LOCAL_CONFIG;
import static com.android.internal.app.AppLocaleStore.AppLocaleResult.LocaleStatus.GET_SUPPORTED_LANGUAGE_FROM_LOCAL_CONFIG;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.spy;


import android.os.LocaleList;

import androidx.test.InstrumentationRegistry;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.runner.AndroidJUnit4;
@@ -62,6 +65,9 @@ public class AppLocaleCollectorTest {
    private static final int CURRENT = LocaleInfo.SUGGESTION_TYPE_CURRENT;
    private static final int CURRENT = LocaleInfo.SUGGESTION_TYPE_CURRENT;
    private static final int SYSTEM = LocaleInfo.SUGGESTION_TYPE_SYSTEM_LANGUAGE;
    private static final int SYSTEM = LocaleInfo.SUGGESTION_TYPE_SYSTEM_LANGUAGE;
    private static final int OTHERAPP = LocaleInfo.SUGGESTION_TYPE_OTHER_APP_LANGUAGE;
    private static final int OTHERAPP = LocaleInfo.SUGGESTION_TYPE_OTHER_APP_LANGUAGE;
    private static final int IME = LocaleInfo.SUGGESTION_TYPE_IME_LANGUAGE;
    private static final int SYSTEM_AVAILABLE =
            LocaleInfo.SUGGESTION_TYPE_SYSTEM_AVAILABLE_LANGUAGE;


    @Before
    @Before
    public void setUp() throws Exception {
    public void setUp() throws Exception {
@@ -77,6 +83,21 @@ public class AppLocaleCollectorTest {
                initAppSupportedLocale());
                initAppSupportedLocale());
    }
    }


    @Test
    public void testGetSystemCurrentLocales() {
        LocaleList.setDefault(
                LocaleList.forLanguageTags("en-US-u-mu-fahrenhe,ar-JO-u-mu-fahrenhe-nu-latn"));

        List<LocaleStore.LocaleInfo> list =
                mAppLocaleCollector.getSystemCurrentLocales();

        LocaleList expected = LocaleList.forLanguageTags("en-US,ar-JO-u-nu-latn");
        assertEquals(list.size(), expected.size());
        for (LocaleStore.LocaleInfo info : list) {
            assertTrue(expected.indexOf(info.getLocale()) != -1);
        }
    }

    @Test
    @Test
    public void testGetSupportedLocaleList() {
    public void testGetSupportedLocaleList() {
        doReturn(mAppCurrentLocale).when(mAppLocaleCollector).getAppCurrentLocale();
        doReturn(mAppCurrentLocale).when(mAppLocaleCollector).getAppCurrentLocale();
@@ -85,7 +106,8 @@ public class AppLocaleCollectorTest {
        doReturn(mImeLocales).when(mAppLocaleCollector).getActiveImeLocales();
        doReturn(mImeLocales).when(mAppLocaleCollector).getActiveImeLocales();
        doReturn(mSystemSupportedLocales).when(mAppLocaleCollector).getSystemSupportedLocale(
        doReturn(mSystemSupportedLocales).when(mAppLocaleCollector).getSystemSupportedLocale(
                anyObject(), eq(null), eq(true));
                anyObject(), eq(null), eq(true));
        doReturn(mSystemCurrentLocales).when(mAppLocaleCollector).getSystemCurrentLocale();
        doReturn(mSystemCurrentLocales).when(
                mAppLocaleCollector).getSystemCurrentLocales();


        Set<LocaleInfo> result = mAppLocaleCollector.getSupportedLocaleList(null, true, false);
        Set<LocaleInfo> result = mAppLocaleCollector.getSupportedLocaleList(null, true, false);


@@ -106,8 +128,10 @@ public class AppLocaleCollectorTest {
        map.put("ko", NONE); // The locale App and system support.
        map.put("ko", NONE); // The locale App and system support.
        map.put("en-AU", OTHERAPP); // The locale other App activates and current App supports.
        map.put("en-AU", OTHERAPP); // The locale other App activates and current App supports.
        map.put("en-CA", OTHERAPP); // The locale other App activates and current App supports.
        map.put("en-CA", OTHERAPP); // The locale other App activates and current App supports.
        map.put("ja-JP", OTHERAPP); // The locale other App activates and current App supports.
        map.put("en-IN", IME); // The locale IME supports.
        map.put("zh-Hant-TW", SIM);  // The locale system activates.
        map.put("ja-JP",
                OTHERAPP | SYSTEM_AVAILABLE | IME); // The locale exists in OTHERAPP, SYSTEM and IME
        map.put("zh-Hant-TW", SYSTEM_AVAILABLE);  // The locale system activates.
        map.put(createLocaleInfo("", SYSTEM).getId(), SYSTEM); // System language title
        map.put(createLocaleInfo("", SYSTEM).getId(), SYSTEM); // System language title
        return map;
        return map;
    }
    }
@@ -124,9 +148,10 @@ public class AppLocaleCollectorTest {
    }
    }


    private List<LocaleInfo> initSystemCurrentLocales() {
    private List<LocaleInfo> initSystemCurrentLocales() {
        return List.of(createLocaleInfo("zh-Hant-TW", SIM),
        return List.of(createLocaleInfo("zh-Hant-TW", SYSTEM_AVAILABLE),
                createLocaleInfo("ja-JP", SYSTEM_AVAILABLE),
                // will be filtered because current App activates this locale.
                // will be filtered because current App activates this locale.
                createLocaleInfo("en-US", SIM));
                createLocaleInfo("en-US", SYSTEM_AVAILABLE));
    }
    }


    private Set<LocaleInfo> initAllAppActivatedLocales() {
    private Set<LocaleInfo> initAllAppActivatedLocales() {
@@ -141,9 +166,11 @@ public class AppLocaleCollectorTest {
    private Set<LocaleInfo> initImeLocales() {
    private Set<LocaleInfo> initImeLocales() {
        return Set.of(
        return Set.of(
                // will be filtered because system activates zh-Hant-TW.
                // will be filtered because system activates zh-Hant-TW.
                createLocaleInfo("zh-TW", OTHERAPP),
                createLocaleInfo("zh-TW", IME),
                // will be filtered because current App's activats this locale.
                // will be filtered because current App's activats this locale.
                createLocaleInfo("en-US", OTHERAPP));
                createLocaleInfo("en-US", IME),
                createLocaleInfo("ja-JP", IME),
                createLocaleInfo("en-IN", IME));
    }
    }


    private HashSet<Locale> initAppSupportedLocale() {
    private HashSet<Locale> initAppSupportedLocale() {
+1 −1
Original line number Original line Diff line number Diff line
@@ -57,7 +57,7 @@ public class LocaleStoreTest {
        Set<String> expectedLanguageTag = Set.of("en-US", "zh-TW", "ja-JP");
        Set<String> expectedLanguageTag = Set.of("en-US", "zh-TW", "ja-JP");
        assertEquals(localeSet.size(), expectedLanguageTag.size());
        assertEquals(localeSet.size(), expectedLanguageTag.size());
        for (LocaleInfo info : localeSet) {
        for (LocaleInfo info : localeSet) {
            assertEquals(info.mSuggestionFlags, LocaleInfo.SUGGESTION_TYPE_OTHER_APP_LANGUAGE);
            assertEquals(info.mSuggestionFlags, LocaleInfo.SUGGESTION_TYPE_IME_LANGUAGE);
            assertTrue(expectedLanguageTag.contains(info.getId()));
            assertTrue(expectedLanguageTag.contains(info.getId()));
        }
        }
    }
    }