From 451376062a4cbc63bf1f74cad58863ff0e439e67 Mon Sep 17 00:00:00 2001 From: Mihai Nita Date: Tue, 22 Dec 2015 23:13:43 -0800 Subject: [PATCH] Add APIs to access various locale lists. This includes APIs to get / set the user locale list, and getters for the curated list of supported locales, localized assets, and pseudo-locales. Change-Id: Ie5771d769cd4a01f6404fa82f6f4f9bcb9b83a9e Bug: 25800339 --- .../android/internal/app/LocalePicker.java | 57 ++++++++++++++++--- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/core/java/com/android/internal/app/LocalePicker.java b/core/java/com/android/internal/app/LocalePicker.java index 4efefa943947d..6a365e0a69274 100644 --- a/core/java/com/android/internal/app/LocalePicker.java +++ b/core/java/com/android/internal/app/LocalePicker.java @@ -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 getAllAssetLocales(Context context, boolean isInDeveloperMode) { final Resources resources = context.getResources(); - final String[] locales = Resources.getSystem().getAssets().getLocales(); + final String[] locales = getSystemAssetLocales(); List localeList = new ArrayList(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(); + } + } }