1/* 2 * Copyright (C) 2011 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.inputmethod; 18 19import com.android.settings.R; 20import com.android.settings.SettingsPreferenceFragment; 21import com.android.settings.Utils; 22 23import android.app.Activity; 24import android.content.Context; 25import android.content.Intent; 26import android.database.Cursor; 27import android.os.Bundle; 28import android.preference.Preference; 29import android.preference.PreferenceGroup; 30import android.provider.UserDictionary; 31import android.text.TextUtils; 32import android.view.inputmethod.InputMethodInfo; 33import android.view.inputmethod.InputMethodManager; 34import android.view.inputmethod.InputMethodSubtype; 35 36import java.util.List; 37import java.util.Locale; 38import java.util.TreeSet; 39 40public class UserDictionaryList extends SettingsPreferenceFragment { 41 public static final String USER_DICTIONARY_SETTINGS_INTENT_ACTION = 42 "android.settings.USER_DICTIONARY_SETTINGS"; 43 private String mLocale; 44 45 @Override 46 public void onCreate(Bundle icicle) { 47 super.onCreate(icicle); 48 setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getActivity())); 49 getActivity().getActionBar().setTitle(R.string.user_dict_settings_title); 50 } 51 52 @Override 53 public void onActivityCreated(Bundle savedInstanceState) { 54 super.onActivityCreated(savedInstanceState); 55 56 final Intent intent = getActivity().getIntent(); 57 final String localeFromIntent = 58 null == intent ? null : intent.getStringExtra("locale"); 59 60 final Bundle arguments = getArguments(); 61 final String localeFromArguments = 62 null == arguments ? null : arguments.getString("locale"); 63 64 final String locale; 65 if (null != localeFromArguments) { 66 locale = localeFromArguments; 67 } else if (null != localeFromIntent) { 68 locale = localeFromIntent; 69 } else { 70 locale = null; 71 } 72 mLocale = locale; 73 } 74 75 public static TreeSet<String> getUserDictionaryLocalesSet(Context context) { 76 final Cursor cursor = context.getContentResolver().query( 77 UserDictionary.Words.CONTENT_URI, new String[] { UserDictionary.Words.LOCALE }, 78 null, null, null); 79 final TreeSet<String> localeSet = new TreeSet<String>(); 80 if (null == cursor) { 81 // The user dictionary service is not present or disabled. Return null. 82 return null; 83 } 84 try { 85 if (cursor.moveToFirst()) { 86 final int columnIndex = cursor.getColumnIndex(UserDictionary.Words.LOCALE); 87 do { 88 final String locale = cursor.getString(columnIndex); 89 localeSet.add(null != locale ? locale : ""); 90 } while (cursor.moveToNext()); 91 } 92 } finally { 93 cursor.close(); 94 } 95 96 // CAVEAT: Keep this for consistency of the implementation between Keyboard and Settings 97 // if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { 98 // // For ICS, we need to show "For all languages" in case that the keyboard locale 99 // // is different from the system locale 100 // localeSet.add(""); 101 // } 102 103 final InputMethodManager imm = 104 (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 105 final List<InputMethodInfo> imis = imm.getEnabledInputMethodList(); 106 for (final InputMethodInfo imi : imis) { 107 final List<InputMethodSubtype> subtypes = 108 imm.getEnabledInputMethodSubtypeList( 109 imi, true /* allowsImplicitlySelectedSubtypes */); 110 for (InputMethodSubtype subtype : subtypes) { 111 final String locale = subtype.getLocale(); 112 if (!TextUtils.isEmpty(locale)) { 113 localeSet.add(locale); 114 } 115 } 116 } 117 118 // We come here after we have collected locales from existing user dictionary entries and 119 // enabled subtypes. If we already have the locale-without-country version of the system 120 // locale, we don't add the system locale to avoid confusion even though it's technically 121 // correct to add it. 122 if (!localeSet.contains(Locale.getDefault().getLanguage().toString())) { 123 localeSet.add(Locale.getDefault().toString()); 124 } 125 126 return localeSet; 127 } 128 129 /** 130 * Creates the entries that allow the user to go into the user dictionary for each locale. 131 * @param userDictGroup The group to put the settings in. 132 */ 133 protected void createUserDictSettings(PreferenceGroup userDictGroup) { 134 final Activity activity = getActivity(); 135 userDictGroup.removeAll(); 136 final TreeSet<String> localeSet = 137 UserDictionaryList.getUserDictionaryLocalesSet(activity); 138 if (mLocale != null) { 139 // If the caller explicitly specify empty string as a locale, we'll show "all languages" 140 // in the list. 141 localeSet.add(mLocale); 142 } 143 if (localeSet.size() > 1) { 144 // Have an "All languages" entry in the languages list if there are two or more active 145 // languages 146 localeSet.add(""); 147 } 148 149 if (localeSet.isEmpty()) { 150 userDictGroup.addPreference(createUserDictionaryPreference(null, activity)); 151 } else { 152 for (String locale : localeSet) { 153 userDictGroup.addPreference(createUserDictionaryPreference(locale, activity)); 154 } 155 } 156 } 157 158 /** 159 * Create a single User Dictionary Preference object, with its parameters set. 160 * @param locale The locale for which this user dictionary is for. 161 * @return The corresponding preference. 162 */ 163 protected Preference createUserDictionaryPreference(String locale, Activity activity) { 164 final Preference newPref = new Preference(getActivity()); 165 final Intent intent = new Intent(USER_DICTIONARY_SETTINGS_INTENT_ACTION); 166 if (null == locale) { 167 newPref.setTitle(Locale.getDefault().getDisplayName()); 168 } else { 169 if ("".equals(locale)) 170 newPref.setTitle(getString(R.string.user_dict_settings_all_languages)); 171 else 172 newPref.setTitle(Utils.createLocaleFromString(locale).getDisplayName()); 173 intent.putExtra("locale", locale); 174 newPref.getExtras().putString("locale", locale); 175 } 176 newPref.setIntent(intent); 177 newPref.setFragment(com.android.settings.UserDictionarySettings.class.getName()); 178 return newPref; 179 } 180 181 @Override 182 public void onResume() { 183 super.onResume(); 184 createUserDictSettings(getPreferenceScreen()); 185 } 186} 187