diff --git a/java/src/com/android/inputmethod/compat/UserDictionaryCompatUtils.java b/java/src/com/android/inputmethod/compat/UserDictionaryCompatUtils.java new file mode 100644 index 000000000..ff6561c58 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/UserDictionaryCompatUtils.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.inputmethod.compat; + +import android.content.Context; +import android.provider.UserDictionary.Words; + +import java.lang.reflect.Method; +import java.util.Locale; + +public final class UserDictionaryCompatUtils { + // UserDictionary.Words#addWord(Context, String, int, String, Locale) was introduced + // in API level 16 (Build.VERSION_CODES.JELLY_BEAN). + private static final Method METHOD_addWord = CompatUtils.getMethod(Words.class, "addWord", + Context.class, String.class, Integer.TYPE, String.class, Locale.class); + + public static void addWord(final Context context, final String word, final int freq, + final String shortcut, final Locale locale) { + if (hasNewerAddWord()) { + CompatUtils.invoke(Words.class, null, METHOD_addWord, context, word, freq, shortcut, + locale); + } else { + // Fall back to the pre-JellyBean method. + final int localeType; + if (null == locale) { + localeType = Words.LOCALE_TYPE_ALL; + } else { + localeType = Words.LOCALE_TYPE_CURRENT; + } + Words.addWord(context, word, freq, localeType); + } + } + + private static final boolean hasNewerAddWord() { + return null != METHOD_addWord; + } +} diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 0f1f14957..f08e10763 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -1243,10 +1243,6 @@ public final class LatinIME extends InputMethodService implements KeyboardAction } else { wordToEdit = word; } - mPositionalInfoForUserDictPendingAddition = - new PositionalInfoForUserDictPendingAddition( - wordToEdit, mLastSelectionEnd, getCurrentInputEditorInfo(), - mLastComposedWord.mCapitalizedMode); mUserDictionary.addWordToUserDictionary(wordToEdit); } diff --git a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java index 0d5bde623..8d2e7623f 100644 --- a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java @@ -20,7 +20,6 @@ import android.content.ContentProviderClient; import android.content.ContentResolver; import android.content.ContentUris; import android.content.Context; -import android.content.Intent; import android.database.ContentObserver; import android.database.Cursor; import android.net.Uri; @@ -28,7 +27,10 @@ import android.os.Build; import android.provider.UserDictionary.Words; import android.text.TextUtils; +import com.android.inputmethod.compat.UserDictionaryCompatUtils; + import java.util.Arrays; +import java.util.Locale; /** * An expandable dictionary that stores the words in the user dictionary provider into a binary @@ -61,10 +63,6 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { private static final String NAME = "userunigram"; - // This is not exported by the framework so we pretty much have to write it here verbatim - private static final String ACTION_USER_DICTIONARY_INSERT = - "com.android.settings.USER_DICTIONARY_INSERT"; - private ContentObserver mObserver; final private String mLocale; final private boolean mAlsoUseMoreRestrictiveLocales; @@ -211,23 +209,19 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { /** * Adds a word to the user dictionary and makes it persistent. * - * This will call upon the system interface to do the actual work through the intent readied by - * the system to this effect. - * * @param word the word to add. If the word is capitalized, then the dictionary will * recognize it as a capitalized word when searched. */ public synchronized void addWordToUserDictionary(final String word) { - // TODO: do something for the UI. With the following, any sufficiently long word will - // look like it will go to the user dictionary but it won't. - // Safeguard against adding long words. Can cause stack overflow. - if (word.length() >= MAX_WORD_LENGTH) return; - - Intent intent = new Intent(ACTION_USER_DICTIONARY_INSERT); - intent.putExtra(Words.WORD, word); - intent.putExtra(Words.LOCALE, mLocale); - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - mContext.startActivity(intent); + // Update the user dictionary provider + final Locale locale; + if (USER_DICTIONARY_ALL_LANGUAGES == mLocale) { + locale = null; + } else { + locale = LocaleUtils.constructLocaleFromString(mLocale); + } + UserDictionaryCompatUtils.addWord(mContext, word, + HISTORICAL_DEFAULT_USER_DICTIONARY_FREQUENCY, null, locale); } private int scaleFrequencyFromDefaultToLatinIme(final int defaultFrequency) {