Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 45137606 authored by Mihai Nita's avatar Mihai Nita
Browse files

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
parent bb36206f
Loading
Loading
Loading
Loading
+48 −7
Original line number Diff line number Diff line
@@ -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) {
        updateLocales(new LocaleList(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 {
            IActivityManager am = ActivityManagerNative.getDefault();
            Configuration config = am.getConfiguration();
            final IActivityManager am = ActivityManagerNative.getDefault();
            final Configuration config = am.getConfiguration();

            config.setLocale(locale);
            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();
        }
    }
}