1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17package com.android.settings; 18 19import android.app.ActivityManagerNative; 20import android.app.IActivityManager; 21import android.app.ListActivity; 22import android.app.backup.BackupManager; 23import android.content.res.Configuration; 24import android.os.Bundle; 25import android.os.RemoteException; 26import android.os.SystemProperties; 27import android.util.Log; 28import android.view.View; 29import android.widget.ArrayAdapter; 30import android.widget.ListView; 31 32import java.io.BufferedWriter; 33import java.io.FileOutputStream; 34import java.text.Collator; 35import java.util.Arrays; 36import java.util.Locale; 37 38public class LocalePicker extends ListActivity { 39 private static final String TAG = "LocalePicker"; 40 private static final boolean DEBUG = false; 41 42 Loc[] mLocales; 43 String[] mSpecialLocaleCodes; 44 String[] mSpecialLocaleNames; 45 46 private static class Loc implements Comparable { 47 static Collator sCollator = Collator.getInstance(); 48 49 String label; 50 Locale locale; 51 52 public Loc(String label, Locale locale) { 53 this.label = label; 54 this.locale = locale; 55 } 56 57 @Override 58 public String toString() { 59 return this.label; 60 } 61 62 public int compareTo(Object o) { 63 return sCollator.compare(this.label, ((Loc) o).label); 64 } 65 } 66 67 int getContentView() { 68 return R.layout.locale_picker; 69 } 70 71 @Override 72 public void onCreate(Bundle icicle) { 73 super.onCreate(icicle); 74 setContentView(getContentView()); 75 76 mSpecialLocaleCodes = getResources().getStringArray(R.array.special_locale_codes); 77 mSpecialLocaleNames = getResources().getStringArray(R.array.special_locale_names); 78 79 String[] locales = getAssets().getLocales(); 80 Arrays.sort(locales); 81 82 final int origSize = locales.length; 83 Loc[] preprocess = new Loc[origSize]; 84 int finalSize = 0; 85 for (int i = 0 ; i < origSize; i++ ) { 86 String s = locales[i]; 87 int len = s.length(); 88 if (len == 5) { 89 String language = s.substring(0, 2); 90 String country = s.substring(3, 5); 91 Locale l = new Locale(language, country); 92 93 if (finalSize == 0) { 94 if (DEBUG) { 95 Log.v(TAG, "adding initial "+ toTitleCase(l.getDisplayLanguage(l))); 96 } 97 preprocess[finalSize++] = 98 new Loc(toTitleCase(l.getDisplayLanguage(l)), l); 99 } else { 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( 105 language)) { 106 if (DEBUG) { 107 Log.v(TAG, "backing up and fixing "+ 108 preprocess[finalSize-1].label+" to "+ 109 getDisplayName(preprocess[finalSize-1].locale)); 110 } 111 preprocess[finalSize-1].label = toTitleCase( 112 getDisplayName(preprocess[finalSize-1].locale)); 113 if (DEBUG) { 114 Log.v(TAG, " and adding "+ toTitleCase(getDisplayName(l))); 115 } 116 preprocess[finalSize++] = 117 new Loc(toTitleCase(getDisplayName(l)), l); 118 } else { 119 String displayName; 120 if (s.equals("zz_ZZ")) { 121 displayName = "Pseudo..."; 122 } else { 123 displayName = toTitleCase(l.getDisplayLanguage(l)); 124 } 125 if (DEBUG) { 126 Log.v(TAG, "adding "+displayName); 127 } 128 preprocess[finalSize++] = new Loc(displayName, l); 129 } 130 } 131 } 132 } 133 mLocales = new Loc[finalSize]; 134 for (int i = 0; i < finalSize ; i++) { 135 mLocales[i] = preprocess[i]; 136 } 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); 143 } 144 145 private static String toTitleCase(String s) { 146 if (s.length() == 0) { 147 return s; 148 } 149 150 return Character.toUpperCase(s.charAt(0)) + s.substring(1); 151 } 152 153 private String getDisplayName(Locale l) { 154 String code = l.toString(); 155 156 for (int i = 0; i < mSpecialLocaleCodes.length; i++) { 157 if (mSpecialLocaleCodes[i].equals(code)) { 158 return mSpecialLocaleNames[i]; 159 } 160 } 161 162 return l.getDisplayName(l); 163 } 164 165 @Override 166 public void onResume() { 167 super.onResume(); 168 getListView().requestFocus(); 169 } 170 171 @Override 172 protected void onListItemClick(ListView l, View v, int position, long id) { 173 try { 174 IActivityManager am = ActivityManagerNative.getDefault(); 175 Configuration config = am.getConfiguration(); 176 177 Loc loc = mLocales[position]; 178 config.locale = loc.locale; 179 180 // indicate this isn't some passing default - the user wants this remembered 181 config.userSetLocale = true; 182 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 188 } 189 finish(); 190 } 191} 192