blob: 386d7e0f4d27c4164289f3075bf14d0e55375be1 [file] [log] [blame]
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -07001/*
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.content.res.Configuration;
23import android.os.Bundle;
24import android.os.RemoteException;
25import android.os.SystemProperties;
26import android.util.Log;
27import android.view.View;
28import android.widget.ArrayAdapter;
29import android.widget.ListView;
30
31import java.io.BufferedWriter;
32import java.io.FileOutputStream;
33import java.util.Arrays;
34import java.util.Locale;
35
36public 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 Projectabc48f82008-12-17 18:06:01 -080066 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 Projectde2d9f52008-10-21 07:00:00 -070072 String s = locales[i];
73 int len = s.length();
The Android Open Source Projectabc48f82008-12-17 18:06:01 -080074 if (len == 2) {
75 Locale l = new Locale(s);
The Android Open Source Project1feaa852009-02-10 15:44:05 -080076 preprocess[finalSize++] = new Loc(toTitleCase(l.getDisplayLanguage()), l);
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070077 } else if (len == 5) {
The Android Open Source Projectabc48f82008-12-17 18:06:01 -080078 String language = s.substring(0, 2);
79 String country = s.substring(3, 5);
80 Locale l = new Locale(language, country);
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070081
The Android Open Source Projectabc48f82008-12-17 18:06:01 -080082 if (finalSize == 0) {
The Android Open Source Project1feaa852009-02-10 15:44:05 -080083 preprocess[finalSize++] = new Loc(toTitleCase(l.getDisplayLanguage()), l);
The Android Open Source Projectabc48f82008-12-17 18:06:01 -080084 } 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;
The Android Open Source Project1feaa852009-02-10 15:44:05 -080094 preprocess[finalSize-1].label = toTitleCase(l.getDisplayLanguage());
The Android Open Source Projectabc48f82008-12-17 18:06:01 -080095 } else {
The Android Open Source Project1feaa852009-02-10 15:44:05 -080096 preprocess[finalSize-1].label = toTitleCase(preprocess[finalSize-1].locale.getDisplayName());
97 preprocess[finalSize++] = new Loc(toTitleCase(l.getDisplayName()), l);
The Android Open Source Projectabc48f82008-12-17 18:06:01 -080098 }
99 } else {
100 String displayName;
101 if (s.equals("zz_ZZ")) {
102 displayName = "Pseudo...";
103 } else {
The Android Open Source Project1feaa852009-02-10 15:44:05 -0800104 displayName = toTitleCase(l.getDisplayLanguage());
The Android Open Source Projectabc48f82008-12-17 18:06:01 -0800105 }
106 preprocess[finalSize++] = new Loc(displayName, l);
107 }
108 }
109 }
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700110 }
The Android Open Source Projectabc48f82008-12-17 18:06:01 -0800111 mLocales = new Loc[finalSize];
112 for (int i = 0; i < finalSize ; i++) {
113 mLocales[i] = preprocess[i];
114 }
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700115 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
The Android Open Source Project1feaa852009-02-10 15:44:05 -0800121 private static String toTitleCase(String s) {
122 if (s.length() == 0) {
123 return s;
124 }
125
126 return Character.toUpperCase(s.charAt(0)) + s.substring(1);
127 }
128
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700129 @Override
130 public void onResume() {
131 super.onResume();
132 getListView().requestFocus();
133 }
134
135 @Override
136 protected void onListItemClick(ListView l, View v, int position, long id) {
137 try {
138 IActivityManager am = ActivityManagerNative.getDefault();
139 Configuration config = am.getConfiguration();
140
141 Loc loc = mLocales[position];
142 config.locale = loc.locale;
The Android Open Source Projectabc48f82008-12-17 18:06:01 -0800143
144 // indicate this isn't some passing default - the user wants this remembered
145 config.userSetLocale = true;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700146
147 am.updateConfiguration(config);
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700148 } catch (RemoteException e) {
149 // Intentionally left blank
150 }
151 finish();
152 }
153}