Merge "Add APIs to access various locale lists."

This commit is contained in:
Mihai Niță
2015-12-28 15:35:22 +00:00
committed by Android (Google) Code Review

View File

@@ -28,6 +28,7 @@ import android.content.res.Resources;
import android.os.Bundle;
import android.os.RemoteException;
import android.provider.Settings;
import android.util.LocaleList;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@@ -45,6 +46,7 @@ import java.util.ArrayList;
public class LocalePicker extends ListFragment {
private static final String TAG = "LocalePicker";
private static final boolean DEBUG = false;
private static final String[] pseudoLocales = { "en-XA", "ar-XB" };
public static interface LocaleSelectionListener {
// You can add any argument if you really need it...
@@ -57,7 +59,7 @@ public class LocalePicker extends ListFragment {
static final Collator sCollator = Collator.getInstance();
String label;
Locale locale;
final Locale locale;
public LocaleInfo(String label, Locale locale) {
this.label = label;
@@ -83,17 +85,30 @@ public class LocalePicker extends ListFragment {
}
}
public static String[] getSystemAssetLocales() {
return Resources.getSystem().getAssets().getLocales();
}
public static String[] getSupportedLocales(Context context) {
return context.getResources().getStringArray(R.array.supported_locales);
}
public static String[] getPseudoLocales() {
return pseudoLocales;
}
public static List<LocaleInfo> getAllAssetLocales(Context context, boolean isInDeveloperMode) {
final Resources resources = context.getResources();
final String[] locales = Resources.getSystem().getAssets().getLocales();
final String[] locales = getSystemAssetLocales();
List<String> localeList = new ArrayList<String>(locales.length);
Collections.addAll(localeList, locales);
// Don't show the pseudolocales unless we're in developer mode. http://b/17190407.
if (!isInDeveloperMode) {
localeList.remove("ar-XB");
localeList.remove("en-XA");
for (String locale : pseudoLocales) {
localeList.remove(locale);
}
}
Collections.sort(localeList);
@@ -240,13 +255,24 @@ public class LocalePicker extends ListFragment {
/**
* Requests the system to update the system locale. Note that the system looks halted
* for a while during the Locale migration, so the caller need to take care of it.
*
* @see #updateLocales(LocaleList)
*/
public static void updateLocale(Locale locale) {
try {
IActivityManager am = ActivityManagerNative.getDefault();
Configuration config = am.getConfiguration();
updateLocales(new LocaleList(locale));
}
config.setLocale(locale);
/**
* Requests the system to update the list of system locales.
* Note that the system looks halted for a while during the Locale migration,
* so the caller need to take care of it.
*/
public static void updateLocales(LocaleList locales) {
try {
final IActivityManager am = ActivityManagerNative.getDefault();
final Configuration config = am.getConfiguration();
config.setLocales(locales);
config.userSetLocale = true;
am.updateConfiguration(config);
@@ -256,4 +282,19 @@ public class LocalePicker extends ListFragment {
// Intentionally left blank
}
}
/**
* Get the locale list.
*
* @return The locale list.
*/
public static LocaleList getLocales() {
try {
return ActivityManagerNative.getDefault()
.getConfiguration().getLocales();
} catch (RemoteException e) {
// If something went wrong
return LocaleList.getDefault();
}
}
}