Merge "[Panlingual] Suggested locales update"

This commit is contained in:
Daniel Huang
2022-01-21 09:12:06 +00:00
committed by Android (Google) Code Review
2 changed files with 88 additions and 36 deletions

View File

@@ -37,6 +37,8 @@ import androidx.appcompat.app.AlertDialog;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceGroup; import androidx.preference.PreferenceGroup;
import com.android.internal.app.LocalePicker;
import com.android.internal.app.LocalePicker.LocaleInfo;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settings.applications.AppInfoBase; import com.android.settings.applications.AppInfoBase;
@@ -45,8 +47,11 @@ import com.android.settingslib.applications.AppUtils;
import com.android.settingslib.widget.LayoutPreference; import com.android.settingslib.widget.LayoutPreference;
import com.android.settingslib.widget.RadioButtonPreference; import com.android.settingslib.widget.RadioButtonPreference;
import com.google.common.collect.Iterables;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Locale; import java.util.Locale;
/** /**
@@ -200,8 +205,8 @@ public class AppLocaleDetails extends AppInfoBase implements RadioButtonPreferen
private TelephonyManager mTelephonyManager; private TelephonyManager mTelephonyManager;
private LocaleManager mLocaleManager; private LocaleManager mLocaleManager;
private Collection<Locale> mSuggestedLocales = new ArrayList<>();; private Collection<Locale> mSuggestedLocales = new ArrayList<>();
private Collection<Locale> mSupportedLocales = new ArrayList<>();; private Collection<Locale> mSupportedLocales = new ArrayList<>();
AppLocaleDetailsHelper(Context context, String packageName) { AppLocaleDetailsHelper(Context context, String packageName) {
mContext = context; mContext = context;
@@ -230,25 +235,41 @@ public class AppLocaleDetails extends AppInfoBase implements RadioButtonPreferen
@VisibleForTesting @VisibleForTesting
void handleSuggestedLocales() { void handleSuggestedLocales() {
LocaleList currentSystemLocales = getCurrentSystemLocales(); LocaleList currentSystemLocales = getCurrentSystemLocales();
Locale simLocale = mTelephonyManager.getSimLocale();
Locale appLocale = getAppDefaultLocale(mContext, mPackageName); Locale appLocale = getAppDefaultLocale(mContext, mPackageName);
String simCountry = mTelephonyManager.getSimCountryIso().toUpperCase(Locale.US);
String networkCountry = mTelephonyManager.getNetworkCountryIso().toUpperCase(Locale.US);
// 1st locale in suggested languages group. // 1st locale in suggested languages group.
if (appLocale != null) { if (appLocale != null) {
mSuggestedLocales.add(appLocale); mSuggestedLocales.add(appLocale);
} }
// 2nd locale in suggested languages group. // 2nd locale in suggested languages group.
if (simLocale != null && !compareLocale(simLocale, appLocale)) { final List<LocaleInfo> localeInfos = LocalePicker.getAllAssetLocales(mContext, false);
mSuggestedLocales.add(simLocale); for (LocaleInfo localeInfo : localeInfos) {
Locale locale = localeInfo.getLocale();
String localeCountry = locale.getCountry().toUpperCase(Locale.US);
if (!compareLocale(locale, appLocale)
&& isCountrySuggestedLocale(localeCountry, simCountry, networkCountry)) {
mSuggestedLocales.add(locale);
}
} }
// Other locales in suggested languages group. // Other locales in suggested languages group.
for (int i = 0; i < currentSystemLocales.size(); i++) { for (int i = 0; i < currentSystemLocales.size(); i++) {
Locale locale = currentSystemLocales.get(i); Locale locale = currentSystemLocales.get(i);
if (!compareLocale(locale, appLocale) && !compareLocale(locale, simLocale)) { boolean isInSuggestedLocales = false;
for (int j = 0; j < mSuggestedLocales.size(); j++) {
Locale suggestedLocale = Iterables.get(mSuggestedLocales, j);
if (compareLocale(locale, suggestedLocale)) {
isInSuggestedLocales = true;
break;
}
}
if (!isInSuggestedLocales) {
mSuggestedLocales.add(locale); mSuggestedLocales.add(locale);
} }
} }
} }
@VisibleForTesting
static boolean compareLocale(Locale source, Locale target) { static boolean compareLocale(Locale source, Locale target) {
if (source == null && target == null) { if (source == null && target == null) {
return true; return true;
@@ -259,6 +280,13 @@ public class AppLocaleDetails extends AppInfoBase implements RadioButtonPreferen
} }
} }
private static boolean isCountrySuggestedLocale(String localeCountry,
String simCountry,
String networkCountry) {
return ((!simCountry.isEmpty() && simCountry.equals(localeCountry))
|| (!networkCountry.isEmpty() && networkCountry.equals(localeCountry)));
}
@VisibleForTesting @VisibleForTesting
void handleSupportedLocales() { void handleSupportedLocales() {
//TODO Waiting for PackageManager api //TODO Waiting for PackageManager api

View File

@@ -53,7 +53,6 @@ public class AppLocaleDetailsTest {
private Context mContext; private Context mContext;
private LocaleList mSystemLocales; private LocaleList mSystemLocales;
private Locale mSimLocale;
private LocaleList mAppLocale; private LocaleList mAppLocale;
private String[] mAssetLocales; private String[] mAssetLocales;
@@ -69,7 +68,8 @@ public class AppLocaleDetailsTest {
when(mContext.getSystemService(LocaleManager.class)).thenReturn(mLocaleManager); when(mContext.getSystemService(LocaleManager.class)).thenReturn(mLocaleManager);
setupInitialLocales("en", setupInitialLocales("en",
"uk", "tw",
"jp",
"en, uk, jp, ne", "en, uk, jp, ne",
new String[]{"en", "ne", "ms", "pa"}); new String[]{"en", "ne", "ms", "pa"});
} }
@@ -88,6 +88,8 @@ public class AppLocaleDetailsTest {
@Test @Test
@UiThreadTest @UiThreadTest
public void handleAllLocalesData_1stLocaleOfSuggestedLocaleListIsAppLocale() { public void handleAllLocalesData_1stLocaleOfSuggestedLocaleListIsAppLocale() {
Locale simCountryLocale = new Locale("zh", "TW");
Locale networkCountryLocale = new Locale("ja", "JP");
DummyAppLocaleDetailsHelper helper = DummyAppLocaleDetailsHelper helper =
new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME); new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME);
@@ -95,25 +97,17 @@ public class AppLocaleDetailsTest {
Locale locale = Iterables.get(helper.getSuggestedLocales(), 0); Locale locale = Iterables.get(helper.getSuggestedLocales(), 0);
assertTrue(locale.equals(mAppLocale.get(0))); assertTrue(locale.equals(mAppLocale.get(0)));
assertTrue(helper.getSuggestedLocales().contains(simCountryLocale));
assertTrue(helper.getSuggestedLocales().contains(networkCountryLocale));
} }
@Test @Test
@UiThreadTest @UiThreadTest
public void handleAllLocalesData_2ndLocaleOfSuggestedLocaleListIsSimLocale() { public void handleAllLocalesData_withoutAppLocale_1stSuggestedLocaleIsSimCountryLocale() {
DummyAppLocaleDetailsHelper helper = Locale simCountryLocale = new Locale("zh", "TW");
new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME);
helper.handleAllLocalesData();
Locale locale = Iterables.get(helper.getSuggestedLocales(), 1);
assertTrue(locale.equals(mSimLocale));
}
@Test
@UiThreadTest
public void handleAllLocalesData_withoutAppLocale_1stLocaleOfSuggestedLocaleListIsSimLocal() {
setupInitialLocales("", setupInitialLocales("",
"uk", "tw",
"",
"en, uk, jp, ne", "en, uk, jp, ne",
new String[]{"en", "ne", "ms", "pa"}); new String[]{"en", "ne", "ms", "pa"});
DummyAppLocaleDetailsHelper helper = DummyAppLocaleDetailsHelper helper =
@@ -122,13 +116,34 @@ public class AppLocaleDetailsTest {
helper.handleAllLocalesData(); helper.handleAllLocalesData();
Locale locale = Iterables.get(helper.getSuggestedLocales(), 0); Locale locale = Iterables.get(helper.getSuggestedLocales(), 0);
assertTrue(locale.equals(mSimLocale)); assertTrue(locale.equals(simCountryLocale));
assertFalse(helper.getSuggestedLocales().contains(mAppLocale.get(0)));
} }
@Test @Test
@UiThreadTest @UiThreadTest
public void handleAllLocalesData_noAppAndSimLocale_1stLocaleIsFirstOneInSystemLocales() { public void handleAllLocalesData_withoutAppLocale_1stSuggestedLocaleIsNetworkCountryLocale() {
Locale networkCountryLocale = new Locale("en", "GB");
setupInitialLocales("", setupInitialLocales("",
"",
"gb",
"en, uk, jp, ne",
new String[]{"en", "ne", "ms", "pa"});
DummyAppLocaleDetailsHelper helper =
new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME);
helper.handleAllLocalesData();
Locale locale = Iterables.get(helper.getSuggestedLocales(), 0);
assertTrue(locale.equals(networkCountryLocale));
assertFalse(helper.getSuggestedLocales().contains(mAppLocale.get(0)));
}
@Test
@UiThreadTest
public void handleAllLocalesData_noAppAndSimNetworkLocale_1stLocaleIsFirstOneInSystemLocales() {
setupInitialLocales("",
"",
"", "",
"en, uk, jp, ne", "en, uk, jp, ne",
new String[]{"en", "ne", "ms", "pa"}); new String[]{"en", "ne", "ms", "pa"});
@@ -177,8 +192,16 @@ public class AppLocaleDetailsTest {
* *
* @param appLocale Application locale, it shall be a language tag. * @param appLocale Application locale, it shall be a language tag.
* example: "en" * example: "en"
* @param simLocale SIM carrier locale, it shall be a language tag. *
* example: "en" * @param simCountry The ISO-3166-1 alpha-2 country code equivalent for the SIM
* provider's country code.
* example: "us"
*
* @param networkCountry The ISO-3166-1 alpha-2 country code equivalent of the MCC
* (Mobile Country Code) of the current registered operato
* or the cell nearby.
* example: "us"
*
* @param systemLocales System locales, a locale list by a multiple language tags with comma. * @param systemLocales System locales, a locale list by a multiple language tags with comma.
* example: "en, uk, jp" * example: "en, uk, jp"
* @param assetLocales Asset locales, a locale list by a multiple language tags with String * @param assetLocales Asset locales, a locale list by a multiple language tags with String
@@ -186,14 +209,15 @@ public class AppLocaleDetailsTest {
* example: new String[] {"en", "ne", "ms", "pa"} * example: new String[] {"en", "ne", "ms", "pa"}
*/ */
private void setupInitialLocales(String appLocale, private void setupInitialLocales(String appLocale,
String simLocale, String simCountry,
String networkCountry,
String systemLocales, String systemLocales,
String[] assetLocales) { String[] assetLocales) {
mAppLocale = LocaleList.forLanguageTags(appLocale); mAppLocale = LocaleList.forLanguageTags(appLocale);
mSimLocale = Locale.forLanguageTag(simLocale);
mSystemLocales = LocaleList.forLanguageTags(systemLocales); mSystemLocales = LocaleList.forLanguageTags(systemLocales);
mAssetLocales = assetLocales; mAssetLocales = assetLocales;
when(mTelephonyManager.getSimLocale()).thenReturn(simLocale.isEmpty() ? null : mSimLocale); when(mTelephonyManager.getSimCountryIso()).thenReturn(simCountry);
when(mTelephonyManager.getNetworkCountryIso()).thenReturn(networkCountry);
when(mLocaleManager.getApplicationLocales(anyString())).thenReturn(mAppLocale); when(mLocaleManager.getApplicationLocales(anyString())).thenReturn(mAppLocale);
} }