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