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