The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.app.ActivityManagerNative; |
| 20 | import android.app.IActivityManager; |
| 21 | import android.app.ListActivity; |
| 22 | import android.content.res.Configuration; |
| 23 | import android.os.Bundle; |
| 24 | import android.os.RemoteException; |
| 25 | import android.os.SystemProperties; |
| 26 | import android.util.Log; |
| 27 | import android.view.View; |
| 28 | import android.widget.ArrayAdapter; |
| 29 | import android.widget.ListView; |
| 30 | |
| 31 | import java.io.BufferedWriter; |
| 32 | import java.io.FileOutputStream; |
| 33 | import java.util.Arrays; |
| 34 | import java.util.Locale; |
| 35 | |
| 36 | public class LocalePicker extends ListActivity { |
| 37 | private static final String TAG = "LocalePicker"; |
| 38 | |
| 39 | Loc[] mLocales; |
| 40 | |
| 41 | private static class Loc { |
| 42 | String label; |
| 43 | Locale locale; |
| 44 | |
| 45 | public Loc(String label, Locale locale) { |
| 46 | this.label = label; |
| 47 | this.locale = locale; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public String toString() { |
| 52 | return this.label; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | int getContentView() { |
| 57 | return R.layout.locale_picker; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void onCreate(Bundle icicle) { |
| 62 | super.onCreate(icicle); |
| 63 | setContentView(getContentView()); |
| 64 | |
| 65 | String[] locales = getAssets().getLocales(); |
The Android Open Source Project | abc48f8 | 2008-12-17 18:06:01 -0800 | [diff] [blame] | 66 | Arrays.sort(locales); |
| 67 | |
| 68 | final int origSize = locales.length; |
| 69 | Loc[] preprocess = new Loc[origSize]; |
| 70 | int finalSize = 0; |
| 71 | for (int i = 0 ; i < origSize; i++ ) { |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 72 | String s = locales[i]; |
| 73 | int len = s.length(); |
The Android Open Source Project | abc48f8 | 2008-12-17 18:06:01 -0800 | [diff] [blame] | 74 | if (len == 2) { |
| 75 | Locale l = new Locale(s); |
| 76 | preprocess[finalSize++] = new Loc(l.getDisplayLanguage(), l); |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 77 | } else if (len == 5) { |
The Android Open Source Project | abc48f8 | 2008-12-17 18:06:01 -0800 | [diff] [blame] | 78 | String language = s.substring(0, 2); |
| 79 | String country = s.substring(3, 5); |
| 80 | Locale l = new Locale(language, country); |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 81 | |
The Android Open Source Project | abc48f8 | 2008-12-17 18:06:01 -0800 | [diff] [blame] | 82 | if (finalSize == 0) { |
| 83 | preprocess[finalSize++] = new Loc(l.getDisplayLanguage(), l); |
| 84 | } else { |
| 85 | // check previous entry: |
| 86 | // same lang and no country -> overwrite it with a lang-only name |
| 87 | // same lang and a country -> upgrade to full name and |
| 88 | // insert ours with full name |
| 89 | // diff lang -> insert ours with lang-only name |
| 90 | if (preprocess[finalSize-1].locale.getLanguage().equals(language)) { |
| 91 | String prevCountry = preprocess[finalSize-1].locale.getCountry(); |
| 92 | if (prevCountry.length() == 0) { |
| 93 | preprocess[finalSize-1].locale = l; |
| 94 | preprocess[finalSize-1].label = l.getDisplayLanguage(); |
| 95 | } else { |
| 96 | preprocess[finalSize-1].label = preprocess[finalSize-1].locale.getDisplayName(); |
| 97 | preprocess[finalSize++] = new Loc(l.getDisplayName(), l); |
| 98 | } |
| 99 | } else { |
| 100 | String displayName; |
| 101 | if (s.equals("zz_ZZ")) { |
| 102 | displayName = "Pseudo..."; |
| 103 | } else { |
| 104 | displayName = l.getDisplayLanguage(); |
| 105 | } |
| 106 | preprocess[finalSize++] = new Loc(displayName, l); |
| 107 | } |
| 108 | } |
| 109 | } |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | } |
The Android Open Source Project | abc48f8 | 2008-12-17 18:06:01 -0800 | [diff] [blame] | 111 | mLocales = new Loc[finalSize]; |
| 112 | for (int i = 0; i < finalSize ; i++) { |
| 113 | mLocales[i] = preprocess[i]; |
| 114 | } |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 115 | int layoutId = R.layout.locale_picker_item; |
| 116 | int fieldId = R.id.locale; |
| 117 | ArrayAdapter<Loc> adapter = new ArrayAdapter<Loc>(this, layoutId, fieldId, mLocales); |
| 118 | getListView().setAdapter(adapter); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void onResume() { |
| 123 | super.onResume(); |
| 124 | getListView().requestFocus(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | protected void onListItemClick(ListView l, View v, int position, long id) { |
| 129 | try { |
| 130 | IActivityManager am = ActivityManagerNative.getDefault(); |
| 131 | Configuration config = am.getConfiguration(); |
| 132 | |
| 133 | Loc loc = mLocales[position]; |
| 134 | config.locale = loc.locale; |
The Android Open Source Project | abc48f8 | 2008-12-17 18:06:01 -0800 | [diff] [blame] | 135 | |
| 136 | // indicate this isn't some passing default - the user wants this remembered |
| 137 | config.userSetLocale = true; |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 138 | |
| 139 | am.updateConfiguration(config); |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 140 | } catch (RemoteException e) { |
| 141 | // Intentionally left blank |
| 142 | } |
| 143 | finish(); |
| 144 | } |
| 145 | } |