The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings; |
| 18 | |
| 19 | import android.content.ContentResolver; |
The Android Open Source Project | 8a15609 | 2009-03-02 22:54:43 -0800 | [diff] [blame^] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
The Android Open Source Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 22 | import android.content.res.Configuration; |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 23 | import android.os.Bundle; |
The Android Open Source Project | 8a15609 | 2009-03-02 22:54:43 -0800 | [diff] [blame^] | 24 | import android.os.SystemProperties; |
| 25 | import android.preference.CheckBoxPreference; |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | import android.preference.Preference; |
| 27 | import android.preference.PreferenceActivity; |
| 28 | import android.preference.PreferenceGroup; |
| 29 | import android.preference.PreferenceScreen; |
The Android Open Source Project | 8a15609 | 2009-03-02 22:54:43 -0800 | [diff] [blame^] | 30 | import android.provider.Settings; |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 31 | import android.provider.Settings.System; |
The Android Open Source Project | 8a15609 | 2009-03-02 22:54:43 -0800 | [diff] [blame^] | 32 | import android.text.TextUtils; |
| 33 | import android.view.inputmethod.InputMethodInfo; |
| 34 | import android.view.inputmethod.InputMethodManager; |
| 35 | |
| 36 | import java.util.HashSet; |
| 37 | import java.util.List; |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 38 | |
| 39 | public 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 Project | 8a15609 | 2009-03-02 22:54:43 -0800 | [diff] [blame^] | 61 | 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 Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 | @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 Project | 1feaa85 | 2009-02-10 15:44:05 -0800 | [diff] [blame] | 84 | 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 Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | } |
The Android Open Source Project | 8a15609 | 2009-03-02 22:54:43 -0800 | [diff] [blame^] | 96 | |
| 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 Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
The Android Open Source Project | 8a15609 | 2009-03-02 22:54:43 -0800 | [diff] [blame^] | 137 | 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 Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { |
The Android Open Source Project | 8a15609 | 2009-03-02 22:54:43 -0800 | [diff] [blame^] | 205 | |
| 206 | // Physical keyboard stuff |
The Android Open Source Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 207 | 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 Project | 8a15609 | 2009-03-02 22:54:43 -0800 | [diff] [blame^] | 214 | |
| 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 Project | de2d9f5 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 244 | return super.onPreferenceTreeClick(preferenceScreen, preference); |
| 245 | } |
| 246 | |
| 247 | } |