[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 android.app.Activity;
import android.app.LocaleConfig;
import android.app.LocaleManager;
import android.app.settings.SettingsEnums;
import android.content.Context;
@@ -289,12 +290,18 @@ public class AppLocaleDetails extends AppInfoBase implements RadioButtonPreferen
@VisibleForTesting
void handleSupportedLocales() {
//TODO Waiting for PackageManager api
String[] languages = getAssetSystemLocales();
for (String language : languages) {
mSupportedLocales.add(Locale.forLanguageTag(language));
LocaleList localeList = getPackageLocales();
if (localeList == null) {
String[] languages = getAssetSystemLocales();
for (String language : languages) {
mSupportedLocales.add(Locale.forLanguageTag(language));
}
} else {
for (int i = 0; i < localeList.size(); i++) {
mSupportedLocales.add(localeList.get(i));
}
}
if (mSuggestedLocales != null || !mSuggestedLocales.isEmpty()) {
mSupportedLocales.removeAll(mSuggestedLocales);
}
@@ -349,9 +356,23 @@ public class AppLocaleDetails extends AppInfoBase implements RadioButtonPreferen
packageManager.getPackageInfo(mPackageName, PackageManager.MATCH_ALL)
.applicationInfo).getAssets().getNonSystemLocales();
} 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];
}
@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 mAppLocale;
private String[] mAssetLocales;
private LocaleList mPackageLocales;
@Before
@UiThreadTest
@@ -67,11 +68,13 @@ public class AppLocaleDetailsTest {
when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(LocaleManager.class)).thenReturn(mLocaleManager);
setupInitialLocales("en",
"tw",
"jp",
"en, uk, jp, ne",
new String[]{"en", "ne", "ms", "pa"});
setupInitialLocales(
/* appLocale= */ "en",
/* simCountry= */ "tw",
/* networkCountry= */ "jp",
/* systemLocales= */ "en, uk, jp, ne",
/* packageLocales= */ "pa, cn, tw, en",
/* assetLocales= */ new String[]{"en", "ne", "ms", "pa"});
}
@Test
@@ -105,11 +108,13 @@ public class AppLocaleDetailsTest {
@UiThreadTest
public void handleAllLocalesData_withoutAppLocale_1stSuggestedLocaleIsSimCountryLocale() {
Locale simCountryLocale = new Locale("zh", "TW");
setupInitialLocales("",
"tw",
"",
"en, uk, jp, ne",
new String[]{"en", "ne", "ms", "pa"});
setupInitialLocales(
/* appLocale= */ "",
/* simCountry= */ "tw",
/* networkCountry= */ "",
/* systemLocales= */ "en, uk, jp, ne",
/* packageLocales= */ "",
/* assetLocales= */ new String[]{});
DummyAppLocaleDetailsHelper helper =
new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME);
@@ -124,11 +129,13 @@ public class AppLocaleDetailsTest {
@UiThreadTest
public void handleAllLocalesData_withoutAppLocale_1stSuggestedLocaleIsNetworkCountryLocale() {
Locale networkCountryLocale = new Locale("en", "GB");
setupInitialLocales("",
"",
"gb",
"en, uk, jp, ne",
new String[]{"en", "ne", "ms", "pa"});
setupInitialLocales(
/* appLocale= */ "",
/* simCountry= */ "",
/* networkCountry= */ "gb",
/* systemLocales= */ "en, uk, jp, ne",
/* packageLocales= */ "",
/* assetLocales= */ new String[]{});
DummyAppLocaleDetailsHelper helper =
new DummyAppLocaleDetailsHelper(mContext, APP_PACKAGE_NAME);
@@ -142,11 +149,32 @@ public class AppLocaleDetailsTest {
@Test
@UiThreadTest
public void handleAllLocalesData_noAppAndSimNetworkLocale_1stLocaleIsFirstOneInSystemLocales() {
setupInitialLocales("",
"",
"",
"en, uk, jp, ne",
new String[]{"en", "ne", "ms", "pa"});
setupInitialLocales(
/* appLocale= */ "",
/* simCountry= */ "",
/* networkCountry= */ "",
/* 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 =
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.
* 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
* array.
* example: new String[] {"en", "ne", "ms", "pa"}
@@ -212,10 +245,12 @@ public class AppLocaleDetailsTest {
String simCountry,
String networkCountry,
String systemLocales,
String packageLocales,
String[] assetLocales) {
mAppLocale = LocaleList.forLanguageTags(appLocale);
mSystemLocales = LocaleList.forLanguageTags(systemLocales);
mAssetLocales = assetLocales;
mPackageLocales = LocaleList.forLanguageTags(packageLocales);
when(mTelephonyManager.getSimCountryIso()).thenReturn(simCountry);
when(mTelephonyManager.getNetworkCountryIso()).thenReturn(networkCountry);
when(mLocaleManager.getApplicationLocales(anyString())).thenReturn(mAppLocale);
@@ -237,6 +272,10 @@ public class AppLocaleDetailsTest {
LocaleList getCurrentSystemLocales() {
return mSystemLocales;
}
}
@Override
LocaleList getPackageLocales() {
return mPackageLocales;
}
}
}