blob: 51b770d0942f744ee15bfd8515c10dc85d3c703b [file] [log] [blame]
The Android Open Source Projectabc48f82008-12-17 18:06:01 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.settings;
18
19import java.util.HashSet;
20import java.util.List;
21
22import android.content.Context;
23import android.content.Intent;
24import android.os.Bundle;
25import android.os.RemoteException;
26import android.os.SystemProperties;
27import android.preference.Preference;
28import android.preference.PreferenceActivity;
29import android.preference.PreferenceScreen;
30import android.preference.CheckBoxPreference;
31import android.provider.Settings;
32import android.text.TextUtils;
33import android.view.inputmethod.InputMethodInfo;
34import android.view.inputmethod.InputMethodManager;
35
36/*
37 * Displays preferences for input methods.
38 */
39public class InputMethodsSettings extends PreferenceActivity {
40 private List<InputMethodInfo> mInputMethodProperties;
41
42 final TextUtils.SimpleStringSplitter mStringColonSplitter
43 = new TextUtils.SimpleStringSplitter(':');
44
45 private String mLastInputMethodId;
46 private String mLastTickedInputMethodId;
47
48 static public String getInputMethodIdFromKey(String key) {
49 return key;
50 }
51
52 @Override
53 protected void onCreate(Bundle icicle) {
54 super.onCreate(icicle);
55
56 addPreferencesFromResource(R.xml.input_methods_prefs);
57
58 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
59
60 mInputMethodProperties = imm.getInputMethodList();
61
62 mLastInputMethodId = Settings.Secure.getString(getContentResolver(),
63 Settings.Secure.DEFAULT_INPUT_METHOD);
64
65 int N = (mInputMethodProperties == null ? 0 : mInputMethodProperties
66 .size());
67 for (int i = 0; i < N; ++i) {
68 InputMethodInfo property = mInputMethodProperties.get(i);
69 String prefKey = property.getId();
70
71 CharSequence label = property.loadLabel(getPackageManager());
72
73 // Add a check box.
74 CheckBoxPreference chkbxPref = new CheckBoxPreference(this);
75 chkbxPref.setKey(prefKey);
76 chkbxPref.setTitle(label);
77 getPreferenceScreen().addPreference(chkbxPref);
78
79 // If setting activity is available, add a setting screen entry.
80 if (null != property.getSettingsActivity()) {
81 PreferenceScreen prefScreen = new PreferenceScreen(this, null);
82 prefScreen.setKey(property.getSettingsActivity());
The Android Open Source Project5962e182009-01-09 17:51:25 -080083 prefScreen.setTitle(getResources().getString(
84 R.string.input_methods_settings_label_format, label));
The Android Open Source Projectabc48f82008-12-17 18:06:01 -080085 getPreferenceScreen().addPreference(prefScreen);
86 }
87 }
88 }
89
90 @Override
91 protected void onResume() {
92 super.onResume();
93
94 final HashSet<String> enabled = new HashSet<String>();
95 String enabledStr = Settings.Secure.getString(getContentResolver(),
96 Settings.Secure.ENABLED_INPUT_METHODS);
97 if (enabledStr != null) {
98 final TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
99 splitter.setString(enabledStr);
100 while (splitter.hasNext()) {
101 enabled.add(splitter.next());
102 }
103 }
104
105 // Update the statuses of the Check Boxes.
106 int N = mInputMethodProperties.size();
107 for (int i = 0; i < N; ++i) {
108 final String id = mInputMethodProperties.get(i).getId();
109 CheckBoxPreference pref = (CheckBoxPreference) findPreference(mInputMethodProperties
110 .get(i).getId());
111 pref.setChecked(enabled.contains(id));
112 }
113 mLastTickedInputMethodId = null;
114 }
115
116 @Override
117 protected void onPause() {
118 super.onPause();
119
120 StringBuilder builder = new StringBuilder(256);
121
122 boolean haveLastInputMethod = false;
123
124 int firstEnabled = -1;
125 int N = mInputMethodProperties.size();
126 for (int i = 0; i < N; ++i) {
127 final String id = mInputMethodProperties.get(i).getId();
128 CheckBoxPreference pref = (CheckBoxPreference) findPreference(id);
129 boolean hasIt = id.equals(mLastInputMethodId);
130 if (pref.isChecked()) {
131 if (builder.length() > 0) builder.append(':');
132 builder.append(id);
133 if (firstEnabled < 0) {
134 firstEnabled = i;
135 }
136 if (hasIt) haveLastInputMethod = true;
137 } else if (hasIt) {
138 mLastInputMethodId = mLastTickedInputMethodId;
139 }
140 }
141
142 // If the last input method is unset, set it as the first enabled one.
143 if (null == mLastInputMethodId || "".equals(mLastInputMethodId)) {
144 if (firstEnabled >= 0) {
145 mLastInputMethodId = mInputMethodProperties.get(firstEnabled).getId();
146 } else {
147 mLastInputMethodId = null;
148 }
149 }
150
151 Settings.Secure.putString(getContentResolver(),
152 Settings.Secure.ENABLED_INPUT_METHODS, builder.toString());
153 Settings.Secure.putString(getContentResolver(),
154 Settings.Secure.DEFAULT_INPUT_METHOD, mLastInputMethodId);
155 }
156
157 @Override
158 public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
159 Preference preference) {
160
161 // Those monkeys kept committing suicide, so we add this property
162 // to disable this functionality
163 if (!TextUtils.isEmpty(SystemProperties.get("ro.monkey"))) {
164 return false;
165 }
166
167 if (preference instanceof CheckBoxPreference) {
168 CheckBoxPreference chkPref = (CheckBoxPreference) preference;
169 String id = getInputMethodIdFromKey(chkPref.getKey());
170 if (chkPref.isChecked()) {
171 mLastTickedInputMethodId = id;
172 } else if (id.equals(mLastTickedInputMethodId)) {
173 mLastTickedInputMethodId = null;
174 }
175 } else if (preference instanceof PreferenceScreen) {
176 if (preference.getIntent() == null) {
177 PreferenceScreen pref = (PreferenceScreen) preference;
178 String activityName = pref.getKey();
179 String packageName = activityName.substring(0, activityName
180 .lastIndexOf("."));
181 if (activityName.length() > 0) {
182 Intent i = new Intent(Intent.ACTION_MAIN);
183 i.setClassName(packageName, activityName);
184 startActivity(i);
185 }
186 }
187 }
188
189 return false;
190 }
191}