[Panlingual] Add PackageManager api to Per app language.

Bug: 216099500
Test: atest pass
Test: local test with Settings app
Change-Id: Icbdb1475d45e0652de369711ea21b5043f36dc19
This commit is contained in:
tom hsu
2022-01-24 22:10:02 +08:00
committed by Tom Hsu
parent 031104f18e
commit b48802c152
2 changed files with 87 additions and 27 deletions

View File

@@ -18,6 +18,7 @@ package com.android.settings.applications.appinfo;
import static com.android.settings.widget.EntityHeaderController.ActionType; import static com.android.settings.widget.EntityHeaderController.ActionType;
import android.app.Activity; import android.app.Activity;
import android.app.LocaleConfig;
import android.app.LocaleManager; import android.app.LocaleManager;
import android.app.settings.SettingsEnums; import android.app.settings.SettingsEnums;
import android.content.Context; import android.content.Context;
@@ -289,12 +290,18 @@ public class AppLocaleDetails extends AppInfoBase implements RadioButtonPreferen
@VisibleForTesting @VisibleForTesting
void handleSupportedLocales() { void handleSupportedLocales() {
//TODO Waiting for PackageManager api LocaleList localeList = getPackageLocales();
String[] languages = getAssetSystemLocales(); if (localeList == null) {
String[] languages = getAssetSystemLocales();
for (String language : languages) { for (String language : languages) {
mSupportedLocales.add(Locale.forLanguageTag(language)); mSupportedLocales.add(Locale.forLanguageTag(language));
}
} else {
for (int i = 0; i < localeList.size(); i++) {
mSupportedLocales.add(localeList.get(i));
}
} }
if (mSuggestedLocales != null || !mSuggestedLocales.isEmpty()) { if (mSuggestedLocales != null || !mSuggestedLocales.isEmpty()) {
mSupportedLocales.removeAll(mSuggestedLocales); mSupportedLocales.removeAll(mSuggestedLocales);
} }
@@ -349,9 +356,23 @@ public class AppLocaleDetails extends AppInfoBase implements RadioButtonPreferen
packageManager.getPackageInfo(mPackageName, PackageManager.MATCH_ALL) packageManager.getPackageInfo(mPackageName, PackageManager.MATCH_ALL)
.applicationInfo).getAssets().getNonSystemLocales(); .applicationInfo).getAssets().getNonSystemLocales();
} catch (PackageManager.NameNotFoundException e) { } catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Can not found the package name : " + e); Log.w(TAG, "Can not found the package name : " + mPackageName + " / " + e);
} }
return new String[0]; return new String[0];
} }
@VisibleForTesting
LocaleList getPackageLocales() {
try {
LocaleConfig localeConfig =
new LocaleConfig(mContext.createPackageContext(mPackageName, 0));
if (localeConfig.getStatus() == LocaleConfig.STATUS_SUCCESS) {
return localeConfig.getSupportedLocales();
}
} catch (PackageManager.NameNotFoundException e) {
Log.w(TAG, "Can not found the package name : " + mPackageName + " / " + e);
}
return null;
}
} }
} }

View File

@@ -55,6 +55,7 @@ public class AppLocaleDetailsTest {
private LocaleList mSystemLocales; private LocaleList mSystemLocales;
private LocaleList mAppLocale; private LocaleList mAppLocale;
private String[] mAssetLocales; private String[] mAssetLocales;
private LocaleList mPackageLocales;
@Before @Before
@UiThreadTest @UiThreadTest
@@ -67,11 +68,13 @@ public class AppLocaleDetailsTest {
when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager); when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(LocaleManager.class)).thenReturn(mLocaleManager); when(mContext.getSystemService(LocaleManager.class)).thenReturn(mLocaleManager);
setupInitialLocales("en", setupInitialLocales(
"tw", /* appLocale= */ "en",
"jp", /* simCountry= */ "tw",
"en, uk, jp, ne", /* networkCountry= */ "jp",
new String[]{"en", "ne", "ms", "pa"}); /* systemLocales= */ "en, uk, jp, ne",
/* packageLocales= */ "pa, cn, tw, en",
/* assetLocales= */ new String[]{"en", "ne", "ms", "pa"});
} }
@Test @Test
@@ -105,11 +108,13 @@ public class AppLocaleDetailsTest {
@UiThreadTest @UiThreadTest
public void handleAllLocalesData_withoutAppLocale_1stSuggestedLocaleIsSimCountryLocale() { public void handleAllLocalesData_withoutAppLocale_1stSuggestedLocaleIsSimCountryLocale() {
Locale simCountryLocale = new Locale("zh", "TW"); Locale simCountryLocale = new Locale("zh", "TW");
setupInitialLocales("", setupInitialLocales(
"tw", /* appLocale= */ "",
"", /* simCountry= */ "tw",
"en, uk, jp, ne", /* networkCountry= */ "",
new String[]{"en", "ne", "ms", "pa"}); /* systemLocales= */ "en, uk, jp, ne",
/* packageLocales= */ "",
/* assetLocales= */ new String[]{});
DummyAppLocaleDetailsHelper helper = DummyAppLocaleDetailsHelper helper =
new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME); new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME);
@@ -124,11 +129,13 @@ public class AppLocaleDetailsTest {
@UiThreadTest @UiThreadTest
public void handleAllLocalesData_withoutAppLocale_1stSuggestedLocaleIsNetworkCountryLocale() { public void handleAllLocalesData_withoutAppLocale_1stSuggestedLocaleIsNetworkCountryLocale() {
Locale networkCountryLocale = new Locale("en", "GB"); Locale networkCountryLocale = new Locale("en", "GB");
setupInitialLocales("", setupInitialLocales(
"", /* appLocale= */ "",
"gb", /* simCountry= */ "",
"en, uk, jp, ne", /* networkCountry= */ "gb",
new String[]{"en", "ne", "ms", "pa"}); /* systemLocales= */ "en, uk, jp, ne",
/* packageLocales= */ "",
/* assetLocales= */ new String[]{});
DummyAppLocaleDetailsHelper helper = DummyAppLocaleDetailsHelper helper =
new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME); new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME);
@@ -142,11 +149,32 @@ public class AppLocaleDetailsTest {
@Test @Test
@UiThreadTest @UiThreadTest
public void handleAllLocalesData_noAppAndSimNetworkLocale_1stLocaleIsFirstOneInSystemLocales() { public void handleAllLocalesData_noAppAndSimNetworkLocale_1stLocaleIsFirstOneInSystemLocales() {
setupInitialLocales("", setupInitialLocales(
"", /* appLocale= */ "",
"", /* simCountry= */ "",
"en, uk, jp, ne", /* networkCountry= */ "",
new String[]{"en", "ne", "ms", "pa"}); /* systemLocales= */ "en, uk, jp, ne",
/* packageLocales= */ "",
/* assetLocales= */ new String[]{});
DummyAppLocaleDetailsHelper helper =
new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME);
helper.handleAllLocalesData();
Locale locale = Iterables.get(helper.getSuggestedLocales(), 0);
assertTrue(locale.equals(mSystemLocales.get(0)));
}
@Test
@UiThreadTest
public void handleAllLocalesData_hasPackageAndSystemLocales_1stLocaleIs1stOneInSystemLocales() {
setupInitialLocales(
/* appLocale= */ "",
/* simCountry= */ "",
/* networkCountry= */ "",
/* systemLocales= */ "en, uk, jp, ne",
/* packageLocales= */ "pa, cn, tw, en",
/* assetLocales= */ new String[]{});
DummyAppLocaleDetailsHelper helper = DummyAppLocaleDetailsHelper helper =
new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME); new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME);
@@ -204,6 +232,11 @@ public class AppLocaleDetailsTest {
* *
* @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 packageLocales PackageManager locales, a locale list by a multiple language tags with
* comma.
* 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
* array. * array.
* example: new String[] {"en", "ne", "ms", "pa"} * example: new String[] {"en", "ne", "ms", "pa"}
@@ -212,10 +245,12 @@ public class AppLocaleDetailsTest {
String simCountry, String simCountry,
String networkCountry, String networkCountry,
String systemLocales, String systemLocales,
String packageLocales,
String[] assetLocales) { String[] assetLocales) {
mAppLocale = LocaleList.forLanguageTags(appLocale); mAppLocale = LocaleList.forLanguageTags(appLocale);
mSystemLocales = LocaleList.forLanguageTags(systemLocales); mSystemLocales = LocaleList.forLanguageTags(systemLocales);
mAssetLocales = assetLocales; mAssetLocales = assetLocales;
mPackageLocales = LocaleList.forLanguageTags(packageLocales);
when(mTelephonyManager.getSimCountryIso()).thenReturn(simCountry); when(mTelephonyManager.getSimCountryIso()).thenReturn(simCountry);
when(mTelephonyManager.getNetworkCountryIso()).thenReturn(networkCountry); when(mTelephonyManager.getNetworkCountryIso()).thenReturn(networkCountry);
when(mLocaleManager.getApplicationLocales(anyString())).thenReturn(mAppLocale); when(mLocaleManager.getApplicationLocales(anyString())).thenReturn(mAppLocale);
@@ -237,6 +272,10 @@ public class AppLocaleDetailsTest {
LocaleList getCurrentSystemLocales() { LocaleList getCurrentSystemLocales() {
return mSystemLocales; return mSystemLocales;
} }
}
@Override
LocaleList getPackageLocales() {
return mPackageLocales;
}
}
} }