aboutsummaryrefslogtreecommitdiffstats
path: root/java/src
diff options
context:
space:
mode:
Diffstat (limited to 'java/src')
-rw-r--r--java/src/com/android/inputmethod/keyboard/Keyboard.java1
-rw-r--r--java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java72
-rw-r--r--java/src/com/android/inputmethod/latin/LatinIME.java58
-rw-r--r--java/src/com/android/inputmethod/latin/UserBigramDictionary.java62
-rw-r--r--java/src/com/android/inputmethod/latin/UserUnigramDictionary.java276
-rw-r--r--java/src/com/android/inputmethod/latin/WordComposer.java16
6 files changed, 90 insertions, 395 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java
index cdf07ed70..973f64b4d 100644
--- a/java/src/com/android/inputmethod/keyboard/Keyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java
@@ -383,6 +383,7 @@ public class Keyboard {
case CODE_ACTION_ENTER: return "actionEnter";
case CODE_ACTION_NEXT: return "actionNext";
case CODE_ACTION_PREVIOUS: return "actionPrevious";
+ case CODE_LANGUAGE_SWITCH: return "languageSwitch";
case CODE_UNSPECIFIED: return "unspec";
case CODE_TAB: return "tab";
case CODE_ENTER: return "enter";
diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
index 97f4d07d9..343842552 100644
--- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
@@ -84,7 +84,7 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
private static final int ALPHA_OPAQUE = 255;
private boolean mNeedsToDisplayLanguage;
private Locale mSpacebarLocale;
- private int mSpacebarTextAlpha;
+ private int mSpacebarTextAlpha = ALPHA_OPAQUE;
private final float mSpacebarTextRatio;
private float mSpacebarTextSize;
private final int mSpacebarTextColor;
@@ -101,7 +101,7 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
// Stuff to draw altCodeWhileTyping keys.
private ValueAnimator mAltCodeKeyWhileTypingFadeoutAnimator;
private ValueAnimator mAltCodeKeyWhileTypingFadeinAnimator;
- private int mAltCodeKeyWhileTypingAnimAlpha;
+ private int mAltCodeKeyWhileTypingAnimAlpha = ALPHA_OPAQUE;
// More keys keyboard
private PopupWindow mMoreKeysWindow;
@@ -154,16 +154,8 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
}
break;
case MSG_TYPING_STATE_EXPIRED:
- final ValueAnimator fadeout = keyboardView.mAltCodeKeyWhileTypingFadeoutAnimator;
- if (fadeout != null && fadeout.isStarted()) {
- fadeout.cancel();
- }
- // TODO: Start the fade in animation with an initial value that is the same as the
- // final value when the above fade out animation gets cancelled.
- final ValueAnimator fadein = keyboardView.mAltCodeKeyWhileTypingFadeinAnimator;
- if (fadein != null && !fadein.isStarted()) {
- fadein.start();
- }
+ cancelAndStartAnimators(keyboardView.mAltCodeKeyWhileTypingFadeoutAnimator,
+ keyboardView.mAltCodeKeyWhileTypingFadeinAnimator);
break;
}
}
@@ -238,26 +230,34 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
removeMessages(MSG_LONGPRESS_KEY);
}
+ private static void cancelAndStartAnimators(ValueAnimator animatorToCancel,
+ ValueAnimator animatorToStart) {
+ if (animatorToCancel != null && animatorToCancel.isStarted()) {
+ animatorToCancel.cancel();
+ }
+ // TODO: Start the animation with an initial value that is the same as the final value
+ // of the above animation when it gets cancelled.
+ if (animatorToStart != null && !animatorToStart.isStarted()) {
+ animatorToStart.start();
+ }
+ }
+
+ private void cancelTypingStateTimer() {
+ removeMessages(MSG_TYPING_STATE_EXPIRED);
+ }
+
@Override
public void startTypingStateTimer() {
final boolean isTyping = isTypingState();
- removeMessages(MSG_TYPING_STATE_EXPIRED);
+ cancelTypingStateTimer();
sendMessageDelayed(
obtainMessage(MSG_TYPING_STATE_EXPIRED), mParams.mIgnoreAltCodeKeyTimeout);
- final LatinKeyboardView keyboardView = getOuterInstance();
if (isTyping) {
return;
}
- final ValueAnimator fadein = keyboardView.mAltCodeKeyWhileTypingFadeinAnimator;
- if (fadein != null && fadein.isStarted()) {
- fadein.cancel();
- }
- // TODO: Start the fade out animation with an initial value that is the same as the
- // final value when the above fade in animation gets cancelled.
- final ValueAnimator fadeout = keyboardView.mAltCodeKeyWhileTypingFadeoutAnimator;
- if (fadeout != null && !fadeout.isStarted()) {
- fadeout.start();
- }
+ final LatinKeyboardView keyboardView = getOuterInstance();
+ cancelAndStartAnimators(keyboardView.mAltCodeKeyWhileTypingFadeinAnimator,
+ keyboardView.mAltCodeKeyWhileTypingFadeoutAnimator);
}
@Override
@@ -289,6 +289,7 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
public void cancelAllMessages() {
cancelKeyTimers();
+ cancelTypingStateTimer();
}
}
@@ -323,15 +324,6 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
public final int mLongPressSpaceKeyTimeout;
public final int mIgnoreAltCodeKeyTimeout;
- KeyTimerParams() {
- mKeyRepeatStartTimeout = 0;
- mKeyRepeatInterval = 0;
- mLongPressKeyTimeout = 0;
- mLongPressShiftKeyTimeout = 0;
- mLongPressSpaceKeyTimeout = 0;
- mIgnoreAltCodeKeyTimeout = 0;
- }
-
public KeyTimerParams(TypedArray latinKeyboardViewAttr) {
mKeyRepeatStartTimeout = latinKeyboardViewAttr.getInt(
R.styleable.LatinKeyboardView_keyRepeatStartTimeout, 0);
@@ -424,12 +416,6 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
updateAltCodeKeyWhileTyping();
}
});
- fadeout.addListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator a) {
- final ValueAnimator valueAnimator = (ValueAnimator)a;
- }
- });
}
mAltCodeKeyWhileTypingFadeoutAnimator = fadeout;
@@ -442,12 +428,6 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
updateAltCodeKeyWhileTyping();
}
});
- fadein.addListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator a) {
- final ValueAnimator valueAnimator = (ValueAnimator)a;
- }
- });
}
mAltCodeKeyWhileTypingFadeinAnimator = fadein;
}
@@ -510,8 +490,6 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
mSpacebarTextSize = keyHeight * mSpacebarTextRatio;
mSpacebarLocale = keyboard.mId.mLocale;
- mSpacebarTextAlpha = ALPHA_OPAQUE;
- mAltCodeKeyWhileTypingAnimAlpha = ALPHA_OPAQUE;
}
/**
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index fa5a46fca..234a501de 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -204,7 +204,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
private UserDictionary mUserDictionary;
private UserBigramDictionary mUserBigramDictionary;
- private UserUnigramDictionary mUserUnigramDictionary;
private boolean mIsUserDictionaryAvailable;
private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
@@ -528,12 +527,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
resetContactsDictionary(oldContactsDictionary);
- mUserUnigramDictionary
- = new UserUnigramDictionary(this, this, localeStr, Suggest.DIC_USER_UNIGRAM);
- mSuggest.setUserUnigramDictionary(mUserUnigramDictionary);
-
+ // TODO: rename UserBigramDictionary into UserHistoryDictionary
mUserBigramDictionary
= new UserBigramDictionary(this, this, localeStr, Suggest.DIC_USER_BIGRAM);
+ mSuggest.setUserUnigramDictionary(mUserBigramDictionary);
mSuggest.setUserBigramDictionary(mUserBigramDictionary);
LocaleUtils.setSystemLocale(res, savedLocale);
@@ -776,7 +773,6 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
KeyboardView inputView = mKeyboardSwitcher.getKeyboardView();
if (inputView != null) inputView.closing();
- if (mUserUnigramDictionary != null) mUserUnigramDictionary.flushPendingWrites();
if (mUserBigramDictionary != null) mUserBigramDictionary.flushPendingWrites();
}
@@ -1114,8 +1110,7 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
if (ic != null) {
ic.commitText(typedWord, 1);
}
- addToUserUnigramAndBigramDictionaries(typedWord,
- UserUnigramDictionary.FREQUENCY_FOR_TYPED);
+ addToUserHistoryDictionary(typedWord);
}
updateSuggestions();
}
@@ -1834,9 +1829,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mExpectingUpdateSelection = true;
commitChosenWord(autoCorrection, LastComposedWord.COMMIT_TYPE_DECIDED_WORD,
separatorCodePoint);
- // Add the word to the user unigram dictionary if it's not a known word
- addToUserUnigramAndBigramDictionaries(autoCorrection,
- UserUnigramDictionary.FREQUENCY_FOR_TYPED);
+ // Add the word to the user history dictionary
+ addToUserHistoryDictionary(autoCorrection);
if (!typedWord.equals(autoCorrection) && null != ic) {
// This will make the correction flash for a short while as a visual clue
// to the user that auto-correction happened.
@@ -1895,13 +1889,8 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
mExpectingUpdateSelection = true;
commitChosenWord(suggestion, LastComposedWord.COMMIT_TYPE_MANUAL_PICK,
LastComposedWord.NOT_A_SEPARATOR);
- // Add the word to the auto dictionary if it's not a known word
- if (index == 0) {
- addToUserUnigramAndBigramDictionaries(suggestion,
- UserUnigramDictionary.FREQUENCY_FOR_PICKED);
- } else {
- addToOnlyBigramDictionary(suggestion, 1);
- }
+ // Add the word to the user history dictionary
+ addToUserHistoryDictionary(suggestion);
mSpaceState = SPACE_STATE_PHANTOM;
// TODO: is this necessary?
mKeyboardSwitcher.updateShiftState();
@@ -2002,21 +1991,10 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
setSuggestionStripShown(isSuggestionsStripVisible());
}
- private void addToUserUnigramAndBigramDictionaries(CharSequence suggestion,
- int frequencyDelta) {
- checkAddToDictionary(suggestion, frequencyDelta, false);
- }
-
- private void addToOnlyBigramDictionary(CharSequence suggestion, int frequencyDelta) {
- checkAddToDictionary(suggestion, frequencyDelta, true);
- }
-
/**
* Adds to the UserBigramDictionary and/or UserUnigramDictionary
- * @param selectedANotTypedWord true if it should be added to bigram dictionary if possible
*/
- private void checkAddToDictionary(CharSequence suggestion, int frequencyDelta,
- boolean selectedANotTypedWord) {
+ private void addToUserHistoryDictionary(final CharSequence suggestion) {
if (suggestion == null || suggestion.length() < 1) return;
// Only auto-add to dictionary if auto-correct is ON. Otherwise we'll be
@@ -2027,28 +2005,14 @@ public class LatinIME extends InputMethodServiceCompatWrapper implements Keyboar
return;
}
- if (null != mSuggest && null != mUserUnigramDictionary) {
- final boolean selectedATypedWordAndItsInUserUnigramDic =
- !selectedANotTypedWord && mUserUnigramDictionary.isValidWord(suggestion);
- final boolean isValidWord = AutoCorrection.isValidWord(
- mSuggest.getUnigramDictionaries(), suggestion, true);
- final boolean needsToAddToUserUnigramDictionary =
- selectedATypedWordAndItsInUserUnigramDic || !isValidWord;
- if (needsToAddToUserUnigramDictionary) {
- mUserUnigramDictionary.addWord(suggestion.toString(), frequencyDelta);
- }
- }
-
if (mUserBigramDictionary != null) {
- // We don't want to register as bigrams words separated by a separator.
- // For example "I will, and you too" : we don't want the pair ("will" "and") to be
- // a bigram.
+ mUserBigramDictionary.addUnigram(suggestion.toString());
final InputConnection ic = getCurrentInputConnection();
if (null != ic) {
final CharSequence prevWord =
EditingUtils.getPreviousWord(ic, mSettingsValues.mWordSeparators);
- if (!TextUtils.isEmpty(prevWord)) {
- mUserBigramDictionary.addBigrams(prevWord.toString(), suggestion.toString());
+ if (null != prevWord) {
+ mUserBigramDictionary.addBigramPair(prevWord.toString(), suggestion.toString());
}
}
}
diff --git a/java/src/com/android/inputmethod/latin/UserBigramDictionary.java b/java/src/com/android/inputmethod/latin/UserBigramDictionary.java
index e6a59d0ab..42d3a70cd 100644
--- a/java/src/com/android/inputmethod/latin/UserBigramDictionary.java
+++ b/java/src/com/android/inputmethod/latin/UserBigramDictionary.java
@@ -24,6 +24,7 @@ import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.os.AsyncTask;
import android.provider.BaseColumns;
+import android.text.TextUtils;
import android.util.Log;
import java.util.HashMap;
@@ -155,19 +156,49 @@ public class UserBigramDictionary extends ExpandableDictionary {
}
/**
- * Pair will be added to the userbigram database.
+ * Return whether the passed charsequence is in the dictionary.
*/
- public int addBigrams(String word1, String word2) {
+ @Override
+ public boolean isValidWord(final CharSequence word) {
+ // TODO: figure out what is the correct thing to do here.
+ return false;
+ }
+
+ /**
+ * Add a single word without context.
+ *
+ * This is a temporary method to match the interface to UserUnigramDictionary. In the end
+ * this should be merged with addBigramPair.
+ */
+ public void addUnigram(final String newWord) {
+ addBigramPair(null, newWord);
+ }
+
+ /**
+ * Pair will be added to the user history dictionary.
+ *
+ * The first word may be null. That means we don't know the context, in other words,
+ * it's only a unigram. The first word may also be an empty string : this means start
+ * context, as in beginning of a sentence for example.
+ * The second word may not be null (a NullPointerException would be thrown).
+ */
+ public int addBigramPair(final String word1, String word2) {
// remove caps if second word is autocapitalized
if (mIme != null && mIme.isAutoCapitalized()) {
word2 = Character.toLowerCase(word2.charAt(0)) + word2.substring(1);
}
// Do not insert a word as a bigram of itself
- if (word1.equals(word2)) {
+ if (word2.equals(word1)) {
return 0;
}
- int freq = super.addBigram(word1, word2, FREQUENCY_FOR_TYPED);
+ int freq;
+ if (null == word1) {
+ freq = FREQUENCY_FOR_TYPED;
+ super.addWord(word2, FREQUENCY_FOR_TYPED);
+ } else {
+ freq = super.addBigram(word1, word2, FREQUENCY_FOR_TYPED);
+ }
if (freq > FREQUENCY_MAX) freq = FREQUENCY_MAX;
synchronized (mPendingWritesLock) {
if (freq == FREQUENCY_FOR_TYPED || mPendingWrites.isEmpty()) {
@@ -225,7 +256,10 @@ public class UserBigramDictionary extends ExpandableDictionary {
int frequency = cursor.getInt(frequencyIndex);
// Safeguard against adding really long words. Stack may overflow due
// to recursive lookup
- if (word1.length() < MAX_WORD_LENGTH && word2.length() < MAX_WORD_LENGTH) {
+ if (null == word1) {
+ super.addWord(word2, frequency);
+ } else if (word1.length() < MAX_WORD_LENGTH
+ && word2.length() < MAX_WORD_LENGTH) {
super.setBigram(word1, word2, frequency);
}
cursor.moveToNext();
@@ -367,13 +401,23 @@ public class UserBigramDictionary extends ExpandableDictionary {
// Write all the entries to the db
Iterator<Bigram> iterator = mMap.iterator();
while (iterator.hasNext()) {
+ // TODO: this process of making a text search for each pair each time
+ // is terribly inefficient. Optimize this.
Bigram bi = iterator.next();
// find pair id
- Cursor c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID },
- MAIN_COLUMN_WORD1 + "=? AND " + MAIN_COLUMN_WORD2 + "=? AND "
- + MAIN_COLUMN_LOCALE + "=?",
- new String[] { bi.mWord1, bi.mWord2, mLocale }, null, null, null);
+ final Cursor c;
+ if (null != bi.mWord1) {
+ c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID },
+ MAIN_COLUMN_WORD1 + "=? AND " + MAIN_COLUMN_WORD2 + "=? AND "
+ + MAIN_COLUMN_LOCALE + "=?",
+ new String[] { bi.mWord1, bi.mWord2, mLocale }, null, null, null);
+ } else {
+ c = db.query(MAIN_TABLE_NAME, new String[] { MAIN_COLUMN_ID },
+ MAIN_COLUMN_WORD1 + " IS NULL AND " + MAIN_COLUMN_WORD2 + "=? AND "
+ + MAIN_COLUMN_LOCALE + "=?",
+ new String[] { bi.mWord2, mLocale }, null, null, null);
+ }
int pairId;
if (c.moveToFirst()) {
diff --git a/java/src/com/android/inputmethod/latin/UserUnigramDictionary.java b/java/src/com/android/inputmethod/latin/UserUnigramDictionary.java
deleted file mode 100644
index 869865d7b..000000000
--- a/java/src/com/android/inputmethod/latin/UserUnigramDictionary.java
+++ /dev/null
@@ -1,276 +0,0 @@
-/*
- * Copyright (C) 2010 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.latin;
-
-import android.content.ContentValues;
-import android.content.Context;
-import android.database.Cursor;
-import android.database.sqlite.SQLiteDatabase;
-import android.database.sqlite.SQLiteOpenHelper;
-import android.database.sqlite.SQLiteQueryBuilder;
-import android.os.AsyncTask;
-import android.provider.BaseColumns;
-import android.util.Log;
-
-import java.util.HashMap;
-import java.util.Map.Entry;
-import java.util.Set;
-
-/**
- * This class (inherited from the old AutoDictionary) is used for user history
- * based dictionary. It stores words that the user typed to supply a provision
- * for suggesting and re-ordering of candidates.
- */
-public class UserUnigramDictionary extends ExpandableDictionary {
- static final boolean ENABLE_USER_UNIGRAM_DICTIONARY = false;
-
- // Weight added to a user picking a new word from the suggestion strip
- static final int FREQUENCY_FOR_PICKED = 3;
- // Weight added to a user typing a new word that doesn't get corrected (or is reverted)
- static final int FREQUENCY_FOR_TYPED = 1;
- // If the user touches a typed word 2 times or more, it will become valid.
- private static final int VALIDITY_THRESHOLD = 2 * FREQUENCY_FOR_PICKED;
-
- private LatinIME mIme;
- // Locale for which this user unigram dictionary is storing words
- private String mLocale;
-
- private HashMap<String,Integer> mPendingWrites = new HashMap<String,Integer>();
- private final Object mPendingWritesLock = new Object();
-
- // TODO: we should probably change the database name
- private static final String DATABASE_NAME = "auto_dict.db";
- private static final int DATABASE_VERSION = 1;
-
- // These are the columns in the dictionary
- // TODO: Consume less space by using a unique id for locale instead of the whole
- // 2-5 character string.
- private static final String COLUMN_ID = BaseColumns._ID;
- private static final String COLUMN_WORD = "word";
- private static final String COLUMN_FREQUENCY = "freq";
- private static final String COLUMN_LOCALE = "locale";
-
- /** Sort by descending order of frequency. */
- public static final String DEFAULT_SORT_ORDER = COLUMN_FREQUENCY + " DESC";
-
- /** Name of the words table in the database */
- private static final String USER_UNIGRAM_DICT_TABLE_NAME = "words";
-
- private static HashMap<String, String> sDictProjectionMap;
-
- static {
- if (ENABLE_USER_UNIGRAM_DICTIONARY) {
- sDictProjectionMap = new HashMap<String, String>();
- sDictProjectionMap.put(COLUMN_ID, COLUMN_ID);
- sDictProjectionMap.put(COLUMN_WORD, COLUMN_WORD);
- sDictProjectionMap.put(COLUMN_FREQUENCY, COLUMN_FREQUENCY);
- sDictProjectionMap.put(COLUMN_LOCALE, COLUMN_LOCALE);
- }
- }
-
- private static DatabaseHelper sOpenHelper = null;
-
- public UserUnigramDictionary(Context context, LatinIME ime, String locale, int dicTypeId) {
- super(context, dicTypeId);
- // Super must be first statement of the constructor... I'd like not to do it if the
- // user unigram dictionary is not enabled, but Java won't let me.
- if (!ENABLE_USER_UNIGRAM_DICTIONARY) return;
- mIme = ime;
- mLocale = locale;
- if (sOpenHelper == null) {
- sOpenHelper = new DatabaseHelper(getContext());
- }
- if (mLocale != null && mLocale.length() > 1) {
- loadDictionary();
- }
- }
-
- @Override
- public synchronized boolean isValidWord(CharSequence word) {
- if (!ENABLE_USER_UNIGRAM_DICTIONARY) return false;
- final int frequency = getWordFrequency(word);
- return frequency >= VALIDITY_THRESHOLD;
- }
-
- @Override
- public void close() {
- super.close();
- if (!ENABLE_USER_UNIGRAM_DICTIONARY) return;
- flushPendingWrites();
- // Don't close the database as locale changes will require it to be reopened anyway
- // Also, the database is written to somewhat frequently, so it needs to be kept alive
- // throughout the life of the process.
- // mOpenHelper.close();
- }
-
- @Override
- public void loadDictionaryAsync() {
- if (!ENABLE_USER_UNIGRAM_DICTIONARY) return;
- // Load the words that correspond to the current input locale
- final Cursor cursor = query(COLUMN_LOCALE + "=?", new String[] { mLocale });
- if (null == cursor) return;
- try {
- if (cursor.moveToFirst()) {
- int wordIndex = cursor.getColumnIndex(COLUMN_WORD);
- int frequencyIndex = cursor.getColumnIndex(COLUMN_FREQUENCY);
- while (!cursor.isAfterLast()) {
- String word = cursor.getString(wordIndex);
- int frequency = cursor.getInt(frequencyIndex);
- // Safeguard against adding really long words. Stack may overflow due
- // to recursive lookup
- if (word.length() < getMaxWordLength()) {
- super.addWord(word, frequency);
- }
- cursor.moveToNext();
- }
- }
- } finally {
- cursor.close();
- }
- }
-
- @Override
- public void addWord(String newWord, int addFrequency) {
- if (!ENABLE_USER_UNIGRAM_DICTIONARY) return;
- String word = newWord;
- final int length = word.length();
- // Don't add very short or very long words.
- if (length < 2 || length > getMaxWordLength()) return;
- if (mIme.isAutoCapitalized()) {
- // Remove caps before adding
- word = Character.toLowerCase(word.charAt(0)) + word.substring(1);
- }
- int freq = getWordFrequency(word);
- freq = freq < 0 ? addFrequency : freq + addFrequency;
- super.addWord(word, freq);
-
- synchronized (mPendingWritesLock) {
- // Write a null frequency if it is to be deleted from the db
- mPendingWrites.put(word, freq == 0 ? null : new Integer(freq));
- }
- }
-
- /**
- * Schedules a background thread to write any pending words to the database.
- */
- public void flushPendingWrites() {
- if (!ENABLE_USER_UNIGRAM_DICTIONARY) return;
- synchronized (mPendingWritesLock) {
- // Nothing pending? Return
- if (mPendingWrites.isEmpty()) return;
- // Create a background thread to write the pending entries
- new UpdateDbTask(sOpenHelper, mPendingWrites, mLocale).execute();
- // Create a new map for writing new entries into while the old one is written to db
- mPendingWrites = new HashMap<String, Integer>();
- }
- }
-
- /**
- * This class helps open, create, and upgrade the database file.
- */
- private static class DatabaseHelper extends SQLiteOpenHelper {
-
- DatabaseHelper(Context context) {
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE " + USER_UNIGRAM_DICT_TABLE_NAME + " ("
- + COLUMN_ID + " INTEGER PRIMARY KEY,"
- + COLUMN_WORD + " TEXT,"
- + COLUMN_FREQUENCY + " INTEGER,"
- + COLUMN_LOCALE + " TEXT"
- + ");");
- }
-
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.w("UserUnigramDictionary", "Upgrading database from version " + oldVersion + " to "
- + newVersion + ", which will destroy all old data");
- db.execSQL("DROP TABLE IF EXISTS " + USER_UNIGRAM_DICT_TABLE_NAME);
- onCreate(db);
- }
- }
-
- private static Cursor query(String selection, String[] selectionArgs) {
- SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
- qb.setTables(USER_UNIGRAM_DICT_TABLE_NAME);
- qb.setProjectionMap(sDictProjectionMap);
-
- // Get the database and run the query
- try {
- SQLiteDatabase db = sOpenHelper.getReadableDatabase();
- Cursor c = qb.query(db, null, selection, selectionArgs, null, null,
- DEFAULT_SORT_ORDER);
- return c;
- } catch (android.database.sqlite.SQLiteCantOpenDatabaseException e) {
- // Can't open the database : presumably we can't access storage. That may happen
- // when the device is wedged; do a best effort to still start the keyboard.
- return null;
- }
- }
-
- /**
- * Async task to write pending words to the database so that it stays in sync with
- * the in-memory trie.
- */
- private static class UpdateDbTask extends AsyncTask<Void, Void, Void> {
- private final HashMap<String, Integer> mMap;
- private final DatabaseHelper mDbHelper;
- private final String mLocale;
-
- public UpdateDbTask(DatabaseHelper openHelper, HashMap<String, Integer> pendingWrites,
- String locale) {
- mMap = pendingWrites;
- mLocale = locale;
- mDbHelper = openHelper;
- }
-
- @Override
- protected Void doInBackground(Void... v) {
- SQLiteDatabase db = null;
- try {
- db = mDbHelper.getWritableDatabase();
- } catch (android.database.sqlite.SQLiteCantOpenDatabaseException e) {
- // With no access to the DB, this is moot. Do nothing: we'll exit through the
- // test for null == db.
- }
- if (null == db) return null;
- // Write all the entries to the db
- Set<Entry<String,Integer>> mEntries = mMap.entrySet();
- for (Entry<String,Integer> entry : mEntries) {
- Integer freq = entry.getValue();
- db.delete(USER_UNIGRAM_DICT_TABLE_NAME, COLUMN_WORD + "=? AND " + COLUMN_LOCALE
- + "=?", new String[] { entry.getKey(), mLocale });
- if (freq != null) {
- db.insert(USER_UNIGRAM_DICT_TABLE_NAME, null,
- getContentValues(entry.getKey(), freq, mLocale));
- }
- }
- return null;
- }
-
- private static ContentValues getContentValues(String word, int frequency, String locale) {
- ContentValues values = new ContentValues(4);
- values.put(COLUMN_WORD, word);
- values.put(COLUMN_FREQUENCY, frequency);
- values.put(COLUMN_LOCALE, locale);
- return values;
- }
- }
-}
diff --git a/java/src/com/android/inputmethod/latin/WordComposer.java b/java/src/com/android/inputmethod/latin/WordComposer.java
index 126ac47e7..a9609310c 100644
--- a/java/src/com/android/inputmethod/latin/WordComposer.java
+++ b/java/src/com/android/inputmethod/latin/WordComposer.java
@@ -177,7 +177,6 @@ public class WordComposer {
private void add(int primaryCode, int[] codes, int keyX, int keyY) {
final int newIndex = mCodes.size();
mTypedWord.appendCodePoint(primaryCode);
- correctPrimaryJuxtapos(primaryCode, codes);
mCodes.add(codes);
if (newIndex < BinaryDictionary.MAX_WORD_LENGTH) {
mXCoordinates[newIndex] = keyX;
@@ -239,21 +238,6 @@ public class WordComposer {
}
/**
- * Swaps the first and second values in the codes array if the primary code is not the first
- * value in the array but the second. This happens when the preferred key is not the key that
- * the user released the finger on.
- * @param primaryCode the preferred character
- * @param codes array of codes based on distance from touch point
- */
- private static void correctPrimaryJuxtapos(int primaryCode, int[] codes) {
- if (codes.length < 2) return;
- if (codes[0] > 0 && codes[1] > 0 && codes[0] != primaryCode && codes[1] == primaryCode) {
- codes[1] = codes[0];
- codes[0] = primaryCode;
- }
- }
-
- /**
* Delete the last keystroke as a result of hitting backspace.
*/
public void deleteLast() {