blob: 9ee82601be7845a1041189ae2f52ec3663a44d69 [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);
76 preprocess[finalSize++] = new Loc(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) {
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 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
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 Projectabc48f82008-12-17 18:06:01 -0800135
136 // indicate this isn't some passing default - the user wants this remembered
137 config.userSetLocale = true;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700138
139 am.updateConfiguration(config);
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700140 } catch (RemoteException e) {
141 // Intentionally left blank
142 }
143 finish();
144 }
145}