diff options
| author | 2023-05-15 11:54:17 +0000 | |
|---|---|---|
| committer | 2023-05-16 04:01:21 +0000 | |
| commit | c73eaaa713cfc1562e2f51567f5d97b7b686700c (patch) | |
| tree | 4029cd7799d8af0233fcda442122febd041f7928 | |
| parent | 6b3ebf828cfd55b780b791094303c9e51960524d (diff) | |
Fix incorrect suggested language
System languages in the app's suggested regsion have to filter out
those not supported by the application.
Bug: 282192969
Test: atest AppLocaleCollectorTest
Change-Id: I8bbc6c8682d7e016baee2ab19396607b84062da8
3 files changed, 93 insertions, 21 deletions
diff --git a/core/java/com/android/internal/app/AppLocaleCollector.java b/core/java/com/android/internal/app/AppLocaleCollector.java index 7cf428ad97a5..56f633fbc6c9 100644 --- a/core/java/com/android/internal/app/AppLocaleCollector.java +++ b/core/java/com/android/internal/app/AppLocaleCollector.java @@ -157,13 +157,13 @@ public class AppLocaleCollector implements LocalePickerWithRegion.LocaleCollecto * Get a list of system locale that removes all extensions except for the numbering system. */ @VisibleForTesting - public List<LocaleStore.LocaleInfo> getSystemCurrentLocales() { - List<LocaleStore.LocaleInfo> sysLocales = LocaleStore.getSystemCurrentLocales(); + public Set<LocaleStore.LocaleInfo> getSystemCurrentLocales() { + Set<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()); + Collectors.toSet()); } @Override @@ -225,7 +225,10 @@ public class AppLocaleCollector implements LocalePickerWithRegion.LocaleCollecto // Add current system language into suggestion list if (!isForCountryMode) { boolean isCurrentLocale, existsInApp, existsInIme; - for (LocaleStore.LocaleInfo localeInfo : getSystemCurrentLocales()) { + // filter out the system locases that are supported by the application. + Set<LocaleStore.LocaleInfo> localeInfoSet = + filterSupportedLocales(getSystemCurrentLocales(), result.mAppSupportedLocales); + for (LocaleStore.LocaleInfo localeInfo : localeInfoSet) { isCurrentLocale = mAppCurrentLocale != null && localeInfo.getLocale().equals(mAppCurrentLocale.getLocale()); // Add the system suggestion flag if the localeInfo exists in mAllAppActiveLocales diff --git a/core/java/com/android/internal/app/LocaleStore.java b/core/java/com/android/internal/app/LocaleStore.java index f4b858f87413..43d263bc8a6d 100644 --- a/core/java/com/android/internal/app/LocaleStore.java +++ b/core/java/com/android/internal/app/LocaleStore.java @@ -32,7 +32,6 @@ import com.android.internal.annotations.VisibleForTesting; import java.io.Serializable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -404,8 +403,8 @@ public class LocaleStore { /** * Returns a list of system locale that removes all extensions except for the numbering system. */ - public static List<LocaleInfo> getSystemCurrentLocales() { - List<LocaleInfo> localeList = new ArrayList<>(); + public static Set<LocaleInfo> getSystemCurrentLocales() { + Set<LocaleInfo> localeList = new HashSet<>(); LocaleList systemLangList = LocaleList.getDefault(); for(int i = 0; i < systemLangList.size(); i++) { Locale sysLocale = getLocaleWithOnlyNumberingSystem(systemLangList.get(i)); diff --git a/tests/Internal/src/com/android/internal/app/AppLocaleCollectorTest.java b/tests/Internal/src/com/android/internal/app/AppLocaleCollectorTest.java index 7d9a6a56262c..d16e90e26aaa 100644 --- a/tests/Internal/src/com/android/internal/app/AppLocaleCollectorTest.java +++ b/tests/Internal/src/com/android/internal/app/AppLocaleCollectorTest.java @@ -37,9 +37,9 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Locale; import java.util.Set; @@ -54,7 +54,7 @@ public class AppLocaleCollectorTest { private LocaleStore.LocaleInfo mAppCurrentLocale; private Set<LocaleInfo> mAllAppActiveLocales; private Set<LocaleInfo> mImeLocales; - private List<LocaleInfo> mSystemCurrentLocales; + private Set<LocaleInfo> mSystemCurrentLocales; private Set<LocaleInfo> mSystemSupportedLocales; private AppLocaleStore.AppLocaleResult mResult; private static final String PKG1 = "pkg1"; @@ -73,14 +73,6 @@ public class AppLocaleCollectorTest { public void setUp() throws Exception { mAppLocaleCollector = spy( new AppLocaleCollector(InstrumentationRegistry.getContext(), PKG1)); - - mAppCurrentLocale = createLocaleInfo("en-US", CURRENT); - mAllAppActiveLocales = initAllAppActivatedLocales(); - mImeLocales = initImeLocales(); - mSystemSupportedLocales = initSystemSupportedLocales(); - mSystemCurrentLocales = initSystemCurrentLocales(); - mResult = new AppLocaleStore.AppLocaleResult(GET_SUPPORTED_LANGUAGE_FROM_LOCAL_CONFIG, - initAppSupportedLocale()); } @Test @@ -88,7 +80,7 @@ public class AppLocaleCollectorTest { LocaleList.setDefault( LocaleList.forLanguageTags("en-US-u-mu-fahrenhe,ar-JO-u-mu-fahrenhe-nu-latn")); - List<LocaleStore.LocaleInfo> list = + Set<LocaleStore.LocaleInfo> list = mAppLocaleCollector.getSystemCurrentLocales(); LocaleList expected = LocaleList.forLanguageTags("en-US,ar-JO-u-nu-latn"); @@ -99,7 +91,69 @@ public class AppLocaleCollectorTest { } @Test - public void testGetSupportedLocaleList() { + public void testGetSupportedLocaleList_filterNonAppsupportedSystemLanguage() { + mAppCurrentLocale = createLocaleInfo("en-US", CURRENT); + + // App supports five locales + HashSet<Locale> appSupported = + getAppSupportedLocales(new String[] { + "en-US", + "fr", + "ar", + "es", + "bn" + }); + // There are six locales in system current locales. + mSystemCurrentLocales = getSystemCurrentLocales(new String[] { + "en-US", + "fr-FR", + "ar-JO", + "ca-AD", + "da-DK", + "es-US" + }); + mAllAppActiveLocales = Collections.emptySet(); + mImeLocales = Collections.emptySet(); + mSystemSupportedLocales = Collections.emptySet(); + mResult = new AppLocaleStore.AppLocaleResult(GET_SUPPORTED_LANGUAGE_FROM_LOCAL_CONFIG, + appSupported); + + doReturn(mAppCurrentLocale).when(mAppLocaleCollector).getAppCurrentLocale(); + doReturn(mResult).when(mAppLocaleCollector).getAppSupportedLocales(); + doReturn(mAllAppActiveLocales).when(mAppLocaleCollector).getAllAppActiveLocales(); + doReturn(mImeLocales).when(mAppLocaleCollector).getActiveImeLocales(); + doReturn(mSystemSupportedLocales).when(mAppLocaleCollector).getSystemSupportedLocale( + anyObject(), eq(null), eq(true)); + doReturn(mSystemCurrentLocales).when( + mAppLocaleCollector).getSystemCurrentLocales(); + + Set<LocaleInfo> result = mAppLocaleCollector.getSupportedLocaleList(null, true, false); + + // The result would show four rather than six locales in the suggested region. + HashMap<String, Integer> expectedResult = new HashMap<>(); + expectedResult.put("en-US", CURRENT); // The locale current App activates. + expectedResult.put("ar-JO", SYSTEM_AVAILABLE); + expectedResult.put("fr-FR", SYSTEM_AVAILABLE); + expectedResult.put("es-US", SYSTEM_AVAILABLE); + expectedResult.put(createLocaleInfo("", SYSTEM).getId(), SYSTEM); // System language title + + assertEquals(result.size(), expectedResult.size()); + for (LocaleStore.LocaleInfo info: result) { + int suggestionFlags = expectedResult.getOrDefault(info.getId(), -1); + assertEquals(info.mSuggestionFlags, suggestionFlags); + } + } + + @Test + public void testGetSupportedLocaleList_withActiveLocalesFromOtherAppAndIme() { + mAppCurrentLocale = createLocaleInfo("en-US", CURRENT); + mAllAppActiveLocales = initAllAppActivatedLocales(); + mImeLocales = initImeLocales(); + mSystemSupportedLocales = initSystemSupportedLocales(); + mSystemCurrentLocales = initSystemCurrentLocales(); + mResult = new AppLocaleStore.AppLocaleResult(GET_SUPPORTED_LANGUAGE_FROM_LOCAL_CONFIG, + initAppSupportedLocale()); + doReturn(mAppCurrentLocale).when(mAppLocaleCollector).getAppCurrentLocale(); doReturn(mResult).when(mAppLocaleCollector).getAppSupportedLocales(); doReturn(mAllAppActiveLocales).when(mAppLocaleCollector).getAllAppActiveLocales(); @@ -147,8 +201,8 @@ public class AppLocaleCollectorTest { ); } - private List<LocaleInfo> initSystemCurrentLocales() { - return List.of(createLocaleInfo("zh-Hant-TW", SYSTEM_AVAILABLE), + private Set<LocaleInfo> initSystemCurrentLocales() { + return Set.of(createLocaleInfo("zh-Hant-TW", SYSTEM_AVAILABLE), createLocaleInfo("ja-JP", SYSTEM_AVAILABLE), // will be filtered because current App activates this locale. createLocaleInfo("en-US", SYSTEM_AVAILABLE)); @@ -188,6 +242,22 @@ public class AppLocaleCollectorTest { return hs; } + private Set<LocaleStore.LocaleInfo> getSystemCurrentLocales(String []languageTags) { + HashSet<LocaleStore.LocaleInfo> hs = new HashSet<>(languageTags.length); + for (String tag:languageTags) { + hs.add(createLocaleInfo(tag, SYSTEM_AVAILABLE)); + } + return hs; + } + + private HashSet<Locale> getAppSupportedLocales(String []languageTags) { + HashSet<Locale> hs = new HashSet<>(languageTags.length); + for (String language:languageTags) { + hs.add(Locale.forLanguageTag(language)); + } + return hs; + } + private LocaleInfo createLocaleInfo(String languageTag, int suggestionFlag) { LocaleInfo localeInfo = LocaleStore.fromLocale(Locale.forLanguageTag(languageTag)); localeInfo.mSuggestionFlags = suggestionFlag; |