UserDictionaryAddWordContents.java revision 6233ee3633da9489e29bcf86327af28249bd1834
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 com.android.inputmethod.compat.UserDictionaryCompatUtils; 20import com.android.inputmethod.latin.LocaleUtils; 21import com.android.inputmethod.latin.R; 22 23import android.app.Activity; 24import android.content.ContentResolver; 25import android.content.Context; 26import android.database.Cursor; 27import android.os.Bundle; 28import android.provider.UserDictionary; 29import android.text.TextUtils; 30import android.view.View; 31import android.widget.EditText; 32 33import java.util.ArrayList; 34import java.util.Locale; 35import java.util.TreeSet; 36 37// Caveat: This class is basically taken from 38// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java 39// in order to deal with some devices that have issues with the user dictionary handling 40 41/** 42 * A container class to factor common code to UserDictionaryAddWordFragment 43 * and UserDictionaryAddWordActivity. 44 */ 45public class UserDictionaryAddWordContents { 46 public static final String EXTRA_MODE = "mode"; 47 public static final String EXTRA_WORD = "word"; 48 public static final String EXTRA_SHORTCUT = "shortcut"; 49 public static final String EXTRA_LOCALE = "locale"; 50 public static final String EXTRA_ORIGINAL_WORD = "originalWord"; 51 public static final String EXTRA_ORIGINAL_SHORTCUT = "originalShortcut"; 52 53 public static final int MODE_EDIT = 0; 54 public static final int MODE_INSERT = 1; 55 56 /* package */ static final int CODE_WORD_ADDED = 0; 57 /* package */ static final int CODE_CANCEL = 1; 58 /* package */ static final int CODE_ALREADY_PRESENT = 2; 59 60 private static final int FREQUENCY_FOR_USER_DICTIONARY_ADDS = 250; 61 62 private final int mMode; // Either MODE_EDIT or MODE_INSERT 63 private final EditText mWordEditText; 64 private final EditText mShortcutEditText; 65 private String mLocale; 66 private final String mOldWord; 67 private final String mOldShortcut; 68 69 /* package */ UserDictionaryAddWordContents(final View view, final Bundle args) { 70 mWordEditText = (EditText)view.findViewById(R.id.user_dictionary_add_word_text); 71 mShortcutEditText = (EditText)view.findViewById(R.id.user_dictionary_add_shortcut); 72 if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { 73 mShortcutEditText.setVisibility(View.GONE); 74 view.findViewById(R.id.user_dictionary_add_shortcut_label).setVisibility(View.GONE); 75 } 76 final String word = args.getString(EXTRA_WORD); 77 if (null != word) { 78 mWordEditText.setText(word); 79 // Use getText in case the edit text modified the text we set. This happens when 80 // it's too long to be edited. 81 mWordEditText.setSelection(mWordEditText.getText().length()); 82 } 83 final String shortcut; 84 if (UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { 85 shortcut = args.getString(EXTRA_SHORTCUT); 86 if (null != shortcut && null != mShortcutEditText) { 87 mShortcutEditText.setText(shortcut); 88 } 89 mOldShortcut = args.getString(EXTRA_SHORTCUT); 90 } else { 91 shortcut = null; 92 mOldShortcut = null; 93 } 94 mMode = args.getInt(EXTRA_MODE); // default return value for #getInt() is 0 = MODE_EDIT 95 mOldWord = args.getString(EXTRA_WORD); 96 updateLocale(args.getString(EXTRA_LOCALE)); 97 } 98 99 // locale may be null, this means default locale 100 // It may also be the empty string, which means "all locales" 101 /* package */ void updateLocale(final String locale) { 102 mLocale = null == locale ? Locale.getDefault().toString() : locale; 103 } 104 105 /* package */ void saveStateIntoBundle(final Bundle outState) { 106 outState.putString(EXTRA_WORD, mWordEditText.getText().toString()); 107 outState.putString(EXTRA_ORIGINAL_WORD, mOldWord); 108 if (null != mShortcutEditText) { 109 outState.putString(EXTRA_SHORTCUT, mShortcutEditText.getText().toString()); 110 } 111 if (null != mOldShortcut) { 112 outState.putString(EXTRA_ORIGINAL_SHORTCUT, mOldShortcut); 113 } 114 outState.putString(EXTRA_LOCALE, mLocale); 115 } 116 117 /* package */ void delete(final Context context) { 118 if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { 119 // Mode edit: remove the old entry. 120 final ContentResolver resolver = context.getContentResolver(); 121 UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver); 122 } 123 // If we are in add mode, nothing was added, so we don't need to do anything. 124 } 125 126 /* package */ 127 int apply(final Context context, final Bundle outParameters) { 128 if (null != outParameters) saveStateIntoBundle(outParameters); 129 final ContentResolver resolver = context.getContentResolver(); 130 if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { 131 // Mode edit: remove the old entry. 132 UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver); 133 } 134 final String newWord = mWordEditText.getText().toString(); 135 final String newShortcut; 136 if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { 137 newShortcut = null; 138 } else if (null == mShortcutEditText) { 139 newShortcut = null; 140 } else { 141 final String tmpShortcut = mShortcutEditText.getText().toString(); 142 if (TextUtils.isEmpty(tmpShortcut)) { 143 newShortcut = null; 144 } else { 145 newShortcut = tmpShortcut; 146 } 147 } 148 if (TextUtils.isEmpty(newWord)) { 149 // If the word is somehow empty, don't insert it. 150 return CODE_CANCEL; 151 } 152 // If there is no shortcut, and the word already exists in the database, then we 153 // should not insert, because either A. the word exists with no shortcut, in which 154 // case the exact same thing we want to insert is already there, or B. the word 155 // exists with at least one shortcut, in which case it has priority on our word. 156 if (hasWord(newWord, context)) return CODE_ALREADY_PRESENT; 157 158 // Disallow duplicates. If the same word with no shortcut is defined, remove it; if 159 // the same word with the same shortcut is defined, remove it; but we don't mind if 160 // there is the same word with a different, non-empty shortcut. 161 UserDictionarySettings.deleteWord(newWord, null, resolver); 162 if (!TextUtils.isEmpty(newShortcut)) { 163 // If newShortcut is empty we just deleted this, no need to do it again 164 UserDictionarySettings.deleteWord(newWord, newShortcut, resolver); 165 } 166 167 // In this class we use the empty string to represent 'all locales' and mLocale cannot 168 // be null. However the addWord method takes null to mean 'all locales'. 169 UserDictionaryCompatUtils.addWord(context, newWord.toString(), 170 FREQUENCY_FOR_USER_DICTIONARY_ADDS, newShortcut, TextUtils.isEmpty(mLocale) ? 171 null : LocaleUtils.constructLocaleFromString(mLocale)); 172 173 return CODE_WORD_ADDED; 174 } 175 176 private static final String[] HAS_WORD_PROJECTION = { UserDictionary.Words.WORD }; 177 private static final String HAS_WORD_SELECTION_ONE_LOCALE = UserDictionary.Words.WORD 178 + "=? AND " + UserDictionary.Words.LOCALE + "=?"; 179 private static final String HAS_WORD_SELECTION_ALL_LOCALES = UserDictionary.Words.WORD 180 + "=? AND " + UserDictionary.Words.LOCALE + " is null"; 181 private boolean hasWord(final String word, final Context context) { 182 final Cursor cursor; 183 // mLocale == "" indicates this is an entry for all languages. Here, mLocale can't 184 // be null at all (it's ensured by the updateLocale method). 185 if ("".equals(mLocale)) { 186 cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI, 187 HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ALL_LOCALES, 188 new String[] { word }, null /* sort order */); 189 } else { 190 cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI, 191 HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ONE_LOCALE, 192 new String[] { word, mLocale }, null /* sort order */); 193 } 194 try { 195 if (null == cursor) return false; 196 return cursor.getCount() > 0; 197 } finally { 198 if (null != cursor) cursor.close(); 199 } 200 } 201 202 public static class LocaleRenderer { 203 private final String mLocaleString; 204 private final String mDescription; 205 // LocaleString may NOT be null. 206 public LocaleRenderer(final Context context, final String localeString) { 207 mLocaleString = localeString; 208 if (null == localeString) { 209 mDescription = context.getString(R.string.user_dict_settings_more_languages); 210 } else if ("".equals(localeString)) { 211 mDescription = context.getString(R.string.user_dict_settings_all_languages); 212 } else { 213 mDescription = LocaleUtils.constructLocaleFromString(localeString).getDisplayName(); 214 } 215 } 216 @Override 217 public String toString() { 218 return mDescription; 219 } 220 public String getLocaleString() { 221 return mLocaleString; 222 } 223 // "More languages..." is null ; "All languages" is the empty string. 224 public boolean isMoreLanguages() { 225 return null == mLocaleString; 226 } 227 } 228 229 private static void addLocaleDisplayNameToList(final Context context, 230 final ArrayList<LocaleRenderer> list, final String locale) { 231 if (null != locale) { 232 list.add(new LocaleRenderer(context, locale)); 233 } 234 } 235 236 // Helper method to get the list of locales to display for this word 237 public ArrayList<LocaleRenderer> getLocalesList(final Activity activity) { 238 final TreeSet<String> locales = UserDictionaryList.getUserDictionaryLocalesSet(activity); 239 // Remove our locale if it's in, because we're always gonna put it at the top 240 locales.remove(mLocale); // mLocale may not be null 241 final String systemLocale = Locale.getDefault().toString(); 242 // The system locale should be inside. We want it at the 2nd spot. 243 locales.remove(systemLocale); // system locale may not be null 244 locales.remove(""); // Remove the empty string if it's there 245 final ArrayList<LocaleRenderer> localesList = new ArrayList<LocaleRenderer>(); 246 // Add the passed locale, then the system locale at the top of the list. Add an 247 // "all languages" entry at the bottom of the list. 248 addLocaleDisplayNameToList(activity, localesList, mLocale); 249 if (!systemLocale.equals(mLocale)) { 250 addLocaleDisplayNameToList(activity, localesList, systemLocale); 251 } 252 for (final String l : locales) { 253 // TODO: sort in unicode order 254 addLocaleDisplayNameToList(activity, localesList, l); 255 } 256 if (!"".equals(mLocale)) { 257 // If mLocale is "", then we already inserted the "all languages" item, so don't do it 258 addLocaleDisplayNameToList(activity, localesList, ""); // meaning: all languages 259 } 260 localesList.add(new LocaleRenderer(activity, null)); // meaning: select another locale 261 return localesList; 262 } 263} 264