blob: 8463d26fcac1addc678b2d37eeb3f9dbda80b444 [file] [log] [blame]
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2008 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.content.ContentResolver;
The Android Open Source Project8a156092009-03-02 22:54:43 -080020import android.content.Context;
21import android.content.Intent;
The Android Open Source Project1feaa852009-02-10 15:44:05 -080022import android.content.res.Configuration;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070023import android.os.Bundle;
The Android Open Source Project8a156092009-03-02 22:54:43 -080024import android.os.SystemProperties;
25import android.preference.CheckBoxPreference;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070026import android.preference.Preference;
27import android.preference.PreferenceActivity;
28import android.preference.PreferenceGroup;
29import android.preference.PreferenceScreen;
The Android Open Source Project8a156092009-03-02 22:54:43 -080030import android.provider.Settings;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070031import android.provider.Settings.System;
The Android Open Source Project8a156092009-03-02 22:54:43 -080032import android.text.TextUtils;
33import android.view.inputmethod.InputMethodInfo;
34import android.view.inputmethod.InputMethodManager;
35
36import java.util.HashSet;
37import java.util.List;
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070038
39public class LanguageSettings extends PreferenceActivity {
40
41 private final String[] mSettingsUiKey = {
42 "auto_caps",
43 "auto_replace",
44 "auto_punctuate",
45 };
46
47 // Note: Order of this array should correspond to the order of the above array
48 private final String[] mSettingsSystemId = {
49 System.TEXT_AUTO_CAPS,
50 System.TEXT_AUTO_REPLACE,
51 System.TEXT_AUTO_PUNCTUATE,
52 };
53
54 // Note: Order of this array should correspond to the order of the above array
55 private final int[] mSettingsDefault = {
56 1,
57 1,
58 1,
59 };
60
The Android Open Source Project8a156092009-03-02 22:54:43 -080061 private List<InputMethodInfo> mInputMethodProperties;
62
63 final TextUtils.SimpleStringSplitter mStringColonSplitter
64 = new TextUtils.SimpleStringSplitter(':');
65
66 private String mLastInputMethodId;
67 private String mLastTickedInputMethodId;
68
69 static public String getInputMethodIdFromKey(String key) {
70 return key;
71 }
72
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070073 @Override
74 protected void onCreate(Bundle icicle) {
75 super.onCreate(icicle);
76
77 addPreferencesFromResource(R.xml.language_settings);
78
79 if (getAssets().getLocales().length == 1) {
80 getPreferenceScreen().
81 removePreference(findPreference("language_category"));
82 }
83
The Android Open Source Project1feaa852009-02-10 15:44:05 -080084 Configuration config = getResources().getConfiguration();
85 if (config.keyboard != Configuration.KEYBOARD_QWERTY) {
86 getPreferenceScreen().removePreference(
87 getPreferenceScreen().findPreference("hardkeyboard_category"));
88 } else {
89 ContentResolver resolver = getContentResolver();
90 for (int i = 0; i < mSettingsUiKey.length; i++) {
91 CheckBoxPreference pref = (CheckBoxPreference) findPreference(mSettingsUiKey[i]);
92 pref.setChecked(System.getInt(resolver, mSettingsSystemId[i],
93 mSettingsDefault[i]) > 0);
94 }
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -070095 }
The Android Open Source Project8a156092009-03-02 22:54:43 -080096
97 onCreateIMM();
98 }
99
100 private void onCreateIMM() {
101 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
102
103 mInputMethodProperties = imm.getInputMethodList();
104
105 mLastInputMethodId = Settings.Secure.getString(getContentResolver(),
106 Settings.Secure.DEFAULT_INPUT_METHOD);
107
108 PreferenceGroup textCategory = (PreferenceGroup) findPreference("text_category");
109
110 int N = (mInputMethodProperties == null ? 0 : mInputMethodProperties
111 .size());
112 for (int i = 0; i < N; ++i) {
113 InputMethodInfo property = mInputMethodProperties.get(i);
114 String prefKey = property.getId();
115
116 CharSequence label = property.loadLabel(getPackageManager());
117
118 // Add a check box.
119 CheckBoxPreference chkbxPref = new CheckBoxPreference(this);
120 chkbxPref.setKey(prefKey);
121 chkbxPref.setTitle(label);
122 textCategory.addPreference(chkbxPref);
123
124 // If setting activity is available, add a setting screen entry.
125 if (null != property.getSettingsActivity()) {
126 PreferenceScreen prefScreen = new PreferenceScreen(this, null);
127 prefScreen.setKey(property.getSettingsActivity());
128 CharSequence settingsLabel = getResources().getString(
129 R.string.input_methods_settings_label_format, label);
130 prefScreen.setTitle(settingsLabel);
131 textCategory.addPreference(prefScreen);
132 }
133 }
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700134 }
135
136 @Override
The Android Open Source Project8a156092009-03-02 22:54:43 -0800137 protected void onResume() {
138 super.onResume();
139
140 final HashSet<String> enabled = new HashSet<String>();
141 String enabledStr = Settings.Secure.getString(getContentResolver(),
142 Settings.Secure.ENABLED_INPUT_METHODS);
143 if (enabledStr != null) {
144 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
145 splitter.setString(enabledStr);
146 while (splitter.hasNext()) {
147 enabled.add(splitter.next());
148 }
149 }
150
151 // Update the statuses of the Check Boxes.
152 int N = mInputMethodProperties.size();
153 for (int i = 0; i < N; ++i) {
154 final String id = mInputMethodProperties.get(i).getId();
155 CheckBoxPreference pref = (CheckBoxPreference) findPreference(mInputMethodProperties
156 .get(i).getId());
157 pref.setChecked(enabled.contains(id));
158 }
159 mLastTickedInputMethodId = null;
160 }
161
162 @Override
163 protected void onPause() {
164 super.onPause();
165
166 StringBuilder builder = new StringBuilder(256);
167
168 boolean haveLastInputMethod = false;
169
170 int firstEnabled = -1;
171 int N = mInputMethodProperties.size();
172 for (int i = 0; i < N; ++i) {
173 final String id = mInputMethodProperties.get(i).getId();
174 CheckBoxPreference pref = (CheckBoxPreference) findPreference(id);
175 boolean hasIt = id.equals(mLastInputMethodId);
176 if (pref.isChecked()) {
177 if (builder.length() > 0) builder.append(':');
178 builder.append(id);
179 if (firstEnabled < 0) {
180 firstEnabled = i;
181 }
182 if (hasIt) haveLastInputMethod = true;
183 } else if (hasIt) {
184 mLastInputMethodId = mLastTickedInputMethodId;
185 }
186 }
187
188 // If the last input method is unset, set it as the first enabled one.
189 if (null == mLastInputMethodId || "".equals(mLastInputMethodId)) {
190 if (firstEnabled >= 0) {
191 mLastInputMethodId = mInputMethodProperties.get(firstEnabled).getId();
192 } else {
193 mLastInputMethodId = null;
194 }
195 }
196
197 Settings.Secure.putString(getContentResolver(),
198 Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
199 Settings.Secure.putString(getContentResolver(),
200 Settings.Secure.DEFAULT_INPUT_METHOD, mLastInputMethodId);
201 }
202
203 @Override
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700204 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
The Android Open Source Project8a156092009-03-02 22:54:43 -0800205
206 // Physical keyboard stuff
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700207 for (int i = 0; i < mSettingsUiKey.length; i++) {
208 if (mSettingsUiKey[i].equals(preference.getKey())) {
209 System.putInt(getContentResolver(), mSettingsSystemId[i],
210 ((CheckBoxPreference)preference).isChecked()? 1 : 0);
211 return true;
212 }
213 }
The Android Open Source Project8a156092009-03-02 22:54:43 -0800214
215 // Input Method stuff
216 // Those monkeys kept committing suicide, so we add this property
217 // to disable this functionality
218 if (!TextUtils.isEmpty(SystemProperties.get("ro.monkey"))) {
219 return false;
220 }
221
222 if (preference instanceof CheckBoxPreference) {
223 CheckBoxPreference chkPref = (CheckBoxPreference) preference;
224 String id = getInputMethodIdFromKey(chkPref.getKey());
225 if (chkPref.isChecked()) {
226 mLastTickedInputMethodId = id;
227 } else if (id.equals(mLastTickedInputMethodId)) {
228 mLastTickedInputMethodId = null;
229 }
230 } else if (preference instanceof PreferenceScreen) {
231 if (preference.getIntent() == null) {
232 PreferenceScreen pref = (PreferenceScreen) preference;
233 String activityName = pref.getKey();
234 String packageName = activityName.substring(0, activityName
235 .lastIndexOf("."));
236 if (activityName.length() > 0) {
237 Intent i = new Intent(Intent.ACTION_MAIN);
238 i.setClassName(packageName, activityName);
239 startActivity(i);
240 }
241 }
242 }
243
The Android Open Source Projectde2d9f52008-10-21 07:00:00 -0700244 return super.onPreferenceTreeClick(preferenceScreen, preference);
245 }
246
247}