Merge "Adding empty locale-config case" into tm-dev
This commit is contained in:
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package com.android.internal.app;
|
package com.android.internal.app;
|
||||||
|
|
||||||
|
import static com.android.internal.app.AppLocaleStore.AppLocaleResult.LocaleStatus;
|
||||||
|
|
||||||
import android.app.LocaleConfig;
|
import android.app.LocaleConfig;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
@@ -25,41 +27,43 @@ import android.util.Log;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class AppLocaleStore {
|
class AppLocaleStore {
|
||||||
private static final String TAG = AppLocaleStore.class.getSimpleName();
|
private static final String TAG = AppLocaleStore.class.getSimpleName();
|
||||||
|
|
||||||
public static ArrayList<Locale> getAppSupportedLocales(Context context, String packageName) {
|
public static AppLocaleResult getAppSupportedLocales(
|
||||||
|
Context context, String packageName) {
|
||||||
|
LocaleConfig localeConfig = null;
|
||||||
|
AppLocaleResult.LocaleStatus localeStatus = LocaleStatus.UNKNOWN_FAILURE;
|
||||||
ArrayList<Locale> appSupportedLocales = new ArrayList<>();
|
ArrayList<Locale> appSupportedLocales = new ArrayList<>();
|
||||||
LocaleList packageLocaleList = getPackageLocales(context, packageName);
|
|
||||||
|
|
||||||
if (packageLocaleList != null && packageLocaleList.size() > 0) {
|
try {
|
||||||
|
localeConfig = new LocaleConfig(context.createPackageContext(packageName, 0));
|
||||||
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
|
Log.w(TAG, "Can not found the package name : " + packageName + " / " + e);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (localeConfig != null) {
|
||||||
|
if (localeConfig.getStatus() == LocaleConfig.STATUS_SUCCESS) {
|
||||||
|
LocaleList packageLocaleList = localeConfig.getSupportedLocales();
|
||||||
|
if (packageLocaleList.size() > 0) {
|
||||||
|
localeStatus = LocaleStatus.GET_SUPPORTED_LANGUAGE_FROM_LOCAL_CONFIG;
|
||||||
for (int i = 0; i < packageLocaleList.size(); i++) {
|
for (int i = 0; i < packageLocaleList.size(); i++) {
|
||||||
appSupportedLocales.add(packageLocaleList.get(i));
|
appSupportedLocales.add(packageLocaleList.get(i));
|
||||||
}
|
}
|
||||||
Log.d(TAG, "getAppSupportedLocales from LocaleConfig. Size: "
|
|
||||||
+ appSupportedLocales.size());
|
|
||||||
} else {
|
} else {
|
||||||
|
localeStatus = LocaleStatus.NO_SUPPORTED_LANGUAGE;
|
||||||
|
}
|
||||||
|
} else if (localeConfig.getStatus() == LocaleConfig.STATUS_NOT_SPECIFIED) {
|
||||||
|
localeStatus = LocaleStatus.GET_SUPPORTED_LANGUAGE_FROM_ASSET;
|
||||||
String[] languages = getAssetLocales(context, packageName);
|
String[] languages = getAssetLocales(context, packageName);
|
||||||
for (String language : languages) {
|
for (String language : languages) {
|
||||||
appSupportedLocales.add(Locale.forLanguageTag(language));
|
appSupportedLocales.add(Locale.forLanguageTag(language));
|
||||||
}
|
}
|
||||||
Log.d(TAG, "getAppSupportedLocales from asset. Size: "
|
|
||||||
+ appSupportedLocales.size());
|
|
||||||
}
|
}
|
||||||
return appSupportedLocales;
|
|
||||||
}
|
}
|
||||||
|
Log.d(TAG, "getAppSupportedLocales(). status: " + localeStatus
|
||||||
private static LocaleList getPackageLocales(Context context, String packageName) {
|
+ ", appSupportedLocales:" + appSupportedLocales.size());
|
||||||
try {
|
return new AppLocaleResult(localeStatus, appSupportedLocales);
|
||||||
LocaleConfig localeConfig =
|
|
||||||
new LocaleConfig(context.createPackageContext(packageName, 0));
|
|
||||||
if (localeConfig.getStatus() == LocaleConfig.STATUS_SUCCESS) {
|
|
||||||
return localeConfig.getSupportedLocales();
|
|
||||||
}
|
|
||||||
} catch (PackageManager.NameNotFoundException e) {
|
|
||||||
Log.w(TAG, "Can not found the package name : " + packageName + " / " + e);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String[] getAssetLocales(Context context, String packageName) {
|
private static String[] getAssetLocales(Context context, String packageName) {
|
||||||
@@ -82,4 +86,20 @@ public class AppLocaleStore {
|
|||||||
return new String[0];
|
return new String[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static class AppLocaleResult {
|
||||||
|
enum LocaleStatus {
|
||||||
|
UNKNOWN_FAILURE,
|
||||||
|
NO_SUPPORTED_LANGUAGE,
|
||||||
|
GET_SUPPORTED_LANGUAGE_FROM_LOCAL_CONFIG,
|
||||||
|
GET_SUPPORTED_LANGUAGE_FROM_ASSET,
|
||||||
|
}
|
||||||
|
|
||||||
|
LocaleStatus mLocaleStatus;
|
||||||
|
ArrayList<Locale> mAppSupportedLocales;
|
||||||
|
|
||||||
|
public AppLocaleResult(LocaleStatus localeStatus, ArrayList<Locale> appSupportedLocales) {
|
||||||
|
this.mLocaleStatus = localeStatus;
|
||||||
|
this.mAppSupportedLocales = appSupportedLocales;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package com.android.internal.app;
|
package com.android.internal.app;
|
||||||
|
|
||||||
|
import static com.android.internal.app.AppLocaleStore.AppLocaleResult.LocaleStatus;
|
||||||
|
|
||||||
import android.app.FragmentManager;
|
import android.app.FragmentManager;
|
||||||
import android.app.FragmentTransaction;
|
import android.app.FragmentTransaction;
|
||||||
import android.app.ListFragment;
|
import android.app.ListFragment;
|
||||||
@@ -158,9 +160,18 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
|
|||||||
if (appCurrentLocale != null && !isForCountryMode) {
|
if (appCurrentLocale != null && !isForCountryMode) {
|
||||||
mLocaleList.add(appCurrentLocale);
|
mLocaleList.add(appCurrentLocale);
|
||||||
}
|
}
|
||||||
mLocaleList = filterTheLanguagesNotSupportedInApp(context, appPackageName);
|
|
||||||
|
|
||||||
if (!isForCountryMode) {
|
AppLocaleStore.AppLocaleResult result =
|
||||||
|
AppLocaleStore.getAppSupportedLocales(context, appPackageName);
|
||||||
|
boolean shouldShowList =
|
||||||
|
result.mLocaleStatus == LocaleStatus.GET_SUPPORTED_LANGUAGE_FROM_LOCAL_CONFIG
|
||||||
|
|| result.mLocaleStatus == LocaleStatus.GET_SUPPORTED_LANGUAGE_FROM_ASSET;
|
||||||
|
|
||||||
|
mLocaleList = filterTheLanguagesNotSupportedInApp(
|
||||||
|
shouldShowList, result.mAppSupportedLocales);
|
||||||
|
|
||||||
|
// Add "system language"
|
||||||
|
if (!isForCountryMode && shouldShowList) {
|
||||||
mLocaleList.add(LocaleStore.getSystemDefaultLocaleInfo(appCurrentLocale == null));
|
mLocaleList.add(LocaleStore.getSystemDefaultLocaleInfo(appCurrentLocale == null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -168,11 +179,9 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Set<LocaleStore.LocaleInfo> filterTheLanguagesNotSupportedInApp(
|
private Set<LocaleStore.LocaleInfo> filterTheLanguagesNotSupportedInApp(
|
||||||
Context context, String appPackageName) {
|
boolean shouldShowList, ArrayList<Locale> supportedLocales) {
|
||||||
ArrayList<Locale> supportedLocales =
|
|
||||||
AppLocaleStore.getAppSupportedLocales(context, appPackageName);
|
|
||||||
|
|
||||||
Set<LocaleStore.LocaleInfo> filteredList = new HashSet<>();
|
Set<LocaleStore.LocaleInfo> filteredList = new HashSet<>();
|
||||||
|
if (shouldShowList) {
|
||||||
for(LocaleStore.LocaleInfo li: mLocaleList) {
|
for(LocaleStore.LocaleInfo li: mLocaleList) {
|
||||||
for(Locale l: supportedLocales) {
|
for(Locale l: supportedLocales) {
|
||||||
if(LocaleList.matchesLanguageAndScript(li.getLocale(), l)) {
|
if(LocaleList.matchesLanguageAndScript(li.getLocale(), l)) {
|
||||||
@@ -181,6 +190,7 @@ public class LocalePickerWithRegion extends ListFragment implements SearchView.O
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.d(TAG, "mLocaleList after app-supported filter: " + filteredList.size());
|
Log.d(TAG, "mLocaleList after app-supported filter: " + filteredList.size());
|
||||||
|
}
|
||||||
|
|
||||||
return filteredList;
|
return filteredList;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user