2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings;
19 import android.app.ActivityManagerNative;
20 import android.app.IActivityManager;
21 import android.app.ListActivity;
22 import android.app.backup.BackupManager;
23 import android.content.res.Configuration;
24 import android.os.Bundle;
25 import android.os.RemoteException;
26 import android.os.SystemProperties;
27 import android.util.Log;
28 import android.view.View;
29 import android.widget.ArrayAdapter;
30 import android.widget.ListView;
32 import java.io.BufferedWriter;
33 import java.io.FileOutputStream;
34 import java.text.Collator;
35 import java.util.Arrays;
36 import java.util.Locale;
38 public class LocalePicker extends ListActivity {
39 private static final String TAG = "LocalePicker";
40 private static final boolean DEBUG = false;
43 String[] mSpecialLocaleCodes;
44 String[] mSpecialLocaleNames;
46 private static class Loc implements Comparable {
47 static Collator sCollator = Collator.getInstance();
52 public Loc(String label, Locale locale) {
58 public String toString() {
62 public int compareTo(Object o) {
63 return sCollator.compare(this.label, ((Loc) o).label);
67 int getContentView() {
68 return R.layout.locale_picker;
72 public void onCreate(Bundle icicle) {
73 super.onCreate(icicle);
74 setContentView(getContentView());
76 mSpecialLocaleCodes = getResources().getStringArray(R.array.special_locale_codes);
77 mSpecialLocaleNames = getResources().getStringArray(R.array.special_locale_names);
79 String[] locales = getAssets().getLocales();
82 final int origSize = locales.length;
83 Loc[] preprocess = new Loc[origSize];
85 for (int i = 0 ; i < origSize; i++ ) {
86 String s = locales[i];
89 String language = s.substring(0, 2);
90 String country = s.substring(3, 5);
91 Locale l = new Locale(language, country);
95 Log.v(TAG, "adding initial "+ toTitleCase(l.getDisplayLanguage(l)));
97 preprocess[finalSize++] =
98 new Loc(toTitleCase(l.getDisplayLanguage(l)), l);
100 // check previous entry:
101 // same lang and a country -> upgrade to full name and
102 // insert ours with full name
103 // diff lang -> insert ours with lang-only name
104 if (preprocess[finalSize-1].locale.getLanguage().equals(
107 Log.v(TAG, "backing up and fixing "+
108 preprocess[finalSize-1].label+" to "+
109 getDisplayName(preprocess[finalSize-1].locale));
111 preprocess[finalSize-1].label = toTitleCase(
112 getDisplayName(preprocess[finalSize-1].locale));
114 Log.v(TAG, " and adding "+ toTitleCase(getDisplayName(l)));
116 preprocess[finalSize++] =
117 new Loc(toTitleCase(getDisplayName(l)), l);
120 if (s.equals("zz_ZZ")) {
121 displayName = "Pseudo...";
123 displayName = toTitleCase(l.getDisplayLanguage(l));
126 Log.v(TAG, "adding "+displayName);
128 preprocess[finalSize++] = new Loc(displayName, l);
133 mLocales = new Loc[finalSize];
134 for (int i = 0; i < finalSize ; i++) {
135 mLocales[i] = preprocess[i];
137 Arrays.sort(mLocales);
138 int layoutId = R.layout.locale_picker_item;
139 int fieldId = R.id.locale;
140 ArrayAdapter<Loc> adapter =
141 new ArrayAdapter<Loc>(this, layoutId, fieldId, mLocales);
142 getListView().setAdapter(adapter);
145 private static String toTitleCase(String s) {
146 if (s.length() == 0) {
150 return Character.toUpperCase(s.charAt(0)) + s.substring(1);
153 private String getDisplayName(Locale l) {
154 String code = l.toString();
156 for (int i = 0; i < mSpecialLocaleCodes.length; i++) {
157 if (mSpecialLocaleCodes[i].equals(code)) {
158 return mSpecialLocaleNames[i];
162 return l.getDisplayName(l);
166 public void onResume() {
168 getListView().requestFocus();
172 protected void onListItemClick(ListView l, View v, int position, long id) {
174 IActivityManager am = ActivityManagerNative.getDefault();
175 Configuration config = am.getConfiguration();
177 Loc loc = mLocales[position];
178 config.locale = loc.locale;
180 // indicate this isn't some passing default - the user wants this remembered
181 config.userSetLocale = true;
183 am.updateConfiguration(config);
184 // Trigger the dirty bit for the Settings Provider.
185 BackupManager.dataChanged("com.android.providers.settings");
186 } catch (RemoteException e) {
187 // Intentionally left blank