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