From 729a22a2ccc7c6aeeb9ecf7da0f2542c14c60646 Mon Sep 17 00:00:00 2001 From: Calvin Pan Date: Tue, 12 Apr 2022 05:47:24 +0000 Subject: [PATCH] Add current system language into suggestion list Bug: 228905883 Test: Add system language and change app language Change-Id: I7b1f78bb13902dc50f3e8ce8e1c62590ac623b9c --- .../internal/app/LocalePickerWithRegion.java | 18 +++++++++++++++++- .../com/android/internal/app/LocaleStore.java | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/core/java/com/android/internal/app/LocalePickerWithRegion.java b/core/java/com/android/internal/app/LocalePickerWithRegion.java index 213af26a436fa..380ace0c70235 100644 --- a/core/java/com/android/internal/app/LocalePickerWithRegion.java +++ b/core/java/com/android/internal/app/LocalePickerWithRegion.java @@ -128,6 +128,12 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O boolean isForCountryMode = parent != null; if (!TextUtils.isEmpty(appPackageName) && !isForCountryMode) { + // Filter current system locale to add them into suggestion + LocaleList systemLangList = LocaleList.getDefault(); + for(int i = 0; i < systemLangList.size(); i++) { + langTagsToIgnore.add(systemLangList.get(i).toLanguageTag()); + } + if (appCurrentLocale != null) { Log.d(TAG, "appCurrentLocale: " + appCurrentLocale.getLocale().toLanguageTag()); langTagsToIgnore.add(appCurrentLocale.getLocale().toLanguageTag()); @@ -167,9 +173,20 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O result.mLocaleStatus == LocaleStatus.GET_SUPPORTED_LANGUAGE_FROM_LOCAL_CONFIG || result.mLocaleStatus == LocaleStatus.GET_SUPPORTED_LANGUAGE_FROM_ASSET; + // Add current system language into suggestion list + for(LocaleStore.LocaleInfo localeInfo: LocaleStore.getSystemCurrentLocaleInfo()) { + if (appCurrentLocale == null || + !localeInfo.getLocale().equals(appCurrentLocale.getLocale())) { + mLocaleList.add(localeInfo); + } + } + + // Filter the language not support in app mLocaleList = filterTheLanguagesNotSupportedInApp( shouldShowList, result.mAppSupportedLocales); + Log.d(TAG, "mLocaleList after app-supported filter: " + mLocaleList.size()); + // Add "system language" if (!isForCountryMode && shouldShowList) { mLocaleList.add(LocaleStore.getSystemDefaultLocaleInfo(appCurrentLocale == null)); @@ -189,7 +206,6 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O } } } - Log.d(TAG, "mLocaleList after app-supported filter: " + filteredList.size()); } return filteredList; diff --git a/core/java/com/android/internal/app/LocaleStore.java b/core/java/com/android/internal/app/LocaleStore.java index eb11b9b8b1384..9480362d4fe58 100644 --- a/core/java/com/android/internal/app/LocaleStore.java +++ b/core/java/com/android/internal/app/LocaleStore.java @@ -25,9 +25,11 @@ import android.telephony.TelephonyManager; import android.util.Log; import java.io.Serializable; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.IllformedLocaleException; +import java.util.List; import java.util.Locale; import java.util.Set; @@ -277,6 +279,22 @@ public class LocaleStore { return null; } + /** + * Returns a list of system languages with LocaleInfo + */ + public static List getSystemCurrentLocaleInfo() { + List localeList = new ArrayList<>(); + + LocaleList systemLangList = LocaleList.getDefault(); + for(int i = 0; i < systemLangList.size(); i++) { + LocaleInfo systemLocaleInfo = new LocaleInfo(systemLangList.get(i)); + systemLocaleInfo.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM; + systemLocaleInfo.mIsTranslated = true; + localeList.add(systemLocaleInfo); + } + return localeList; + } + /** * The "system default" is special case for per-app picker. Intentionally keep the locale * empty to let activity know "system default" been selected.