blob: 46d9b52b68f130cad50f29910d06c6404c6b97f1 [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();
66 final int N = locales.length;
67 mLocales = new Loc[N];
68 for (int i = 0; i < N; i++) {
69 Locale locale = null;
70 String s = locales[i];
71 int len = s.length();
72 if (len == 0) {
73 locale = new Locale("en", "US");
74 } else if (len == 2) {
75 locale = new Locale(s);
76 } else if (len == 5) {
77 locale = new Locale(s.substring(0, 2), s.substring(3, 5));
78 }
79 String displayName = "";
80 if (locale != null) {
81 displayName = locale.getDisplayName();
82 }
83 if ("zz_ZZ".equals(s)) {
84 displayName = "Pseudo...";
85 }
86
87 mLocales[i] = new Loc(displayName, locale);
88 }
89
90 int layoutId = R.layout.locale_picker_item;
91 int fieldId = R.id.locale;
92 ArrayAdapter<Loc> adapter = new ArrayAdapter<Loc>(this, layoutId, fieldId, mLocales);
93 getListView().setAdapter(adapter);
94 }
95
96 @Override
97 public void onResume() {
98 super.onResume();
99 getListView().requestFocus();
100 }
101
102 @Override
103 protected void onListItemClick(ListView l, View v, int position, long id) {
104 try {
105 IActivityManager am = ActivityManagerNative.getDefault();
106 Configuration config = am.getConfiguration();
107
108 Loc loc = mLocales[position];
109 config.locale = loc.locale;
110 final String language = loc.locale.getLanguage();
111 final String region = loc.locale.getCountry();
112
113 am.updateConfiguration(config);
114
115 // Update the System properties
116 SystemProperties.set("user.language", language);
117 SystemProperties.set("user.region", region);
118 // Write to file for persistence across reboots
119 try {
120 BufferedWriter bw = new BufferedWriter(new java.io.FileWriter(
121 System.getenv("ANDROID_DATA") + "/locale"));
122 bw.write(language + "_" + region);
123 bw.close();
124 } catch (java.io.IOException ioe) {
125 Log.e(TAG,
126 "Unable to persist locale. Error writing to locale file."
127 + ioe);
128 }
129 } catch (RemoteException e) {
130 // Intentionally left blank
131 }
132 finish();
133 }
134}