FileDocCategorySizeDatePackage
LocalePicker.javaAPI DocAndroid 1.5 API5352Wed May 06 22:42:48 BST 2009com.android.settings

LocalePicker

public class LocalePicker extends android.app.ListActivity

Fields Summary
private static final String
TAG
Loc[]
mLocales
Constructors Summary
Methods Summary
intgetContentView()

        return R.layout.locale_picker;
    
public voidonCreate(android.os.Bundle icicle)

        super.onCreate(icicle);
        setContentView(getContentView());

        String[] locales = getAssets().getLocales();
        Arrays.sort(locales);

        final int origSize = locales.length;
        Loc[] preprocess = new Loc[origSize];
        int finalSize = 0;
        for (int i = 0 ; i < origSize; i++ ) {
            String s = locales[i];
            int len = s.length();
            if (len == 2) {
                Locale l = new Locale(s);
                preprocess[finalSize++] = new Loc(toTitleCase(l.getDisplayLanguage()), l);
            } else if (len == 5) {
                String language = s.substring(0, 2);
                String country = s.substring(3, 5);
                Locale l = new Locale(language, country);

                if (finalSize == 0) {
                    preprocess[finalSize++] = new Loc(toTitleCase(l.getDisplayLanguage()), l);
                } else {
                    // check previous entry:
                    //  same lang and no country -> overwrite it with a lang-only name
                    //  same lang and a country -> upgrade to full name and 
                    //    insert ours with full name
                    //  diff lang -> insert ours with lang-only name
                    if (preprocess[finalSize-1].locale.getLanguage().equals(language)) {
                       String prevCountry = preprocess[finalSize-1].locale.getCountry();
                       if (prevCountry.length() == 0) {
                            preprocess[finalSize-1].locale = l;
                            preprocess[finalSize-1].label = toTitleCase(l.getDisplayLanguage());
                        } else {
                            preprocess[finalSize-1].label = toTitleCase(preprocess[finalSize-1].locale.getDisplayName());
                            preprocess[finalSize++] = new Loc(toTitleCase(l.getDisplayName()), l);
                        }
                    } else {
                        String displayName;
                        if (s.equals("zz_ZZ")) {
                            displayName = "Pseudo...";
                        } else {
                            displayName = toTitleCase(l.getDisplayLanguage());
                        }
                        preprocess[finalSize++] = new Loc(displayName, l);
                    }
                }
            }
        }
        mLocales = new Loc[finalSize];
        for (int i = 0; i < finalSize ; i++) {
            mLocales[i] = preprocess[i];
        }
        int layoutId = R.layout.locale_picker_item;
        int fieldId = R.id.locale;
        ArrayAdapter<Loc> adapter = new ArrayAdapter<Loc>(this, layoutId, fieldId, mLocales);
        getListView().setAdapter(adapter);
    
protected voidonListItemClick(android.widget.ListView l, android.view.View v, int position, long id)

        try {
            IActivityManager am = ActivityManagerNative.getDefault();
            Configuration config = am.getConfiguration();

            Loc loc = mLocales[position];
            config.locale = loc.locale;

            // indicate this isn't some passing default - the user wants this remembered
            config.userSetLocale = true;

            am.updateConfiguration(config);
        } catch (RemoteException e) {
            // Intentionally left blank
        }
        finish();
    
public voidonResume()

        super.onResume();
        getListView().requestFocus();
    
private static java.lang.StringtoTitleCase(java.lang.String s)

        if (s.length() == 0) {
            return s;
        }

        return Character.toUpperCase(s.charAt(0)) + s.substring(1);