diff options
Diffstat (limited to 'java/src')
12 files changed, 322 insertions, 70 deletions
diff --git a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java index a01c301ee..ab7bd4914 100644 --- a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java +++ b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java @@ -52,6 +52,10 @@ public final class InputMethodManagerCompatWrapper { sInstance.mImm = ImfUtils.getInputMethodManager(context); } + public InputMethodSubtype getCurrentInputMethodSubtype() { + return mImm.getCurrentInputMethodSubtype(); + } + public InputMethodSubtype getLastInputMethodSubtype() { return mImm.getLastInputMethodSubtype(); } @@ -65,6 +69,10 @@ public final class InputMethodManagerCompatWrapper { onlyCurrentIme); } + public void setInputMethodAndSubtype(IBinder token, String id, InputMethodSubtype subtype) { + mImm.setInputMethodAndSubtype(token, id, subtype); + } + public void showInputMethodPicker() { mImm.showInputMethodPicker(); } diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java index 5e111fb9a..631e647e8 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java +++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardState.java @@ -487,6 +487,11 @@ public final class KeyboardState { // After chording input while normal state. setShifted(UNSHIFT); } + // After chording input, automatic shift state may have been changed depending on + // what characters were input. + mShiftKeyState.onRelease(); + mSwitchActions.requestUpdatingShiftState(); + return; } else if (mAlphabetShiftState.isShiftLockShifted() && withSliding) { // In shift locked state, shift has been pressed and slid out to other key. setShiftLocked(true); diff --git a/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java b/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java index d12607721..939451899 100644 --- a/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java +++ b/java/src/com/android/inputmethod/latin/AdditionalSubtypeSettings.java @@ -85,6 +85,9 @@ public final class AdditionalSubtypeSettings extends PreferenceFragment { } static final class SubtypeLocaleAdapter extends ArrayAdapter<SubtypeLocaleItem> { + private static final String TAG = SubtypeLocaleAdapter.class.getSimpleName(); + private static final boolean DEBUG_SUBTYPE_ID = false; + public SubtypeLocaleAdapter(final Context context) { super(context, android.R.layout.simple_spinner_item); setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); @@ -94,6 +97,11 @@ public final class AdditionalSubtypeSettings extends PreferenceFragment { final int count = imi.getSubtypeCount(); for (int i = 0; i < count; i++) { final InputMethodSubtype subtype = imi.getSubtypeAt(i); + if (DEBUG_SUBTYPE_ID) { + android.util.Log.d(TAG, String.format("%-6s 0x%08x %11d %s", + subtype.getLocale(), subtype.hashCode(), subtype.hashCode(), + SubtypeLocale.getSubtypeDisplayName(subtype, context.getResources()))); + } if (subtype.containsExtraValueKey(ASCII_CAPABLE)) { items.add(createItem(context, subtype.getLocale())); } diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java index b0b65edb6..9a3f88f52 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java @@ -154,6 +154,9 @@ public final class BinaryDictionaryFileDumper { for (int mode = MODE_MIN; mode <= MODE_MAX; ++mode) { InputStream originalSourceStream = null; InputStream inputStream = null; + InputStream uncompressedStream = null; + InputStream decryptedStream = null; + BufferedInputStream bufferedStream = null; File outputFile = null; FileOutputStream outputStream = null; AssetFileDescriptor afd = null; @@ -173,18 +176,19 @@ public final class BinaryDictionaryFileDumper { // Get the appropriate decryption method for this try switch (mode) { case COMPRESSED_CRYPTED_COMPRESSED: - inputStream = FileTransforms.getUncompressedStream( - FileTransforms.getDecryptedStream( - FileTransforms.getUncompressedStream( - originalSourceStream))); + uncompressedStream = + FileTransforms.getUncompressedStream(originalSourceStream); + decryptedStream = FileTransforms.getDecryptedStream(uncompressedStream); + inputStream = FileTransforms.getUncompressedStream(decryptedStream); break; case CRYPTED_COMPRESSED: - inputStream = FileTransforms.getUncompressedStream( - FileTransforms.getDecryptedStream(originalSourceStream)); + decryptedStream = FileTransforms.getDecryptedStream(originalSourceStream); + inputStream = FileTransforms.getUncompressedStream(decryptedStream); break; case COMPRESSED_CRYPTED: - inputStream = FileTransforms.getDecryptedStream( - FileTransforms.getUncompressedStream(originalSourceStream)); + uncompressedStream = + FileTransforms.getUncompressedStream(originalSourceStream); + inputStream = FileTransforms.getDecryptedStream(uncompressedStream); break; case COMPRESSED_ONLY: inputStream = FileTransforms.getUncompressedStream(originalSourceStream); @@ -195,8 +199,9 @@ public final class BinaryDictionaryFileDumper { case NONE: inputStream = originalSourceStream; break; - } - checkMagicAndCopyFileTo(new BufferedInputStream(inputStream), outputStream); + } + bufferedStream = new BufferedInputStream(inputStream); + checkMagicAndCopyFileTo(bufferedStream, outputStream); outputStream.flush(); outputStream.close(); final File finalFile = new File(finalFileName); @@ -228,8 +233,11 @@ public final class BinaryDictionaryFileDumper { try { // inputStream.close() will close afd, we should not call afd.close(). if (null != inputStream) inputStream.close(); + if (null != uncompressedStream) uncompressedStream.close(); + if (null != decryptedStream) decryptedStream.close(); + if (null != bufferedStream) bufferedStream.close(); } catch (Exception e) { - Log.e(TAG, "Exception while closing a cross-process file descriptor : " + e); + Log.e(TAG, "Exception while closing a file descriptor : " + e); } try { if (null != outputStream) outputStream.close(); diff --git a/java/src/com/android/inputmethod/latin/InputAttributes.java b/java/src/com/android/inputmethod/latin/InputAttributes.java index 2f7608a03..422fc72d3 100644 --- a/java/src/com/android/inputmethod/latin/InputAttributes.java +++ b/java/src/com/android/inputmethod/latin/InputAttributes.java @@ -29,6 +29,7 @@ public final class InputAttributes { final public boolean mInputTypeNoAutoCorrect; final public boolean mIsSettingsSuggestionStripOn; final public boolean mApplicationSpecifiedCompletionOn; + final public boolean mShouldInsertSpacesAutomatically; final private int mInputType; public InputAttributes(final EditorInfo editorInfo, final boolean isFullscreenMode) { @@ -54,6 +55,7 @@ public final class InputAttributes { mIsSettingsSuggestionStripOn = false; mInputTypeNoAutoCorrect = false; mApplicationSpecifiedCompletionOn = false; + mShouldInsertSpacesAutomatically = false; } else { final int variation = inputType & InputType.TYPE_MASK_VARIATION; final boolean flagNoSuggestions = @@ -65,6 +67,7 @@ public final class InputAttributes { final boolean flagAutoComplete = 0 != (inputType & InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE); + // TODO: Have a helper method in InputTypeUtils // Make sure that passwords are not displayed in {@link SuggestionStripView}. if (InputTypeUtils.isPasswordInputType(inputType) || InputTypeUtils.isVisiblePasswordInputType(inputType) @@ -78,6 +81,8 @@ public final class InputAttributes { mIsSettingsSuggestionStripOn = true; } + mShouldInsertSpacesAutomatically = InputTypeUtils.isAutoSpaceFriendlyType(inputType); + // If it's a browser edit field and auto correct is not ON explicitly, then // disable auto correction, but keep suggestions on. // If NO_SUGGESTIONS is set, don't do prediction. diff --git a/java/src/com/android/inputmethod/latin/InputTypeUtils.java b/java/src/com/android/inputmethod/latin/InputTypeUtils.java index 500866a13..9a4503bf4 100644 --- a/java/src/com/android/inputmethod/latin/InputTypeUtils.java +++ b/java/src/com/android/inputmethod/latin/InputTypeUtils.java @@ -29,31 +29,37 @@ public final class InputTypeUtils implements InputType { TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD; private static final int TEXT_VISIBLE_PASSWORD_INPUT_TYPE = TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD; + private static final int[] SUPPRESSING_AUTO_SPACES_FIELD_VARIATION = { + InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS, + InputType.TYPE_TEXT_VARIATION_PASSWORD, + InputType.TYPE_TEXT_VARIATION_URI, + InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD, + InputType.TYPE_TEXT_VARIATION_WEB_PASSWORD }; private InputTypeUtils() { // This utility class is not publicly instantiable. } - private static boolean isWebEditTextInputType(int inputType) { + private static boolean isWebEditTextInputType(final int inputType) { return inputType == (TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_EDIT_TEXT); } - private static boolean isWebPasswordInputType(int inputType) { + private static boolean isWebPasswordInputType(final int inputType) { return WEB_TEXT_PASSWORD_INPUT_TYPE != 0 && inputType == WEB_TEXT_PASSWORD_INPUT_TYPE; } - private static boolean isWebEmailAddressInputType(int inputType) { + private static boolean isWebEmailAddressInputType(final int inputType) { return WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE != 0 && inputType == WEB_TEXT_EMAIL_ADDRESS_INPUT_TYPE; } - private static boolean isNumberPasswordInputType(int inputType) { + private static boolean isNumberPasswordInputType(final int inputType) { return NUMBER_PASSWORD_INPUT_TYPE != 0 && inputType == NUMBER_PASSWORD_INPUT_TYPE; } - private static boolean isTextPasswordInputType(int inputType) { + private static boolean isTextPasswordInputType(final int inputType) { return inputType == TEXT_PASSWORD_INPUT_TYPE; } @@ -61,12 +67,12 @@ public final class InputTypeUtils implements InputType { return variation == TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS; } - public static boolean isEmailVariation(int variation) { + public static boolean isEmailVariation(final int variation) { return variation == TYPE_TEXT_VARIATION_EMAIL_ADDRESS || isWebEmailAddressVariation(variation); } - public static boolean isWebInputType(int inputType) { + public static boolean isWebInputType(final int inputType) { final int maskedInputType = inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION); return isWebEditTextInputType(maskedInputType) || isWebPasswordInputType(maskedInputType) @@ -74,7 +80,7 @@ public final class InputTypeUtils implements InputType { } // Please refer to TextView.isPasswordInputType - public static boolean isPasswordInputType(int inputType) { + public static boolean isPasswordInputType(final int inputType) { final int maskedInputType = inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION); return isTextPasswordInputType(maskedInputType) || isWebPasswordInputType(maskedInputType) @@ -82,9 +88,18 @@ public final class InputTypeUtils implements InputType { } // Please refer to TextView.isVisiblePasswordInputType - public static boolean isVisiblePasswordInputType(int inputType) { + public static boolean isVisiblePasswordInputType(final int inputType) { final int maskedInputType = inputType & (TYPE_MASK_CLASS | TYPE_MASK_VARIATION); return maskedInputType == TEXT_VISIBLE_PASSWORD_INPUT_TYPE; } + + public static boolean isAutoSpaceFriendlyType(final int inputType) { + if (TYPE_CLASS_TEXT != (TYPE_MASK_CLASS & inputType)) return false; + final int variation = TYPE_MASK_VARIATION & inputType; + for (final int fieldVariation : SUPPRESSING_AUTO_SPACES_FIELD_VARIATION) { + if (variation == fieldVariation) return false; + } + return true; + } } diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index cf7eea70b..e2a76a456 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -141,7 +141,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction private SharedPreferences mPrefs; /* package for tests */ final KeyboardSwitcher mKeyboardSwitcher; private final SubtypeSwitcher mSubtypeSwitcher; - private boolean mShouldSwitchToLastSubtype = true; + private final SubtypeState mSubtypeState = new SubtypeState(); private boolean mIsMainDictionaryAvailable; private UserBinaryDictionary mUserDictionary; @@ -149,6 +149,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction private boolean mIsUserDictionaryAvailable; private LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD; + private PositionalInfoForUserDictPendingAddition + mPositionalInfoForUserDictPendingAddition = null; private final WordComposer mWordComposer = new WordComposer(); private RichInputConnection mConnection = new RichInputConnection(this); @@ -365,6 +367,34 @@ public final class LatinIME extends InputMethodService implements KeyboardAction } } + static final class SubtypeState { + private InputMethodSubtype mLastActiveSubtype; + private boolean mCurrentSubtypeUsed; + + public void currentSubtypeUsed() { + mCurrentSubtypeUsed = true; + } + + public void switchSubtype(final IBinder token, final InputMethodManagerCompatWrapper imm, + final Context context) { + final InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype(); + final InputMethodSubtype lastActiveSubtype = mLastActiveSubtype; + final boolean currentSubtypeUsed = mCurrentSubtypeUsed; + if (currentSubtypeUsed) { + mLastActiveSubtype = currentSubtype; + mCurrentSubtypeUsed = false; + } + if (currentSubtypeUsed + && ImfUtils.checkIfSubtypeBelongsToThisImeAndEnabled(context, lastActiveSubtype) + && !currentSubtype.equals(lastActiveSubtype)) { + final String id = ImfUtils.getInputMethodIdOfThisIme(context); + imm.setInputMethodAndSubtype(token, id, lastActiveSubtype); + return; + } + imm.switchToNextInputMethod(token, true /* onlyCurrentIme */); + } + } + public LatinIME() { super(); mSubtypeSwitcher = SubtypeSwitcher.getInstance(); @@ -683,8 +713,6 @@ public final class LatinIME extends InputMethodService implements KeyboardAction accessUtils.onStartInputViewInternal(mainKeyboardView, editorInfo, restarting); } - final boolean selectionChanged = mLastSelectionStart != editorInfo.initialSelStart - || mLastSelectionEnd != editorInfo.initialSelEnd; final boolean inputTypeChanged = !mCurrentSettings.isSameInputType(editorInfo); final boolean isDifferentTextField = !restarting || inputTypeChanged; if (isDifferentTextField) { @@ -704,21 +732,17 @@ public final class LatinIME extends InputMethodService implements KeyboardAction updateFullscreenMode(); mApplicationSpecifiedCompletions = null; - if (isDifferentTextField || selectionChanged) { - // If the selection changed, we reset the input state. Essentially, we come here with - // restarting == true when the app called setText() or similar. We should reset the - // state if the app set the text to something else, but keep it if it set a suggestion - // or something. - mEnteredText = null; - resetComposingState(true /* alsoResetLastComposedWord */); - mDeleteCount = 0; - mSpaceState = SPACE_STATE_NONE; + // The app calling setText() has the effect of clearing the composing + // span, so we should reset our state unconditionally, even if restarting is true. + mEnteredText = null; + resetComposingState(true /* alsoResetLastComposedWord */); + mDeleteCount = 0; + mSpaceState = SPACE_STATE_NONE; - if (mSuggestionStripView != null) { - // This will set the punctuation suggestions if next word suggestion is off; - // otherwise it will clear the suggestion strip. - setPunctuationSuggestions(); - } + if (mSuggestionStripView != null) { + // This will set the punctuation suggestions if next word suggestion is off; + // otherwise it will clear the suggestion strip. + setPunctuationSuggestions(); } mConnection.resetCachesUponCursorMove(editorInfo.initialSelStart); @@ -736,20 +760,17 @@ public final class LatinIME extends InputMethodService implements KeyboardAction // TODO: Come up with a more comprehensive way to reset the keyboard layout when // a keyboard layout set doesn't get reloaded in this method. switcher.resetKeyboardStateToAlphabet(); + // In apps like Talk, we come here when the text is sent and the field gets emptied and + // we need to re-evaluate the shift state, but not the whole layout which would be + // disruptive. + // Space state must be updated before calling updateShiftState + switcher.updateShiftState(); } setSuggestionStripShownInternal( isSuggestionsStripVisible(), /* needsInputViewShown */ false); mLastSelectionStart = editorInfo.initialSelStart; mLastSelectionEnd = editorInfo.initialSelEnd; - // If we come here something in the text state is very likely to have changed. - // We should update the shift state regardless of whether we are restarting or not, because - // this is not perceived as a layout change that may be disruptive like we may have with - // switcher.loadKeyboard; in apps like Talk, we come here when the text is sent and the - // field gets emptied and we need to re-evaluate the shift state, but not the whole layout - // which would be disruptive. - // Space state must be updated before calling updateShiftState - mKeyboardSwitcher.updateShiftState(); mHandler.cancelUpdateSuggestionStrip(); mHandler.cancelDoubleSpacesTimer(); @@ -761,6 +782,19 @@ public final class LatinIME extends InputMethodService implements KeyboardAction mainKeyboardView.setGesturePreviewMode(mCurrentSettings.mGesturePreviewTrailEnabled, mCurrentSettings.mGestureFloatingPreviewTextEnabled); + // If we have a user dictionary addition in progress, we should check now if we should + // replace the previously committed string with the word that has actually been added + // to the user dictionary. + if (null != mPositionalInfoForUserDictPendingAddition + && mPositionalInfoForUserDictPendingAddition.tryReplaceWithActualWord( + mConnection, editorInfo, mLastSelectionEnd)) { + mPositionalInfoForUserDictPendingAddition = null; + } + // If tryReplaceWithActualWord returns false, we don't know what word was + // added to the user dictionary yet, so we keep the data and defer processing. The word will + // be replaced when the user dictionary reports back with the actual word, which ends + // up calling #onWordAddedToUserDictionary() in this class. + if (TRACE) Debug.startMethodTracing("/data/trace/latinime"); } @@ -891,6 +925,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction // Make a note of the cursor position mLastSelectionStart = newSelStart; mLastSelectionEnd = newSelEnd; + mSubtypeState.currentSubtypeUsed(); } /** @@ -1188,9 +1223,31 @@ public final class LatinIME extends InputMethodService implements KeyboardAction // Callback for the {@link SuggestionStripView}, to call when the "add to dictionary" hint is // pressed. @Override - public boolean addWordToUserDictionary(final String word) { + public void addWordToUserDictionary(final String word) { + if (TextUtils.isEmpty(word)) { + // Probably never supposed to happen, but just in case. + mPositionalInfoForUserDictPendingAddition = null; + return; + } + mPositionalInfoForUserDictPendingAddition = + new PositionalInfoForUserDictPendingAddition( + word, mLastSelectionEnd, getCurrentInputEditorInfo()); mUserDictionary.addWordToUserDictionary(word, 128); - return true; + } + + public void onWordAddedToUserDictionary(final String newSpelling) { + // If word was added but not by us, bail out + if (null == mPositionalInfoForUserDictPendingAddition) return; + if (mWordComposer.isComposingWord()) { + // We are late... give up and return + mPositionalInfoForUserDictPendingAddition = null; + return; + } + mPositionalInfoForUserDictPendingAddition.setActualWordBeingAdded(newSpelling); + if (mPositionalInfoForUserDictPendingAddition.tryReplaceWithActualWord( + mConnection, getCurrentInputEditorInfo(), mLastSelectionEnd)) { + mPositionalInfoForUserDictPendingAddition = null; + } } private static boolean isAlphabet(final int code) { @@ -1239,19 +1296,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction mImm.switchToNextInputMethod(token, false /* onlyCurrentIme */); return; } - if (mShouldSwitchToLastSubtype) { - final InputMethodSubtype lastSubtype = mImm.getLastInputMethodSubtype(); - final boolean lastSubtypeBelongsToThisIme = - ImfUtils.checkIfSubtypeBelongsToThisImeAndEnabled(this, lastSubtype); - if (lastSubtypeBelongsToThisIme && mImm.switchToLastInputMethod(token)) { - mShouldSwitchToLastSubtype = false; - } else { - mImm.switchToNextInputMethod(token, true /* onlyCurrentIme */); - mShouldSwitchToLastSubtype = true; - } - } else { - mImm.switchToNextInputMethod(token, true /* onlyCurrentIme */); - } + mSubtypeState.switchSubtype(token, mImm, this); } private void sendDownUpKeyEventForBackwardCompatibility(final int code) { @@ -1320,7 +1365,6 @@ public final class LatinIME extends InputMethodService implements KeyboardAction handleBackspace(spaceState); mDeleteCount++; mExpectingUpdateSelection = true; - mShouldSwitchToLastSubtype = true; LatinImeLogger.logOnDelete(x, y); break; case Keyboard.CODE_SHIFT: @@ -1376,7 +1420,6 @@ public final class LatinIME extends InputMethodService implements KeyboardAction handleCharacter(primaryCode, keyX, keyY, spaceState); } mExpectingUpdateSelection = true; - mShouldSwitchToLastSubtype = true; break; } switcher.onCodeInput(primaryCode); @@ -1405,7 +1448,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction mHandler.postUpdateSuggestionStrip(); final CharSequence text = specificTldProcessingOnTextInput(rawText); if (SPACE_STATE_PHANTOM == mSpaceState) { - sendKeyCodePoint(Keyboard.CODE_SPACE); + promotePhantomSpace(); } mConnection.commitText(text, 1); mConnection.endBatchEdit(); @@ -1568,7 +1611,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction mWordComposer.setBatchInputWord(batchInputText); mConnection.beginBatchEdit(); if (SPACE_STATE_PHANTOM == mSpaceState) { - sendKeyCodePoint(Keyboard.CODE_SPACE); + promotePhantomSpace(); } mConnection.setComposingText(batchInputText, 1); mExpectingUpdateSelection = true; @@ -1724,7 +1767,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction // Sanity check throw new RuntimeException("Should not be composing here"); } - sendKeyCodePoint(Keyboard.CODE_SPACE); + promotePhantomSpace(); } // NOTE: isCursorTouchingWord() is a blocking IPC call, so it often takes several @@ -1802,7 +1845,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction if (SPACE_STATE_PHANTOM == spaceState && mCurrentSettings.isPhantomSpacePromotingSymbol(primaryCode)) { - sendKeyCodePoint(Keyboard.CODE_SPACE); + promotePhantomSpace(); } sendKeyCodePoint(primaryCode); @@ -2071,7 +2114,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction int firstChar = Character.codePointAt(suggestion, 0); if ((!mCurrentSettings.isWeakSpaceStripper(firstChar)) && (!mCurrentSettings.isWeakSpaceSwapper(firstChar))) { - sendKeyCodePoint(Keyboard.CODE_SPACE); + promotePhantomSpace(); } } @@ -2249,6 +2292,13 @@ public final class LatinIME extends InputMethodService implements KeyboardAction mHandler.postUpdateSuggestionStrip(); } + // This essentially inserts a space, and that's it. + public void promotePhantomSpace() { + if (mCurrentSettings.shouldInsertSpacesAutomatically()) { + sendKeyCodePoint(Keyboard.CODE_SPACE); + } + } + // Used by the RingCharBuffer public boolean isWordSeparator(final int code) { return mCurrentSettings.isWordSeparator(code); diff --git a/java/src/com/android/inputmethod/latin/PositionalInfoForUserDictPendingAddition.java b/java/src/com/android/inputmethod/latin/PositionalInfoForUserDictPendingAddition.java new file mode 100644 index 000000000..8a2d22256 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/PositionalInfoForUserDictPendingAddition.java @@ -0,0 +1,98 @@ +/* + * 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 + * 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.view.inputmethod.EditorInfo; + +/** + * Holder class for data about a word already committed but that may still be edited. + * + * When the user chooses to add a word to the user dictionary by pressing the appropriate + * suggestion, a dialog is presented to give a chance to edit the word before it is actually + * registered as a user dictionary word. If the word is actually modified, the IME needs to + * go back and replace the word that was committed with the amended version. + * The word we need to replace with will only be known after it's actually committed, so + * the IME needs to take a note of what it has to replace and where it is. + * This class encapsulates this data. + */ +public class PositionalInfoForUserDictPendingAddition { + final private String mOriginalWord; + final private int mCursorPos; // Position of the cursor after the word + final private EditorInfo mEditorInfo; // On what binding this has been added + private String mActualWordBeingAdded; + + public PositionalInfoForUserDictPendingAddition(final String word, final int cursorPos, + final EditorInfo editorInfo) { + mOriginalWord = word; + mCursorPos = cursorPos; + mEditorInfo = editorInfo; + } + + public void setActualWordBeingAdded(final String actualWordBeingAdded) { + mActualWordBeingAdded = actualWordBeingAdded; + } + + /** + * Try to replace the string at the remembered position with the actual word being added. + * + * After the user validated the word being added, the IME has to replace the old version + * (which has been committed in the text view) with the amended version if it's different. + * This method tries to do that, but may fail because the IME is not yet ready to do so - + * for example, it is still waiting for the new string, or it is waiting to return to the text + * view in which the amendment should be made. In these cases, we should keep the data + * and wait until all conditions are met. + * This method returns true if the replacement has been successfully made and this data + * can be forgotten; it returns false if the replacement can't be made yet and we need to + * keep this until a later time. + * The IME knows about the actual word being added through a callback called by the + * user dictionary facility of the device. When this callback comes, the keyboard may still + * be connected to the edition dialog, or it may have already returned to the original text + * field. Replacement has to work in both cases. + * Accordingly, this method is called at two different points in time : upon getting the + * event that a new word was added to the user dictionary, and upon starting up in a + * new text field. + * @param connection The RichInputConnection through which to contact the editor. + * @param editorInfo Information pertaining to the editor we are currently in. + * @param currentCursorPosition The current cursor position, for checking purposes. + * @return true if the edit has been successfully made, false if we need to try again later + */ + public boolean tryReplaceWithActualWord(final RichInputConnection connection, + final EditorInfo editorInfo, final int currentCursorPosition) { + // If we still don't know the actual word being added, we need to try again later. + if (null == mActualWordBeingAdded) return false; + // The entered text and the registered text were the same anyway : we can + // return success right away even if focus has not returned yet to the text field we + // want to amend. + if (mActualWordBeingAdded.equals(mOriginalWord)) return true; + // Not the same text field : we need to try again later. This happens when the addition + // is reported by the user dictionary provider before the focus has moved back to the + // original text view, so the IME is still in the text view of the dialog and has no way to + // edit the original text view at this time. + if (!mEditorInfo.packageName.equals(editorInfo.packageName) + || mEditorInfo.fieldId != editorInfo.fieldId) { + return false; + } + // Same text field, but not the same cursor position : we give up, so we return success + // so that it won't be tried again + if (currentCursorPosition != mCursorPos) return true; + // We have made all the checks : do the replacement and report success + connection.setComposingRegion(currentCursorPosition - mOriginalWord.length(), + currentCursorPosition); + connection.commitText(mActualWordBeingAdded, mActualWordBeingAdded.length()); + return true; + } +} diff --git a/java/src/com/android/inputmethod/latin/RichInputConnection.java b/java/src/com/android/inputmethod/latin/RichInputConnection.java index 21441369e..3b732278b 100644 --- a/java/src/com/android/inputmethod/latin/RichInputConnection.java +++ b/java/src/com/android/inputmethod/latin/RichInputConnection.java @@ -145,7 +145,8 @@ public final class RichInputConnection { mCurrentCursorPosition = newCursorPosition; mComposingText.setLength(0); mCommittedTextBeforeComposingText.setLength(0); - mCommittedTextBeforeComposingText.append(getTextBeforeCursor(DEFAULT_TEXT_CACHE_SIZE, 0)); + final CharSequence textBeforeCursor = getTextBeforeCursor(DEFAULT_TEXT_CACHE_SIZE, 0); + if (null != textBeforeCursor) mCommittedTextBeforeComposingText.append(textBeforeCursor); mCharAfterTheCursor = getTextAfterCursor(1, 0); if (null != mIC) { mIC.finishComposingText(); @@ -338,6 +339,24 @@ public final class RichInputConnection { } } + public void setComposingRegion(final int start, final int end) { + if (DEBUG_BATCH_NESTING) checkBatchEdit(); + if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); + mCurrentCursorPosition = end; + final CharSequence textBeforeCursor = + getTextBeforeCursor(DEFAULT_TEXT_CACHE_SIZE + (end - start), 0); + final int indexOfStartOfComposingText = + Math.max(textBeforeCursor.length() - (end - start), 0); + mComposingText.append(textBeforeCursor.subSequence(indexOfStartOfComposingText, + textBeforeCursor.length())); + mCommittedTextBeforeComposingText.setLength(0); + mCommittedTextBeforeComposingText.append( + textBeforeCursor.subSequence(0, indexOfStartOfComposingText)); + if (null != mIC) { + mIC.setComposingRegion(start, end); + } + } + public void setComposingText(final CharSequence text, final int i) { if (DEBUG_BATCH_NESTING) checkBatchEdit(); if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java index 2a778aa0d..2f49fe92e 100644 --- a/java/src/com/android/inputmethod/latin/SettingsValues.java +++ b/java/src/com/android/inputmethod/latin/SettingsValues.java @@ -271,6 +271,10 @@ public final class SettingsValues { return mPhantomSpacePromotingSymbols.contains(String.valueOf((char)code)); } + public boolean shouldInsertSpacesAutomatically() { + return mInputAttributes.mShouldInsertSpacesAutomatically; + } + private static boolean isAutoCorrectEnabled(final Resources res, final String currentAutoCorrectionSetting) { final String autoCorrectionOff = res.getString( diff --git a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java index 60e6fa127..8570746b1 100644 --- a/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/UserBinaryDictionary.java @@ -18,10 +18,12 @@ package com.android.inputmethod.latin; import android.content.ContentProviderClient; import android.content.ContentResolver; +import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.database.ContentObserver; import android.database.Cursor; +import android.net.Uri; import android.provider.UserDictionary.Words; import android.text.TextUtils; @@ -87,8 +89,25 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { mObserver = new ContentObserver(null) { @Override - public void onChange(boolean self) { + public void onChange(final boolean self) { + // This hook is deprecated as of API level 16, but should still be supported for + // cases where the IME is running on an older version of the platform. + onChange(self, null); + } + // The following hook is only available as of API level 16, and as such it will only + // work on JellyBean+ devices. On older versions of the platform, the hook + // above will be called instead. + @Override + public void onChange(final boolean self, final Uri uri) { setRequiresReload(true); + // We want to report back to Latin IME in case the user just entered the word. + // If the user changed the word in the dialog box, then we want to replace + // what was entered in the text field. + if (null == uri || !(context instanceof LatinIME)) return; + final long changedRowId = ContentUris.parseId(uri); + if (-1 == changedRowId) return; // Unknown content... Not sure why we're here + final String changedWord = getChangedWordForUri(uri); + ((LatinIME)context).onWordAddedToUserDictionary(changedWord); } }; cres.registerContentObserver(Words.CONTENT_URI, true, mObserver); @@ -96,6 +115,19 @@ public class UserBinaryDictionary extends ExpandableBinaryDictionary { loadDictionary(); } + private String getChangedWordForUri(final Uri uri) { + final Cursor cursor = mContext.getContentResolver().query(uri, + PROJECTION_QUERY, null, null, null); + if (cursor == null) return null; + try { + if (!cursor.moveToFirst()) return null; + final int indexWord = cursor.getColumnIndex(Words.WORD); + return cursor.getString(indexWord); + } finally { + cursor.close(); + } + } + @Override public synchronized void close() { if (mObserver != null) { diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java index e926fa209..a1c95ad8a 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java +++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java @@ -73,7 +73,7 @@ import java.util.ArrayList; public final class SuggestionStripView extends RelativeLayout implements OnClickListener, OnLongClickListener { public interface Listener { - public boolean addWordToUserDictionary(String word); + public void addWordToUserDictionary(String word); public void pickSuggestionManually(int index, CharSequence word); } |