diff options
Diffstat (limited to 'java/src')
17 files changed, 380 insertions, 937 deletions
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index 51a0f537f..18e01fb49 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -873,6 +873,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy { keyPreview, ViewLayoutUtils.newLayoutParam(mPreviewPlacer, 0, 0)); } + @SuppressWarnings("deprecation") // setBackgroundDrawable is replaced by setBackground in API16 @Override public void showKeyPreview(PointerTracker tracker) { if (!mShowKeyPreviewPopup) return; diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java index 5aa9a0887..4ab6832c3 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java @@ -21,8 +21,6 @@ import android.util.Log; import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.latin.Constants; -import com.android.inputmethod.latin.ResearchLogger; -import com.android.inputmethod.latin.define.ProductionFlag; /** * Keyboard state machine. diff --git a/java/src/com/android/inputmethod/latin/ContactsDictionary.java b/java/src/com/android/inputmethod/latin/ContactsDictionary.java deleted file mode 100644 index cbfbd0ec8..000000000 --- a/java/src/com/android/inputmethod/latin/ContactsDictionary.java +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (C) 2009 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.ContentResolver; -import android.content.Context; -import android.database.ContentObserver; -import android.database.Cursor; -import android.os.SystemClock; -import android.provider.BaseColumns; -import android.provider.ContactsContract.Contacts; -import android.text.TextUtils; -import android.util.Log; - -import com.android.inputmethod.keyboard.Keyboard; - -// TODO: This class is superseded by {@link ContactsBinaryDictionary}. Should be cleaned up. -/** - * An expandable dictionary that stores the words from Contacts provider. - * - * @deprecated Use {@link ContactsBinaryDictionary}. - */ -@Deprecated -public class ContactsDictionary extends ExpandableDictionary { - - private static final String[] PROJECTION = { - BaseColumns._ID, - Contacts.DISPLAY_NAME, - }; - - private static final String TAG = "ContactsDictionary"; - - /** - * Frequency for contacts information into the dictionary - */ - private static final int FREQUENCY_FOR_CONTACTS = 40; - private static final int FREQUENCY_FOR_CONTACTS_BIGRAM = 90; - - private static final int INDEX_NAME = 1; - - private ContentObserver mObserver; - - private long mLastLoadedContacts; - - public ContactsDictionary(final Context context, final int dicTypeId) { - super(context, dicTypeId); - registerObserver(context); - loadDictionary(); - } - - private synchronized void registerObserver(final Context context) { - // Perform a managed query. The Activity will handle closing and requerying the cursor - // when needed. - if (mObserver != null) return; - ContentResolver cres = context.getContentResolver(); - cres.registerContentObserver( - Contacts.CONTENT_URI, true, mObserver = new ContentObserver(null) { - @Override - public void onChange(boolean self) { - setRequiresReload(true); - } - }); - } - - public void reopen(final Context context) { - registerObserver(context); - } - - @Override - public synchronized void close() { - if (mObserver != null) { - getContext().getContentResolver().unregisterContentObserver(mObserver); - mObserver = null; - } - super.close(); - } - - @Override - public void startDictionaryLoadingTaskLocked() { - long now = SystemClock.uptimeMillis(); - if (mLastLoadedContacts == 0 - || now - mLastLoadedContacts > 30 * 60 * 1000 /* 30 minutes */) { - super.startDictionaryLoadingTaskLocked(); - } - } - - @Override - public void loadDictionaryAsync() { - try { - Cursor cursor = getContext().getContentResolver() - .query(Contacts.CONTENT_URI, PROJECTION, null, null, null); - if (cursor != null) { - addWords(cursor); - } - } catch(IllegalStateException e) { - Log.e(TAG, "Contacts DB is having problems"); - } - mLastLoadedContacts = SystemClock.uptimeMillis(); - } - - @Override - public void getBigrams(final WordComposer codes, final CharSequence previousWord, - final WordCallback callback) { - // Do not return bigrams from Contacts when nothing was typed. - if (codes.size() <= 0) return; - super.getBigrams(codes, previousWord, callback); - } - - private void addWords(Cursor cursor) { - clearDictionary(); - - final int maxWordLength = getMaxWordLength(); - try { - if (cursor.moveToFirst()) { - while (!cursor.isAfterLast()) { - String name = cursor.getString(INDEX_NAME); - - if (name != null && -1 == name.indexOf('@')) { - int len = name.length(); - String prevWord = null; - - // TODO: Better tokenization for non-Latin writing systems - for (int i = 0; i < len; i++) { - if (Character.isLetter(name.charAt(i))) { - int j; - for (j = i + 1; j < len; j++) { - char c = name.charAt(j); - - if (!(c == Keyboard.CODE_DASH - || c == Keyboard.CODE_SINGLE_QUOTE - || Character.isLetter(c))) { - break; - } - } - - String word = name.substring(i, j); - i = j - 1; - - // Safeguard against adding really long words. Stack - // may overflow due to recursion - // Also don't add single letter words, possibly confuses - // capitalization of i. - final int wordLen = word.length(); - if (wordLen < maxWordLength && wordLen > 1) { - super.addWord(word, null /* shortcut */, - FREQUENCY_FOR_CONTACTS); - if (!TextUtils.isEmpty(prevWord)) { - super.setBigramAndGetFrequency(prevWord, word, - FREQUENCY_FOR_CONTACTS_BIGRAM); - } - prevWord = word; - } - } - } - } - cursor.moveToNext(); - } - } - cursor.close(); - } catch(IllegalStateException e) { - Log.e(TAG, "Contacts DB is having problems"); - } - } -} diff --git a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java index 34a92fd30..4a5471c85 100644 --- a/java/src/com/android/inputmethod/latin/ExpandableDictionary.java +++ b/java/src/com/android/inputmethod/latin/ExpandableDictionary.java @@ -514,8 +514,10 @@ public class ExpandableDictionary extends Dictionary { /** * Adds bigrams to the in-memory trie structure that is being used to retrieve any word + * @param word1 the first word of this bigram + * @param word2 the second word of this bigram * @param frequency frequency for this bigram - * @param addFrequency if true, it adds to current frequency, else it overwrites the old value + * @param fcp an instance of ForgettingCurveParams to use for decay policy * @return returns the final bigram frequency */ private int setBigramAndGetFrequency( diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index ae9e197a1..562753374 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -56,7 +56,6 @@ import android.view.WindowManager; import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.CorrectionInfo; import android.view.inputmethod.EditorInfo; -import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputMethodSubtype; import com.android.inputmethod.accessibility.AccessibilityUtils; @@ -103,12 +102,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen */ private static final String SCHEME_PACKAGE = "package"; - /** Whether to use the binary version of the contacts dictionary */ - public static final boolean USE_BINARY_CONTACTS_DICTIONARY = true; - - /** Whether to use the binary version of the user dictionary */ - public static final boolean USE_BINARY_USER_DICTIONARY = true; - // TODO: migrate this to SettingsValues private int mSuggestionVisibility; private static final int SUGGESTION_VISIBILITY_SHOW_VALUE @@ -162,13 +155,13 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private boolean mShouldSwitchToLastSubtype = true; private boolean mIsMainDictionaryAvailable; - // TODO: revert this back to the concrete class after transition. - private Dictionary mUserDictionary; + private UserBinaryDictionary mUserDictionary; private UserHistoryDictionary mUserHistoryDictionary; private boolean mIsUserDictionaryAvailable; private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; private WordComposer mWordComposer = new WordComposer(); + private RichInputConnection mConnection = new RichInputConnection(); private int mCorrectionMode; @@ -469,7 +462,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen final Locale subtypeLocale = mSubtypeSwitcher.getCurrentSubtypeLocale(); final String localeStr = subtypeLocale.toString(); - final Dictionary oldContactsDictionary; + final ContactsBinaryDictionary oldContactsDictionary; if (mSuggest != null) { oldContactsDictionary = mSuggest.getContactsDictionary(); mSuggest.close(); @@ -483,13 +476,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mIsMainDictionaryAvailable = DictionaryFactory.isDictionaryAvailable(this, subtypeLocale); - if (USE_BINARY_USER_DICTIONARY) { - mUserDictionary = new UserBinaryDictionary(this, localeStr); - mIsUserDictionaryAvailable = ((UserBinaryDictionary)mUserDictionary).isEnabled(); - } else { - mUserDictionary = new UserDictionary(this, localeStr); - mIsUserDictionaryAvailable = ((UserDictionary)mUserDictionary).isEnabled(); - } + mUserDictionary = new UserBinaryDictionary(this, localeStr); + mIsUserDictionaryAvailable = mUserDictionary.isEnabled(); mSuggest.setUserDictionary(mUserDictionary); resetContactsDictionary(oldContactsDictionary); @@ -510,10 +498,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen * * @param oldContactsDictionary an optional dictionary to use, or null */ - private void resetContactsDictionary(final Dictionary oldContactsDictionary) { + private void resetContactsDictionary(final ContactsBinaryDictionary oldContactsDictionary) { final boolean shouldSetDictionary = (null != mSuggest && mSettingsValues.mUseContactsDict); - final Dictionary dictionaryToUse; + final ContactsBinaryDictionary dictionaryToUse; if (!shouldSetDictionary) { // Make sure the dictionary is closed. If it is already closed, this is a no-op, // so it's safe to call it anyways. @@ -522,32 +510,20 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } else { final Locale locale = mSubtypeSwitcher.getCurrentSubtypeLocale(); if (null != oldContactsDictionary) { - if (USE_BINARY_CONTACTS_DICTIONARY) { - ContactsBinaryDictionary oldContactsBinaryDictionary = - (ContactsBinaryDictionary)oldContactsDictionary; - if (!oldContactsBinaryDictionary.mLocale.equals(locale)) { - // If the locale has changed then recreate the contacts dictionary. This - // allows locale dependent rules for handling bigram name predictions. - oldContactsDictionary.close(); - dictionaryToUse = new ContactsBinaryDictionary( - this, Suggest.DIC_CONTACTS, locale); - } else { - // Make sure the old contacts dictionary is opened. If it is already open, - // this is a no-op, so it's safe to call it anyways. - oldContactsBinaryDictionary.reopen(this); - dictionaryToUse = oldContactsDictionary; - } + if (!oldContactsDictionary.mLocale.equals(locale)) { + // If the locale has changed then recreate the contacts dictionary. This + // allows locale dependent rules for handling bigram name predictions. + oldContactsDictionary.close(); + dictionaryToUse = new ContactsBinaryDictionary( + this, Suggest.DIC_CONTACTS, locale); } else { - ((ContactsDictionary)oldContactsDictionary).reopen(this); + // Make sure the old contacts dictionary is opened. If it is already open, + // this is a no-op, so it's safe to call it anyways. + oldContactsDictionary.reopen(this); dictionaryToUse = oldContactsDictionary; } } else { - if (USE_BINARY_CONTACTS_DICTIONARY) { - dictionaryToUse = new ContactsBinaryDictionary(this, Suggest.DIC_CONTACTS, - locale); - } else { - dictionaryToUse = new ContactsDictionary(this, Suggest.DIC_CONTACTS); - } + dictionaryToUse = new ContactsBinaryDictionary(this, Suggest.DIC_CONTACTS, locale); } } @@ -582,9 +558,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (mDisplayOrientation != conf.orientation) { mDisplayOrientation = conf.orientation; mHandler.startOrientationChanging(); - final InputConnection ic = getCurrentInputConnection(); - commitTyped(ic, LastComposedWord.NOT_A_SEPARATOR); - if (ic != null) ic.finishComposingText(); // For voice input + mConnection.beginBatchEdit(getCurrentInputConnection()); + commitTyped(LastComposedWord.NOT_A_SEPARATOR); + mConnection.finishComposingText(); + mConnection.endBatchEdit(); if (isShowingOptionDialog()) mOptionsDialog.dismiss(); } @@ -747,6 +724,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (TRACE) Debug.startMethodTracing("/data/trace/latinime"); } + @Override public void onTargetApplicationKnown(final ApplicationInfo info) { mTargetApplicationInfo = info; } @@ -805,7 +783,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen ResearchLogger.latinIME_onUpdateSelection(mLastSelectionStart, mLastSelectionEnd, oldSelStart, oldSelEnd, newSelStart, newSelEnd, composingSpanStart, composingSpanEnd, mExpectingUpdateSelection, - expectingUpdateSelectionFromLogger, getCurrentInputConnection()); + expectingUpdateSelectionFromLogger, mConnection); if (expectingUpdateSelectionFromLogger) { return; } @@ -1047,10 +1025,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private void resetEntireInputState() { resetComposingState(true /* alsoResetLastComposedWord */); updateSuggestions(); - final InputConnection ic = getCurrentInputConnection(); - if (ic != null) { - ic.finishComposingText(); - } + mConnection.finishComposingText(); } private void resetComposingState(final boolean alsoResetLastComposedWord) { @@ -1059,15 +1034,13 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; } - public void commitTyped(final InputConnection ic, final int separatorCode) { + public void commitTyped(final int separatorCode) { if (!mWordComposer.isComposingWord()) return; final CharSequence typedWord = mWordComposer.getTypedWord(); if (typedWord.length() > 0) { - if (ic != null) { - ic.commitText(typedWord, 1); - if (ProductionFlag.IS_EXPERIMENTAL) { - ResearchLogger.latinIME_commitText(typedWord); - } + mConnection.commitText(typedWord, 1); + if (ProductionFlag.IS_EXPERIMENTAL) { + ResearchLogger.latinIME_commitText(typedWord); } final CharSequence prevWord = addToUserHistoryDictionary(typedWord); mLastComposedWord = mWordComposer.commitWord( @@ -1096,27 +1069,23 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // unless needed. if (mWordComposer.isComposingWord()) return Constants.TextUtils.CAP_MODE_OFF; - final InputConnection ic = getCurrentInputConnection(); - if (ic == null) return Constants.TextUtils.CAP_MODE_OFF; // TODO: This blocking IPC call is heavy. Consider doing this without using IPC calls. // Note: getCursorCapsMode() returns the current capitalization mode that is any // combination of CAP_MODE_CHARACTERS, CAP_MODE_WORDS, and CAP_MODE_SENTENCES. 0 means none // of them. - return ic.getCursorCapsMode(inputType); + return mConnection.getCursorCapsMode(inputType); } - // "ic" may be null - private void swapSwapperAndSpaceWhileInBatchEdit(final InputConnection ic) { - if (null == ic) return; - CharSequence lastTwo = ic.getTextBeforeCursor(2, 0); + private void swapSwapperAndSpaceWhileInBatchEdit() { + CharSequence lastTwo = mConnection.getTextBeforeCursor(2, 0); // It is guaranteed lastTwo.charAt(1) is a swapper - else this method is not called. if (lastTwo != null && lastTwo.length() == 2 && lastTwo.charAt(0) == Keyboard.CODE_SPACE) { - ic.deleteSurroundingText(2, 0); + mConnection.deleteSurroundingText(2, 0); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_deleteSurroundingText(2); } - ic.commitText(lastTwo.charAt(1) + " ", 1); + mConnection.commitText(lastTwo.charAt(1) + " ", 1); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_swapSwapperAndSpaceWhileInBatchEdit(); } @@ -1124,18 +1093,17 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } } - private boolean maybeDoubleSpaceWhileInBatchEdit(final InputConnection ic) { + private boolean maybeDoubleSpaceWhileInBatchEdit() { if (mCorrectionMode == Suggest.CORRECTION_NONE) return false; - if (ic == null) return false; - final CharSequence lastThree = ic.getTextBeforeCursor(3, 0); + final CharSequence lastThree = mConnection.getTextBeforeCursor(3, 0); if (lastThree != null && lastThree.length() == 3 && canBeFollowedByPeriod(lastThree.charAt(0)) && lastThree.charAt(1) == Keyboard.CODE_SPACE && lastThree.charAt(2) == Keyboard.CODE_SPACE && mHandler.isAcceptingDoubleSpaces()) { mHandler.cancelDoubleSpacesTimer(); - ic.deleteSurroundingText(2, 0); - ic.commitText(". ", 1); + mConnection.deleteSurroundingText(2, 0); + mConnection.commitText(". ", 1); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_doubleSpaceAutoPeriod(); } @@ -1157,13 +1125,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen || codePoint == Keyboard.CODE_CLOSING_ANGLE_BRACKET; } - // "ic" may be null - private static void removeTrailingSpaceWhileInBatchEdit(final InputConnection ic) { - if (ic == null) return; - final CharSequence lastOne = ic.getTextBeforeCursor(1, 0); + private void removeTrailingSpace() { + final CharSequence lastOne = mConnection.getTextBeforeCursor(1, 0); if (lastOne != null && lastOne.length() == 1 && lastOne.charAt(0) == Keyboard.CODE_SPACE) { - ic.deleteSurroundingText(1, 0); + mConnection.deleteSurroundingText(1, 0); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_deleteSurroundingText(1); } @@ -1172,11 +1138,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen @Override public boolean addWordToDictionary(String word) { - if (USE_BINARY_USER_DICTIONARY) { - ((UserBinaryDictionary)mUserDictionary).addWordToUserDictionary(word, 128); - } else { - ((UserDictionary)mUserDictionary).addWordToUserDictionary(word, 128); - } + mUserDictionary.addWordToUserDictionary(word, 128); // Suggestion strip should be updated after the operation of adding word to the // user dictionary mHandler.postUpdateSuggestions(); @@ -1219,12 +1181,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } private void performEditorAction(int actionId) { - final InputConnection ic = getCurrentInputConnection(); - if (ic != null) { - ic.performEditorAction(actionId); - if (ProductionFlag.IS_EXPERIMENTAL) { - ResearchLogger.latinIME_performEditorAction(actionId); - } + mConnection.performEditorAction(actionId); + if (ProductionFlag.IS_EXPERIMENTAL) { + ResearchLogger.latinIME_performEditorAction(actionId); } } @@ -1247,12 +1206,12 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } } - static private void sendUpDownEnterOrBackspace(final int code, final InputConnection ic) { + private void sendUpDownEnterOrBackspace(final int code) { final long eventTime = SystemClock.uptimeMillis(); - ic.sendKeyEvent(new KeyEvent(eventTime, eventTime, + mConnection.sendKeyEvent(new KeyEvent(eventTime, eventTime, KeyEvent.ACTION_DOWN, code, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE)); - ic.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime, + mConnection.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime, KeyEvent.ACTION_UP, code, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE)); } @@ -1265,24 +1224,21 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen return; } - final InputConnection ic = getCurrentInputConnection(); - if (ic != null) { - // 16 is android.os.Build.VERSION_CODES.JELLY_BEAN but we can't write it because - // we want to be able to compile against the Ice Cream Sandwich SDK. - if (Keyboard.CODE_ENTER == code && mTargetApplicationInfo != null - && mTargetApplicationInfo.targetSdkVersion < 16) { - // Backward compatibility mode. Before Jelly bean, the keyboard would simulate - // a hardware keyboard event on pressing enter or delete. This is bad for many - // reasons (there are race conditions with commits) but some applications are - // relying on this behavior so we continue to support it for older apps. - sendUpDownEnterOrBackspace(KeyEvent.KEYCODE_ENTER, ic); - } else { - final String text = new String(new int[] { code }, 0, 1); - ic.commitText(text, text.length()); - } - if (ProductionFlag.IS_EXPERIMENTAL) { - ResearchLogger.latinIME_sendKeyCodePoint(code); - } + // 16 is android.os.Build.VERSION_CODES.JELLY_BEAN but we can't write it because + // we want to be able to compile against the Ice Cream Sandwich SDK. + if (Keyboard.CODE_ENTER == code && mTargetApplicationInfo != null + && mTargetApplicationInfo.targetSdkVersion < 16) { + // Backward compatibility mode. Before Jelly bean, the keyboard would simulate + // a hardware keyboard event on pressing enter or delete. This is bad for many + // reasons (there are race conditions with commits) but some applications are + // relying on this behavior so we continue to support it for older apps. + sendUpDownEnterOrBackspace(KeyEvent.KEYCODE_ENTER); + } else { + final String text = new String(new int[] { code }, 0, 1); + mConnection.commitText(text, text.length()); + } + if (ProductionFlag.IS_EXPERIMENTAL) { + ResearchLogger.latinIME_sendKeyCodePoint(code); } } @@ -1294,6 +1250,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mDeleteCount = 0; } mLastKeyTime = when; + mConnection.beginBatchEdit(getCurrentInputConnection()); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_onCodeInput(primaryCode, x, y); @@ -1378,23 +1335,22 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen && primaryCode != Keyboard.CODE_SWITCH_ALPHA_SYMBOL) mLastComposedWord.deactivate(); mEnteredText = null; + mConnection.endBatchEdit(); } @Override public void onTextInput(CharSequence text) { - final InputConnection ic = getCurrentInputConnection(); - if (ic == null) return; - ic.beginBatchEdit(); - commitTyped(ic, LastComposedWord.NOT_A_SEPARATOR); - text = specificTldProcessingOnTextInput(ic, text); + mConnection.beginBatchEdit(getCurrentInputConnection()); + commitTyped(LastComposedWord.NOT_A_SEPARATOR); + text = specificTldProcessingOnTextInput(text); if (SPACE_STATE_PHANTOM == mSpaceState) { sendKeyCodePoint(Keyboard.CODE_SPACE); } - ic.commitText(text, 1); + mConnection.commitText(text, 1); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_commitText(text); } - ic.endBatchEdit(); + mConnection.endBatchEdit(); mKeyboardSwitcher.updateShiftState(); mKeyboardSwitcher.onCodeInput(Keyboard.CODE_OUTPUT_TEXT); mSpaceState = SPACE_STATE_NONE; @@ -1402,9 +1358,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen resetComposingState(true /* alsoResetLastComposedWord */); } - // ic may not be null - private CharSequence specificTldProcessingOnTextInput(final InputConnection ic, - final CharSequence text) { + private CharSequence specificTldProcessingOnTextInput(final CharSequence text) { if (text.length() <= 1 || text.charAt(0) != Keyboard.CODE_PERIOD || !Character.isLetter(text.charAt(1))) { // Not a tld: do nothing. @@ -1413,7 +1367,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // We have a TLD (or something that looks like this): make sure we don't add // a space even if currently in phantom mode. mSpaceState = SPACE_STATE_NONE; - final CharSequence lastOne = ic.getTextBeforeCursor(1, 0); + final CharSequence lastOne = mConnection.getTextBeforeCursor(1, 0); if (lastOne != null && lastOne.length() == 1 && lastOne.charAt(0) == Keyboard.CODE_PERIOD) { return text.subSequence(1, text.length()); @@ -1429,24 +1383,15 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } private void handleBackspace(final int spaceState) { - final InputConnection ic = getCurrentInputConnection(); - if (ic == null) return; - ic.beginBatchEdit(); - handleBackspaceWhileInBatchEdit(spaceState, ic); - ic.endBatchEdit(); - } - - // "ic" may not be null. - private void handleBackspaceWhileInBatchEdit(final int spaceState, final InputConnection ic) { // In many cases, we may have to put the keyboard in auto-shift state again. mHandler.postUpdateShiftState(); - if (mEnteredText != null && sameAsTextBeforeCursor(ic, mEnteredText)) { + if (mEnteredText != null && sameAsTextBeforeCursor(mEnteredText)) { // Cancel multi-character input: remove the text we just entered. // This is triggered on backspace after a key that inputs multiple characters, // like the smiley key or the .com key. final int length = mEnteredText.length(); - ic.deleteSurroundingText(length, 0); + mConnection.deleteSurroundingText(length, 0); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_deleteSurroundingText(length); } @@ -1460,7 +1405,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen final int length = mWordComposer.size(); if (length > 0) { mWordComposer.deleteLast(); - ic.setComposingText(getTextWithUnderline(mWordComposer.getTypedWord()), 1); + mConnection.setComposingText(getTextWithUnderline(mWordComposer.getTypedWord()), 1); // If we have deleted the last remaining character of a word, then we are not // isComposingWord() any more. if (!mWordComposer.isComposingWord()) { @@ -1471,7 +1416,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mHandler.postUpdateSuggestions(); } } else { - ic.deleteSurroundingText(1, 0); + mConnection.deleteSurroundingText(1, 0); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_deleteSurroundingText(1); } @@ -1479,17 +1424,17 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } else { if (mLastComposedWord.canRevertCommit()) { Utils.Stats.onAutoCorrectionCancellation(); - revertCommit(ic); + revertCommit(); return; } if (SPACE_STATE_DOUBLE == spaceState) { - if (revertDoubleSpaceWhileInBatchEdit(ic)) { + if (revertDoubleSpaceWhileInBatchEdit()) { // No need to reset mSpaceState, it has already be done (that's why we // receive it as a parameter) return; } } else if (SPACE_STATE_SWAP_PUNCTUATION == spaceState) { - if (revertSwapPunctuation(ic)) { + if (revertSwapPunctuation()) { // Likewise return; } @@ -1500,8 +1445,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (mLastSelectionStart != mLastSelectionEnd) { // If there is a selection, remove it. final int lengthToDelete = mLastSelectionEnd - mLastSelectionStart; - ic.setSelection(mLastSelectionEnd, mLastSelectionEnd); - ic.deleteSurroundingText(lengthToDelete, 0); + mConnection.setSelection(mLastSelectionEnd, mLastSelectionEnd); + mConnection.deleteSurroundingText(lengthToDelete, 0); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_deleteSurroundingText(lengthToDelete); } @@ -1519,31 +1464,30 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // a hardware keyboard event on pressing enter or delete. This is bad for many // reasons (there are race conditions with commits) but some applications are // relying on this behavior so we continue to support it for older apps. - sendUpDownEnterOrBackspace(KeyEvent.KEYCODE_DEL, ic); + sendUpDownEnterOrBackspace(KeyEvent.KEYCODE_DEL); } else { - ic.deleteSurroundingText(1, 0); + mConnection.deleteSurroundingText(1, 0); } if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_deleteSurroundingText(1); } if (mDeleteCount > DELETE_ACCELERATE_AT) { - ic.deleteSurroundingText(1, 0); + mConnection.deleteSurroundingText(1, 0); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_deleteSurroundingText(1); } } } if (isSuggestionsRequested()) { - restartSuggestionsOnWordBeforeCursorIfAtEndOfWord(ic); + restartSuggestionsOnWordBeforeCursorIfAtEndOfWord(); } } } - // ic may be null - private boolean maybeStripSpaceWhileInBatchEdit(final InputConnection ic, final int code, + private boolean maybeStripSpace(final int code, final int spaceState, final boolean isFromSuggestionStrip) { if (Keyboard.CODE_ENTER == code && SPACE_STATE_SWAP_PUNCTUATION == spaceState) { - removeTrailingSpaceWhileInBatchEdit(ic); + removeTrailingSpace(); return false; } else if ((SPACE_STATE_WEAK == spaceState || SPACE_STATE_SWAP_PUNCTUATION == spaceState) @@ -1552,7 +1496,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen return true; } else { if (mSettingsValues.isWeakSpaceStripper(code)) { - removeTrailingSpaceWhileInBatchEdit(ic); + removeTrailingSpace(); } return false; } @@ -1563,16 +1507,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private void handleCharacter(final int primaryCode, final int x, final int y, final int spaceState) { - final InputConnection ic = getCurrentInputConnection(); - if (null != ic) ic.beginBatchEdit(); - // TODO: if ic is null, does it make any sense to call this? - handleCharacterWhileInBatchEdit(primaryCode, x, y, spaceState, ic); - if (null != ic) ic.endBatchEdit(); - } - - // "ic" may be null without this crashing, but the behavior will be really strange - private void handleCharacterWhileInBatchEdit(final int primaryCode, - final int x, final int y, final int spaceState, final InputConnection ic) { boolean isComposingWord = mWordComposer.isComposingWord(); if (SPACE_STATE_PHANTOM == spaceState && @@ -1605,23 +1539,21 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (isComposingWord) { mWordComposer.add( primaryCode, x, y, mKeyboardSwitcher.getKeyboardView().getKeyDetector()); - if (ic != null) { - // If it's the first letter, make note of auto-caps state - if (mWordComposer.size() == 1) { - mWordComposer.setAutoCapitalized( - getCurrentAutoCapsState() != Constants.TextUtils.CAP_MODE_OFF); - } - ic.setComposingText(getTextWithUnderline(mWordComposer.getTypedWord()), 1); + // If it's the first letter, make note of auto-caps state + if (mWordComposer.size() == 1) { + mWordComposer.setAutoCapitalized( + getCurrentAutoCapsState() != Constants.TextUtils.CAP_MODE_OFF); } + mConnection.setComposingText(getTextWithUnderline(mWordComposer.getTypedWord()), 1); mHandler.postUpdateSuggestions(); } else { - final boolean swapWeakSpace = maybeStripSpaceWhileInBatchEdit(ic, primaryCode, + final boolean swapWeakSpace = maybeStripSpace(primaryCode, spaceState, KeyboardActionListener.SUGGESTION_STRIP_COORDINATE == x); sendKeyCodePoint(primaryCode); if (swapWeakSpace) { - swapSwapperAndSpaceWhileInBatchEdit(ic); + swapSwapperAndSpaceWhileInBatchEdit(); mSpaceState = SPACE_STATE_WEAK; } // Some characters are not word separators, yet they don't start a new @@ -1648,10 +1580,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen boolean didAutoCorrect = false; // Handle separator - final InputConnection ic = getCurrentInputConnection(); - if (ic != null) { - ic.beginBatchEdit(); - } if (mWordComposer.isComposingWord()) { // In certain languages where single quote is a separator, it's better // not to auto correct, but accept the typed word. For instance, @@ -1660,14 +1588,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen final boolean shouldAutoCorrect = mSettingsValues.mAutoCorrectEnabled && !mInputAttributes.mInputTypeNoAutoCorrect; if (shouldAutoCorrect && primaryCode != Keyboard.CODE_SINGLE_QUOTE) { - commitCurrentAutoCorrection(primaryCode, ic); + commitCurrentAutoCorrection(primaryCode); didAutoCorrect = true; } else { - commitTyped(ic, primaryCode); + commitTyped(primaryCode); } } - final boolean swapWeakSpace = maybeStripSpaceWhileInBatchEdit(ic, primaryCode, spaceState, + final boolean swapWeakSpace = maybeStripSpace(primaryCode, spaceState, KeyboardActionListener.SUGGESTION_STRIP_COORDINATE == x); if (SPACE_STATE_PHANTOM == spaceState && @@ -1678,7 +1606,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen if (Keyboard.CODE_SPACE == primaryCode) { if (isSuggestionsRequested()) { - if (maybeDoubleSpaceWhileInBatchEdit(ic)) { + if (maybeDoubleSpaceWhileInBatchEdit()) { mSpaceState = SPACE_STATE_DOUBLE; } else if (!isShowingPunctuationList()) { mSpaceState = SPACE_STATE_WEAK; @@ -1692,7 +1620,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } } else { if (swapWeakSpace) { - swapSwapperAndSpaceWhileInBatchEdit(ic); + swapSwapperAndSpaceWhileInBatchEdit(); mSpaceState = SPACE_STATE_SWAP_PUNCTUATION; } else if (SPACE_STATE_PHANTOM == spaceState) { // If we are in phantom space state, and the user presses a separator, we want to @@ -1711,9 +1639,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen Utils.Stats.onSeparator((char)primaryCode, x, y); - if (ic != null) { - ic.endBatchEdit(); - } return didAutoCorrect; } @@ -1724,7 +1649,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } private void handleClose() { - commitTyped(getCurrentInputConnection(), LastComposedWord.NOT_A_SEPARATOR); + commitTyped(LastComposedWord.NOT_A_SEPARATOR); requestHideSelf(0); LatinKeyboardView inputView = mKeyboardSwitcher.getKeyboardView(); if (inputView != null) @@ -1794,14 +1719,12 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen private void setAutoCorrectionIndicator(final boolean newAutoCorrectionIndicator) { // Put a blue underline to a word in TextView which will be auto-corrected. - final InputConnection ic = getCurrentInputConnection(); - if (ic == null) return; if (mIsAutoCorrectionIndicatorOn != newAutoCorrectionIndicator && mWordComposer.isComposingWord()) { mIsAutoCorrectionIndicatorOn = newAutoCorrectionIndicator; final CharSequence textWithUnderline = getTextWithUnderline(mWordComposer.getTypedWord()); - ic.setComposingText(textWithUnderline, 1); + mConnection.setComposingText(textWithUnderline, 1); } } @@ -1824,14 +1747,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } // TODO: May need a better way of retrieving previous word - final InputConnection ic = getCurrentInputConnection(); - final CharSequence prevWord; - if (null == ic) { - prevWord = null; - } else { - prevWord = EditingUtils.getPreviousWord(ic, mSettingsValues.mWordSeparators); - } - + final CharSequence prevWord = mConnection.getPreviousWord(mSettingsValues.mWordSeparators); final CharSequence typedWord = mWordComposer.getTypedWord(); // getSuggestedWords handles gracefully a null value of prevWord final SuggestedWords suggestedWords = mSuggest.getSuggestedWords(mWordComposer, @@ -1885,8 +1801,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen setSuggestionStripShown(isSuggestionsStripVisible()); } - private void commitCurrentAutoCorrection(final int separatorCodePoint, - final InputConnection ic) { + private void commitCurrentAutoCorrection(final int separatorCodePoint) { // Complete any pending suggestions query first if (mHandler.hasPendingUpdateSuggestions()) { mHandler.cancelUpdateSuggestions(); @@ -1907,10 +1822,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mExpectingUpdateSelection = true; commitChosenWord(autoCorrection, LastComposedWord.COMMIT_TYPE_DECIDED_WORD, separatorCodePoint); - if (!typedWord.equals(autoCorrection) && null != ic) { + if (!typedWord.equals(autoCorrection)) { // This will make the correction flash for a short while as a visual clue // to the user that auto-correction happened. - ic.commitCorrection(new CorrectionInfo(mLastSelectionEnd - typedWord.length(), + mConnection.commitCorrection( + new CorrectionInfo(mLastSelectionEnd - typedWord.length(), typedWord, autoCorrection)); } } @@ -1919,14 +1835,13 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen @Override public void pickSuggestionManually(final int index, final CharSequence suggestion, int x, int y) { - final InputConnection ic = getCurrentInputConnection(); - if (null != ic) ic.beginBatchEdit(); - pickSuggestionManuallyWhileInBatchEdit(index, suggestion, x, y, ic); - if (null != ic) ic.endBatchEdit(); + mConnection.beginBatchEdit(getCurrentInputConnection()); + pickSuggestionManuallyWhileInBatchEdit(index, suggestion, x, y); + mConnection.endBatchEdit(); } public void pickSuggestionManuallyWhileInBatchEdit(final int index, - final CharSequence suggestion, final int x, final int y, final InputConnection ic) { + final CharSequence suggestion, final int x, final int y) { final SuggestedWords suggestedWords = mSuggestionsView.getSuggestions(); // If this is a punctuation picked from the suggestion strip, pass it to onCodeInput if (suggestion.length() == 1 && isShowingPunctuationList()) { @@ -1960,13 +1875,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } mKeyboardSwitcher.updateShiftState(); resetComposingState(true /* alsoResetLastComposedWord */); - if (ic != null) { - final CompletionInfo completionInfo = mApplicationSpecifiedCompletions[index]; - ic.commitCompletion(completionInfo); - if (ProductionFlag.IS_EXPERIMENTAL) { - ResearchLogger.latinIME_pickApplicationSpecifiedCompletion(index, - completionInfo.getText(), x, y); - } + final CompletionInfo completionInfo = mApplicationSpecifiedCompletions[index]; + mConnection.commitCompletion(completionInfo); + if (ProductionFlag.IS_EXPERIMENTAL) { + ResearchLogger.latinIME_pickApplicationSpecifiedCompletion(index, + completionInfo.getText(), x, y); } return; } @@ -2028,21 +1941,18 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen */ private void commitChosenWord(final CharSequence chosenWord, final int commitType, final int separatorCode) { - final InputConnection ic = getCurrentInputConnection(); - if (ic != null) { - if (mSettingsValues.mEnableSuggestionSpanInsertion) { - final SuggestedWords suggestedWords = mSuggestionsView.getSuggestions(); - ic.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan( - this, chosenWord, suggestedWords, mIsMainDictionaryAvailable), - 1); - if (ProductionFlag.IS_EXPERIMENTAL) { - ResearchLogger.latinIME_commitText(chosenWord); - } - } else { - ic.commitText(chosenWord, 1); - if (ProductionFlag.IS_EXPERIMENTAL) { - ResearchLogger.latinIME_commitText(chosenWord); - } + if (mSettingsValues.mEnableSuggestionSpanInsertion) { + final SuggestedWords suggestedWords = mSuggestionsView.getSuggestions(); + mConnection.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan( + this, chosenWord, suggestedWords, mIsMainDictionaryAvailable), + 1); + if (ProductionFlag.IS_EXPERIMENTAL) { + ResearchLogger.latinIME_commitText(chosenWord); + } + } else { + mConnection.commitText(chosenWord, 1); + if (ProductionFlag.IS_EXPERIMENTAL) { + ResearchLogger.latinIME_commitText(chosenWord); } } // Add the word to the user history dictionary @@ -2066,8 +1976,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen final SuggestedWords suggestedWords; if (mCorrectionMode == Suggest.CORRECTION_FULL_BIGRAM) { - final CharSequence prevWord = EditingUtils.getThisWord(getCurrentInputConnection(), - mSettingsValues.mWordSeparators); + final CharSequence prevWord = mConnection.getThisWord(mSettingsValues.mWordSeparators); if (!TextUtils.isEmpty(prevWord)) { suggestedWords = mSuggest.getBigramPredictions(prevWord); } else { @@ -2104,13 +2013,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } if (mUserHistoryDictionary != null) { - final InputConnection ic = getCurrentInputConnection(); - final CharSequence prevWord; - if (null != ic) { - prevWord = EditingUtils.getPreviousWord(ic, mSettingsValues.mWordSeparators); - } else { - prevWord = null; - } + final CharSequence prevWord + = mConnection.getPreviousWord(mSettingsValues.mWordSeparators); final String secondWord; if (mWordComposer.isAutoCapitalized() && !mWordComposer.isMostlyCaps()) { secondWord = suggestion.toString().toLowerCase( @@ -2130,10 +2034,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } public boolean isCursorTouchingWord() { - final InputConnection ic = getCurrentInputConnection(); - if (ic == null) return false; - CharSequence before = ic.getTextBeforeCursor(1, 0); - CharSequence after = ic.getTextAfterCursor(1, 0); + CharSequence before = mConnection.getTextBeforeCursor(1, 0); + CharSequence after = mConnection.getTextAfterCursor(1, 0); if (!TextUtils.isEmpty(before) && !mSettingsValues.isWordSeparator(before.charAt(0)) && !mSettingsValues.isSymbolExcludedFromWordSeparators(before.charAt(0))) { return true; @@ -2145,37 +2047,33 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen return false; } - // "ic" must not be null - private static boolean sameAsTextBeforeCursor(final InputConnection ic, - final CharSequence text) { - final CharSequence beforeText = ic.getTextBeforeCursor(text.length(), 0); + private boolean sameAsTextBeforeCursor(final CharSequence text) { + final CharSequence beforeText = mConnection.getTextBeforeCursor(text.length(), 0); return TextUtils.equals(text, beforeText); } - // "ic" must not be null /** * Check if the cursor is actually at the end of a word. If so, restart suggestions on this * word, else do nothing. */ - private void restartSuggestionsOnWordBeforeCursorIfAtEndOfWord( - final InputConnection ic) { + private void restartSuggestionsOnWordBeforeCursorIfAtEndOfWord() { // Bail out if the cursor is not at the end of a word (cursor must be preceded by // non-whitespace, non-separator, non-start-of-text) // Example ("|" is the cursor here) : <SOL>"|a" " |a" " | " all get rejected here. - final CharSequence textBeforeCursor = ic.getTextBeforeCursor(1, 0); + final CharSequence textBeforeCursor = mConnection.getTextBeforeCursor(1, 0); if (TextUtils.isEmpty(textBeforeCursor) || mSettingsValues.isWordSeparator(textBeforeCursor.charAt(0))) return; // Bail out if the cursor is in the middle of a word (cursor must be followed by whitespace, // separator or end of line/text) // Example: "test|"<EOL> "te|st" get rejected here - final CharSequence textAfterCursor = ic.getTextAfterCursor(1, 0); + final CharSequence textAfterCursor = mConnection.getTextAfterCursor(1, 0); if (!TextUtils.isEmpty(textAfterCursor) && !mSettingsValues.isWordSeparator(textAfterCursor.charAt(0))) return; // Bail out if word before cursor is 0-length or a single non letter (like an apostrophe) // Example: " -|" gets rejected here but "e-|" and "e|" are okay - CharSequence word = EditingUtils.getWordAtCursor(ic, mSettingsValues.mWordSeparators); + CharSequence word = mConnection.getWordAtCursor(mSettingsValues.mWordSeparators); // We don't suggest on leading single quotes, so we have to remove them from the word if // it starts with single quotes. while (!TextUtils.isEmpty(word) && Keyboard.CODE_SINGLE_QUOTE == word.charAt(0)) { @@ -2193,24 +2091,21 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen } // Okay, we are at the end of a word. Restart suggestions. - restartSuggestionsOnWordBeforeCursor(ic, word); + restartSuggestionsOnWordBeforeCursor(word); } - // "ic" must not be null - private void restartSuggestionsOnWordBeforeCursor(final InputConnection ic, - final CharSequence word) { + private void restartSuggestionsOnWordBeforeCursor(final CharSequence word) { mWordComposer.setComposingWord(word, mKeyboardSwitcher.getKeyboard()); final int length = word.length(); - ic.deleteSurroundingText(length, 0); + mConnection.deleteSurroundingText(length, 0); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_deleteSurroundingText(length); } - ic.setComposingText(word, 1); + mConnection.setComposingText(word, 1); mHandler.postUpdateSuggestions(); } - // "ic" must not be null - private void revertCommit(final InputConnection ic) { + private void revertCommit() { final CharSequence previousWord = mLastComposedWord.mPrevWord; final String originallyTypedWord = mLastComposedWord.mTypedWord; final CharSequence committedWord = mLastComposedWord.mCommittedWord; @@ -2224,7 +2119,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen throw new RuntimeException("revertCommit, but we are composing a word"); } final String wordBeforeCursor = - ic.getTextBeforeCursor(deleteLength, 0) + mConnection.getTextBeforeCursor(deleteLength, 0) .subSequence(0, cancelLength).toString(); if (!TextUtils.equals(committedWord, wordBeforeCursor)) { throw new RuntimeException("revertCommit check failed: we thought we were " @@ -2232,7 +2127,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen + "\", but before the cursor we found \"" + wordBeforeCursor + "\""); } } - ic.deleteSurroundingText(deleteLength, 0); + mConnection.deleteSurroundingText(deleteLength, 0); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_deleteSurroundingText(deleteLength); } @@ -2244,9 +2139,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // This is the case when we cancel a manual pick. // We should restart suggestion on the word right away. mWordComposer.resumeSuggestionOnLastComposedWord(mLastComposedWord); - ic.setComposingText(originallyTypedWord, 1); + mConnection.setComposingText(originallyTypedWord, 1); } else { - ic.commitText(originallyTypedWord, 1); + mConnection.commitText(originallyTypedWord, 1); // Re-insert the separator sendKeyCodePoint(mLastComposedWord.mSeparatorCode); Utils.Stats.onSeparator(mLastComposedWord.mSeparatorCode, WordComposer.NOT_A_COORDINATE, @@ -2262,12 +2157,11 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen mHandler.postUpdateSuggestions(); } - // "ic" must not be null - private boolean revertDoubleSpaceWhileInBatchEdit(final InputConnection ic) { + private boolean revertDoubleSpaceWhileInBatchEdit() { mHandler.cancelDoubleSpacesTimer(); // Here we test whether we indeed have a period and a space before us. This should not // be needed, but it's there just in case something went wrong. - final CharSequence textBeforeCursor = ic.getTextBeforeCursor(2, 0); + final CharSequence textBeforeCursor = mConnection.getTextBeforeCursor(2, 0); if (!". ".equals(textBeforeCursor)) { // Theoretically we should not be coming here if there isn't ". " before the // cursor, but the application may be changing the text while we are typing, so @@ -2276,21 +2170,21 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen + "\". \" just before the cursor."); return false; } - ic.deleteSurroundingText(2, 0); + mConnection.deleteSurroundingText(2, 0); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_deleteSurroundingText(2); } - ic.commitText(" ", 1); + mConnection.commitText(" ", 1); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_revertDoubleSpaceWhileInBatchEdit(); } return true; } - private static boolean revertSwapPunctuation(final InputConnection ic) { + private boolean revertSwapPunctuation() { // Here we test whether we indeed have a space and something else before us. This should not // be needed, but it's there just in case something went wrong. - final CharSequence textBeforeCursor = ic.getTextBeforeCursor(2, 0); + final CharSequence textBeforeCursor = mConnection.getTextBeforeCursor(2, 0); // NOTE: This does not work with surrogate pairs. Hopefully when the keyboard is able to // enter surrogate pairs this code will have been removed. if (TextUtils.isEmpty(textBeforeCursor) @@ -2302,16 +2196,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen + "find a space just before the cursor."); return false; } - ic.beginBatchEdit(); - ic.deleteSurroundingText(2, 0); + mConnection.deleteSurroundingText(2, 0); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_deleteSurroundingText(2); } - ic.commitText(" " + textBeforeCursor.subSequence(0, 1), 1); + mConnection.commitText(" " + textBeforeCursor.subSequence(0, 1), 1); if (ProductionFlag.IS_EXPERIMENTAL) { ResearchLogger.latinIME_revertSwapPunctuation(); } - ic.endBatchEdit(); return true; } @@ -2376,12 +2268,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen // This is a stopgap solution to avoid leaving a high surrogate alone in a text view. // In the future, we need to deprecate deteleSurroundingText() and have a surrogate // pair-friendly way of deleting characters in InputConnection. - final InputConnection ic = getCurrentInputConnection(); - if (null != ic) { - final CharSequence lastChar = ic.getTextBeforeCursor(1, 0); - if (!TextUtils.isEmpty(lastChar) && Character.isHighSurrogate(lastChar.charAt(0))) { - ic.deleteSurroundingText(1, 0); - } + final CharSequence lastChar = mConnection.getTextBeforeCursor(1, 0); + if (!TextUtils.isEmpty(lastChar) && Character.isHighSurrogate(lastChar.charAt(0))) { + mConnection.deleteSurroundingText(1, 0); } } } diff --git a/java/src/com/android/inputmethod/latin/ResearchLogger.java b/java/src/com/android/inputmethod/latin/ResearchLogger.java index bb003f766..f27d07074 100644 --- a/java/src/com/android/inputmethod/latin/ResearchLogger.java +++ b/java/src/com/android/inputmethod/latin/ResearchLogger.java @@ -101,13 +101,13 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang private static class NullOutputStream extends OutputStream { /** {@inheritDoc} */ @Override - public void write(byte[] buffer, int offset, int count) throws IOException { + public void write(byte[] buffer, int offset, int count) { // nop } /** {@inheritDoc} */ @Override - public void write(byte[] buffer) throws IOException { + public void write(byte[] buffer) { // nop } @@ -612,12 +612,13 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang final int lastSelectionEnd, final int oldSelStart, final int oldSelEnd, final int newSelStart, final int newSelEnd, final int composingSpanStart, final int composingSpanEnd, final boolean expectingUpdateSelection, - final boolean expectingUpdateSelectionFromLogger, final InputConnection connection) { + final boolean expectingUpdateSelectionFromLogger, + final RichInputConnection connection) { final Object[] values = { lastSelectionStart, lastSelectionEnd, oldSelStart, oldSelEnd, newSelStart, newSelEnd, composingSpanStart, composingSpanEnd, expectingUpdateSelection, expectingUpdateSelectionFromLogger, - EditingUtils.getWordRangeAtCursor(connection, WHITESPACE_SEPARATORS, 1).mWord + connection.getWordRangeAtCursor(WHITESPACE_SEPARATORS, 1).mWord }; getInstance().writeEvent(EVENTKEYS_LATINIME_ONUPDATESELECTION, values); } diff --git a/java/src/com/android/inputmethod/latin/EditingUtils.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java index 479b3bf5a..c9ee09396 100644 --- a/java/src/com/android/inputmethod/latin/EditingUtils.java +++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 The Android Open Source Project + * Copyright (C) 2012 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 @@ -16,6 +16,10 @@ package com.android.inputmethod.latin; +import android.util.Log; +import android.view.KeyEvent; +import android.view.inputmethod.CompletionInfo; +import android.view.inputmethod.CorrectionInfo; import android.view.inputmethod.ExtractedText; import android.view.inputmethod.ExtractedTextRequest; import android.view.inputmethod.InputConnection; @@ -23,40 +27,108 @@ import android.view.inputmethod.InputConnection; import java.util.regex.Pattern; /** - * Utility methods to deal with editing text through an InputConnection. + * Wrapper for InputConnection to simplify interaction */ -public class EditingUtils { - /** - * Number of characters we want to look back in order to identify the previous word - */ +public class RichInputConnection { + private static final String TAG = RichInputConnection.class.getSimpleName(); + private static final boolean DBG = false; // Provision for a long word pair and a separator private static final int LOOKBACK_CHARACTER_NUM = BinaryDictionary.MAX_WORD_LENGTH * 2 + 1; + private static final Pattern spaceRegex = Pattern.compile("\\s+"); private static final int INVALID_CURSOR_POSITION = -1; - private EditingUtils() { - // Unintentional empty constructor for singleton. + InputConnection mIC; + int mNestLevel; + public RichInputConnection() { + mIC = null; + mNestLevel = 0; } - private static int getCursorPosition(InputConnection connection) { - if (null == connection) return INVALID_CURSOR_POSITION; - final ExtractedText extracted = connection.getExtractedText(new ExtractedTextRequest(), 0); - if (extracted == null) { - return INVALID_CURSOR_POSITION; + public void beginBatchEdit(final InputConnection newInputConnection) { + if (++mNestLevel == 1) { + mIC = newInputConnection; + if (null != mIC) mIC.beginBatchEdit(); + } else { + if (DBG) { + throw new RuntimeException("Nest level too deep"); + } else { + Log.e(TAG, "Nest level too deep : " + mNestLevel); + } } - return extracted.startOffset + extracted.selectionStart; + } + public void endBatchEdit() { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (--mNestLevel == 0 && null != mIC) mIC.endBatchEdit(); } - /** - * @param connection connection to the current text field. - * @param separators characters which may separate words - * @return the word that surrounds the cursor, including up to one trailing - * separator. For example, if the field contains "he|llo world", where | - * represents the cursor, then "hello " will be returned. - */ - public static String getWordAtCursor(InputConnection connection, String separators) { - // getWordRangeAtCursor returns null if the connection is null - Range r = getWordRangeAtCursor(connection, separators, 0); - return (r == null) ? null : r.mWord; + public void finishComposingText() { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null != mIC) mIC.finishComposingText(); + } + + public void commitText(final CharSequence text, final int i) { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null != mIC) mIC.commitText(text, i); + } + + public int getCursorCapsMode(final int inputType) { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null == mIC) return Constants.TextUtils.CAP_MODE_OFF; + return mIC.getCursorCapsMode(inputType); + } + + public CharSequence getTextBeforeCursor(final int i, final int j) { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null != mIC) return mIC.getTextBeforeCursor(i, j); + return null; + } + + public CharSequence getTextAfterCursor(final int i, final int j) { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null != mIC) return mIC.getTextAfterCursor(i, j); + return null; + } + + public void deleteSurroundingText(final int i, final int j) { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null != mIC) mIC.deleteSurroundingText(i, j); + } + + public void performEditorAction(final int actionId) { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null != mIC) mIC.performEditorAction(actionId); + } + + public void sendKeyEvent(final KeyEvent keyEvent) { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null != mIC) mIC.sendKeyEvent(keyEvent); + } + + public void setComposingText(final CharSequence text, final int i) { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null != mIC) mIC.setComposingText(text, i); + } + + public void setSelection(final int from, final int to) { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null != mIC) mIC.setSelection(from, to); + } + + public void commitCorrection(final CorrectionInfo correctionInfo) { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null != mIC) mIC.commitCorrection(correctionInfo); + } + + public void commitCompletion(final CompletionInfo completionInfo) { + if (mNestLevel <= 0) Log.e(TAG, "Batch edit not in progress!"); // TODO: exception instead + if (null != mIC) mIC.commitCompletion(completionInfo); + } + + public CharSequence getPreviousWord(final String sentenceSeperators) { + //TODO: Should fix this. This could be slow! + if (null == mIC) return null; + CharSequence prev = mIC.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0); + return getPreviousWord(prev, sentenceSeperators); } /** @@ -85,22 +157,96 @@ public class EditingUtils { } } + private static boolean isSeparator(int code, String sep) { + return sep.indexOf(code) != -1; + } + + // Get the word before the whitespace preceding the non-whitespace preceding the cursor. + // Also, it won't return words that end in a separator. + // Example : + // "abc def|" -> abc + // "abc def |" -> abc + // "abc def. |" -> abc + // "abc def . |" -> def + // "abc|" -> null + // "abc |" -> null + // "abc. def|" -> null + public static CharSequence getPreviousWord(CharSequence prev, String sentenceSeperators) { + if (prev == null) return null; + String[] w = spaceRegex.split(prev); + + // If we can't find two words, or we found an empty word, return null. + if (w.length < 2 || w[w.length - 2].length() <= 0) return null; + + // If ends in a separator, return null + char lastChar = w[w.length - 2].charAt(w[w.length - 2].length() - 1); + if (sentenceSeperators.contains(String.valueOf(lastChar))) return null; + + return w[w.length - 2]; + } + + public CharSequence getThisWord(String sentenceSeperators) { + if (null == mIC) return null; + final CharSequence prev = mIC.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0); + return getThisWord(prev, sentenceSeperators); + } + + // Get the word immediately before the cursor, even if there is whitespace between it and + // the cursor - but not if there is punctuation. + // Example : + // "abc def|" -> def + // "abc def |" -> def + // "abc def. |" -> null + // "abc def . |" -> null + public static CharSequence getThisWord(CharSequence prev, String sentenceSeperators) { + if (prev == null) return null; + String[] w = spaceRegex.split(prev); + + // No word : return null + if (w.length < 1 || w[w.length - 1].length() <= 0) return null; + + // If ends in a separator, return null + char lastChar = w[w.length - 1].charAt(w[w.length - 1].length() - 1); + if (sentenceSeperators.contains(String.valueOf(lastChar))) return null; + + return w[w.length - 1]; + } + + /** + * @param separators characters which may separate words + * @return the word that surrounds the cursor, including up to one trailing + * separator. For example, if the field contains "he|llo world", where | + * represents the cursor, then "hello " will be returned. + */ + public String getWordAtCursor(String separators) { + // getWordRangeAtCursor returns null if the connection is null + Range r = getWordRangeAtCursor(separators, 0); + return (r == null) ? null : r.mWord; + } + + private int getCursorPosition() { + if (null == mIC) return INVALID_CURSOR_POSITION; + final ExtractedText extracted = mIC.getExtractedText(new ExtractedTextRequest(), 0); + if (extracted == null) { + return INVALID_CURSOR_POSITION; + } + return extracted.startOffset + extracted.selectionStart; + } + /** * Returns the text surrounding the cursor. * - * @param connection the InputConnection to the TextView * @param sep a string of characters that split words. * @param additionalPrecedingWordsCount the number of words before the current word that should * be included in the returned range * @return a range containing the text surrounding the cursor */ - public static Range getWordRangeAtCursor(InputConnection connection, String sep, - int additionalPrecedingWordsCount) { - if (connection == null || sep == null) { + public Range getWordRangeAtCursor(String sep, int additionalPrecedingWordsCount) { + if (mIC == null || sep == null) { return null; } - CharSequence before = connection.getTextBeforeCursor(1000, 0); - CharSequence after = connection.getTextAfterCursor(1000, 0); + CharSequence before = mIC.getTextBeforeCursor(1000, 0); + CharSequence after = mIC.getTextAfterCursor(1000, 0); if (before == null || after == null) { return null; } @@ -141,7 +287,7 @@ public class EditingUtils { } } - int cursor = getCursorPosition(connection); + int cursor = getCursorPosition(); if (start >= 0 && cursor + end <= after.length() + before.length()) { String word = before.toString().substring(start, before.length()) + after.toString().substring(0, end); @@ -151,68 +297,4 @@ public class EditingUtils { return null; } - private static boolean isSeparator(int code, String sep) { - return sep.indexOf(code) != -1; - } - - private static final Pattern spaceRegex = Pattern.compile("\\s+"); - - public static CharSequence getPreviousWord(InputConnection connection, - String sentenceSeperators) { - //TODO: Should fix this. This could be slow! - if (null == connection) return null; - CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0); - return getPreviousWord(prev, sentenceSeperators); - } - - // Get the word before the whitespace preceding the non-whitespace preceding the cursor. - // Also, it won't return words that end in a separator. - // Example : - // "abc def|" -> abc - // "abc def |" -> abc - // "abc def. |" -> abc - // "abc def . |" -> def - // "abc|" -> null - // "abc |" -> null - // "abc. def|" -> null - public static CharSequence getPreviousWord(CharSequence prev, String sentenceSeperators) { - if (prev == null) return null; - String[] w = spaceRegex.split(prev); - - // If we can't find two words, or we found an empty word, return null. - if (w.length < 2 || w[w.length - 2].length() <= 0) return null; - - // If ends in a separator, return null - char lastChar = w[w.length - 2].charAt(w[w.length - 2].length() - 1); - if (sentenceSeperators.contains(String.valueOf(lastChar))) return null; - - return w[w.length - 2]; - } - - public static CharSequence getThisWord(InputConnection connection, String sentenceSeperators) { - if (null == connection) return null; - final CharSequence prev = connection.getTextBeforeCursor(LOOKBACK_CHARACTER_NUM, 0); - return getThisWord(prev, sentenceSeperators); - } - - // Get the word immediately before the cursor, even if there is whitespace between it and - // the cursor - but not if there is punctuation. - // Example : - // "abc def|" -> def - // "abc def |" -> def - // "abc def. |" -> null - // "abc def . |" -> null - public static CharSequence getThisWord(CharSequence prev, String sentenceSeperators) { - if (prev == null) return null; - String[] w = spaceRegex.split(prev); - - // No word : return null - if (w.length < 1 || w[w.length - 1].length() <= 0) return null; - - // If ends in a separator, return null - char lastChar = w[w.length - 1].charAt(w[w.length - 1].length() - 1); - if (sentenceSeperators.contains(String.valueOf(lastChar))) return null; - - return w[w.length - 1]; - } } diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java index 4aae6a85e..dfe207cf2 100644 --- a/java/src/com/android/inputmethod/latin/SettingsValues.java +++ b/java/src/com/android/inputmethod/latin/SettingsValues.java @@ -29,7 +29,6 @@ import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; -import java.util.Map; /** * When you call the constructor of this class, you may want to change the current system locale by diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index 336a76f4b..68b7b913f 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -66,7 +66,7 @@ public class Suggest implements Dictionary.WordCallback { private static final boolean DBG = LatinImeLogger.sDBG; private boolean mHasMainDictionary; - private Dictionary mContactsDict; + private ContactsBinaryDictionary mContactsDict; private WhitelistDictionary mWhiteListDictionary; private final ConcurrentHashMap<String, Dictionary> mUnigramDictionaries = new ConcurrentHashMap<String, Dictionary>(); @@ -148,7 +148,7 @@ public class Suggest implements Dictionary.WordCallback { return mHasMainDictionary; } - public Dictionary getContactsDictionary() { + public ContactsBinaryDictionary getContactsDictionary() { return mContactsDict; } @@ -164,7 +164,7 @@ public class Suggest implements Dictionary.WordCallback { * Sets an optional user dictionary resource to be loaded. The user dictionary is consulted * before the main dictionary, if set. This refers to the system-managed user dictionary. */ - public void setUserDictionary(Dictionary userDictionary) { + public void setUserDictionary(UserBinaryDictionary userDictionary) { addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER, userDictionary); } @@ -173,13 +173,13 @@ public class Suggest implements Dictionary.WordCallback { * the contacts dictionary by passing null to this method. In this case no contacts dictionary * won't be used. */ - public void setContactsDictionary(Dictionary contactsDictionary) { + public void setContactsDictionary(ContactsBinaryDictionary contactsDictionary) { mContactsDict = contactsDictionary; addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary); addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_CONTACTS, contactsDictionary); } - public void setUserHistoryDictionary(Dictionary userHistoryDictionary) { + public void setUserHistoryDictionary(UserHistoryDictionary userHistoryDictionary) { addOrReplaceDictionary(mUnigramDictionaries, DICT_KEY_USER_HISTORY_UNIGRAM, userHistoryDictionary); addOrReplaceDictionary(mBigramDictionaries, DICT_KEY_USER_HISTORY_BIGRAM, diff --git a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedContactsDictionary.java b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedContactsDictionary.java deleted file mode 100644 index a8b871cdf..000000000 --- a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedContactsDictionary.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright (C) 2011 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.Context; - -import com.android.inputmethod.keyboard.ProximityInfo; - -public class SynchronouslyLoadedContactsDictionary extends ContactsDictionary { - private boolean mClosed; - - public SynchronouslyLoadedContactsDictionary(final Context context) { - super(context, Suggest.DIC_CONTACTS); - mClosed = false; - } - - @Override - public synchronized void getWords(final WordComposer codes, - final CharSequence prevWordForBigrams, final WordCallback callback, - final ProximityInfo proximityInfo) { - blockingReloadDictionaryIfRequired(); - getWordsInner(codes, prevWordForBigrams, callback, proximityInfo); - } - - @Override - public synchronized boolean isValidWord(CharSequence word) { - blockingReloadDictionaryIfRequired(); - return getWordFrequency(word) > -1; - } - - // Protect against multiple closing - @Override - public synchronized void close() { - // Actually with the current implementation of ContactsDictionary it's safe to close - // several times, so the following protection is really only for foolproofing - if (mClosed) return; - mClosed = true; - super.close(); - } -} diff --git a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserDictionary.java b/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserDictionary.java deleted file mode 100644 index 23a49c192..000000000 --- a/java/src/com/android/inputmethod/latin/SynchronouslyLoadedUserDictionary.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2011 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.Context; - -import com.android.inputmethod.keyboard.ProximityInfo; - -public class SynchronouslyLoadedUserDictionary extends UserDictionary { - private boolean mClosed; - - public SynchronouslyLoadedUserDictionary(final Context context, final String locale) { - this(context, locale, false); - } - - public SynchronouslyLoadedUserDictionary(final Context context, final String locale, - final boolean alsoUseMoreRestrictiveLocales) { - super(context, locale, alsoUseMoreRestrictiveLocales); - } - - @Override - public synchronized void getWords(final WordComposer codes, - final CharSequence prevWordForBigrams, final WordCallback callback, - final ProximityInfo proximityInfo) { - blockingReloadDictionaryIfRequired(); - getWordsInner(codes, prevWordForBigrams, callback, proximityInfo); - } - - @Override - public synchronized boolean isValidWord(CharSequence word) { - blockingReloadDictionaryIfRequired(); - return super.isValidWord(word); - } - - // Protect against multiple closing - @Override - public synchronized void close() { - if (mClosed) return; - mClosed = true; - super.close(); - } -} diff --git a/java/src/com/android/inputmethod/latin/UserDictionary.java b/java/src/com/android/inputmethod/latin/UserDictionary.java deleted file mode 100644 index c1efadd44..000000000 --- a/java/src/com/android/inputmethod/latin/UserDictionary.java +++ /dev/null @@ -1,225 +0,0 @@ -/* - * Copyright (C) 2008 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.ContentProviderClient; -import android.content.ContentResolver; -import android.content.Context; -import android.content.Intent; -import android.database.ContentObserver; -import android.database.Cursor; -import android.provider.UserDictionary.Words; -import android.text.TextUtils; - -import com.android.inputmethod.keyboard.ProximityInfo; - -import java.util.Arrays; - -// TODO: This class is superseded by {@link UserBinaryDictionary}. Should be cleaned up. -/** - * An expandable dictionary that stores the words in the user unigram dictionary. - * - * @deprecated Use {@link UserBinaryDictionary}. - */ -@Deprecated -public class UserDictionary extends ExpandableDictionary { - - // TODO: use Words.SHORTCUT when it's public in the SDK - final static String SHORTCUT = "shortcut"; - private static final String[] PROJECTION_QUERY = { - Words.WORD, - SHORTCUT, - Words.FREQUENCY, - }; - - // 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; - - public UserDictionary(final Context context, final String locale) { - this(context, locale, false); - } - - public UserDictionary(final Context context, final String locale, - final boolean alsoUseMoreRestrictiveLocales) { - super(context, Suggest.DIC_USER); - if (null == locale) throw new NullPointerException(); // Catch the error earlier - mLocale = locale; - mAlsoUseMoreRestrictiveLocales = alsoUseMoreRestrictiveLocales; - // Perform a managed query. The Activity will handle closing and re-querying the cursor - // when needed. - ContentResolver cres = context.getContentResolver(); - - mObserver = new ContentObserver(null) { - @Override - public void onChange(boolean self) { - setRequiresReload(true); - } - }; - cres.registerContentObserver(Words.CONTENT_URI, true, mObserver); - - loadDictionary(); - } - - @Override - public synchronized void close() { - if (mObserver != null) { - getContext().getContentResolver().unregisterContentObserver(mObserver); - mObserver = null; - } - super.close(); - } - - @Override - public void loadDictionaryAsync() { - // Split the locale. For example "en" => ["en"], "de_DE" => ["de", "DE"], - // "en_US_foo_bar_qux" => ["en", "US", "foo_bar_qux"] because of the limit of 3. - // This is correct for locale processing. - // For this example, we'll look at the "en_US_POSIX" case. - final String[] localeElements = - TextUtils.isEmpty(mLocale) ? new String[] {} : mLocale.split("_", 3); - final int length = localeElements.length; - - final StringBuilder request = new StringBuilder("(locale is NULL)"); - String localeSoFar = ""; - // At start, localeElements = ["en", "US", "POSIX"] ; localeSoFar = "" ; - // and request = "(locale is NULL)" - for (int i = 0; i < length; ++i) { - // i | localeSoFar | localeElements - // 0 | "" | ["en", "US", "POSIX"] - // 1 | "en_" | ["en", "US", "POSIX"] - // 2 | "en_US_" | ["en", "en_US", "POSIX"] - localeElements[i] = localeSoFar + localeElements[i]; - localeSoFar = localeElements[i] + "_"; - // i | request - // 0 | "(locale is NULL)" - // 1 | "(locale is NULL) or (locale=?)" - // 2 | "(locale is NULL) or (locale=?) or (locale=?)" - request.append(" or (locale=?)"); - } - // At the end, localeElements = ["en", "en_US", "en_US_POSIX"]; localeSoFar = en_US_POSIX_" - // and request = "(locale is NULL) or (locale=?) or (locale=?) or (locale=?)" - - final String[] requestArguments; - // If length == 3, we already have all the arguments we need (common prefix is meaningless - // inside variants - if (mAlsoUseMoreRestrictiveLocales && length < 3) { - request.append(" or (locale like ?)"); - // The following creates an array with one more (null) position - final String[] localeElementsWithMoreRestrictiveLocalesIncluded = - Arrays.copyOf(localeElements, length + 1); - localeElementsWithMoreRestrictiveLocalesIncluded[length] = - localeElements[length - 1] + "_%"; - requestArguments = localeElementsWithMoreRestrictiveLocalesIncluded; - // If for example localeElements = ["en"] - // then requestArguments = ["en", "en_%"] - // and request = (locale is NULL) or (locale=?) or (locale like ?) - // If localeElements = ["en", "en_US"] - // then requestArguments = ["en", "en_US", "en_US_%"] - } else { - requestArguments = localeElements; - } - final Cursor cursor = getContext().getContentResolver() - .query(Words.CONTENT_URI, PROJECTION_QUERY, request.toString(), - requestArguments, null); - try { - addWords(cursor); - } finally { - if (null != cursor) cursor.close(); - } - } - - public boolean isEnabled() { - final ContentResolver cr = getContext().getContentResolver(); - final ContentProviderClient client = cr.acquireContentProviderClient(Words.CONTENT_URI); - if (client != null) { - client.release(); - return true; - } else { - return false; - } - } - - /** - * 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. - * @param frequency the frequency of occurrence of the word. A frequency of 255 is considered - * the highest. - * @TODO use a higher or float range for frequency - */ - public synchronized void addWordToUserDictionary(final String word, final int frequency) { - // Force load the dictionary here synchronously - if (getRequiresReload()) loadDictionaryAsync(); - // 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() >= getMaxWordLength()) return; - - // TODO: Add an argument to the intent to specify the frequency. - 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); - getContext().startActivity(intent); - } - - @Override - public synchronized void getWords(final WordComposer codes, - final CharSequence prevWordForBigrams, final WordCallback callback, - final ProximityInfo proximityInfo) { - super.getWords(codes, prevWordForBigrams, callback, proximityInfo); - } - - @Override - public synchronized boolean isValidWord(CharSequence word) { - return super.isValidWord(word); - } - - private void addWords(Cursor cursor) { - clearDictionary(); - if (cursor == null) return; - final int maxWordLength = getMaxWordLength(); - if (cursor.moveToFirst()) { - final int indexWord = cursor.getColumnIndex(Words.WORD); - final int indexShortcut = cursor.getColumnIndex(SHORTCUT); - final int indexFrequency = cursor.getColumnIndex(Words.FREQUENCY); - while (!cursor.isAfterLast()) { - String word = cursor.getString(indexWord); - String shortcut = cursor.getString(indexShortcut); - int frequency = cursor.getInt(indexFrequency); - // Safeguard against adding really long words. Stack may overflow due - // to recursion - if (word.length() < maxWordLength) { - super.addWord(word, null, frequency); - } - if (null != shortcut && shortcut.length() < maxWordLength) { - super.addWord(shortcut, word, frequency); - } - cursor.moveToNext(); - } - } - } -} diff --git a/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java b/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java index 9c54e0b81..10f92d29e 100644 --- a/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java +++ b/java/src/com/android/inputmethod/latin/UserHistoryDictionary.java @@ -440,7 +440,7 @@ public class UserHistoryDictionary extends ExpandableDictionary { if (nw != null) { final ForgettingCurveParams fcp = nw.getFcParams(); final byte prevFc = word1Bigrams.get(word2); - final byte fc = (byte)fcp.getFc(); + final byte fc = fcp.getFc(); final boolean isValid = fcp.isValid(); if (prevFc > 0 && prevFc == fc) { // No need to update since we found no changes for this entry. diff --git a/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java b/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java index e5516dc62..6e71885cc 100644 --- a/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java +++ b/java/src/com/android/inputmethod/latin/UserHistoryForgettingCurveUtils.java @@ -50,7 +50,7 @@ public class UserHistoryForgettingCurveUtils { } private ForgettingCurveParams(long now, boolean isValid) { - this((int)pushCount((byte)0, isValid), now, now, isValid); + this(pushCount((byte)0, isValid), now, now, isValid); } /** This constructor is called when the user history bigram dictionary is being restored. */ @@ -199,20 +199,20 @@ public class UserHistoryForgettingCurveUtils { public static final int[][] SCORE_TABLE = new int[FC_LEVEL_MAX][ELAPSED_TIME_MAX + 1]; static { for (int i = 0; i < FC_LEVEL_MAX; ++i) { - final double initialFreq; + final float initialFreq; if (i >= 2) { - initialFreq = (double)FC_FREQ_MAX; + initialFreq = FC_FREQ_MAX; } else if (i == 1) { - initialFreq = (double)FC_FREQ_MAX / 2; + initialFreq = FC_FREQ_MAX / 2; } else if (i == 0) { - initialFreq = (double)FC_FREQ_MAX / 4; + initialFreq = FC_FREQ_MAX / 4; } else { continue; } for (int j = 0; j < ELAPSED_TIME_MAX; ++j) { - final double elapsedHour = j * ELAPSED_TIME_INTERVAL_HOURS; + final float elapsedHours = j * ELAPSED_TIME_INTERVAL_HOURS; final double freq = - initialFreq * Math.pow(initialFreq, elapsedHour / HALF_LIFE_HOURS); + initialFreq * Math.pow(initialFreq, elapsedHours / HALF_LIFE_HOURS); final int intFreq = Math.min(FC_FREQ_MAX, Math.max(0, (int)freq)); SCORE_TABLE[i][j] = intFreq; } diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java index 4178955bc..903b5a357 100644 --- a/java/src/com/android/inputmethod/latin/Utils.java +++ b/java/src/com/android/inputmethod/latin/Utils.java @@ -44,10 +44,8 @@ import java.io.IOException; import java.io.PrintWriter; import java.nio.channels.FileChannel; import java.text.SimpleDateFormat; -import java.util.Collections; import java.util.Date; import java.util.HashMap; -import java.util.Map; public class Utils { private Utils() { diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java index 89c59f809..2c3eee74c 100644 --- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java +++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java @@ -787,9 +787,9 @@ public class BinaryDictInputOutput { // (discretizedFrequency + 0.5) times this value to get the median value of the step, // which is the best approximation. This is how we get the most precise result with // only four bits. - final double stepSize = - (double)(MAX_TERMINAL_FREQUENCY - unigramFrequency) / (1.5 + MAX_BIGRAM_FREQUENCY); - final double firstStepStart = 1 + unigramFrequency + (stepSize / 2.0); + final float stepSize = + (MAX_TERMINAL_FREQUENCY - unigramFrequency) / (1.5f + MAX_BIGRAM_FREQUENCY); + final float firstStepStart = 1 + unigramFrequency + (stepSize / 2.0f); final int discretizedFrequency = (int)((bigramFrequency - firstStepStart) / stepSize); // If the bigram freq is less than half-a-step higher than the unigram freq, we get -1 // here. The best approximation would be the unigram freq itself, so we should not diff --git a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java index d34cad205..7fffc31c0 100644 --- a/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java +++ b/java/src/com/android/inputmethod/latin/spellcheck/AndroidSpellCheckerService.java @@ -30,18 +30,17 @@ import android.view.textservice.TextInfo; import com.android.inputmethod.compat.SuggestionsInfoCompatUtils; import com.android.inputmethod.keyboard.ProximityInfo; import com.android.inputmethod.latin.BinaryDictionary; +import com.android.inputmethod.latin.ContactsBinaryDictionary; import com.android.inputmethod.latin.Dictionary; import com.android.inputmethod.latin.Dictionary.WordCallback; import com.android.inputmethod.latin.DictionaryCollection; import com.android.inputmethod.latin.DictionaryFactory; -import com.android.inputmethod.latin.LatinIME; import com.android.inputmethod.latin.LocaleUtils; import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.StringUtils; import com.android.inputmethod.latin.SynchronouslyLoadedContactsBinaryDictionary; -import com.android.inputmethod.latin.SynchronouslyLoadedContactsDictionary; import com.android.inputmethod.latin.SynchronouslyLoadedUserBinaryDictionary; -import com.android.inputmethod.latin.SynchronouslyLoadedUserDictionary; +import com.android.inputmethod.latin.UserBinaryDictionary; import com.android.inputmethod.latin.WhitelistDictionary; import com.android.inputmethod.latin.WordComposer; @@ -73,11 +72,11 @@ public class AndroidSpellCheckerService extends SpellCheckerService private final static String[] EMPTY_STRING_ARRAY = new String[0]; private Map<String, DictionaryPool> mDictionaryPools = Collections.synchronizedMap(new TreeMap<String, DictionaryPool>()); - private Map<String, Dictionary> mUserDictionaries = - Collections.synchronizedMap(new TreeMap<String, Dictionary>()); + private Map<String, UserBinaryDictionary> mUserDictionaries = + Collections.synchronizedMap(new TreeMap<String, UserBinaryDictionary>()); private Map<String, Dictionary> mWhitelistDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>()); - private Dictionary mContactsDictionary; + private ContactsBinaryDictionary mContactsDictionary; // The threshold for a candidate to be offered as a suggestion. private float mSuggestionThreshold; @@ -154,13 +153,9 @@ public class AndroidSpellCheckerService extends SpellCheckerService private void startUsingContactsDictionaryLocked() { if (null == mContactsDictionary) { - if (LatinIME.USE_BINARY_CONTACTS_DICTIONARY) { - // TODO: use the right locale for each session - mContactsDictionary = - new SynchronouslyLoadedContactsBinaryDictionary(this, Locale.getDefault()); - } else { - mContactsDictionary = new SynchronouslyLoadedContactsDictionary(this); - } + // TODO: use the right locale for each session + mContactsDictionary = + new SynchronouslyLoadedContactsBinaryDictionary(this, Locale.getDefault()); } final Iterator<WeakReference<DictionaryCollection>> iterator = mDictionaryCollectionsList.iterator(); @@ -368,8 +363,9 @@ public class AndroidSpellCheckerService extends SpellCheckerService private void closeAllDictionaries() { final Map<String, DictionaryPool> oldPools = mDictionaryPools; mDictionaryPools = Collections.synchronizedMap(new TreeMap<String, DictionaryPool>()); - final Map<String, Dictionary> oldUserDictionaries = mUserDictionaries; - mUserDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>()); + final Map<String, UserBinaryDictionary> oldUserDictionaries = mUserDictionaries; + mUserDictionaries = + Collections.synchronizedMap(new TreeMap<String, UserBinaryDictionary>()); final Map<String, Dictionary> oldWhitelistDictionaries = mWhitelistDictionaries; mWhitelistDictionaries = Collections.synchronizedMap(new TreeMap<String, Dictionary>()); new Thread("spellchecker_close_dicts") { @@ -389,7 +385,7 @@ public class AndroidSpellCheckerService extends SpellCheckerService // The synchronously loaded contacts dictionary should have been in one // or several pools, but it is shielded against multiple closing and it's // safe to call it several times. - final Dictionary dictToClose = mContactsDictionary; + final ContactsBinaryDictionary dictToClose = mContactsDictionary; // TODO: revert to the concrete type when USE_BINARY_CONTACTS_DICTIONARY // is no longer needed mContactsDictionary = null; @@ -421,13 +417,9 @@ public class AndroidSpellCheckerService extends SpellCheckerService DictionaryFactory.createMainDictionaryFromManager(this, locale, true /* useFullEditDistance */); final String localeStr = locale.toString(); - Dictionary userDictionary = mUserDictionaries.get(localeStr); + UserBinaryDictionary userDictionary = mUserDictionaries.get(localeStr); if (null == userDictionary) { - if (LatinIME.USE_BINARY_USER_DICTIONARY) { - userDictionary = new SynchronouslyLoadedUserBinaryDictionary(this, localeStr, true); - } else { - userDictionary = new SynchronouslyLoadedUserDictionary(this, localeStr, true); - } + userDictionary = new SynchronouslyLoadedUserBinaryDictionary(this, localeStr, true); mUserDictionaries.put(localeStr, userDictionary); } dictionaryCollection.addDictionary(userDictionary); @@ -440,17 +432,11 @@ public class AndroidSpellCheckerService extends SpellCheckerService synchronized (mUseContactsLock) { if (mUseContactsDictionary) { if (null == mContactsDictionary) { - // TODO: revert to the concrete type when USE_BINARY_CONTACTS_DICTIONARY is no - // longer needed - if (LatinIME.USE_BINARY_CONTACTS_DICTIONARY) { - // TODO: use the right locale. We can't do it right now because the - // spell checker is reusing the contacts dictionary across sessions - // without regard for their locale, so we need to fix that first. - mContactsDictionary = new SynchronouslyLoadedContactsBinaryDictionary(this, - Locale.getDefault()); - } else { - mContactsDictionary = new SynchronouslyLoadedContactsDictionary(this); - } + // TODO: use the right locale. We can't do it right now because the + // spell checker is reusing the contacts dictionary across sessions + // without regard for their locale, so we need to fix that first. + mContactsDictionary = new SynchronouslyLoadedContactsBinaryDictionary(this, + Locale.getDefault()); } } dictionaryCollection.addDictionary(mContactsDictionary); |