2013-12-18 09:08:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin.inputlogic;
|
|
|
|
|
2014-08-25 00:41:49 +00:00
|
|
|
import android.graphics.Color;
|
2013-12-19 05:34:35 +00:00
|
|
|
import android.os.SystemClock;
|
2014-01-09 09:36:23 +00:00
|
|
|
import android.text.SpannableString;
|
2014-08-27 07:32:33 +00:00
|
|
|
import android.text.Spanned;
|
2013-12-19 08:43:22 +00:00
|
|
|
import android.text.TextUtils;
|
2014-08-27 07:32:33 +00:00
|
|
|
import android.text.style.BackgroundColorSpan;
|
2013-12-20 09:05:35 +00:00
|
|
|
import android.text.style.SuggestionSpan;
|
2013-12-19 07:07:24 +00:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.KeyCharacterMap;
|
|
|
|
import android.view.KeyEvent;
|
2013-12-19 09:18:18 +00:00
|
|
|
import android.view.inputmethod.CorrectionInfo;
|
2013-12-19 05:34:35 +00:00
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
|
2013-12-19 07:07:24 +00:00
|
|
|
import com.android.inputmethod.compat.SuggestionSpanUtils;
|
2014-03-12 07:41:45 +00:00
|
|
|
import com.android.inputmethod.event.Event;
|
2014-03-04 06:29:54 +00:00
|
|
|
import com.android.inputmethod.event.InputTransaction;
|
2015-02-25 19:27:48 +00:00
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
2013-12-19 05:34:35 +00:00
|
|
|
import com.android.inputmethod.keyboard.KeyboardSwitcher;
|
2013-12-20 09:05:35 +00:00
|
|
|
import com.android.inputmethod.latin.Dictionary;
|
2014-05-23 00:30:55 +00:00
|
|
|
import com.android.inputmethod.latin.DictionaryFacilitator;
|
2013-12-18 09:08:35 +00:00
|
|
|
import com.android.inputmethod.latin.LastComposedWord;
|
|
|
|
import com.android.inputmethod.latin.LatinIME;
|
2014-09-29 01:52:18 +00:00
|
|
|
import com.android.inputmethod.latin.NgramContext;
|
2013-12-18 09:08:35 +00:00
|
|
|
import com.android.inputmethod.latin.RichInputConnection;
|
|
|
|
import com.android.inputmethod.latin.Suggest;
|
2013-12-19 13:08:31 +00:00
|
|
|
import com.android.inputmethod.latin.Suggest.OnGetSuggestedWordsCallback;
|
2013-12-18 09:08:35 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords;
|
2013-12-20 09:05:35 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
2013-12-18 09:08:35 +00:00
|
|
|
import com.android.inputmethod.latin.WordComposer;
|
2014-10-23 09:37:32 +00:00
|
|
|
import com.android.inputmethod.latin.common.Constants;
|
2014-10-29 02:37:21 +00:00
|
|
|
import com.android.inputmethod.latin.common.InputPointers;
|
2014-10-28 12:31:09 +00:00
|
|
|
import com.android.inputmethod.latin.common.StringUtils;
|
2014-07-17 01:41:46 +00:00
|
|
|
import com.android.inputmethod.latin.define.DebugFlags;
|
2013-12-19 07:07:24 +00:00
|
|
|
import com.android.inputmethod.latin.settings.SettingsValues;
|
2014-07-08 07:36:06 +00:00
|
|
|
import com.android.inputmethod.latin.settings.SettingsValuesForSuggestion;
|
2014-01-09 08:27:33 +00:00
|
|
|
import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
|
2014-01-24 14:17:40 +00:00
|
|
|
import com.android.inputmethod.latin.suggestions.SuggestionStripViewAccessor;
|
2013-12-19 13:08:31 +00:00
|
|
|
import com.android.inputmethod.latin.utils.AsyncResultHolder;
|
2013-12-19 05:34:35 +00:00
|
|
|
import com.android.inputmethod.latin.utils.InputTypeUtils;
|
2013-12-18 09:08:35 +00:00
|
|
|
import com.android.inputmethod.latin.utils.RecapitalizeStatus;
|
2014-09-08 03:43:48 +00:00
|
|
|
import com.android.inputmethod.latin.utils.StatsUtils;
|
2013-12-20 09:05:35 +00:00
|
|
|
import com.android.inputmethod.latin.utils.TextRange;
|
2013-12-18 09:08:35 +00:00
|
|
|
|
2013-12-20 09:05:35 +00:00
|
|
|
import java.util.ArrayList;
|
2013-12-18 09:08:35 +00:00
|
|
|
import java.util.TreeSet;
|
2014-01-14 06:23:58 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2013-12-18 09:08:35 +00:00
|
|
|
|
2014-10-22 05:04:07 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
2013-12-18 09:08:35 +00:00
|
|
|
/**
|
|
|
|
* This class manages the input logic.
|
|
|
|
*/
|
|
|
|
public final class InputLogic {
|
2013-12-19 07:07:24 +00:00
|
|
|
private static final String TAG = InputLogic.class.getSimpleName();
|
|
|
|
|
2013-12-18 09:08:35 +00:00
|
|
|
// TODO : Remove this member when we can.
|
2014-10-20 05:48:56 +00:00
|
|
|
final LatinIME mLatinIME;
|
2014-01-24 14:17:40 +00:00
|
|
|
private final SuggestionStripViewAccessor mSuggestionStripViewAccessor;
|
2013-12-18 09:08:35 +00:00
|
|
|
|
2014-01-10 04:07:30 +00:00
|
|
|
// Never null.
|
|
|
|
private InputLogicHandler mInputLogicHandler = InputLogicHandler.NULL_HANDLER;
|
2013-12-24 07:30:55 +00:00
|
|
|
|
2013-12-18 09:08:35 +00:00
|
|
|
// TODO : make all these fields private as soon as possible.
|
|
|
|
// Current space state of the input method. This can be any of the above constants.
|
2014-02-28 09:56:05 +00:00
|
|
|
private int mSpaceState;
|
2013-12-18 09:08:35 +00:00
|
|
|
// Never null
|
2014-09-22 03:40:41 +00:00
|
|
|
public SuggestedWords mSuggestedWords = SuggestedWords.getEmptyInstance();
|
2014-05-23 00:30:55 +00:00
|
|
|
public final Suggest mSuggest;
|
|
|
|
private final DictionaryFacilitator mDictionaryFacilitator;
|
2013-12-18 09:08:35 +00:00
|
|
|
|
|
|
|
public LastComposedWord mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
|
2014-06-30 13:15:36 +00:00
|
|
|
// This has package visibility so it can be accessed from InputLogicHandler.
|
|
|
|
/* package */ final WordComposer mWordComposer;
|
2013-12-18 09:08:35 +00:00
|
|
|
public final RichInputConnection mConnection;
|
2014-02-28 09:56:05 +00:00
|
|
|
private final RecapitalizeStatus mRecapitalizeStatus = new RecapitalizeStatus();
|
2013-12-18 09:08:35 +00:00
|
|
|
|
2013-12-25 12:32:44 +00:00
|
|
|
private int mDeleteCount;
|
2013-12-19 10:29:05 +00:00
|
|
|
private long mLastKeyTime;
|
2014-05-23 11:18:17 +00:00
|
|
|
public final TreeSet<Long> mCurrentlyPressedHardwareKeys = new TreeSet<>();
|
2013-12-18 09:08:35 +00:00
|
|
|
|
|
|
|
// Keeps track of most recently inserted text (multi-character key) for reverting
|
2013-12-25 12:32:44 +00:00
|
|
|
private String mEnteredText;
|
2013-12-18 09:08:35 +00:00
|
|
|
|
|
|
|
// TODO: This boolean is persistent state and causes large side effects at unexpected times.
|
|
|
|
// Find a way to remove it for readability.
|
2014-02-28 09:56:05 +00:00
|
|
|
private boolean mIsAutoCorrectionIndicatorOn;
|
2014-03-05 06:55:45 +00:00
|
|
|
private long mDoubleSpacePeriodCountdownStart;
|
2013-12-18 09:08:35 +00:00
|
|
|
|
2014-05-08 05:53:56 +00:00
|
|
|
/**
|
|
|
|
* Create a new instance of the input logic.
|
|
|
|
* @param latinIME the instance of the parent LatinIME. We should remove this when we can.
|
|
|
|
* @param suggestionStripViewAccessor an object to access the suggestion strip view.
|
2014-05-23 00:30:55 +00:00
|
|
|
* @param dictionaryFacilitator facilitator for getting suggestions and updating user history
|
|
|
|
* dictionary.
|
2014-05-08 05:53:56 +00:00
|
|
|
*/
|
2014-01-24 14:17:40 +00:00
|
|
|
public InputLogic(final LatinIME latinIME,
|
2014-05-23 00:30:55 +00:00
|
|
|
final SuggestionStripViewAccessor suggestionStripViewAccessor,
|
|
|
|
final DictionaryFacilitator dictionaryFacilitator) {
|
2013-12-18 09:08:35 +00:00
|
|
|
mLatinIME = latinIME;
|
2014-01-24 14:17:40 +00:00
|
|
|
mSuggestionStripViewAccessor = suggestionStripViewAccessor;
|
2013-12-18 09:08:35 +00:00
|
|
|
mWordComposer = new WordComposer();
|
|
|
|
mConnection = new RichInputConnection(latinIME);
|
2014-01-10 04:07:30 +00:00
|
|
|
mInputLogicHandler = InputLogicHandler.NULL_HANDLER;
|
2014-05-23 00:30:55 +00:00
|
|
|
mSuggest = new Suggest(dictionaryFacilitator);
|
|
|
|
mDictionaryFacilitator = dictionaryFacilitator;
|
2013-12-18 09:08:35 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Initializes the input logic for input in an editor.
|
|
|
|
*
|
|
|
|
* Call this when input starts or restarts in some editor (typically, in onStartInputView).
|
|
|
|
*
|
2014-05-08 05:53:56 +00:00
|
|
|
* @param combiningSpec the combining spec string for this subtype
|
2014-08-27 04:32:25 +00:00
|
|
|
* @param settingsValues the current settings values
|
2013-12-19 10:29:05 +00:00
|
|
|
*/
|
2014-08-27 04:32:25 +00:00
|
|
|
public void startInput(final String combiningSpec, final SettingsValues settingsValues) {
|
2013-12-25 12:03:24 +00:00
|
|
|
mEnteredText = null;
|
2014-09-19 00:55:04 +00:00
|
|
|
if (!mWordComposer.getTypedWord().isEmpty()) {
|
|
|
|
// For messaging apps that offer send button, the IME does not get the opportunity
|
|
|
|
// to capture the last word. This block should capture those uncommitted words.
|
|
|
|
// The timestamp at which it is captured is not accurate but close enough.
|
|
|
|
StatsUtils.onWordCommitUserTyped(
|
|
|
|
mWordComposer.getTypedWord(), mWordComposer.isBatchMode());
|
|
|
|
}
|
2014-05-14 11:19:16 +00:00
|
|
|
mWordComposer.restartCombining(combiningSpec);
|
2013-12-25 12:03:24 +00:00
|
|
|
resetComposingState(true /* alsoResetLastComposedWord */);
|
|
|
|
mDeleteCount = 0;
|
|
|
|
mSpaceState = SpaceState.NONE;
|
2014-06-04 06:38:41 +00:00
|
|
|
mRecapitalizeStatus.disable(); // Do not perform recapitalize until the cursor is moved once
|
2013-12-25 12:03:24 +00:00
|
|
|
mCurrentlyPressedHardwareKeys.clear();
|
2014-09-22 03:40:41 +00:00
|
|
|
mSuggestedWords = SuggestedWords.getEmptyInstance();
|
2013-12-25 12:03:24 +00:00
|
|
|
// In some cases (namely, after rotation of the device) editorInfo.initialSelStart is lying
|
|
|
|
// so we try using some heuristics to find out about these and fix them.
|
2014-01-09 05:09:26 +00:00
|
|
|
mConnection.tryFixLyingCursorPosition();
|
2014-03-05 06:55:45 +00:00
|
|
|
cancelDoubleSpacePeriodCountdown();
|
2014-03-31 05:43:08 +00:00
|
|
|
if (InputLogicHandler.NULL_HANDLER == mInputLogicHandler) {
|
|
|
|
mInputLogicHandler = new InputLogicHandler(mLatinIME, this);
|
|
|
|
} else {
|
|
|
|
mInputLogicHandler.reset();
|
|
|
|
}
|
2014-08-27 04:32:25 +00:00
|
|
|
|
2014-10-06 08:20:10 +00:00
|
|
|
if (settingsValues.mShouldShowLxxSuggestionUi) {
|
2014-10-06 10:32:01 +00:00
|
|
|
mConnection.requestCursorUpdates(true /* enableMonitor */,
|
|
|
|
true /* requestImmediateCallback */);
|
2014-08-27 04:32:25 +00:00
|
|
|
}
|
2013-12-19 07:07:24 +00:00
|
|
|
}
|
|
|
|
|
2014-05-08 05:53:56 +00:00
|
|
|
/**
|
|
|
|
* Call this when the subtype changes.
|
|
|
|
* @param combiningSpec the spec string for the combining rules
|
2014-08-27 04:32:25 +00:00
|
|
|
* @param settingsValues the current settings values
|
2014-05-08 05:53:56 +00:00
|
|
|
*/
|
2014-08-27 04:32:25 +00:00
|
|
|
public void onSubtypeChanged(final String combiningSpec, final SettingsValues settingsValues) {
|
2014-06-27 12:38:57 +00:00
|
|
|
finishInput();
|
2014-08-27 04:32:25 +00:00
|
|
|
startInput(combiningSpec, settingsValues);
|
2014-05-08 05:53:56 +00:00
|
|
|
}
|
|
|
|
|
2014-06-30 13:15:36 +00:00
|
|
|
/**
|
|
|
|
* Call this when the orientation changes.
|
|
|
|
* @param settingsValues the current values of the settings.
|
|
|
|
*/
|
|
|
|
public void onOrientationChange(final SettingsValues settingsValues) {
|
|
|
|
// If !isComposingWord, #commitTyped() is a no-op, but still, it's better to avoid
|
|
|
|
// the useless IPC of {begin,end}BatchEdit.
|
|
|
|
if (mWordComposer.isComposingWord()) {
|
|
|
|
mConnection.beginBatchEdit();
|
|
|
|
// If we had a composition in progress, we need to commit the word so that the
|
|
|
|
// suggestionsSpan will be added. This will allow resuming on the same suggestions
|
|
|
|
// after rotation is finished.
|
|
|
|
commitTyped(settingsValues, LastComposedWord.NOT_A_SEPARATOR);
|
|
|
|
mConnection.endBatchEdit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Clean up the input logic after input is finished.
|
|
|
|
*/
|
2013-12-19 07:07:24 +00:00
|
|
|
public void finishInput() {
|
2013-12-25 12:03:24 +00:00
|
|
|
if (mWordComposer.isComposingWord()) {
|
|
|
|
mConnection.finishComposingText();
|
2014-09-18 05:53:59 +00:00
|
|
|
StatsUtils.onWordCommitUserTyped(
|
|
|
|
mWordComposer.getTypedWord(), mWordComposer.isBatchMode());
|
2013-12-25 12:03:24 +00:00
|
|
|
}
|
|
|
|
resetComposingState(true /* alsoResetLastComposedWord */);
|
2014-03-31 05:43:08 +00:00
|
|
|
mInputLogicHandler.reset();
|
2013-12-18 09:08:35 +00:00
|
|
|
}
|
2013-12-19 05:34:35 +00:00
|
|
|
|
2014-04-07 13:32:06 +00:00
|
|
|
// Normally this class just gets out of scope after the process ends, but in unit tests, we
|
|
|
|
// create several instances of LatinIME in the same process, which results in several
|
|
|
|
// instances of InputLogic. This cleans up the associated handler so that tests don't leak
|
|
|
|
// handlers.
|
|
|
|
public void recycle() {
|
|
|
|
final InputLogicHandler inputLogicHandler = mInputLogicHandler;
|
|
|
|
mInputLogicHandler = InputLogicHandler.NULL_HANDLER;
|
|
|
|
inputLogicHandler.destroy();
|
2014-05-23 00:30:55 +00:00
|
|
|
mDictionaryFacilitator.closeDictionaries();
|
2014-04-07 13:32:06 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 06:44:26 +00:00
|
|
|
/**
|
|
|
|
* React to a string input.
|
|
|
|
*
|
|
|
|
* This is triggered by keys that input many characters at once, like the ".com" key or
|
|
|
|
* some additional keys for example.
|
|
|
|
*
|
|
|
|
* @param settingsValues the current values of the settings.
|
2014-03-19 06:59:49 +00:00
|
|
|
* @param event the input event containing the data.
|
2014-06-27 12:38:57 +00:00
|
|
|
* @return the complete transaction object
|
2013-12-20 06:44:26 +00:00
|
|
|
*/
|
2014-06-27 12:38:57 +00:00
|
|
|
public InputTransaction onTextInput(final SettingsValues settingsValues, final Event event,
|
2015-03-20 17:43:38 +00:00
|
|
|
final int keyboardShiftMode, final LatinIME.UIHandler handler) {
|
2014-08-01 05:15:33 +00:00
|
|
|
final String rawText = event.getTextToCommit().toString();
|
2014-06-27 12:38:57 +00:00
|
|
|
final InputTransaction inputTransaction = new InputTransaction(settingsValues, event,
|
|
|
|
SystemClock.uptimeMillis(), mSpaceState,
|
|
|
|
getActualCapsMode(settingsValues, keyboardShiftMode));
|
2013-12-20 06:44:26 +00:00
|
|
|
mConnection.beginBatchEdit();
|
|
|
|
if (mWordComposer.isComposingWord()) {
|
2013-12-25 12:04:27 +00:00
|
|
|
commitCurrentAutoCorrection(settingsValues, rawText, handler);
|
2013-12-20 06:44:26 +00:00
|
|
|
} else {
|
|
|
|
resetComposingState(true /* alsoResetLastComposedWord */);
|
|
|
|
}
|
2014-08-14 03:48:50 +00:00
|
|
|
handler.postUpdateSuggestionStrip(SuggestedWords.INPUT_STYLE_TYPING);
|
2013-12-20 06:44:26 +00:00
|
|
|
final String text = performSpecificTldProcessingOnTextInput(rawText);
|
|
|
|
if (SpaceState.PHANTOM == mSpaceState) {
|
2014-11-18 02:49:45 +00:00
|
|
|
insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues);
|
2013-12-20 06:44:26 +00:00
|
|
|
}
|
|
|
|
mConnection.commitText(text, 1);
|
2014-09-18 05:53:59 +00:00
|
|
|
StatsUtils.onWordCommitUserTyped(mEnteredText, mWordComposer.isBatchMode());
|
2013-12-20 06:44:26 +00:00
|
|
|
mConnection.endBatchEdit();
|
|
|
|
// Space state must be updated before calling updateShiftState
|
|
|
|
mSpaceState = SpaceState.NONE;
|
|
|
|
mEnteredText = text;
|
2014-06-27 12:38:57 +00:00
|
|
|
inputTransaction.setDidAffectContents();
|
|
|
|
inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW);
|
|
|
|
return inputTransaction;
|
2013-12-20 06:44:26 +00:00
|
|
|
}
|
|
|
|
|
2014-01-29 07:15:12 +00:00
|
|
|
/**
|
|
|
|
* A suggestion was picked from the suggestion strip.
|
|
|
|
* @param settingsValues the current values of the settings.
|
|
|
|
* @param suggestionInfo the suggestion info.
|
2014-03-04 09:06:50 +00:00
|
|
|
* @param keyboardShiftState the shift state of the keyboard, as returned by
|
|
|
|
* {@link com.android.inputmethod.keyboard.KeyboardSwitcher#getKeyboardShiftMode()}
|
|
|
|
* @return the complete transaction object
|
2014-01-29 07:15:12 +00:00
|
|
|
*/
|
|
|
|
// Called from {@link SuggestionStripView} through the {@link SuggestionStripView#Listener}
|
|
|
|
// interface
|
2014-03-04 09:06:50 +00:00
|
|
|
public InputTransaction onPickSuggestionManually(final SettingsValues settingsValues,
|
2014-06-26 09:43:14 +00:00
|
|
|
final SuggestedWordInfo suggestionInfo, final int keyboardShiftState,
|
|
|
|
final int currentKeyboardScriptId, final LatinIME.UIHandler handler) {
|
2014-01-29 07:15:12 +00:00
|
|
|
final SuggestedWords suggestedWords = mSuggestedWords;
|
|
|
|
final String suggestion = suggestionInfo.mWord;
|
|
|
|
// If this is a punctuation picked from the suggestion strip, pass it to onCodeInput
|
2014-02-18 08:07:46 +00:00
|
|
|
if (suggestion.length() == 1 && suggestedWords.isPunctuationSuggestions()) {
|
2015-03-18 21:48:19 +00:00
|
|
|
// We still want to log a suggestion click.
|
|
|
|
StatsUtils.onPickSuggestionManually(
|
|
|
|
mSuggestedWords, suggestionInfo, mDictionaryFacilitator);
|
2014-01-29 07:15:12 +00:00
|
|
|
// Word separators are suggested before the user inputs something.
|
|
|
|
// Rely on onCodeInput to do the complicated swapping/stripping logic consistently.
|
2014-03-18 06:14:27 +00:00
|
|
|
final Event event = Event.createPunctuationSuggestionPickedEvent(suggestionInfo);
|
2014-06-26 09:43:14 +00:00
|
|
|
return onCodeInput(settingsValues, event, keyboardShiftState,
|
|
|
|
currentKeyboardScriptId, handler);
|
2014-01-29 07:15:12 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 09:06:50 +00:00
|
|
|
final Event event = Event.createSuggestionPickedEvent(suggestionInfo);
|
|
|
|
final InputTransaction inputTransaction = new InputTransaction(settingsValues,
|
|
|
|
event, SystemClock.uptimeMillis(), mSpaceState, keyboardShiftState);
|
2014-06-27 12:38:57 +00:00
|
|
|
// Manual pick affects the contents of the editor, so we take note of this. It's important
|
|
|
|
// for the sequence of language switching.
|
|
|
|
inputTransaction.setDidAffectContents();
|
2014-01-29 07:15:12 +00:00
|
|
|
mConnection.beginBatchEdit();
|
|
|
|
if (SpaceState.PHANTOM == mSpaceState && suggestion.length() > 0
|
|
|
|
// In the batch input mode, a manually picked suggested word should just replace
|
|
|
|
// the current batch input text and there is no need for a phantom space.
|
|
|
|
&& !mWordComposer.isBatchMode()) {
|
|
|
|
final int firstChar = Character.codePointAt(suggestion, 0);
|
|
|
|
if (!settingsValues.isWordSeparator(firstChar)
|
|
|
|
|| settingsValues.isUsuallyPrecededBySpace(firstChar)) {
|
2014-11-18 02:49:45 +00:00
|
|
|
insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues);
|
2014-01-29 07:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 05:39:51 +00:00
|
|
|
// TODO: We should not need the following branch. We should be able to take the same
|
|
|
|
// code path as for other kinds, use commitChosenWord, and do everything normally. We will
|
|
|
|
// however need to reset the suggestion strip right away, because we know we can't take
|
|
|
|
// the risk of calling commitCompletion twice because we don't know how the app will react.
|
2014-05-26 09:45:32 +00:00
|
|
|
if (suggestionInfo.isKindOf(SuggestedWordInfo.KIND_APP_DEFINED)) {
|
2014-09-22 03:40:41 +00:00
|
|
|
mSuggestedWords = SuggestedWords.getEmptyInstance();
|
2014-01-29 07:15:12 +00:00
|
|
|
mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
|
2014-03-04 09:06:50 +00:00
|
|
|
inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW);
|
2014-01-29 07:15:12 +00:00
|
|
|
resetComposingState(true /* alsoResetLastComposedWord */);
|
2014-02-26 05:39:51 +00:00
|
|
|
mConnection.commitCompletion(suggestionInfo.mApplicationSpecifiedCompletionInfo);
|
2014-01-29 07:15:12 +00:00
|
|
|
mConnection.endBatchEdit();
|
2014-03-04 09:06:50 +00:00
|
|
|
return inputTransaction;
|
2014-01-29 07:15:12 +00:00
|
|
|
}
|
|
|
|
|
2014-09-14 07:53:33 +00:00
|
|
|
commitChosenWord(settingsValues, suggestion, LastComposedWord.COMMIT_TYPE_MANUAL_PICK,
|
|
|
|
LastComposedWord.NOT_A_SEPARATOR);
|
2014-01-29 07:15:12 +00:00
|
|
|
mConnection.endBatchEdit();
|
|
|
|
// Don't allow cancellation of manual pick
|
|
|
|
mLastComposedWord.deactivate();
|
|
|
|
// Space state must be updated before calling updateShiftState
|
|
|
|
mSpaceState = SpaceState.PHANTOM;
|
2014-03-04 09:06:50 +00:00
|
|
|
inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW);
|
2014-01-29 07:15:12 +00:00
|
|
|
|
2015-02-03 18:18:06 +00:00
|
|
|
// If we're not showing the "Touch again to save", then update the suggestion strip.
|
|
|
|
// That's going to be predictions (or punctuation suggestions), so INPUT_STYLE_NONE.
|
|
|
|
handler.postUpdateSuggestionStrip(SuggestedWords.INPUT_STYLE_NONE);
|
2014-09-08 03:43:48 +00:00
|
|
|
|
2015-03-17 22:18:21 +00:00
|
|
|
StatsUtils.onPickSuggestionManually(
|
|
|
|
mSuggestedWords, suggestionInfo, mDictionaryFacilitator);
|
2014-09-18 05:53:59 +00:00
|
|
|
StatsUtils.onWordCommitSuggestionPickedManually(
|
|
|
|
suggestionInfo.mWord, mWordComposer.isBatchMode());
|
2014-03-04 09:06:50 +00:00
|
|
|
return inputTransaction;
|
2014-01-29 07:15:12 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 05:00:26 +00:00
|
|
|
/**
|
|
|
|
* Consider an update to the cursor position. Evaluate whether this update has happened as
|
|
|
|
* part of normal typing or whether it was an explicit cursor move by the user. In any case,
|
|
|
|
* do the necessary adjustments.
|
|
|
|
* @param oldSelStart old selection start
|
|
|
|
* @param oldSelEnd old selection end
|
|
|
|
* @param newSelStart new selection start
|
|
|
|
* @param newSelEnd new selection end
|
2014-10-15 05:02:32 +00:00
|
|
|
* @param settingsValues the current values of the settings.
|
2014-01-24 05:00:26 +00:00
|
|
|
* @return whether the cursor has moved as a result of user interaction.
|
|
|
|
*/
|
2014-03-03 05:19:52 +00:00
|
|
|
public boolean onUpdateSelection(final int oldSelStart, final int oldSelEnd,
|
2014-10-15 05:02:32 +00:00
|
|
|
final int newSelStart, final int newSelEnd, final SettingsValues settingsValues) {
|
2014-01-24 05:09:42 +00:00
|
|
|
if (mConnection.isBelatedExpectedUpdate(oldSelStart, newSelStart, oldSelEnd, newSelEnd)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// TODO: the following is probably better done in resetEntireInputState().
|
|
|
|
// it should only happen when the cursor moved, and the very purpose of the
|
|
|
|
// test below is to narrow down whether this happened or not. Likewise with
|
|
|
|
// the call to updateShiftState.
|
|
|
|
// We set this to NONE because after a cursor move, we don't want the space
|
|
|
|
// state-related special processing to kick in.
|
|
|
|
mSpaceState = SpaceState.NONE;
|
2014-01-24 05:03:08 +00:00
|
|
|
|
2014-01-24 05:18:11 +00:00
|
|
|
final boolean selectionChangedOrSafeToReset =
|
|
|
|
oldSelStart != newSelStart || oldSelEnd != newSelEnd // selection changed
|
|
|
|
|| !mWordComposer.isComposingWord(); // safe to reset
|
2014-01-24 05:09:42 +00:00
|
|
|
final boolean hasOrHadSelection = (oldSelStart != oldSelEnd || newSelStart != newSelEnd);
|
|
|
|
final int moveAmount = newSelStart - oldSelStart;
|
Fix a bug on rotation with selection.
The symptom : when text is selected and the device is rotated,
sometimes the keyboard sets the word as being composed around
the start of the selection. Upon the next rotation this ends up
with the keyboard committing some text in place of the selection.
The cause : another bug in the framework with rotation >.>
The keyboard receives a call to startInput with a wrong cursor
position, namely one that does not represent a selection. The
keyboard sets a composition according to this wrong data. When
the keyboard is rotated again, it commits the text, which takes
the place of the selection.
The solution : actually when restarting input the keyboard
realizes that the cursor position is wrong. We cancel composition
at that time.
For robustness, this change also implements two other defensive
changes : upon call to onUpdateSelection, we actually realize
that the previous values were wrong, so we also fix it at that
time, and in addition, when rotating, we finishComposingText()
instead of commitText() which is less dangerous. Implementing
this later change also allows us to let less internal variables
from InputLogic escape to LatinIME, so it's also a good change
for design.
Bug: 14140799
Change-Id: Ib10de18e53e376ac1bbc8487e13d969828483346
2014-06-05 08:48:10 +00:00
|
|
|
// As an added small gift from the framework, it happens upon rotation when there
|
|
|
|
// is a selection that we get a wrong cursor position delivered to startInput() that
|
|
|
|
// does not get reflected in the oldSel{Start,End} parameters to the next call to
|
|
|
|
// onUpdateSelection. In this case, we may have set a composition, and when we're here
|
|
|
|
// we realize we shouldn't have. In theory, in this case, selectionChangedOrSafeToReset
|
|
|
|
// should be true, but that is if the framework had taken that wrong cursor position
|
|
|
|
// into account, which means we have to reset the entire composing state whenever there
|
|
|
|
// is or was a selection regardless of whether it changed or not.
|
2014-10-15 05:02:32 +00:00
|
|
|
if (hasOrHadSelection || !settingsValues.needsToLookupSuggestions()
|
|
|
|
|| (selectionChangedOrSafeToReset
|
|
|
|
&& !mWordComposer.moveCursorByAndReturnIfInsideComposingWord(moveAmount))) {
|
2014-01-24 05:09:42 +00:00
|
|
|
// If we are composing a word and moving the cursor, we would want to set a
|
|
|
|
// suggestion span for recorrection to work correctly. Unfortunately, that
|
|
|
|
// would involve the keyboard committing some new text, which would move the
|
|
|
|
// cursor back to where it was. Latin IME could then fix the position of the cursor
|
|
|
|
// again, but the asynchronous nature of the calls results in this wreaking havoc
|
|
|
|
// with selection on double tap and the like.
|
|
|
|
// Another option would be to send suggestions each time we set the composing
|
|
|
|
// text, but that is probably too expensive to do, so we decided to leave things
|
|
|
|
// as is.
|
2014-02-19 12:41:45 +00:00
|
|
|
// Also, we're posting a resume suggestions message, and this will update the
|
|
|
|
// suggestions strip in a few milliseconds, so if we cleared the suggestion strip here
|
|
|
|
// we'd have the suggestion strip noticeably janky. To avoid that, we don't clear
|
|
|
|
// it here, which means we'll keep outdated suggestions for a split second but the
|
|
|
|
// visual result is better.
|
2014-03-03 05:19:52 +00:00
|
|
|
resetEntireInputState(newSelStart, newSelEnd, false /* clearSuggestionStrip */);
|
2014-01-24 05:09:42 +00:00
|
|
|
} else {
|
|
|
|
// resetEntireInputState calls resetCachesUponCursorMove, but forcing the
|
|
|
|
// composition to end. But in all cases where we don't reset the entire input
|
|
|
|
// state, we still want to tell the rich input connection about the new cursor
|
|
|
|
// position so that it can update its caches.
|
|
|
|
mConnection.resetCachesUponCursorMoveAndReturnSuccess(
|
|
|
|
newSelStart, newSelEnd, false /* shouldFinishComposition */);
|
2014-01-24 05:00:26 +00:00
|
|
|
}
|
2014-01-24 05:09:42 +00:00
|
|
|
|
2014-06-04 06:38:41 +00:00
|
|
|
// The cursor has been moved : we now accept to perform recapitalization
|
|
|
|
mRecapitalizeStatus.enable();
|
2014-01-24 05:09:42 +00:00
|
|
|
// We moved the cursor. If we are touching a word, we need to resume suggestion.
|
2014-10-01 09:32:02 +00:00
|
|
|
mLatinIME.mHandler.postResumeSuggestions(true /* shouldDelay */);
|
2014-06-04 06:19:10 +00:00
|
|
|
// Stop the last recapitalization, if started.
|
|
|
|
mRecapitalizeStatus.stop();
|
2014-01-24 05:09:42 +00:00
|
|
|
return true;
|
2014-01-24 05:00:26 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* React to a code input. It may be a code point to insert, or a symbolic value that influences
|
|
|
|
* the keyboard behavior.
|
|
|
|
*
|
|
|
|
* Typically, this is called whenever a key is pressed on the software keyboard. This is not
|
|
|
|
* the entry point for gesture input; see the onBatchInput* family of functions for this.
|
|
|
|
*
|
2014-03-04 08:28:20 +00:00
|
|
|
* @param settingsValues the current settings values.
|
2014-03-12 07:41:45 +00:00
|
|
|
* @param event the event to handle.
|
2014-03-04 08:28:20 +00:00
|
|
|
* @param keyboardShiftMode the current shift mode of the keyboard, as returned by
|
|
|
|
* {@link com.android.inputmethod.keyboard.KeyboardSwitcher#getKeyboardShiftMode()}
|
|
|
|
* @return the complete transaction object
|
2013-12-19 10:29:05 +00:00
|
|
|
*/
|
2014-10-22 05:04:07 +00:00
|
|
|
public InputTransaction onCodeInput(final SettingsValues settingsValues,
|
|
|
|
@Nonnull final Event event, final int keyboardShiftMode,
|
2014-06-26 09:43:14 +00:00
|
|
|
final int currentKeyboardScriptId, final LatinIME.UIHandler handler) {
|
2014-07-01 05:05:57 +00:00
|
|
|
final Event processedEvent = mWordComposer.processEvent(event);
|
2014-07-31 09:33:30 +00:00
|
|
|
final InputTransaction inputTransaction = new InputTransaction(settingsValues,
|
|
|
|
processedEvent, SystemClock.uptimeMillis(), mSpaceState,
|
2014-03-04 08:28:20 +00:00
|
|
|
getActualCapsMode(settingsValues, keyboardShiftMode));
|
2014-07-31 09:33:30 +00:00
|
|
|
if (processedEvent.mKeyCode != Constants.CODE_DELETE
|
2014-03-04 07:31:51 +00:00
|
|
|
|| inputTransaction.mTimestamp > mLastKeyTime + Constants.LONG_PRESS_MILLISECONDS) {
|
2013-12-19 05:34:35 +00:00
|
|
|
mDeleteCount = 0;
|
|
|
|
}
|
2014-03-04 07:31:51 +00:00
|
|
|
mLastKeyTime = inputTransaction.mTimestamp;
|
2013-12-19 05:34:35 +00:00
|
|
|
mConnection.beginBatchEdit();
|
|
|
|
if (!mWordComposer.isComposingWord()) {
|
2014-08-01 05:15:33 +00:00
|
|
|
// TODO: is this useful? It doesn't look like it should be done here, but rather after
|
|
|
|
// a word is committed.
|
2013-12-19 05:34:35 +00:00
|
|
|
mIsAutoCorrectionIndicatorOn = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Consolidate the double-space period timer, mLastKeyTime, and the space state.
|
2014-07-31 09:33:30 +00:00
|
|
|
if (processedEvent.mCodePoint != Constants.CODE_SPACE) {
|
2014-03-05 06:55:45 +00:00
|
|
|
cancelDoubleSpacePeriodCountdown();
|
2013-12-19 05:34:35 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 13:28:24 +00:00
|
|
|
Event currentEvent = processedEvent;
|
|
|
|
while (null != currentEvent) {
|
|
|
|
if (currentEvent.isConsumed()) {
|
|
|
|
handleConsumedEvent(currentEvent, inputTransaction);
|
|
|
|
} else if (currentEvent.isFunctionalKeyEvent()) {
|
|
|
|
handleFunctionalEvent(currentEvent, inputTransaction, currentKeyboardScriptId,
|
|
|
|
handler);
|
|
|
|
} else {
|
|
|
|
handleNonFunctionalEvent(currentEvent, inputTransaction, handler);
|
|
|
|
}
|
|
|
|
currentEvent = currentEvent.mNextEvent;
|
2013-12-19 05:34:35 +00:00
|
|
|
}
|
2014-08-04 13:07:15 +00:00
|
|
|
if (!inputTransaction.didAutoCorrect() && processedEvent.mKeyCode != Constants.CODE_SHIFT
|
2014-07-31 09:33:30 +00:00
|
|
|
&& processedEvent.mKeyCode != Constants.CODE_CAPSLOCK
|
|
|
|
&& processedEvent.mKeyCode != Constants.CODE_SWITCH_ALPHA_SYMBOL)
|
2013-12-19 05:34:35 +00:00
|
|
|
mLastComposedWord.deactivate();
|
2014-07-31 09:33:30 +00:00
|
|
|
if (Constants.CODE_DELETE != processedEvent.mKeyCode) {
|
2013-12-19 05:34:35 +00:00
|
|
|
mEnteredText = null;
|
|
|
|
}
|
|
|
|
mConnection.endBatchEdit();
|
2014-03-04 08:28:20 +00:00
|
|
|
return inputTransaction;
|
2013-12-19 05:34:35 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 11:45:59 +00:00
|
|
|
public void onStartBatchInput(final SettingsValues settingsValues,
|
2013-12-25 12:04:27 +00:00
|
|
|
final KeyboardSwitcher keyboardSwitcher, final LatinIME.UIHandler handler) {
|
|
|
|
mInputLogicHandler.onStartBatchInput();
|
2013-12-24 09:07:20 +00:00
|
|
|
handler.showGesturePreviewAndSuggestionStrip(
|
2014-09-22 03:40:41 +00:00
|
|
|
SuggestedWords.getEmptyInstance(), false /* dismissGestureFloatingPreviewText */);
|
2013-12-20 11:45:59 +00:00
|
|
|
handler.cancelUpdateSuggestionStrip();
|
2014-02-21 07:43:11 +00:00
|
|
|
++mAutoCommitSequenceNumber;
|
2013-12-20 11:45:59 +00:00
|
|
|
mConnection.beginBatchEdit();
|
2015-02-05 02:10:04 +00:00
|
|
|
if (mWordComposer.isComposingWord()) {
|
2013-12-20 11:45:59 +00:00
|
|
|
if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) {
|
|
|
|
// If we are in the middle of a recorrection, we need to commit the recorrection
|
|
|
|
// first so that we can insert the batch input at the current cursor position.
|
2015-04-02 00:05:37 +00:00
|
|
|
// We also need to unlearn the original word that is now being corrected.
|
|
|
|
unlearnWord(mWordComposer.getTypedWord(), settingsValues,
|
|
|
|
Constants.EVENT_BACKSPACE);
|
2014-03-03 05:19:52 +00:00
|
|
|
resetEntireInputState(mConnection.getExpectedSelectionStart(),
|
2014-02-19 12:41:45 +00:00
|
|
|
mConnection.getExpectedSelectionEnd(), true /* clearSuggestionStrip */);
|
2014-03-28 05:35:38 +00:00
|
|
|
} else if (mWordComposer.isSingleLetter()) {
|
2013-12-20 11:45:59 +00:00
|
|
|
// We auto-correct the previous (typed, not gestured) string iff it's one character
|
|
|
|
// long. The reason for this is, even in the middle of gesture typing, you'll still
|
|
|
|
// tap one-letter words and you want them auto-corrected (typically, "i" in English
|
|
|
|
// should become "I"). However for any longer word, we assume that the reason for
|
|
|
|
// tapping probably is that the word you intend to type is not in the dictionary,
|
|
|
|
// so we do not attempt to correct, on the assumption that if that was a dictionary
|
|
|
|
// word, the user would probably have gestured instead.
|
|
|
|
commitCurrentAutoCorrection(settingsValues, LastComposedWord.NOT_A_SEPARATOR,
|
2013-12-25 12:04:27 +00:00
|
|
|
handler);
|
2013-12-20 11:45:59 +00:00
|
|
|
} else {
|
|
|
|
commitTyped(settingsValues, LastComposedWord.NOT_A_SEPARATOR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor();
|
|
|
|
if (Character.isLetterOrDigit(codePointBeforeCursor)
|
|
|
|
|| settingsValues.isUsuallyFollowedBySpace(codePointBeforeCursor)) {
|
|
|
|
final boolean autoShiftHasBeenOverriden = keyboardSwitcher.getKeyboardShiftMode() !=
|
|
|
|
getCurrentAutoCapsState(settingsValues);
|
|
|
|
mSpaceState = SpaceState.PHANTOM;
|
|
|
|
if (!autoShiftHasBeenOverriden) {
|
|
|
|
// When we change the space state, we need to update the shift state of the
|
|
|
|
// keyboard unless it has been overridden manually. This is happening for example
|
|
|
|
// after typing some letters and a period, then gesturing; the keyboard is not in
|
|
|
|
// caps mode yet, but since a gesture is starting, it should go in caps mode,
|
|
|
|
// unless the user explictly said it should not.
|
2014-03-27 09:37:16 +00:00
|
|
|
keyboardSwitcher.requestUpdatingShiftState(getCurrentAutoCapsState(settingsValues),
|
|
|
|
getCurrentRecapitalizeState());
|
2013-12-20 11:45:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mConnection.endBatchEdit();
|
2014-06-09 10:31:41 +00:00
|
|
|
mWordComposer.setCapitalizedModeAtStartComposingTime(
|
|
|
|
getActualCapsMode(settingsValues, keyboardSwitcher.getKeyboardShiftMode()));
|
2013-12-20 11:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The sequence number member is only used in onUpdateBatchInput. It is increased each time
|
|
|
|
* auto-commit happens. The reason we need this is, when auto-commit happens we trim the
|
|
|
|
* input pointers that are held in a singleton, and to know how much to trim we rely on the
|
|
|
|
* results of the suggestion process that is held in mSuggestedWords.
|
|
|
|
* However, the suggestion process is asynchronous, and sometimes we may enter the
|
|
|
|
* onUpdateBatchInput method twice without having recomputed suggestions yet, or having
|
|
|
|
* received new suggestions generated from not-yet-trimmed input pointers. In this case, the
|
|
|
|
* mIndexOfTouchPointOfSecondWords member will be out of date, and we must not use it lest we
|
|
|
|
* remove an unrelated number of pointers (possibly even more than are left in the input
|
|
|
|
* pointers, leading to a crash).
|
|
|
|
* To avoid that, we increase the sequence number each time we auto-commit and trim the
|
|
|
|
* input pointers, and we do not use any suggested words that have been generated with an
|
|
|
|
* earlier sequence number.
|
|
|
|
*/
|
|
|
|
private int mAutoCommitSequenceNumber = 1;
|
2015-03-20 17:43:38 +00:00
|
|
|
public void onUpdateBatchInput(final InputPointers batchPointers) {
|
2013-12-25 12:04:27 +00:00
|
|
|
mInputLogicHandler.onUpdateBatchInput(batchPointers, mAutoCommitSequenceNumber);
|
2013-12-20 11:45:59 +00:00
|
|
|
}
|
|
|
|
|
2014-03-03 05:19:52 +00:00
|
|
|
public void onEndBatchInput(final InputPointers batchPointers) {
|
2014-03-06 07:10:15 +00:00
|
|
|
mInputLogicHandler.updateTailBatchInput(batchPointers, mAutoCommitSequenceNumber);
|
2014-02-21 07:43:11 +00:00
|
|
|
++mAutoCommitSequenceNumber;
|
2013-12-20 11:45:59 +00:00
|
|
|
}
|
|
|
|
|
2013-12-25 12:04:27 +00:00
|
|
|
public void onCancelBatchInput(final LatinIME.UIHandler handler) {
|
|
|
|
mInputLogicHandler.onCancelBatchInput();
|
2013-12-24 09:07:20 +00:00
|
|
|
handler.showGesturePreviewAndSuggestionStrip(
|
2014-09-22 03:40:41 +00:00
|
|
|
SuggestedWords.getEmptyInstance(), true /* dismissGestureFloatingPreviewText */);
|
2013-12-20 11:45:59 +00:00
|
|
|
}
|
|
|
|
|
2014-01-24 12:57:34 +00:00
|
|
|
// TODO: on the long term, this method should become private, but it will be difficult.
|
|
|
|
// Especially, how do we deal with InputMethodService.onDisplayCompletions?
|
2014-10-07 10:29:05 +00:00
|
|
|
public void setSuggestedWords(final SuggestedWords suggestedWords) {
|
2014-09-22 03:40:41 +00:00
|
|
|
if (!suggestedWords.isEmpty()) {
|
2014-10-07 10:29:05 +00:00
|
|
|
final SuggestedWordInfo suggestedWordInfo;
|
2014-06-30 12:19:51 +00:00
|
|
|
if (suggestedWords.mWillAutoCorrect) {
|
2014-10-07 10:29:05 +00:00
|
|
|
suggestedWordInfo = suggestedWords.getInfo(SuggestedWords.INDEX_OF_AUTO_CORRECTION);
|
2014-06-30 12:19:51 +00:00
|
|
|
} else {
|
|
|
|
// We can't use suggestedWords.getWord(SuggestedWords.INDEX_OF_TYPED_WORD)
|
|
|
|
// because it may differ from mWordComposer.mTypedWord.
|
2014-11-17 09:26:15 +00:00
|
|
|
suggestedWordInfo = suggestedWords.mTypedWordInfo;
|
2014-06-30 12:19:51 +00:00
|
|
|
}
|
2014-10-07 10:29:05 +00:00
|
|
|
mWordComposer.setAutoCorrection(suggestedWordInfo);
|
2014-06-30 12:19:51 +00:00
|
|
|
}
|
2014-01-24 12:57:34 +00:00
|
|
|
mSuggestedWords = suggestedWords;
|
|
|
|
final boolean newAutoCorrectionIndicator = suggestedWords.mWillAutoCorrect;
|
2014-08-25 03:08:55 +00:00
|
|
|
|
2014-01-24 12:57:34 +00:00
|
|
|
// Put a blue underline to a word in TextView which will be auto-corrected.
|
|
|
|
if (mIsAutoCorrectionIndicatorOn != newAutoCorrectionIndicator
|
|
|
|
&& mWordComposer.isComposingWord()) {
|
|
|
|
mIsAutoCorrectionIndicatorOn = newAutoCorrectionIndicator;
|
|
|
|
final CharSequence textWithUnderline =
|
|
|
|
getTextWithUnderline(mWordComposer.getTypedWord());
|
|
|
|
// TODO: when called from an updateSuggestionStrip() call that results from a posted
|
|
|
|
// message, this is called outside any batch edit. Potentially, this may result in some
|
|
|
|
// janky flickering of the screen, although the display speed makes it unlikely in
|
|
|
|
// the practice.
|
2014-08-27 04:55:48 +00:00
|
|
|
setComposingTextInternal(textWithUnderline, 1);
|
2014-01-24 12:57:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 13:04:31 +00:00
|
|
|
/**
|
|
|
|
* Handle a consumed event.
|
|
|
|
*
|
|
|
|
* Consumed events represent events that have already been consumed, typically by the
|
|
|
|
* combining chain.
|
|
|
|
*
|
2014-08-04 13:17:36 +00:00
|
|
|
* @param event The event to handle.
|
2014-08-04 13:04:31 +00:00
|
|
|
* @param inputTransaction The transaction in progress.
|
|
|
|
*/
|
2014-08-04 13:17:36 +00:00
|
|
|
private void handleConsumedEvent(final Event event, final InputTransaction inputTransaction) {
|
2014-08-04 13:04:31 +00:00
|
|
|
// A consumed event may have text to commit and an update to the composing state, so
|
|
|
|
// we evaluate both. With some combiners, it's possible than an event contains both
|
|
|
|
// and we enter both of the following if clauses.
|
2014-08-04 13:17:36 +00:00
|
|
|
final CharSequence textToCommit = event.getTextToCommit();
|
2014-08-04 13:04:31 +00:00
|
|
|
if (!TextUtils.isEmpty(textToCommit)) {
|
|
|
|
mConnection.commitText(textToCommit, 1);
|
|
|
|
inputTransaction.setDidAffectContents();
|
|
|
|
}
|
|
|
|
if (mWordComposer.isComposingWord()) {
|
2014-08-27 04:55:48 +00:00
|
|
|
setComposingTextInternal(mWordComposer.getTypedWord(), 1);
|
2014-08-04 13:04:31 +00:00
|
|
|
inputTransaction.setDidAffectContents();
|
|
|
|
inputTransaction.setRequiresUpdateSuggestions();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a functional key event.
|
|
|
|
*
|
|
|
|
* A functional event is a special key, like delete, shift, emoji, or the settings key.
|
|
|
|
* Non-special keys are those that generate a single code point.
|
|
|
|
* This includes all letters, digits, punctuation, separators, emoji. It excludes keys that
|
|
|
|
* manage keyboard-related stuff like shift, language switch, settings, layout switch, or
|
|
|
|
* any key that results in multiple code points like the ".com" key.
|
|
|
|
*
|
2014-08-04 13:17:36 +00:00
|
|
|
* @param event The event to handle.
|
2014-08-04 13:04:31 +00:00
|
|
|
* @param inputTransaction The transaction in progress.
|
|
|
|
*/
|
2014-08-04 13:17:36 +00:00
|
|
|
private void handleFunctionalEvent(final Event event, final InputTransaction inputTransaction,
|
2014-08-04 13:04:31 +00:00
|
|
|
final int currentKeyboardScriptId, final LatinIME.UIHandler handler) {
|
|
|
|
switch (event.mKeyCode) {
|
|
|
|
case Constants.CODE_DELETE:
|
2014-08-04 13:17:36 +00:00
|
|
|
handleBackspaceEvent(event, inputTransaction, currentKeyboardScriptId);
|
2014-08-04 13:04:31 +00:00
|
|
|
// Backspace is a functional key, but it affects the contents of the editor.
|
|
|
|
inputTransaction.setDidAffectContents();
|
|
|
|
break;
|
|
|
|
case Constants.CODE_SHIFT:
|
|
|
|
performRecapitalization(inputTransaction.mSettingsValues);
|
|
|
|
inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW);
|
2014-08-26 09:54:08 +00:00
|
|
|
if (mSuggestedWords.isPrediction()) {
|
2014-08-04 13:04:31 +00:00
|
|
|
inputTransaction.setRequiresUpdateSuggestions();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Constants.CODE_CAPSLOCK:
|
|
|
|
// Note: Changing keyboard to shift lock state is handled in
|
2014-08-26 09:48:18 +00:00
|
|
|
// {@link KeyboardSwitcher#onEvent(Event)}.
|
2014-08-04 13:04:31 +00:00
|
|
|
break;
|
|
|
|
case Constants.CODE_SYMBOL_SHIFT:
|
|
|
|
// Note: Calling back to the keyboard on the symbol Shift key is handled in
|
|
|
|
// {@link #onPressKey(int,int,boolean)} and {@link #onReleaseKey(int,boolean)}.
|
|
|
|
break;
|
|
|
|
case Constants.CODE_SWITCH_ALPHA_SYMBOL:
|
|
|
|
// Note: Calling back to the keyboard on symbol key is handled in
|
|
|
|
// {@link #onPressKey(int,int,boolean)} and {@link #onReleaseKey(int,boolean)}.
|
|
|
|
break;
|
|
|
|
case Constants.CODE_SETTINGS:
|
|
|
|
onSettingsKeyPressed();
|
|
|
|
break;
|
|
|
|
case Constants.CODE_SHORTCUT:
|
|
|
|
// We need to switch to the shortcut IME. This is handled by LatinIME since the
|
|
|
|
// input logic has no business with IME switching.
|
|
|
|
break;
|
|
|
|
case Constants.CODE_ACTION_NEXT:
|
|
|
|
performEditorAction(EditorInfo.IME_ACTION_NEXT);
|
|
|
|
break;
|
|
|
|
case Constants.CODE_ACTION_PREVIOUS:
|
|
|
|
performEditorAction(EditorInfo.IME_ACTION_PREVIOUS);
|
|
|
|
break;
|
|
|
|
case Constants.CODE_LANGUAGE_SWITCH:
|
|
|
|
handleLanguageSwitchKey();
|
|
|
|
break;
|
|
|
|
case Constants.CODE_EMOJI:
|
|
|
|
// Note: Switching emoji keyboard is being handled in
|
2014-08-26 09:48:18 +00:00
|
|
|
// {@link KeyboardState#onEvent(Event,int)}.
|
2014-08-04 13:04:31 +00:00
|
|
|
break;
|
|
|
|
case Constants.CODE_ALPHA_FROM_EMOJI:
|
|
|
|
// Note: Switching back from Emoji keyboard to the main keyboard is being
|
2014-08-26 09:48:18 +00:00
|
|
|
// handled in {@link KeyboardState#onEvent(Event,int)}.
|
2014-08-04 13:04:31 +00:00
|
|
|
break;
|
|
|
|
case Constants.CODE_SHIFT_ENTER:
|
|
|
|
final Event tmpEvent = Event.createSoftwareKeypressEvent(Constants.CODE_ENTER,
|
|
|
|
event.mKeyCode, event.mX, event.mY, event.isKeyRepeat());
|
2014-08-04 13:17:36 +00:00
|
|
|
handleNonSpecialCharacterEvent(tmpEvent, inputTransaction, handler);
|
2014-08-04 13:04:31 +00:00
|
|
|
// Shift + Enter is treated as a functional key but it results in adding a new
|
|
|
|
// line, so that does affect the contents of the editor.
|
|
|
|
inputTransaction.setDidAffectContents();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new RuntimeException("Unknown key code : " + event.mKeyCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle an event that is not a functional event.
|
|
|
|
*
|
|
|
|
* These events are generally events that cause input, but in some cases they may do other
|
|
|
|
* things like trigger an editor action.
|
|
|
|
*
|
2014-08-04 13:17:36 +00:00
|
|
|
* @param event The event to handle.
|
2014-08-04 13:04:31 +00:00
|
|
|
* @param inputTransaction The transaction in progress.
|
|
|
|
*/
|
2014-08-04 13:17:36 +00:00
|
|
|
private void handleNonFunctionalEvent(final Event event,
|
|
|
|
final InputTransaction inputTransaction,
|
2014-08-04 13:04:31 +00:00
|
|
|
final LatinIME.UIHandler handler) {
|
|
|
|
inputTransaction.setDidAffectContents();
|
|
|
|
switch (event.mCodePoint) {
|
|
|
|
case Constants.CODE_ENTER:
|
|
|
|
final EditorInfo editorInfo = getCurrentInputEditorInfo();
|
|
|
|
final int imeOptionsActionId =
|
|
|
|
InputTypeUtils.getImeOptionsActionIdFromEditorInfo(editorInfo);
|
|
|
|
if (InputTypeUtils.IME_ACTION_CUSTOM_LABEL == imeOptionsActionId) {
|
|
|
|
// Either we have an actionLabel and we should performEditorAction with
|
|
|
|
// actionId regardless of its value.
|
|
|
|
performEditorAction(editorInfo.actionId);
|
|
|
|
} else if (EditorInfo.IME_ACTION_NONE != imeOptionsActionId) {
|
|
|
|
// We didn't have an actionLabel, but we had another action to execute.
|
|
|
|
// EditorInfo.IME_ACTION_NONE explicitly means no action. In contrast,
|
|
|
|
// EditorInfo.IME_ACTION_UNSPECIFIED is the default value for an action, so it
|
|
|
|
// means there should be an action and the app didn't bother to set a specific
|
|
|
|
// code for it - presumably it only handles one. It does not have to be treated
|
|
|
|
// in any specific way: anything that is not IME_ACTION_NONE should be sent to
|
|
|
|
// performEditorAction.
|
|
|
|
performEditorAction(imeOptionsActionId);
|
|
|
|
} else {
|
|
|
|
// No action label, and the action from imeOptions is NONE: this is a regular
|
|
|
|
// enter key that should input a carriage return.
|
2014-08-04 13:17:36 +00:00
|
|
|
handleNonSpecialCharacterEvent(event, inputTransaction, handler);
|
2014-08-04 13:04:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2014-08-04 13:17:36 +00:00
|
|
|
handleNonSpecialCharacterEvent(event, inputTransaction, handler);
|
2014-08-04 13:04:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:34:35 +00:00
|
|
|
/**
|
|
|
|
* Handle inputting a code point to the editor.
|
|
|
|
*
|
|
|
|
* Non-special keys are those that generate a single code point.
|
|
|
|
* This includes all letters, digits, punctuation, separators, emoji. It excludes keys that
|
|
|
|
* manage keyboard-related stuff like shift, language switch, settings, layout switch, or
|
|
|
|
* any key that results in multiple code points like the ".com" key.
|
|
|
|
*
|
2014-08-04 13:17:36 +00:00
|
|
|
* @param event The event to handle.
|
2014-03-04 06:29:54 +00:00
|
|
|
* @param inputTransaction The transaction in progress.
|
2013-12-19 05:34:35 +00:00
|
|
|
*/
|
2014-08-04 13:17:36 +00:00
|
|
|
private void handleNonSpecialCharacterEvent(final Event event,
|
|
|
|
final InputTransaction inputTransaction,
|
2014-07-31 09:33:30 +00:00
|
|
|
final LatinIME.UIHandler handler) {
|
2014-08-04 13:17:36 +00:00
|
|
|
final int codePoint = event.mCodePoint;
|
2013-12-19 07:37:07 +00:00
|
|
|
mSpaceState = SpaceState.NONE;
|
2014-03-12 08:02:13 +00:00
|
|
|
if (inputTransaction.mSettingsValues.isWordSeparator(codePoint)
|
|
|
|
|| Character.getType(codePoint) == Character.OTHER_SYMBOL) {
|
2014-08-04 13:17:36 +00:00
|
|
|
handleSeparatorEvent(event, inputTransaction, handler);
|
2013-12-19 07:37:07 +00:00
|
|
|
} else {
|
2014-03-04 07:31:51 +00:00
|
|
|
if (SpaceState.PHANTOM == inputTransaction.mSpaceState) {
|
2013-12-19 07:37:07 +00:00
|
|
|
if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) {
|
|
|
|
// If we are in the middle of a recorrection, we need to commit the recorrection
|
|
|
|
// first so that we can insert the character at the current cursor position.
|
2015-04-02 00:05:37 +00:00
|
|
|
// We also need to unlearn the original word that is now being corrected.
|
|
|
|
unlearnWord(mWordComposer.getTypedWord(), inputTransaction.mSettingsValues,
|
|
|
|
Constants.EVENT_BACKSPACE);
|
2014-03-03 05:19:52 +00:00
|
|
|
resetEntireInputState(mConnection.getExpectedSelectionStart(),
|
2014-02-19 12:41:45 +00:00
|
|
|
mConnection.getExpectedSelectionEnd(), true /* clearSuggestionStrip */);
|
2013-12-19 07:37:07 +00:00
|
|
|
} else {
|
2014-03-04 07:46:42 +00:00
|
|
|
commitTyped(inputTransaction.mSettingsValues, LastComposedWord.NOT_A_SEPARATOR);
|
2013-12-19 07:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-04 13:17:36 +00:00
|
|
|
handleNonSeparatorEvent(event, inputTransaction.mSettingsValues, inputTransaction);
|
2013-12-19 07:37:07 +00:00
|
|
|
}
|
2013-12-19 05:34:35 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 08:57:10 +00:00
|
|
|
/**
|
|
|
|
* Handle a non-separator.
|
2014-08-04 13:17:36 +00:00
|
|
|
* @param event The event to handle.
|
2013-12-19 08:57:10 +00:00
|
|
|
* @param settingsValues The current settings values.
|
2014-03-04 06:29:54 +00:00
|
|
|
* @param inputTransaction The transaction in progress.
|
2013-12-19 08:57:10 +00:00
|
|
|
*/
|
2014-08-04 13:17:36 +00:00
|
|
|
private void handleNonSeparatorEvent(final Event event, final SettingsValues settingsValues,
|
2014-07-31 09:33:30 +00:00
|
|
|
final InputTransaction inputTransaction) {
|
2014-08-04 13:17:36 +00:00
|
|
|
final int codePoint = event.mCodePoint;
|
2013-12-19 08:57:10 +00:00
|
|
|
// TODO: refactor this method to stop flipping isComposingWord around all the time, and
|
2014-08-04 13:17:36 +00:00
|
|
|
// make it shorter (possibly cut into several pieces). Also factor
|
|
|
|
// handleNonSpecialCharacterEvent which has the same name as other handle* methods but is
|
|
|
|
// not the same.
|
2013-12-19 08:57:10 +00:00
|
|
|
boolean isComposingWord = mWordComposer.isComposingWord();
|
|
|
|
|
|
|
|
// TODO: remove isWordConnector() and use isUsuallyFollowedBySpace() instead.
|
|
|
|
// See onStartBatchInput() to see how to do it.
|
2014-03-04 07:31:51 +00:00
|
|
|
if (SpaceState.PHANTOM == inputTransaction.mSpaceState
|
2014-03-12 08:02:13 +00:00
|
|
|
&& !settingsValues.isWordConnector(codePoint)) {
|
2013-12-19 08:57:10 +00:00
|
|
|
if (isComposingWord) {
|
|
|
|
// Sanity check
|
|
|
|
throw new RuntimeException("Should not be composing here");
|
|
|
|
}
|
2014-11-18 02:49:45 +00:00
|
|
|
insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues);
|
2013-12-19 08:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) {
|
|
|
|
// If we are in the middle of a recorrection, we need to commit the recorrection
|
|
|
|
// first so that we can insert the character at the current cursor position.
|
2015-04-02 00:05:37 +00:00
|
|
|
// We also need to unlearn the original word that is now being corrected.
|
|
|
|
unlearnWord(mWordComposer.getTypedWord(), inputTransaction.mSettingsValues,
|
|
|
|
Constants.EVENT_BACKSPACE);
|
2014-03-03 05:19:52 +00:00
|
|
|
resetEntireInputState(mConnection.getExpectedSelectionStart(),
|
2014-02-19 12:41:45 +00:00
|
|
|
mConnection.getExpectedSelectionEnd(), true /* clearSuggestionStrip */);
|
2013-12-19 08:57:10 +00:00
|
|
|
isComposingWord = false;
|
|
|
|
}
|
|
|
|
// We want to find out whether to start composing a new word with this character. If so,
|
|
|
|
// we need to reset the composing state and switch isComposingWord. The order of the
|
|
|
|
// tests is important for good performance.
|
|
|
|
// We only start composing if we're not already composing.
|
|
|
|
if (!isComposingWord
|
|
|
|
// We only start composing if this is a word code point. Essentially that means it's a
|
|
|
|
// a letter or a word connector.
|
2014-03-12 08:02:13 +00:00
|
|
|
&& settingsValues.isWordCodePoint(codePoint)
|
2013-12-19 08:57:10 +00:00
|
|
|
// We never go into composing state if suggestions are not requested.
|
2014-07-22 17:18:38 +00:00
|
|
|
&& settingsValues.needsToLookupSuggestions() &&
|
2013-12-19 08:57:10 +00:00
|
|
|
// In languages with spaces, we only start composing a word when we are not already
|
|
|
|
// touching a word. In languages without spaces, the above conditions are sufficient.
|
2014-01-17 04:14:29 +00:00
|
|
|
(!mConnection.isCursorTouchingWord(settingsValues.mSpacingAndPunctuations)
|
2014-01-08 09:06:32 +00:00
|
|
|
|| !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces)) {
|
2013-12-19 08:57:10 +00:00
|
|
|
// Reset entirely the composing state anyway, then start composing a new word unless
|
2014-05-30 11:30:17 +00:00
|
|
|
// the character is a word connector. The idea here is, word connectors are not
|
|
|
|
// separators and they should be treated as normal characters, except in the first
|
|
|
|
// position where they should not start composing a word.
|
|
|
|
isComposingWord = !settingsValues.mSpacingAndPunctuations.isWordConnector(codePoint);
|
2013-12-19 08:57:10 +00:00
|
|
|
// Here we don't need to reset the last composed word. It will be reset
|
|
|
|
// when we commit this one, if we ever do; if on the other hand we backspace
|
|
|
|
// it entirely and resume suggestions on the previous word, we'd like to still
|
|
|
|
// have touch coordinates for it.
|
|
|
|
resetComposingState(false /* alsoResetLastComposedWord */);
|
|
|
|
}
|
|
|
|
if (isComposingWord) {
|
2014-08-04 13:17:36 +00:00
|
|
|
mWordComposer.applyProcessedEvent(event);
|
2013-12-19 08:57:10 +00:00
|
|
|
// If it's the first letter, make note of auto-caps state
|
2014-03-28 05:35:38 +00:00
|
|
|
if (mWordComposer.isSingleLetter()) {
|
2014-06-09 10:31:41 +00:00
|
|
|
mWordComposer.setCapitalizedModeAtStartComposingTime(inputTransaction.mShiftState);
|
2013-12-19 08:57:10 +00:00
|
|
|
}
|
2014-08-27 04:55:48 +00:00
|
|
|
setComposingTextInternal(getTextWithUnderline(mWordComposer.getTypedWord()), 1);
|
2013-12-19 08:57:10 +00:00
|
|
|
} else {
|
2014-08-04 13:17:36 +00:00
|
|
|
final boolean swapWeakSpace = tryStripSpaceAndReturnWhetherShouldSwapInstead(event,
|
2014-07-31 09:33:30 +00:00
|
|
|
inputTransaction);
|
2013-12-19 08:57:10 +00:00
|
|
|
|
2014-08-04 13:17:36 +00:00
|
|
|
if (swapWeakSpace && trySwapSwapperAndSpace(event, inputTransaction)) {
|
2013-12-19 08:57:10 +00:00
|
|
|
mSpaceState = SpaceState.WEAK;
|
2014-06-20 07:41:23 +00:00
|
|
|
} else {
|
|
|
|
sendKeyCodePoint(settingsValues, codePoint);
|
2013-12-19 08:57:10 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-05 07:37:44 +00:00
|
|
|
inputTransaction.setRequiresUpdateSuggestions();
|
2013-12-19 08:57:10 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 08:14:11 +00:00
|
|
|
/**
|
|
|
|
* Handle input of a separator code point.
|
2014-08-04 13:17:36 +00:00
|
|
|
* @param event The event to handle.
|
2014-03-04 06:29:54 +00:00
|
|
|
* @param inputTransaction The transaction in progress.
|
2013-12-19 08:14:11 +00:00
|
|
|
*/
|
2014-08-04 13:17:36 +00:00
|
|
|
private void handleSeparatorEvent(final Event event, final InputTransaction inputTransaction,
|
2014-03-04 06:29:54 +00:00
|
|
|
final LatinIME.UIHandler handler) {
|
2014-08-04 13:17:36 +00:00
|
|
|
final int codePoint = event.mCodePoint;
|
2014-05-01 03:33:59 +00:00
|
|
|
final SettingsValues settingsValues = inputTransaction.mSettingsValues;
|
2014-05-29 06:43:40 +00:00
|
|
|
final boolean wasComposingWord = mWordComposer.isComposingWord();
|
2013-12-19 08:14:11 +00:00
|
|
|
// We avoid sending spaces in languages without spaces if we were composing.
|
2014-03-12 08:02:13 +00:00
|
|
|
final boolean shouldAvoidSendingCode = Constants.CODE_SPACE == codePoint
|
2014-05-01 03:33:59 +00:00
|
|
|
&& !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces
|
2014-05-29 06:43:40 +00:00
|
|
|
&& wasComposingWord;
|
2013-12-19 08:14:11 +00:00
|
|
|
if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) {
|
|
|
|
// If we are in the middle of a recorrection, we need to commit the recorrection
|
|
|
|
// first so that we can insert the separator at the current cursor position.
|
2015-04-02 00:05:37 +00:00
|
|
|
// We also need to unlearn the original word that is now being corrected.
|
|
|
|
unlearnWord(mWordComposer.getTypedWord(), inputTransaction.mSettingsValues,
|
|
|
|
Constants.EVENT_BACKSPACE);
|
2014-03-03 05:19:52 +00:00
|
|
|
resetEntireInputState(mConnection.getExpectedSelectionStart(),
|
2014-02-19 12:41:45 +00:00
|
|
|
mConnection.getExpectedSelectionEnd(), true /* clearSuggestionStrip */);
|
2013-12-19 08:14:11 +00:00
|
|
|
}
|
|
|
|
// isComposingWord() may have changed since we stored wasComposing
|
|
|
|
if (mWordComposer.isComposingWord()) {
|
2014-07-22 17:18:38 +00:00
|
|
|
if (settingsValues.mAutoCorrectionEnabledPerUserSettings) {
|
2013-12-19 08:14:11 +00:00
|
|
|
final String separator = shouldAvoidSendingCode ? LastComposedWord.NOT_A_SEPARATOR
|
2014-03-12 08:02:13 +00:00
|
|
|
: StringUtils.newSingleCodePointString(codePoint);
|
2014-05-01 03:33:59 +00:00
|
|
|
commitCurrentAutoCorrection(settingsValues, separator, handler);
|
2014-08-04 13:07:15 +00:00
|
|
|
inputTransaction.setDidAutoCorrect();
|
2013-12-19 08:14:11 +00:00
|
|
|
} else {
|
2014-05-01 03:33:59 +00:00
|
|
|
commitTyped(settingsValues,
|
2014-03-12 08:02:13 +00:00
|
|
|
StringUtils.newSingleCodePointString(codePoint));
|
2013-12-19 08:14:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 13:17:36 +00:00
|
|
|
final boolean swapWeakSpace = tryStripSpaceAndReturnWhetherShouldSwapInstead(event,
|
2014-07-31 09:33:30 +00:00
|
|
|
inputTransaction);
|
2013-12-19 08:14:11 +00:00
|
|
|
|
2014-03-12 08:02:13 +00:00
|
|
|
final boolean isInsideDoubleQuoteOrAfterDigit = Constants.CODE_DOUBLE_QUOTE == codePoint
|
2014-01-17 01:40:05 +00:00
|
|
|
&& mConnection.isInsideDoubleQuoteOrAfterDigit();
|
|
|
|
|
|
|
|
final boolean needsPrecedingSpace;
|
2014-03-04 07:31:51 +00:00
|
|
|
if (SpaceState.PHANTOM != inputTransaction.mSpaceState) {
|
2014-01-17 01:40:05 +00:00
|
|
|
needsPrecedingSpace = false;
|
2014-03-12 08:02:13 +00:00
|
|
|
} else if (Constants.CODE_DOUBLE_QUOTE == codePoint) {
|
2014-01-17 01:40:05 +00:00
|
|
|
// Double quotes behave like they are usually preceded by space iff we are
|
|
|
|
// not inside a double quote or after a digit.
|
|
|
|
needsPrecedingSpace = !isInsideDoubleQuoteOrAfterDigit;
|
2014-05-01 03:33:59 +00:00
|
|
|
} else if (settingsValues.mSpacingAndPunctuations.isClusteringSymbol(codePoint)
|
|
|
|
&& settingsValues.mSpacingAndPunctuations.isClusteringSymbol(
|
|
|
|
mConnection.getCodePointBeforeCursor())) {
|
|
|
|
needsPrecedingSpace = false;
|
2014-01-17 01:40:05 +00:00
|
|
|
} else {
|
2014-05-01 03:33:59 +00:00
|
|
|
needsPrecedingSpace = settingsValues.isUsuallyPrecededBySpace(codePoint);
|
2014-01-17 01:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (needsPrecedingSpace) {
|
2014-11-18 02:49:45 +00:00
|
|
|
insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues);
|
2013-12-19 08:14:11 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 13:17:36 +00:00
|
|
|
if (tryPerformDoubleSpacePeriod(event, inputTransaction)) {
|
2014-06-20 07:41:23 +00:00
|
|
|
mSpaceState = SpaceState.DOUBLE;
|
|
|
|
inputTransaction.setRequiresUpdateSuggestions();
|
2015-02-27 20:48:12 +00:00
|
|
|
StatsUtils.onDoubleSpacePeriod();
|
2014-08-04 13:17:36 +00:00
|
|
|
} else if (swapWeakSpace && trySwapSwapperAndSpace(event, inputTransaction)) {
|
2014-06-20 07:41:23 +00:00
|
|
|
mSpaceState = SpaceState.SWAP_PUNCTUATION;
|
|
|
|
mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
|
|
|
|
} else if (Constants.CODE_SPACE == codePoint) {
|
|
|
|
if (!mSuggestedWords.isPunctuationSuggestions()) {
|
2014-04-03 07:25:05 +00:00
|
|
|
mSpaceState = SpaceState.WEAK;
|
2013-12-19 08:14:11 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 06:55:45 +00:00
|
|
|
startDoubleSpacePeriodCountdown(inputTransaction);
|
2014-06-13 08:17:35 +00:00
|
|
|
if (wasComposingWord || mSuggestedWords.isEmpty()) {
|
2014-05-29 06:43:40 +00:00
|
|
|
inputTransaction.setRequiresUpdateSuggestions();
|
|
|
|
}
|
2014-06-20 07:41:23 +00:00
|
|
|
|
|
|
|
if (!shouldAvoidSendingCode) {
|
|
|
|
sendKeyCodePoint(settingsValues, codePoint);
|
|
|
|
}
|
2013-12-19 08:14:11 +00:00
|
|
|
} else {
|
2014-06-20 07:41:23 +00:00
|
|
|
if ((SpaceState.PHANTOM == inputTransaction.mSpaceState
|
2014-05-01 03:33:59 +00:00
|
|
|
&& settingsValues.isUsuallyFollowedBySpace(codePoint))
|
2014-03-12 08:02:13 +00:00
|
|
|
|| (Constants.CODE_DOUBLE_QUOTE == codePoint
|
2014-01-17 01:40:05 +00:00
|
|
|
&& isInsideDoubleQuoteOrAfterDigit)) {
|
2013-12-19 08:14:11 +00:00
|
|
|
// If we are in phantom space state, and the user presses a separator, we want to
|
|
|
|
// stay in phantom space state so that the next keypress has a chance to add the
|
|
|
|
// space. For example, if I type "Good dat", pick "day" from the suggestion strip
|
|
|
|
// then insert a comma and go on to typing the next word, I want the space to be
|
|
|
|
// inserted automatically before the next word, the same way it is when I don't
|
2014-01-17 01:40:05 +00:00
|
|
|
// input the comma. A double quote behaves like it's usually followed by space if
|
|
|
|
// we're inside a double quote.
|
2013-12-19 08:14:11 +00:00
|
|
|
// The case is a little different if the separator is a space stripper. Such a
|
|
|
|
// separator does not normally need a space on the right (that's the difference
|
|
|
|
// between swappers and strippers), so we should not stay in phantom space state if
|
|
|
|
// the separator is a stripper. Hence the additional test above.
|
|
|
|
mSpaceState = SpaceState.PHANTOM;
|
|
|
|
}
|
|
|
|
|
2014-06-20 07:41:23 +00:00
|
|
|
sendKeyCodePoint(settingsValues, codePoint);
|
|
|
|
|
2013-12-19 08:14:11 +00:00
|
|
|
// Set punctuation right away. onUpdateSelection will fire but tests whether it is
|
|
|
|
// already displayed or not, so it's okay.
|
2014-01-24 14:17:40 +00:00
|
|
|
mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
|
2013-12-19 08:14:11 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 06:29:54 +00:00
|
|
|
inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW);
|
2013-12-19 08:14:11 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:34:35 +00:00
|
|
|
/**
|
|
|
|
* Handle a press on the backspace key.
|
2014-08-04 13:17:36 +00:00
|
|
|
* @param event The event to handle.
|
2014-03-04 06:29:54 +00:00
|
|
|
* @param inputTransaction The transaction in progress.
|
2013-12-19 05:34:35 +00:00
|
|
|
*/
|
2014-08-04 13:17:36 +00:00
|
|
|
private void handleBackspaceEvent(final Event event, final InputTransaction inputTransaction,
|
2014-07-31 09:33:30 +00:00
|
|
|
final int currentKeyboardScriptId) {
|
2013-12-19 10:29:05 +00:00
|
|
|
mSpaceState = SpaceState.NONE;
|
2013-12-19 07:07:24 +00:00
|
|
|
mDeleteCount++;
|
|
|
|
|
2014-03-31 10:43:12 +00:00
|
|
|
// In many cases after backspace, we need to update the shift state. Normally we need
|
|
|
|
// to do this right away to avoid the shift state being out of date in case the user types
|
|
|
|
// backspace then some other character very fast. However, in the case of backspace key
|
|
|
|
// repeat, this can lead to flashiness when the cursor flies over positions where the
|
|
|
|
// shift state should be updated, so if this is a key repeat, we update after a small delay.
|
|
|
|
// Then again, even in the case of a key repeat, if the cursor is at start of text, it
|
|
|
|
// can't go any further back, so we can update right away even if it's a key repeat.
|
|
|
|
final int shiftUpdateKind =
|
2014-07-31 09:33:30 +00:00
|
|
|
event.isKeyRepeat() && mConnection.getExpectedSelectionStart() > 0
|
2014-03-31 10:43:12 +00:00
|
|
|
? InputTransaction.SHIFT_UPDATE_LATER : InputTransaction.SHIFT_UPDATE_NOW;
|
|
|
|
inputTransaction.requireShiftUpdate(shiftUpdateKind);
|
2013-12-19 07:07:24 +00:00
|
|
|
|
|
|
|
if (mWordComposer.isCursorFrontOrMiddleOfComposingWord()) {
|
|
|
|
// If we are in the middle of a recorrection, we need to commit the recorrection
|
|
|
|
// first so that we can remove the character at the current cursor position.
|
2015-04-02 00:05:37 +00:00
|
|
|
// We also need to unlearn the original word that is now being corrected.
|
|
|
|
unlearnWord(mWordComposer.getTypedWord(), inputTransaction.mSettingsValues,
|
|
|
|
Constants.EVENT_BACKSPACE);
|
2014-03-03 05:19:52 +00:00
|
|
|
resetEntireInputState(mConnection.getExpectedSelectionStart(),
|
2014-02-19 12:41:45 +00:00
|
|
|
mConnection.getExpectedSelectionEnd(), true /* clearSuggestionStrip */);
|
2013-12-19 07:07:24 +00:00
|
|
|
// When we exit this if-clause, mWordComposer.isComposingWord() will return false.
|
|
|
|
}
|
|
|
|
if (mWordComposer.isComposingWord()) {
|
|
|
|
if (mWordComposer.isBatchMode()) {
|
|
|
|
final String rejectedSuggestion = mWordComposer.getTypedWord();
|
|
|
|
mWordComposer.reset();
|
|
|
|
mWordComposer.setRejectedBatchModeSuggestion(rejectedSuggestion);
|
2014-06-20 05:46:13 +00:00
|
|
|
if (!TextUtils.isEmpty(rejectedSuggestion)) {
|
2015-02-19 01:50:33 +00:00
|
|
|
unlearnWord(rejectedSuggestion, inputTransaction.mSettingsValues,
|
|
|
|
Constants.EVENT_REJECTION);
|
2014-06-20 05:46:13 +00:00
|
|
|
}
|
2014-09-10 01:54:49 +00:00
|
|
|
StatsUtils.onBackspaceWordDelete(rejectedSuggestion.length());
|
2013-12-19 07:07:24 +00:00
|
|
|
} else {
|
2014-07-31 09:33:30 +00:00
|
|
|
mWordComposer.applyProcessedEvent(event);
|
2014-09-10 01:54:49 +00:00
|
|
|
StatsUtils.onBackspacePressed(1);
|
2013-12-19 07:07:24 +00:00
|
|
|
}
|
2014-05-14 07:55:32 +00:00
|
|
|
if (mWordComposer.isComposingWord()) {
|
2014-08-27 04:55:48 +00:00
|
|
|
setComposingTextInternal(getTextWithUnderline(mWordComposer.getTypedWord()), 1);
|
2014-05-14 07:55:32 +00:00
|
|
|
} else {
|
|
|
|
mConnection.commitText("", 1);
|
|
|
|
}
|
2014-03-05 07:37:44 +00:00
|
|
|
inputTransaction.setRequiresUpdateSuggestions();
|
2013-12-19 07:07:24 +00:00
|
|
|
} else {
|
|
|
|
if (mLastComposedWord.canRevertCommit()) {
|
2014-09-18 05:53:59 +00:00
|
|
|
final String lastComposedWord = mLastComposedWord.mTypedWord;
|
2014-09-14 07:53:33 +00:00
|
|
|
revertCommit(inputTransaction, inputTransaction.mSettingsValues);
|
2014-09-10 01:54:49 +00:00
|
|
|
StatsUtils.onRevertAutoCorrect();
|
2014-09-18 05:53:59 +00:00
|
|
|
StatsUtils.onWordCommitUserTyped(lastComposedWord, mWordComposer.isBatchMode());
|
2015-03-05 23:32:14 +00:00
|
|
|
// Restart suggestions when backspacing into a reverted word. This is required for
|
|
|
|
// the final corrected word to be learned, as learning only occurs when suggestions
|
|
|
|
// are active.
|
|
|
|
//
|
|
|
|
// Note: restartSuggestionsOnWordTouchedByCursor is already called for normal
|
|
|
|
// (non-revert) backspace handling.
|
|
|
|
if (inputTransaction.mSettingsValues.isSuggestionsEnabledPerUserSettings()
|
|
|
|
&& inputTransaction.mSettingsValues.mSpacingAndPunctuations
|
|
|
|
.mCurrentLanguageHasSpaces
|
|
|
|
&& !mConnection.isCursorFollowedByWordCharacter(
|
|
|
|
inputTransaction.mSettingsValues.mSpacingAndPunctuations)) {
|
|
|
|
restartSuggestionsOnWordTouchedByCursor(inputTransaction.mSettingsValues,
|
|
|
|
false /* forStartInput */, currentKeyboardScriptId);
|
|
|
|
}
|
2013-12-19 07:07:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mEnteredText != null && mConnection.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.
|
|
|
|
mConnection.deleteSurroundingText(mEnteredText.length(), 0);
|
2014-09-10 01:54:49 +00:00
|
|
|
StatsUtils.onDeleteMultiCharInput(mEnteredText.length());
|
2013-12-19 07:07:24 +00:00
|
|
|
mEnteredText = null;
|
|
|
|
// If we have mEnteredText, then we know that mHasUncommittedTypedChars == false.
|
|
|
|
// In addition we know that spaceState is false, and that we should not be
|
|
|
|
// reverting any autocorrect at this point. So we can safely return.
|
|
|
|
return;
|
|
|
|
}
|
2014-03-04 07:31:51 +00:00
|
|
|
if (SpaceState.DOUBLE == inputTransaction.mSpaceState) {
|
2014-03-05 06:55:45 +00:00
|
|
|
cancelDoubleSpacePeriodCountdown();
|
2014-09-08 09:31:54 +00:00
|
|
|
if (mConnection.revertDoubleSpacePeriod(
|
|
|
|
inputTransaction.mSettingsValues.mSpacingAndPunctuations)) {
|
2013-12-19 07:07:24 +00:00
|
|
|
// No need to reset mSpaceState, it has already be done (that's why we
|
|
|
|
// receive it as a parameter)
|
2014-05-29 06:43:40 +00:00
|
|
|
inputTransaction.setRequiresUpdateSuggestions();
|
2014-06-09 10:31:41 +00:00
|
|
|
mWordComposer.setCapitalizedModeAtStartComposingTime(
|
|
|
|
WordComposer.CAPS_MODE_OFF);
|
2014-09-10 01:54:49 +00:00
|
|
|
StatsUtils.onRevertDoubleSpacePeriod();
|
2013-12-19 07:07:24 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-03-04 07:31:51 +00:00
|
|
|
} else if (SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState) {
|
2013-12-19 07:07:24 +00:00
|
|
|
if (mConnection.revertSwapPunctuation()) {
|
2014-09-10 01:54:49 +00:00
|
|
|
StatsUtils.onRevertSwapPunctuation();
|
2013-12-19 07:07:24 +00:00
|
|
|
// Likewise
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-19 01:50:33 +00:00
|
|
|
boolean hasUnlearnedWordBeingDeleted = false;
|
|
|
|
|
2013-12-19 07:07:24 +00:00
|
|
|
// No cancelling of commit/double space/swap: we have a regular backspace.
|
|
|
|
// We should backspace one char and restart suggestion if at the end of a word.
|
2014-01-09 05:09:26 +00:00
|
|
|
if (mConnection.hasSelection()) {
|
2013-12-19 07:07:24 +00:00
|
|
|
// If there is a selection, remove it.
|
2015-04-02 00:05:37 +00:00
|
|
|
// We also need to unlearn the selected text.
|
|
|
|
final CharSequence selection = mConnection.getSelectedText(0 /* 0 for no styles */);
|
|
|
|
unlearnWord(selection.toString(), inputTransaction.mSettingsValues,
|
|
|
|
Constants.EVENT_BACKSPACE);
|
2014-01-09 05:09:26 +00:00
|
|
|
final int numCharsDeleted = mConnection.getExpectedSelectionEnd()
|
|
|
|
- mConnection.getExpectedSelectionStart();
|
|
|
|
mConnection.setSelection(mConnection.getExpectedSelectionEnd(),
|
|
|
|
mConnection.getExpectedSelectionEnd());
|
2013-12-19 07:07:24 +00:00
|
|
|
mConnection.deleteSurroundingText(numCharsDeleted, 0);
|
2014-09-10 01:54:49 +00:00
|
|
|
StatsUtils.onBackspaceSelectedText(numCharsDeleted);
|
2013-12-19 07:07:24 +00:00
|
|
|
} else {
|
|
|
|
// There is no selection, just delete one character.
|
2014-10-01 09:06:26 +00:00
|
|
|
if (inputTransaction.mSettingsValues.isBeforeJellyBean()
|
|
|
|
|| inputTransaction.mSettingsValues.mInputAttributes.isTypeNull()
|
|
|
|
|| Constants.NOT_A_CURSOR_POSITION
|
|
|
|
== mConnection.getExpectedSelectionEnd()) {
|
|
|
|
// There are three possible reasons to send a key event: either the field has
|
2013-12-19 07:07:24 +00:00
|
|
|
// type TYPE_NULL, in which case the keyboard should send events, or we are
|
2014-10-01 09:06:26 +00:00
|
|
|
// running in backward compatibility mode, or we don't know the cursor position.
|
|
|
|
// 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, so we retain this behavior if
|
|
|
|
// the app has target SDK < JellyBean.
|
|
|
|
// As for the case where we don't know the cursor position, it can happen
|
|
|
|
// because of bugs in the framework. But the framework should know, so the next
|
|
|
|
// best thing is to leave it to whatever it thinks is best.
|
2013-12-19 07:07:24 +00:00
|
|
|
sendDownUpKeyEvent(KeyEvent.KEYCODE_DEL);
|
2014-09-10 01:54:49 +00:00
|
|
|
int totalDeletedLength = 1;
|
2013-12-19 07:07:24 +00:00
|
|
|
if (mDeleteCount > Constants.DELETE_ACCELERATE_AT) {
|
2015-02-19 01:50:33 +00:00
|
|
|
// If this is an accelerated (i.e., double) deletion, then we need to
|
2015-02-24 18:30:27 +00:00
|
|
|
// consider unlearning here because we may have already reached
|
|
|
|
// the previous word, and will lose it after next deletion.
|
2015-02-19 01:50:33 +00:00
|
|
|
hasUnlearnedWordBeingDeleted |= unlearnWordBeingDeleted(
|
|
|
|
inputTransaction.mSettingsValues, currentKeyboardScriptId);
|
2013-12-19 07:07:24 +00:00
|
|
|
sendDownUpKeyEvent(KeyEvent.KEYCODE_DEL);
|
2014-09-10 01:54:49 +00:00
|
|
|
totalDeletedLength++;
|
2013-12-19 07:07:24 +00:00
|
|
|
}
|
2014-09-10 01:54:49 +00:00
|
|
|
StatsUtils.onBackspacePressed(totalDeletedLength);
|
2013-12-19 07:07:24 +00:00
|
|
|
} else {
|
|
|
|
final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor();
|
|
|
|
if (codePointBeforeCursor == Constants.NOT_A_CODE) {
|
2014-02-13 09:15:22 +00:00
|
|
|
// HACK for backward compatibility with broken apps that haven't realized
|
|
|
|
// yet that hardware keyboards are not the only way of inputting text.
|
|
|
|
// Nothing to delete before the cursor. We should not do anything, but many
|
|
|
|
// broken apps expect something to happen in this case so that they can
|
|
|
|
// catch it and have their broken interface react. If you need the keyboard
|
|
|
|
// to do this, you're doing it wrong -- please fix your app.
|
|
|
|
mConnection.deleteSurroundingText(1, 0);
|
2014-09-10 01:54:49 +00:00
|
|
|
// TODO: Add a new StatsUtils method onBackspaceWhenNoText()
|
2013-12-19 07:07:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
final int lengthToDelete =
|
|
|
|
Character.isSupplementaryCodePoint(codePointBeforeCursor) ? 2 : 1;
|
|
|
|
mConnection.deleteSurroundingText(lengthToDelete, 0);
|
2014-09-10 01:54:49 +00:00
|
|
|
int totalDeletedLength = lengthToDelete;
|
2013-12-19 07:07:24 +00:00
|
|
|
if (mDeleteCount > Constants.DELETE_ACCELERATE_AT) {
|
2015-02-19 01:50:33 +00:00
|
|
|
// If this is an accelerated (i.e., double) deletion, then we need to
|
2015-02-24 18:30:27 +00:00
|
|
|
// consider unlearning here because we may have already reached
|
|
|
|
// the previous word, and will lose it after next deletion.
|
2015-02-19 01:50:33 +00:00
|
|
|
hasUnlearnedWordBeingDeleted |= unlearnWordBeingDeleted(
|
|
|
|
inputTransaction.mSettingsValues, currentKeyboardScriptId);
|
2013-12-19 07:07:24 +00:00
|
|
|
final int codePointBeforeCursorToDeleteAgain =
|
|
|
|
mConnection.getCodePointBeforeCursor();
|
|
|
|
if (codePointBeforeCursorToDeleteAgain != Constants.NOT_A_CODE) {
|
|
|
|
final int lengthToDeleteAgain = Character.isSupplementaryCodePoint(
|
|
|
|
codePointBeforeCursorToDeleteAgain) ? 2 : 1;
|
|
|
|
mConnection.deleteSurroundingText(lengthToDeleteAgain, 0);
|
2014-09-10 01:54:49 +00:00
|
|
|
totalDeletedLength += lengthToDeleteAgain;
|
2013-12-19 07:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-10 01:54:49 +00:00
|
|
|
StatsUtils.onBackspacePressed(totalDeletedLength);
|
2013-12-19 07:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-19 01:50:33 +00:00
|
|
|
if (!hasUnlearnedWordBeingDeleted) {
|
|
|
|
// Consider unlearning the word being deleted (if we have not done so already).
|
|
|
|
unlearnWordBeingDeleted(
|
|
|
|
inputTransaction.mSettingsValues, currentKeyboardScriptId);
|
|
|
|
}
|
2014-11-18 06:32:21 +00:00
|
|
|
if (inputTransaction.mSettingsValues.isSuggestionsEnabledPerUserSettings()
|
2014-03-04 07:46:42 +00:00
|
|
|
&& inputTransaction.mSettingsValues.mSpacingAndPunctuations
|
|
|
|
.mCurrentLanguageHasSpaces
|
2014-01-17 04:14:29 +00:00
|
|
|
&& !mConnection.isCursorFollowedByWordCharacter(
|
2014-03-04 07:46:42 +00:00
|
|
|
inputTransaction.mSettingsValues.mSpacingAndPunctuations)) {
|
|
|
|
restartSuggestionsOnWordTouchedByCursor(inputTransaction.mSettingsValues,
|
2014-11-18 06:32:21 +00:00
|
|
|
false /* forStartInput */, currentKeyboardScriptId);
|
2013-12-19 07:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-19 05:34:35 +00:00
|
|
|
}
|
|
|
|
|
2015-02-19 01:50:33 +00:00
|
|
|
boolean unlearnWordBeingDeleted(
|
|
|
|
final SettingsValues settingsValues,final int currentKeyboardScriptId) {
|
|
|
|
// If we just started backspacing to delete a previous word (but have not
|
|
|
|
// entered the composing state yet), unlearn the word.
|
|
|
|
// TODO: Consider tracking whether or not this word was typed by the user.
|
|
|
|
if (!mConnection.hasSelection()
|
|
|
|
&& settingsValues.isSuggestionsEnabledPerUserSettings()
|
|
|
|
&& settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces
|
|
|
|
&& !mConnection.isCursorFollowedByWordCharacter(
|
|
|
|
settingsValues.mSpacingAndPunctuations)) {
|
|
|
|
final TextRange range = mConnection.getWordRangeAtCursor(
|
|
|
|
settingsValues.mSpacingAndPunctuations,
|
|
|
|
currentKeyboardScriptId);
|
2015-02-24 18:30:27 +00:00
|
|
|
if (range == null) {
|
|
|
|
// Happens if we don't have an input connection at all.
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-19 01:50:33 +00:00
|
|
|
final String wordBeingDeleted = range.mWord.toString();
|
|
|
|
if (!wordBeingDeleted.isEmpty()) {
|
|
|
|
unlearnWord(wordBeingDeleted, settingsValues,
|
|
|
|
Constants.EVENT_BACKSPACE);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlearnWord(final String word, final SettingsValues settingsValues, final int eventType) {
|
|
|
|
final NgramContext ngramContext = mConnection.getNgramContextFromNthPreviousWord(
|
|
|
|
settingsValues.mSpacingAndPunctuations, 2);
|
2015-02-24 00:50:02 +00:00
|
|
|
final long timeStampInSeconds = TimeUnit.MILLISECONDS.toSeconds(
|
2015-02-19 01:50:33 +00:00
|
|
|
System.currentTimeMillis());
|
|
|
|
mDictionaryFacilitator.unlearnFromUserHistory(
|
|
|
|
word, ngramContext, timeStampInSeconds, eventType);
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:34:35 +00:00
|
|
|
/**
|
|
|
|
* Handle a press on the language switch key (the "globe key")
|
|
|
|
*/
|
|
|
|
private void handleLanguageSwitchKey() {
|
2013-12-19 11:36:32 +00:00
|
|
|
mLatinIME.switchToNextSubtype();
|
2013-12-19 05:34:35 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Swap a space with a space-swapping punctuation sign.
|
|
|
|
*
|
|
|
|
* This method will check that there are two characters before the cursor and that the first
|
|
|
|
* one is a space before it does the actual swapping.
|
2014-08-04 13:17:36 +00:00
|
|
|
* @param event The event to handle.
|
2014-03-04 06:29:54 +00:00
|
|
|
* @param inputTransaction The transaction in progress.
|
2014-06-20 06:26:09 +00:00
|
|
|
* @return true if the swap has been performed, false if it was prevented by preliminary checks.
|
2013-12-19 10:29:05 +00:00
|
|
|
*/
|
2014-08-04 13:17:36 +00:00
|
|
|
private boolean trySwapSwapperAndSpace(final Event event,
|
|
|
|
final InputTransaction inputTransaction) {
|
2014-06-20 07:41:23 +00:00
|
|
|
final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor();
|
|
|
|
if (Constants.CODE_SPACE != codePointBeforeCursor) {
|
|
|
|
return false;
|
2013-12-19 08:14:11 +00:00
|
|
|
}
|
2014-06-20 07:41:23 +00:00
|
|
|
mConnection.deleteSurroundingText(1, 0);
|
2014-08-04 13:17:36 +00:00
|
|
|
final String text = event.getTextToCommit() + " ";
|
2014-06-20 07:41:23 +00:00
|
|
|
mConnection.commitText(text, 1);
|
|
|
|
inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW);
|
|
|
|
return true;
|
2013-12-19 08:14:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Strip a trailing space if necessary and returns whether it's a swap weak space situation.
|
2014-08-04 13:17:36 +00:00
|
|
|
* @param event The event to handle.
|
2014-03-04 07:31:51 +00:00
|
|
|
* @param inputTransaction The transaction in progress.
|
2013-12-19 08:14:11 +00:00
|
|
|
* @return whether we should swap the space instead of removing it.
|
|
|
|
*/
|
2014-08-04 13:17:36 +00:00
|
|
|
private boolean tryStripSpaceAndReturnWhetherShouldSwapInstead(final Event event,
|
2014-07-31 09:33:30 +00:00
|
|
|
final InputTransaction inputTransaction) {
|
2014-08-04 13:17:36 +00:00
|
|
|
final int codePoint = event.mCodePoint;
|
|
|
|
final boolean isFromSuggestionStrip = event.isSuggestionStripPress();
|
2014-03-12 08:02:13 +00:00
|
|
|
if (Constants.CODE_ENTER == codePoint &&
|
2014-03-04 07:31:51 +00:00
|
|
|
SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState) {
|
2013-12-19 08:14:11 +00:00
|
|
|
mConnection.removeTrailingSpace();
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-04 07:31:51 +00:00
|
|
|
if ((SpaceState.WEAK == inputTransaction.mSpaceState
|
|
|
|
|| SpaceState.SWAP_PUNCTUATION == inputTransaction.mSpaceState)
|
2013-12-19 08:14:11 +00:00
|
|
|
&& isFromSuggestionStrip) {
|
2014-03-12 08:02:13 +00:00
|
|
|
if (inputTransaction.mSettingsValues.isUsuallyPrecededBySpace(codePoint)) {
|
2014-03-04 07:31:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2014-03-12 08:02:13 +00:00
|
|
|
if (inputTransaction.mSettingsValues.isUsuallyFollowedBySpace(codePoint)) {
|
2014-03-04 07:31:51 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-12-19 08:14:11 +00:00
|
|
|
mConnection.removeTrailingSpace();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 06:55:45 +00:00
|
|
|
public void startDoubleSpacePeriodCountdown(final InputTransaction inputTransaction) {
|
|
|
|
mDoubleSpacePeriodCountdownStart = inputTransaction.mTimestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancelDoubleSpacePeriodCountdown() {
|
|
|
|
mDoubleSpacePeriodCountdownStart = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isDoubleSpacePeriodCountdownActive(final InputTransaction inputTransaction) {
|
|
|
|
return inputTransaction.mTimestamp - mDoubleSpacePeriodCountdownStart
|
|
|
|
< inputTransaction.mSettingsValues.mDoubleSpacePeriodTimeout;
|
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Apply the double-space-to-period transformation if applicable.
|
|
|
|
*
|
|
|
|
* The double-space-to-period transformation means that we replace two spaces with a
|
|
|
|
* period-space sequence of characters. This typically happens when the user presses space
|
|
|
|
* twice in a row quickly.
|
|
|
|
* This method will check that the double-space-to-period is active in settings, that the
|
2014-06-20 07:41:23 +00:00
|
|
|
* two spaces have been input close enough together, that the typed character is a space
|
|
|
|
* and that the previous character allows for the transformation to take place. If all of
|
|
|
|
* these conditions are fulfilled, this method applies the transformation and returns true.
|
|
|
|
* Otherwise, it does nothing and returns false.
|
2013-12-19 10:29:05 +00:00
|
|
|
*
|
2014-08-04 13:17:36 +00:00
|
|
|
* @param event The event to handle.
|
2014-03-05 06:55:45 +00:00
|
|
|
* @param inputTransaction The transaction in progress.
|
2013-12-19 10:29:05 +00:00
|
|
|
* @return true if we applied the double-space-to-period transformation, false otherwise.
|
|
|
|
*/
|
2014-08-04 13:17:36 +00:00
|
|
|
private boolean tryPerformDoubleSpacePeriod(final Event event,
|
|
|
|
final InputTransaction inputTransaction) {
|
2014-06-20 07:41:23 +00:00
|
|
|
// Check the setting, the typed character and the countdown. If any of the conditions is
|
|
|
|
// not fulfilled, return false.
|
|
|
|
if (!inputTransaction.mSettingsValues.mUseDoubleSpacePeriod
|
2014-08-04 13:17:36 +00:00
|
|
|
|| Constants.CODE_SPACE != event.mCodePoint
|
2014-06-20 07:41:23 +00:00
|
|
|
|| !isDoubleSpacePeriodCountdownActive(inputTransaction)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// We only do this when we see one space and an accepted code point before the cursor.
|
|
|
|
// The code point may be a surrogate pair but the space may not, so we need 3 chars.
|
|
|
|
final CharSequence lastTwo = mConnection.getTextBeforeCursor(3, 0);
|
|
|
|
if (null == lastTwo) return false;
|
|
|
|
final int length = lastTwo.length();
|
|
|
|
if (length < 2) return false;
|
2014-09-08 09:31:54 +00:00
|
|
|
if (lastTwo.charAt(length - 1) != Constants.CODE_SPACE) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-20 07:41:23 +00:00
|
|
|
// We know there is a space in pos -1, and we have at least two chars. If we have only two
|
|
|
|
// chars, isSurrogatePairs can't return true as charAt(1) is a space, so this is fine.
|
2013-12-19 08:14:11 +00:00
|
|
|
final int firstCodePoint =
|
2014-06-20 07:41:23 +00:00
|
|
|
Character.isSurrogatePair(lastTwo.charAt(0), lastTwo.charAt(1)) ?
|
|
|
|
Character.codePointAt(lastTwo, length - 3) : lastTwo.charAt(length - 2);
|
2013-12-19 08:14:11 +00:00
|
|
|
if (canBeFollowedByDoubleSpacePeriod(firstCodePoint)) {
|
2014-03-05 06:55:45 +00:00
|
|
|
cancelDoubleSpacePeriodCountdown();
|
2014-06-20 07:41:23 +00:00
|
|
|
mConnection.deleteSurroundingText(1, 0);
|
2014-03-05 06:55:45 +00:00
|
|
|
final String textToInsert = inputTransaction.mSettingsValues.mSpacingAndPunctuations
|
|
|
|
.mSentenceSeparatorAndSpace;
|
2013-12-19 08:14:11 +00:00
|
|
|
mConnection.commitText(textToInsert, 1);
|
2014-06-20 06:26:09 +00:00
|
|
|
inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW);
|
|
|
|
inputTransaction.setRequiresUpdateSuggestions();
|
2013-12-19 08:14:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Returns whether this code point can be followed by the double-space-to-period transformation.
|
|
|
|
*
|
|
|
|
* See #maybeDoubleSpaceToPeriod for details.
|
|
|
|
* Generally, most word characters can be followed by the double-space-to-period transformation,
|
|
|
|
* while most punctuation can't. Some punctuation however does allow for this to take place
|
|
|
|
* after them, like the closing parenthesis for example.
|
|
|
|
*
|
|
|
|
* @param codePoint the code point after which we may want to apply the transformation
|
|
|
|
* @return whether it's fine to apply the transformation after this code point.
|
|
|
|
*/
|
2013-12-19 08:14:11 +00:00
|
|
|
private static boolean canBeFollowedByDoubleSpacePeriod(final int codePoint) {
|
2013-12-19 10:29:05 +00:00
|
|
|
// TODO: This should probably be a blacklist rather than a whitelist.
|
2013-12-19 08:14:11 +00:00
|
|
|
// TODO: This should probably be language-dependant...
|
|
|
|
return Character.isLetterOrDigit(codePoint)
|
|
|
|
|| codePoint == Constants.CODE_SINGLE_QUOTE
|
|
|
|
|| codePoint == Constants.CODE_DOUBLE_QUOTE
|
|
|
|
|| codePoint == Constants.CODE_CLOSING_PARENTHESIS
|
|
|
|
|| codePoint == Constants.CODE_CLOSING_SQUARE_BRACKET
|
|
|
|
|| codePoint == Constants.CODE_CLOSING_CURLY_BRACKET
|
|
|
|
|| codePoint == Constants.CODE_CLOSING_ANGLE_BRACKET
|
|
|
|
|| codePoint == Constants.CODE_PLUS
|
|
|
|
|| codePoint == Constants.CODE_PERCENT
|
|
|
|
|| Character.getType(codePoint) == Character.OTHER_SYMBOL;
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:34:35 +00:00
|
|
|
/**
|
2013-12-19 08:43:22 +00:00
|
|
|
* Performs a recapitalization event.
|
|
|
|
* @param settingsValues The current settings values.
|
2013-12-19 05:34:35 +00:00
|
|
|
*/
|
2013-12-20 12:51:13 +00:00
|
|
|
private void performRecapitalization(final SettingsValues settingsValues) {
|
2014-06-04 06:38:41 +00:00
|
|
|
if (!mConnection.hasSelection() || !mRecapitalizeStatus.mIsEnabled()) {
|
|
|
|
return; // No selection or recapitalize is disabled for now
|
2013-12-19 08:43:22 +00:00
|
|
|
}
|
2014-05-30 05:32:57 +00:00
|
|
|
final int selectionStart = mConnection.getExpectedSelectionStart();
|
|
|
|
final int selectionEnd = mConnection.getExpectedSelectionEnd();
|
|
|
|
final int numCharsSelected = selectionEnd - selectionStart;
|
|
|
|
if (numCharsSelected > Constants.MAX_CHARACTERS_FOR_RECAPITALIZATION) {
|
|
|
|
// We bail out if we have too many characters for performance reasons. We don't want
|
|
|
|
// to suck possibly multiple-megabyte data.
|
|
|
|
return;
|
|
|
|
}
|
2014-06-04 06:19:10 +00:00
|
|
|
// If we have a recapitalize in progress, use it; otherwise, start a new one.
|
|
|
|
if (!mRecapitalizeStatus.isStarted()
|
2014-05-30 05:32:57 +00:00
|
|
|
|| !mRecapitalizeStatus.isSetAt(selectionStart, selectionEnd)) {
|
2013-12-19 08:43:22 +00:00
|
|
|
final CharSequence selectedText =
|
|
|
|
mConnection.getSelectedText(0 /* flags, 0 for no styles */);
|
|
|
|
if (TextUtils.isEmpty(selectedText)) return; // Race condition with the input connection
|
2014-06-04 06:19:10 +00:00
|
|
|
mRecapitalizeStatus.start(selectionStart, selectionEnd, selectedText.toString(),
|
2014-01-20 05:56:56 +00:00
|
|
|
settingsValues.mLocale,
|
|
|
|
settingsValues.mSpacingAndPunctuations.mSortedWordSeparators);
|
2013-12-19 08:43:22 +00:00
|
|
|
// We trim leading and trailing whitespace.
|
|
|
|
mRecapitalizeStatus.trim();
|
|
|
|
}
|
|
|
|
mConnection.finishComposingText();
|
|
|
|
mRecapitalizeStatus.rotate();
|
2014-05-30 05:32:57 +00:00
|
|
|
mConnection.setSelection(selectionEnd, selectionEnd);
|
|
|
|
mConnection.deleteSurroundingText(numCharsSelected, 0);
|
2013-12-19 08:43:22 +00:00
|
|
|
mConnection.commitText(mRecapitalizeStatus.getRecapitalizedString(), 0);
|
2014-01-09 05:09:26 +00:00
|
|
|
mConnection.setSelection(mRecapitalizeStatus.getNewCursorStart(),
|
|
|
|
mRecapitalizeStatus.getNewCursorEnd());
|
2013-12-19 05:34:35 +00:00
|
|
|
}
|
|
|
|
|
2013-12-26 13:24:59 +00:00
|
|
|
private void performAdditionToUserHistoryDictionary(final SettingsValues settingsValues,
|
2014-10-22 05:04:07 +00:00
|
|
|
final String suggestion, @Nonnull final NgramContext ngramContext) {
|
2013-12-19 12:53:08 +00:00
|
|
|
// If correction is not enabled, we don't add words to the user history dictionary.
|
|
|
|
// That's to avoid unintended additions in some sensitive fields, or fields that
|
|
|
|
// expect to receive non-words.
|
2014-07-22 17:18:38 +00:00
|
|
|
if (!settingsValues.mAutoCorrectionEnabledPerUserSettings) return;
|
2013-12-19 12:53:08 +00:00
|
|
|
|
2013-12-26 13:24:59 +00:00
|
|
|
if (TextUtils.isEmpty(suggestion)) return;
|
2014-01-14 06:23:58 +00:00
|
|
|
final boolean wasAutoCapitalized =
|
|
|
|
mWordComposer.wasAutoCapitalized() && !mWordComposer.isMostlyCaps();
|
|
|
|
final int timeStampInSeconds = (int)TimeUnit.MILLISECONDS.toSeconds(
|
|
|
|
System.currentTimeMillis());
|
2014-05-23 00:30:55 +00:00
|
|
|
mDictionaryFacilitator.addToUserHistory(suggestion, wasAutoCapitalized,
|
2014-09-29 01:52:18 +00:00
|
|
|
ngramContext, timeStampInSeconds, settingsValues.mBlockPotentiallyOffensive);
|
2013-12-19 12:53:08 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 03:48:50 +00:00
|
|
|
public void performUpdateSuggestionStripSync(final SettingsValues settingsValues,
|
|
|
|
final int inputStyle) {
|
2013-12-19 13:08:31 +00:00
|
|
|
// Check if we have a suggestion engine attached.
|
2014-07-22 17:18:38 +00:00
|
|
|
if (!settingsValues.needsToLookupSuggestions()) {
|
2013-12-19 13:08:31 +00:00
|
|
|
if (mWordComposer.isComposingWord()) {
|
|
|
|
Log.w(TAG, "Called updateSuggestionsOrPredictions but suggestions were not "
|
|
|
|
+ "requested!");
|
|
|
|
}
|
2014-06-18 23:35:51 +00:00
|
|
|
// Clear the suggestions strip.
|
2014-09-22 03:40:41 +00:00
|
|
|
mSuggestionStripViewAccessor.showSuggestionStrip(SuggestedWords.getEmptyInstance());
|
2013-12-19 13:08:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-13 21:50:54 +00:00
|
|
|
if (!mWordComposer.isComposingWord() && !settingsValues.mBigramPredictionEnabled) {
|
|
|
|
mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-02 21:47:36 +00:00
|
|
|
final AsyncResultHolder<SuggestedWords> holder = new AsyncResultHolder<>("Suggest");
|
2014-08-14 03:48:50 +00:00
|
|
|
mInputLogicHandler.getSuggestedWords(inputStyle, SuggestedWords.NOT_A_SEQUENCE_NUMBER,
|
|
|
|
new OnGetSuggestedWordsCallback() {
|
2013-12-19 13:08:31 +00:00
|
|
|
@Override
|
|
|
|
public void onGetSuggestedWords(final SuggestedWords suggestedWords) {
|
2014-11-17 09:26:15 +00:00
|
|
|
final String typedWordString = mWordComposer.getTypedWord();
|
|
|
|
final SuggestedWordInfo typedWordInfo = new SuggestedWordInfo(
|
2015-03-19 23:07:04 +00:00
|
|
|
typedWordString, "" /* prevWordsContext */,
|
|
|
|
SuggestedWordInfo.MAX_SCORE,
|
2014-11-17 09:26:15 +00:00
|
|
|
SuggestedWordInfo.KIND_TYPED, Dictionary.DICTIONARY_USER_TYPED,
|
|
|
|
SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
|
|
|
|
SuggestedWordInfo.NOT_A_CONFIDENCE);
|
2014-02-26 07:47:55 +00:00
|
|
|
// Show new suggestions if we have at least one. Otherwise keep the old
|
|
|
|
// suggestions with the new typed word. Exception: if the length of the
|
|
|
|
// typed word is <= 1 (after a deletion typically) we clear old suggestions.
|
2014-11-17 09:26:15 +00:00
|
|
|
if (suggestedWords.size() > 1 || typedWordString.length() <= 1) {
|
2014-02-26 07:47:55 +00:00
|
|
|
holder.set(suggestedWords);
|
|
|
|
} else {
|
2014-11-17 09:26:15 +00:00
|
|
|
holder.set(retrieveOlderSuggestions(typedWordInfo, mSuggestedWords));
|
2014-02-26 07:47:55 +00:00
|
|
|
}
|
2013-12-19 13:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// This line may cause the current thread to wait.
|
|
|
|
final SuggestedWords suggestedWords = holder.get(null,
|
|
|
|
Constants.GET_SUGGESTED_WORDS_TIMEOUT);
|
|
|
|
if (suggestedWords != null) {
|
2014-01-24 14:17:40 +00:00
|
|
|
mSuggestionStripViewAccessor.showSuggestionStrip(suggestedWords);
|
2013-12-19 13:08:31 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-19 12:53:08 +00:00
|
|
|
|
2013-12-20 09:05:35 +00:00
|
|
|
/**
|
|
|
|
* Check if the cursor is touching a word. If so, restart suggestions on this word, else
|
|
|
|
* do nothing.
|
|
|
|
*
|
|
|
|
* @param settingsValues the current values of the settings.
|
2014-11-18 06:32:21 +00:00
|
|
|
* @param forStartInput whether we're doing this in answer to starting the input (as opposed
|
|
|
|
* to a cursor move, for example). In ICS, there is a platform bug that we need to work
|
|
|
|
* around only when we come here at input start time.
|
2013-12-20 09:05:35 +00:00
|
|
|
*/
|
|
|
|
public void restartSuggestionsOnWordTouchedByCursor(final SettingsValues settingsValues,
|
2014-11-18 06:32:21 +00:00
|
|
|
final boolean forStartInput,
|
2014-06-26 09:43:14 +00:00
|
|
|
// TODO: remove this argument, put it into settingsValues
|
|
|
|
final int currentKeyboardScriptId) {
|
2013-12-20 09:05:35 +00:00
|
|
|
// HACK: We may want to special-case some apps that exhibit bad behavior in case of
|
|
|
|
// recorrection. This is a temporary, stopgap measure that will be removed later.
|
|
|
|
// TODO: remove this.
|
2014-02-19 12:41:45 +00:00
|
|
|
if (settingsValues.isBrokenByRecorrection()
|
2013-12-20 09:05:35 +00:00
|
|
|
// Recorrection is not supported in languages without spaces because we don't know
|
|
|
|
// how to segment them yet.
|
2014-02-19 12:41:45 +00:00
|
|
|
|| !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces
|
2014-02-03 06:19:56 +00:00
|
|
|
// If no suggestions are requested, don't try restarting suggestions.
|
2014-07-22 17:18:38 +00:00
|
|
|
|| !settingsValues.needsToLookupSuggestions()
|
2014-04-16 06:40:17 +00:00
|
|
|
// If we are currently in a batch input, we must not resume suggestions, or the result
|
|
|
|
// of the batch input will replace the new composition. This may happen in the corner case
|
|
|
|
// that the app moves the cursor on its own accord during a batch input.
|
|
|
|
|| mInputLogicHandler.isInBatchInput()
|
2013-12-20 09:05:35 +00:00
|
|
|
// If the cursor is not touching a word, or if there is a selection, return right away.
|
2014-02-19 12:41:45 +00:00
|
|
|
|| mConnection.hasSelection()
|
2013-12-20 09:05:35 +00:00
|
|
|
// If we don't know the cursor location, return.
|
2014-02-19 12:41:45 +00:00
|
|
|
|| mConnection.getExpectedSelectionStart() < 0) {
|
|
|
|
mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
|
|
|
|
return;
|
|
|
|
}
|
2014-01-09 05:09:26 +00:00
|
|
|
final int expectedCursorPosition = mConnection.getExpectedSelectionStart();
|
2014-02-19 12:41:45 +00:00
|
|
|
if (!mConnection.isCursorTouchingWord(settingsValues.mSpacingAndPunctuations)) {
|
|
|
|
// Show predictions.
|
2014-06-09 10:31:41 +00:00
|
|
|
mWordComposer.setCapitalizedModeAtStartComposingTime(WordComposer.CAPS_MODE_OFF);
|
2014-08-14 03:48:50 +00:00
|
|
|
mLatinIME.mHandler.postUpdateSuggestionStrip(SuggestedWords.INPUT_STYLE_RECORRECTION);
|
2014-02-19 12:41:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-12-20 09:05:35 +00:00
|
|
|
final TextRange range = mConnection.getWordRangeAtCursor(
|
2014-09-03 06:27:01 +00:00
|
|
|
settingsValues.mSpacingAndPunctuations, currentKeyboardScriptId);
|
2013-12-20 09:05:35 +00:00
|
|
|
if (null == range) return; // Happens if we don't have an input connection at all
|
2014-06-27 13:44:24 +00:00
|
|
|
if (range.length() <= 0) {
|
|
|
|
// Race condition, or touching a word in a non-supported script.
|
|
|
|
mLatinIME.setNeutralSuggestionStrip();
|
|
|
|
return;
|
|
|
|
}
|
2013-12-20 09:05:35 +00:00
|
|
|
// If for some strange reason (editor bug or so) we measure the text before the cursor as
|
|
|
|
// longer than what the entire text is supposed to be, the safe thing to do is bail out.
|
2014-02-17 08:53:15 +00:00
|
|
|
if (range.mHasUrlSpans) return; // If there are links, we don't resume suggestions. Making
|
|
|
|
// edits to a linkified text through batch commands would ruin the URL spans, and unless
|
|
|
|
// we take very complicated steps to preserve the whole link, we can't do things right so
|
|
|
|
// we just do not resume because it's safer.
|
2013-12-20 09:05:35 +00:00
|
|
|
final int numberOfCharsInWordBeforeCursor = range.getNumberOfCharsInWordBeforeCursor();
|
2013-12-25 12:43:23 +00:00
|
|
|
if (numberOfCharsInWordBeforeCursor > expectedCursorPosition) return;
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<SuggestedWordInfo> suggestions = new ArrayList<>();
|
2014-11-17 09:26:15 +00:00
|
|
|
final String typedWordString = range.mWord.toString();
|
|
|
|
final SuggestedWordInfo typedWordInfo = new SuggestedWordInfo(typedWordString,
|
2015-03-19 23:07:04 +00:00
|
|
|
"" /* prevWordsContext */, SuggestedWords.MAX_SUGGESTIONS + 1,
|
2014-10-08 05:40:03 +00:00
|
|
|
SuggestedWordInfo.KIND_TYPED, Dictionary.DICTIONARY_USER_TYPED,
|
|
|
|
SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
|
2014-11-17 09:26:15 +00:00
|
|
|
SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */);
|
|
|
|
suggestions.add(typedWordInfo);
|
|
|
|
if (!isResumableWord(settingsValues, typedWordString)) {
|
2014-03-05 09:47:41 +00:00
|
|
|
mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
|
|
|
|
return;
|
|
|
|
}
|
2013-12-20 09:05:35 +00:00
|
|
|
int i = 0;
|
|
|
|
for (final SuggestionSpan span : range.getSuggestionSpansAtWord()) {
|
|
|
|
for (final String s : span.getSuggestions()) {
|
|
|
|
++i;
|
2014-11-17 09:26:15 +00:00
|
|
|
if (!TextUtils.equals(s, typedWordString)) {
|
2013-12-20 09:05:35 +00:00
|
|
|
suggestions.add(new SuggestedWordInfo(s,
|
2015-03-19 23:07:04 +00:00
|
|
|
"" /* prevWordsContext */, SuggestedWords.MAX_SUGGESTIONS - i,
|
2013-12-20 09:05:35 +00:00
|
|
|
SuggestedWordInfo.KIND_RESUMED, Dictionary.DICTIONARY_RESUMED,
|
|
|
|
SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
|
|
|
|
SuggestedWordInfo.NOT_A_CONFIDENCE
|
|
|
|
/* autoCommitFirstWordConfidence */));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-17 09:26:15 +00:00
|
|
|
final int[] codePoints = StringUtils.toCodePointArray(typedWordString);
|
2013-12-27 11:58:32 +00:00
|
|
|
mWordComposer.setComposingWord(codePoints,
|
2014-06-09 10:31:41 +00:00
|
|
|
mLatinIME.getCoordinatesForCurrentKeyboard(codePoints));
|
2013-12-20 09:05:35 +00:00
|
|
|
mWordComposer.setCursorPositionWithinWord(
|
2014-11-17 09:26:15 +00:00
|
|
|
typedWordString.codePointCount(0, numberOfCharsInWordBeforeCursor));
|
2014-11-18 06:32:21 +00:00
|
|
|
if (forStartInput) {
|
|
|
|
mConnection.maybeMoveTheCursorAroundAndRestoreToWorkaroundABug();
|
|
|
|
}
|
2014-01-10 11:18:24 +00:00
|
|
|
mConnection.setComposingRegion(expectedCursorPosition - numberOfCharsInWordBeforeCursor,
|
2014-01-08 07:47:21 +00:00
|
|
|
expectedCursorPosition + range.getNumberOfCharsInWordAfterCursor());
|
2014-10-08 05:40:03 +00:00
|
|
|
if (suggestions.size() <= 1) {
|
2014-06-06 04:12:11 +00:00
|
|
|
// If there weren't any suggestion spans on this word, suggestions#size() will be 1
|
|
|
|
// if shouldIncludeResumedWordInSuggestions is true, 0 otherwise. In this case, we
|
|
|
|
// have no useful suggestions, so we will try to compute some for it instead.
|
2014-08-14 03:48:50 +00:00
|
|
|
mInputLogicHandler.getSuggestedWords(Suggest.SESSION_ID_TYPING,
|
2013-12-20 09:05:35 +00:00
|
|
|
SuggestedWords.NOT_A_SEQUENCE_NUMBER, new OnGetSuggestedWordsCallback() {
|
|
|
|
@Override
|
2014-10-08 05:40:03 +00:00
|
|
|
public void onGetSuggestedWords(final SuggestedWords suggestedWords) {
|
2014-10-20 05:48:56 +00:00
|
|
|
doShowSuggestionsAndClearAutoCorrectionIndicator(suggestedWords);
|
2013-12-20 09:05:35 +00:00
|
|
|
}});
|
|
|
|
} else {
|
|
|
|
// We found suggestion spans in the word. We'll create the SuggestedWords out of
|
2014-02-25 06:37:08 +00:00
|
|
|
// them, and make willAutoCorrect false. We make typedWordValid false, because the
|
|
|
|
// color of the word in the suggestion strip changes according to this parameter,
|
|
|
|
// and false gives the correct color.
|
2014-02-06 06:51:04 +00:00
|
|
|
final SuggestedWords suggestedWords = new SuggestedWords(suggestions,
|
2014-11-17 09:26:15 +00:00
|
|
|
null /* rawSuggestions */, typedWordInfo, false /* typedWordValid */,
|
2014-08-26 09:54:08 +00:00
|
|
|
false /* willAutoCorrect */, false /* isObsoleteSuggestions */,
|
|
|
|
SuggestedWords.INPUT_STYLE_RECORRECTION, SuggestedWords.NOT_A_SEQUENCE_NUMBER);
|
2014-10-20 05:48:56 +00:00
|
|
|
doShowSuggestionsAndClearAutoCorrectionIndicator(suggestedWords);
|
2013-12-20 09:05:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-20 05:48:56 +00:00
|
|
|
void doShowSuggestionsAndClearAutoCorrectionIndicator(final SuggestedWords suggestedWords) {
|
|
|
|
mIsAutoCorrectionIndicatorOn = false;
|
|
|
|
mLatinIME.mHandler.showSuggestionStrip(suggestedWords);
|
|
|
|
}
|
|
|
|
|
2013-12-19 09:30:21 +00:00
|
|
|
/**
|
|
|
|
* Reverts a previous commit with auto-correction.
|
|
|
|
*
|
|
|
|
* This is triggered upon pressing backspace just after a commit with auto-correction.
|
|
|
|
*
|
2014-03-05 07:37:44 +00:00
|
|
|
* @param inputTransaction The transaction in progress.
|
2014-09-14 07:53:33 +00:00
|
|
|
* @param settingsValues the current values of the settings.
|
2013-12-19 09:30:21 +00:00
|
|
|
*/
|
2014-09-14 07:53:33 +00:00
|
|
|
private void revertCommit(final InputTransaction inputTransaction,
|
|
|
|
final SettingsValues settingsValues) {
|
2014-01-09 09:36:23 +00:00
|
|
|
final CharSequence originallyTypedWord = mLastComposedWord.mTypedWord;
|
2014-09-14 07:53:33 +00:00
|
|
|
final String originallyTypedWordString =
|
|
|
|
originallyTypedWord != null ? originallyTypedWord.toString() : "";
|
2014-01-09 09:36:23 +00:00
|
|
|
final CharSequence committedWord = mLastComposedWord.mCommittedWord;
|
2014-02-12 12:21:19 +00:00
|
|
|
final String committedWordString = committedWord.toString();
|
2013-12-19 09:30:21 +00:00
|
|
|
final int cancelLength = committedWord.length();
|
2014-09-14 07:53:33 +00:00
|
|
|
final String separatorString = mLastComposedWord.mSeparatorString;
|
2014-09-22 08:35:31 +00:00
|
|
|
// If our separator is a space, we won't actually commit it,
|
|
|
|
// but set the space state to PHANTOM so that a space will be inserted
|
|
|
|
// on the next keypress
|
|
|
|
final boolean usePhantomSpace = separatorString.equals(Constants.STRING_SPACE);
|
2013-12-19 09:30:21 +00:00
|
|
|
// We want java chars, not codepoints for the following.
|
2014-09-14 07:53:33 +00:00
|
|
|
final int separatorLength = separatorString.length();
|
2013-12-19 09:30:21 +00:00
|
|
|
// TODO: should we check our saved separator against the actual contents of the text view?
|
|
|
|
final int deleteLength = cancelLength + separatorLength;
|
2014-07-17 01:41:46 +00:00
|
|
|
if (DebugFlags.DEBUG_ENABLED) {
|
2013-12-19 09:30:21 +00:00
|
|
|
if (mWordComposer.isComposingWord()) {
|
|
|
|
throw new RuntimeException("revertCommit, but we are composing a word");
|
|
|
|
}
|
|
|
|
final CharSequence wordBeforeCursor =
|
|
|
|
mConnection.getTextBeforeCursor(deleteLength, 0).subSequence(0, cancelLength);
|
|
|
|
if (!TextUtils.equals(committedWord, wordBeforeCursor)) {
|
|
|
|
throw new RuntimeException("revertCommit check failed: we thought we were "
|
|
|
|
+ "reverting \"" + committedWord
|
|
|
|
+ "\", but before the cursor we found \"" + wordBeforeCursor + "\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mConnection.deleteSurroundingText(deleteLength, 0);
|
2014-06-20 05:46:13 +00:00
|
|
|
if (!TextUtils.isEmpty(committedWord)) {
|
2015-02-19 01:50:33 +00:00
|
|
|
unlearnWord(committedWordString, inputTransaction.mSettingsValues,
|
|
|
|
Constants.EVENT_REVERT);
|
2014-01-09 09:36:23 +00:00
|
|
|
}
|
2014-09-22 08:35:31 +00:00
|
|
|
final String stringToCommit = originallyTypedWord +
|
|
|
|
(usePhantomSpace ? "" : separatorString);
|
2013-12-27 11:58:32 +00:00
|
|
|
final SpannableString textToCommit = new SpannableString(stringToCommit);
|
2014-01-09 09:36:23 +00:00
|
|
|
if (committedWord instanceof SpannableString) {
|
|
|
|
final SpannableString committedWordWithSuggestionSpans = (SpannableString)committedWord;
|
|
|
|
final Object[] spans = committedWordWithSuggestionSpans.getSpans(0,
|
|
|
|
committedWord.length(), Object.class);
|
2014-02-12 12:21:19 +00:00
|
|
|
final int lastCharIndex = textToCommit.length() - 1;
|
|
|
|
// We will collect all suggestions in the following array.
|
2014-05-23 11:18:17 +00:00
|
|
|
final ArrayList<String> suggestions = new ArrayList<>();
|
2014-02-12 12:21:19 +00:00
|
|
|
// First, add the committed word to the list of suggestions.
|
|
|
|
suggestions.add(committedWordString);
|
2014-01-09 09:36:23 +00:00
|
|
|
for (final Object span : spans) {
|
2015-03-16 18:19:35 +00:00
|
|
|
// If this is a suggestion span, we check that the word is not the committed word.
|
|
|
|
// That should mostly be the case.
|
2014-02-12 12:21:19 +00:00
|
|
|
// Given this, we add it to the list of suggestions, otherwise we discard it.
|
|
|
|
if (span instanceof SuggestionSpan) {
|
|
|
|
final SuggestionSpan suggestionSpan = (SuggestionSpan)span;
|
|
|
|
for (final String suggestion : suggestionSpan.getSuggestions()) {
|
|
|
|
if (!suggestion.equals(committedWordString)) {
|
|
|
|
suggestions.add(suggestion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If this is not a suggestion span, we just add it as is.
|
|
|
|
textToCommit.setSpan(span, 0 /* start */, lastCharIndex /* end */,
|
|
|
|
committedWordWithSuggestionSpans.getSpanFlags(span));
|
|
|
|
}
|
2013-12-19 09:30:21 +00:00
|
|
|
}
|
2014-02-12 12:21:19 +00:00
|
|
|
// Add the suggestion list to the list of suggestions.
|
2014-10-01 09:17:49 +00:00
|
|
|
textToCommit.setSpan(new SuggestionSpan(mLatinIME /* context */,
|
|
|
|
inputTransaction.mSettingsValues.mLocale,
|
|
|
|
suggestions.toArray(new String[suggestions.size()]), 0 /* flags */,
|
|
|
|
null /* notificationTargetClass */),
|
2014-02-12 12:21:19 +00:00
|
|
|
0 /* start */, lastCharIndex /* end */, 0 /* flags */);
|
2013-12-19 09:30:21 +00:00
|
|
|
}
|
2014-09-14 07:53:33 +00:00
|
|
|
|
2014-03-05 07:37:44 +00:00
|
|
|
if (inputTransaction.mSettingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces) {
|
2015-02-03 18:18:06 +00:00
|
|
|
mConnection.commitText(textToCommit, 1);
|
2014-09-22 08:35:31 +00:00
|
|
|
if (usePhantomSpace) {
|
|
|
|
mSpaceState = SpaceState.PHANTOM;
|
|
|
|
}
|
2013-12-19 09:30:21 +00:00
|
|
|
} else {
|
|
|
|
// For languages without spaces, we revert the typed string but the cursor is flush
|
|
|
|
// with the typed word, so we need to resume suggestions right away.
|
2013-12-27 11:58:32 +00:00
|
|
|
final int[] codePoints = StringUtils.toCodePointArray(stringToCommit);
|
|
|
|
mWordComposer.setComposingWord(codePoints,
|
2014-06-09 10:31:41 +00:00
|
|
|
mLatinIME.getCoordinatesForCurrentKeyboard(codePoints));
|
2015-02-03 18:18:06 +00:00
|
|
|
setComposingTextInternal(textToCommit, 1);
|
2013-12-19 09:30:21 +00:00
|
|
|
}
|
2014-05-28 14:02:16 +00:00
|
|
|
// Don't restart suggestion yet. We'll restart if the user deletes the separator.
|
2013-12-19 09:30:21 +00:00
|
|
|
mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
|
2014-09-14 07:53:33 +00:00
|
|
|
|
2015-02-03 18:18:06 +00:00
|
|
|
// We have a separator between the word and the cursor: we should show predictions.
|
|
|
|
inputTransaction.setRequiresUpdateSuggestions();
|
2013-12-19 09:30:21 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 08:57:10 +00:00
|
|
|
/**
|
|
|
|
* Factor in auto-caps and manual caps and compute the current caps mode.
|
2013-12-19 11:36:32 +00:00
|
|
|
* @param settingsValues the current settings values.
|
2013-12-20 12:51:13 +00:00
|
|
|
* @param keyboardShiftMode the current shift mode of the keyboard. See
|
|
|
|
* KeyboardSwitcher#getKeyboardShiftMode() for possible values.
|
2013-12-19 08:57:10 +00:00
|
|
|
* @return the actual caps mode the keyboard is in right now.
|
|
|
|
*/
|
2013-12-20 12:51:13 +00:00
|
|
|
private int getActualCapsMode(final SettingsValues settingsValues,
|
|
|
|
final int keyboardShiftMode) {
|
2014-03-04 06:29:54 +00:00
|
|
|
if (keyboardShiftMode != WordComposer.CAPS_MODE_AUTO_SHIFTED) {
|
|
|
|
return keyboardShiftMode;
|
|
|
|
}
|
2013-12-19 11:36:32 +00:00
|
|
|
final int auto = getCurrentAutoCapsState(settingsValues);
|
2013-12-19 08:57:10 +00:00
|
|
|
if (0 != (auto & TextUtils.CAP_MODE_CHARACTERS)) {
|
|
|
|
return WordComposer.CAPS_MODE_AUTO_SHIFT_LOCKED;
|
|
|
|
}
|
|
|
|
if (0 != auto) {
|
|
|
|
return WordComposer.CAPS_MODE_AUTO_SHIFTED;
|
|
|
|
}
|
|
|
|
return WordComposer.CAPS_MODE_OFF;
|
|
|
|
}
|
|
|
|
|
2013-12-19 11:36:32 +00:00
|
|
|
/**
|
|
|
|
* Gets the current auto-caps state, factoring in the space state.
|
|
|
|
*
|
|
|
|
* This method tries its best to do this in the most efficient possible manner. It avoids
|
|
|
|
* getting text from the editor if possible at all.
|
|
|
|
* This is called from the KeyboardSwitcher (through a trampoline in LatinIME) because it
|
|
|
|
* needs to know auto caps state to display the right layout.
|
|
|
|
*
|
2014-01-24 02:38:07 +00:00
|
|
|
* @param settingsValues the relevant settings values
|
2013-12-19 11:36:32 +00:00
|
|
|
* @return a caps mode from TextUtils.CAP_MODE_* or Constants.TextUtils.CAP_MODE_OFF.
|
|
|
|
*/
|
2014-01-24 02:38:07 +00:00
|
|
|
public int getCurrentAutoCapsState(final SettingsValues settingsValues) {
|
2013-12-19 11:36:32 +00:00
|
|
|
if (!settingsValues.mAutoCap) return Constants.TextUtils.CAP_MODE_OFF;
|
|
|
|
|
|
|
|
final EditorInfo ei = getCurrentInputEditorInfo();
|
|
|
|
if (ei == null) return Constants.TextUtils.CAP_MODE_OFF;
|
|
|
|
final int inputType = ei.inputType;
|
|
|
|
// Warning: this depends on mSpaceState, which may not be the most current value. If
|
|
|
|
// mSpaceState gets updated later, whoever called this may need to be told about it.
|
2014-01-17 04:14:29 +00:00
|
|
|
return mConnection.getCursorCapsMode(inputType, settingsValues.mSpacingAndPunctuations,
|
2013-12-19 11:36:32 +00:00
|
|
|
SpaceState.PHANTOM == mSpaceState);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getCurrentRecapitalizeState() {
|
2014-06-04 06:19:10 +00:00
|
|
|
if (!mRecapitalizeStatus.isStarted()
|
2014-01-09 05:09:26 +00:00
|
|
|
|| !mRecapitalizeStatus.isSetAt(mConnection.getExpectedSelectionStart(),
|
|
|
|
mConnection.getExpectedSelectionEnd())) {
|
2013-12-19 11:36:32 +00:00
|
|
|
// Not recapitalizing at the moment
|
|
|
|
return RecapitalizeStatus.NOT_A_RECAPITALIZE_MODE;
|
|
|
|
}
|
|
|
|
return mRecapitalizeStatus.getCurrentMode();
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:34:35 +00:00
|
|
|
/**
|
|
|
|
* @return the editor info for the current editor
|
|
|
|
*/
|
|
|
|
private EditorInfo getCurrentInputEditorInfo() {
|
|
|
|
return mLatinIME.getCurrentInputEditorInfo();
|
|
|
|
}
|
|
|
|
|
2013-12-19 08:57:10 +00:00
|
|
|
/**
|
2014-09-29 01:52:18 +00:00
|
|
|
* Get n-gram context from the nth previous word before the cursor as context
|
2014-05-21 06:40:08 +00:00
|
|
|
* for the suggestion process.
|
2014-01-09 08:27:33 +00:00
|
|
|
* @param spacingAndPunctuations the current spacing and punctuations settings.
|
2013-12-19 08:57:10 +00:00
|
|
|
* @param nthPreviousWord reverse index of the word to get (1-indexed)
|
2014-05-21 06:40:08 +00:00
|
|
|
* @return the information of previous words
|
2013-12-19 08:57:10 +00:00
|
|
|
*/
|
2014-09-29 01:52:18 +00:00
|
|
|
public NgramContext getNgramContextFromNthPreviousWordForSuggestion(
|
2014-01-09 08:27:33 +00:00
|
|
|
final SpacingAndPunctuations spacingAndPunctuations, final int nthPreviousWord) {
|
|
|
|
if (spacingAndPunctuations.mCurrentLanguageHasSpaces) {
|
2013-12-19 08:57:10 +00:00
|
|
|
// If we are typing in a language with spaces we can just look up the previous
|
2014-05-21 06:40:08 +00:00
|
|
|
// word information from textview.
|
2014-09-29 01:52:18 +00:00
|
|
|
return mConnection.getNgramContextFromNthPreviousWord(
|
2014-05-21 06:40:08 +00:00
|
|
|
spacingAndPunctuations, nthPreviousWord);
|
2013-12-19 08:57:10 +00:00
|
|
|
}
|
2014-10-20 05:48:56 +00:00
|
|
|
if (LastComposedWord.NOT_A_COMPOSED_WORD == mLastComposedWord) {
|
|
|
|
return NgramContext.BEGINNING_OF_SENTENCE;
|
|
|
|
}
|
|
|
|
return new NgramContext(new NgramContext.WordInfo(
|
|
|
|
mLastComposedWord.mCommittedWord.toString()));
|
2013-12-19 08:57:10 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 09:05:35 +00:00
|
|
|
/**
|
|
|
|
* Tests the passed word for resumability.
|
|
|
|
*
|
|
|
|
* We can resume suggestions on words whose first code point is a word code point (with some
|
|
|
|
* nuances: check the code for details).
|
|
|
|
*
|
|
|
|
* @param settings the current values of the settings.
|
|
|
|
* @param word the word to evaluate.
|
|
|
|
* @return whether it's fine to resume suggestions on this word.
|
|
|
|
*/
|
|
|
|
private static boolean isResumableWord(final SettingsValues settings, final String word) {
|
|
|
|
final int firstCodePoint = word.codePointAt(0);
|
|
|
|
return settings.isWordCodePoint(firstCodePoint)
|
|
|
|
&& Constants.CODE_SINGLE_QUOTE != firstCodePoint
|
|
|
|
&& Constants.CODE_DASH != firstCodePoint;
|
|
|
|
}
|
|
|
|
|
2013-12-19 05:34:35 +00:00
|
|
|
/**
|
|
|
|
* @param actionId the action to perform
|
|
|
|
*/
|
|
|
|
private void performEditorAction(final int actionId) {
|
|
|
|
mConnection.performEditorAction(actionId);
|
|
|
|
}
|
|
|
|
|
2013-12-20 06:44:26 +00:00
|
|
|
/**
|
|
|
|
* Perform the processing specific to inputting TLDs.
|
|
|
|
*
|
|
|
|
* Some keys input a TLD (specifically, the ".com" key) and this warrants some specific
|
|
|
|
* processing. First, if this is a TLD, we ignore PHANTOM spaces -- this is done by type
|
|
|
|
* of character in onCodeInput, but since this gets inputted as a whole string we need to
|
|
|
|
* do it here specifically. Then, if the last character before the cursor is a period, then
|
|
|
|
* we cut the dot at the start of ".com". This is because humans tend to type "www.google."
|
|
|
|
* and then press the ".com" key and instinctively don't expect to get "www.google..com".
|
|
|
|
*
|
|
|
|
* @param text the raw text supplied to onTextInput
|
|
|
|
* @return the text to actually send to the editor
|
|
|
|
*/
|
|
|
|
private String performSpecificTldProcessingOnTextInput(final String text) {
|
|
|
|
if (text.length() <= 1 || text.charAt(0) != Constants.CODE_PERIOD
|
|
|
|
|| !Character.isLetter(text.charAt(1))) {
|
|
|
|
// Not a tld: do nothing.
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
// 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 = SpaceState.NONE;
|
2013-12-20 07:00:26 +00:00
|
|
|
final int codePointBeforeCursor = mConnection.getCodePointBeforeCursor();
|
|
|
|
// If no code point, #getCodePointBeforeCursor returns NOT_A_CODE_POINT.
|
|
|
|
if (Constants.CODE_PERIOD == codePointBeforeCursor) {
|
2013-12-20 06:44:26 +00:00
|
|
|
return text.substring(1);
|
|
|
|
}
|
2014-10-20 05:48:56 +00:00
|
|
|
return text;
|
2013-12-20 06:44:26 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 05:34:35 +00:00
|
|
|
/**
|
|
|
|
* Handle a press on the settings key.
|
|
|
|
*/
|
|
|
|
private void onSettingsKeyPressed() {
|
2013-12-19 11:56:37 +00:00
|
|
|
mLatinIME.displaySettingsDialog();
|
2013-12-19 05:34:35 +00:00
|
|
|
}
|
2013-12-19 07:07:24 +00:00
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Resets the whole input state to the starting state.
|
|
|
|
*
|
|
|
|
* This will clear the composing word, reset the last composed word, clear the suggestion
|
|
|
|
* strip and tell the input connection about it so that it can refresh its caches.
|
|
|
|
*
|
|
|
|
* @param newSelStart the new selection start, in java characters.
|
|
|
|
* @param newSelEnd the new selection end, in java characters.
|
2014-02-19 12:41:45 +00:00
|
|
|
* @param clearSuggestionStrip whether this method should clear the suggestion strip.
|
2013-12-19 10:29:05 +00:00
|
|
|
*/
|
|
|
|
// TODO: how is this different from startInput ?!
|
2014-03-03 05:19:52 +00:00
|
|
|
private void resetEntireInputState(final int newSelStart, final int newSelEnd,
|
|
|
|
final boolean clearSuggestionStrip) {
|
2013-12-19 07:07:24 +00:00
|
|
|
final boolean shouldFinishComposition = mWordComposer.isComposingWord();
|
|
|
|
resetComposingState(true /* alsoResetLastComposedWord */);
|
2014-02-19 12:41:45 +00:00
|
|
|
if (clearSuggestionStrip) {
|
|
|
|
mSuggestionStripViewAccessor.setNeutralSuggestionStrip();
|
|
|
|
}
|
2013-12-19 07:07:24 +00:00
|
|
|
mConnection.resetCachesUponCursorMoveAndReturnSuccess(newSelStart, newSelEnd,
|
|
|
|
shouldFinishComposition);
|
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Resets only the composing state.
|
|
|
|
*
|
|
|
|
* Compare #resetEntireInputState, which also clears the suggestion strip and resets the
|
|
|
|
* input connection caches. This only deals with the composing state.
|
|
|
|
*
|
|
|
|
* @param alsoResetLastComposedWord whether to also reset the last composed word.
|
|
|
|
*/
|
2014-03-03 05:19:52 +00:00
|
|
|
private void resetComposingState(final boolean alsoResetLastComposedWord) {
|
2013-12-19 07:07:24 +00:00
|
|
|
mWordComposer.reset();
|
|
|
|
if (alsoResetLastComposedWord) {
|
|
|
|
mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 07:32:40 +00:00
|
|
|
/**
|
2014-02-26 07:47:55 +00:00
|
|
|
* Make a {@link com.android.inputmethod.latin.SuggestedWords} object containing a typed word
|
|
|
|
* and obsolete suggestions.
|
|
|
|
* See {@link com.android.inputmethod.latin.SuggestedWords#getTypedWordAndPreviousSuggestions(
|
2014-11-17 09:26:15 +00:00
|
|
|
* SuggestedWordInfo, com.android.inputmethod.latin.SuggestedWords)}.
|
|
|
|
* @param typedWordInfo The typed word as a SuggestedWordInfo.
|
2014-02-26 07:47:55 +00:00
|
|
|
* @param previousSuggestedWords The previously suggested words.
|
|
|
|
* @return Obsolete suggestions with the newly typed word.
|
2014-02-26 07:32:40 +00:00
|
|
|
*/
|
2014-11-17 09:26:15 +00:00
|
|
|
static SuggestedWords retrieveOlderSuggestions(final SuggestedWordInfo typedWordInfo,
|
2014-02-26 07:47:55 +00:00
|
|
|
final SuggestedWords previousSuggestedWords) {
|
2014-09-22 03:40:41 +00:00
|
|
|
final SuggestedWords oldSuggestedWords = previousSuggestedWords.isPunctuationSuggestions()
|
|
|
|
? SuggestedWords.getEmptyInstance() : previousSuggestedWords;
|
2014-02-26 07:47:55 +00:00
|
|
|
final ArrayList<SuggestedWords.SuggestedWordInfo> typedWordAndPreviousSuggestions =
|
2014-11-17 09:26:15 +00:00
|
|
|
SuggestedWords.getTypedWordAndPreviousSuggestions(typedWordInfo, oldSuggestedWords);
|
2014-02-26 07:47:55 +00:00
|
|
|
return new SuggestedWords(typedWordAndPreviousSuggestions, null /* rawSuggestions */,
|
2014-11-17 09:26:15 +00:00
|
|
|
typedWordInfo, false /* typedWordValid */, false /* hasAutoCorrectionCandidate */,
|
2014-11-07 08:10:36 +00:00
|
|
|
true /* isObsoleteSuggestions */, oldSuggestedWords.mInputStyle,
|
|
|
|
SuggestedWords.NOT_A_SEQUENCE_NUMBER);
|
2014-02-26 07:32:40 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Gets a chunk of text with or the auto-correction indicator underline span as appropriate.
|
|
|
|
*
|
|
|
|
* This method looks at the old state of the auto-correction indicator to put or not put
|
|
|
|
* the underline span as appropriate. It is important to note that this does not correspond
|
|
|
|
* exactly to whether this word will be auto-corrected to or not: what's important here is
|
|
|
|
* to keep the same indication as before.
|
|
|
|
* When we add a new code point to a composing word, we don't know yet if we are going to
|
|
|
|
* auto-correct it until the suggestions are computed. But in the mean time, we still need
|
|
|
|
* to display the character and to extend the previous underline. To avoid any flickering,
|
|
|
|
* the underline should keep the same color it used to have, even if that's not ultimately
|
|
|
|
* the correct color for this new word. When the suggestions are finished evaluating, we
|
|
|
|
* will call this method again to fix the color of the underline.
|
|
|
|
*
|
|
|
|
* @param text the text on which to maybe apply the span.
|
|
|
|
* @return the same text, with the auto-correction underline span if that's appropriate.
|
|
|
|
*/
|
2014-03-03 05:19:52 +00:00
|
|
|
// TODO: Shouldn't this go in some *Utils class instead?
|
|
|
|
private CharSequence getTextWithUnderline(final String text) {
|
2013-12-19 07:07:24 +00:00
|
|
|
return mIsAutoCorrectionIndicatorOn
|
|
|
|
? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(mLatinIME, text)
|
|
|
|
: text;
|
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Sends a DOWN key event followed by an UP key event to the editor.
|
|
|
|
*
|
|
|
|
* If possible at all, avoid using this method. It causes all sorts of race conditions with
|
|
|
|
* the text view because it goes through a different, asynchronous binder. Also, batch edits
|
|
|
|
* are ignored for key events. Use the normal software input methods instead.
|
|
|
|
*
|
|
|
|
* @param keyCode the key code to send inside the key event.
|
|
|
|
*/
|
|
|
|
private void sendDownUpKeyEvent(final int keyCode) {
|
2013-12-19 07:07:24 +00:00
|
|
|
final long eventTime = SystemClock.uptimeMillis();
|
|
|
|
mConnection.sendKeyEvent(new KeyEvent(eventTime, eventTime,
|
2013-12-19 10:29:05 +00:00
|
|
|
KeyEvent.ACTION_DOWN, keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
|
2013-12-19 07:07:24 +00:00
|
|
|
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE));
|
|
|
|
mConnection.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime,
|
2013-12-19 10:29:05 +00:00
|
|
|
KeyEvent.ACTION_UP, keyCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
|
2013-12-19 07:07:24 +00:00
|
|
|
KeyEvent.FLAG_SOFT_KEYBOARD | KeyEvent.FLAG_KEEP_TOUCH_MODE));
|
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Sends a code point to the editor, using the most appropriate method.
|
|
|
|
*
|
|
|
|
* Normally we send code points with commitText, but there are some cases (where backward
|
|
|
|
* compatibility is a concern for example) where we want to use deprecated methods.
|
|
|
|
*
|
2013-12-20 06:00:44 +00:00
|
|
|
* @param settingsValues the current values of the settings.
|
2013-12-19 10:29:05 +00:00
|
|
|
* @param codePoint the code point to send.
|
|
|
|
*/
|
2014-03-04 07:46:42 +00:00
|
|
|
// TODO: replace these two parameters with an InputTransaction
|
2013-12-20 06:00:44 +00:00
|
|
|
private void sendKeyCodePoint(final SettingsValues settingsValues, final int codePoint) {
|
2013-12-19 07:07:24 +00:00
|
|
|
// TODO: Remove this special handling of digit letters.
|
|
|
|
// For backward compatibility. See {@link InputMethodService#sendKeyChar(char)}.
|
2013-12-19 10:29:05 +00:00
|
|
|
if (codePoint >= '0' && codePoint <= '9') {
|
|
|
|
sendDownUpKeyEvent(codePoint - '0' + KeyEvent.KEYCODE_0);
|
2013-12-19 07:07:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
// TODO: we should do this also when the editor has TYPE_NULL
|
2013-12-20 06:00:44 +00:00
|
|
|
if (Constants.CODE_ENTER == codePoint && settingsValues.isBeforeJellyBean()) {
|
2013-12-19 07:07:24 +00:00
|
|
|
// 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.
|
|
|
|
sendDownUpKeyEvent(KeyEvent.KEYCODE_ENTER);
|
|
|
|
} else {
|
2013-12-19 10:29:05 +00:00
|
|
|
mConnection.commitText(StringUtils.newSingleCodePointString(codePoint), 1);
|
2013-12-19 07:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-19 07:37:07 +00:00
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
2014-11-18 02:49:45 +00:00
|
|
|
* Insert an automatic space, if the options allow it.
|
2013-12-19 10:29:05 +00:00
|
|
|
*
|
2014-11-18 02:49:45 +00:00
|
|
|
* This checks the options and the text before the cursor are appropriate before inserting
|
|
|
|
* an automatic space.
|
2013-12-19 10:29:05 +00:00
|
|
|
*
|
|
|
|
* @param settingsValues the current values of the settings.
|
|
|
|
*/
|
2014-11-18 02:49:45 +00:00
|
|
|
private void insertAutomaticSpaceIfOptionsAndTextAllow(final SettingsValues settingsValues) {
|
2013-12-19 08:14:11 +00:00
|
|
|
if (settingsValues.shouldInsertSpacesAutomatically()
|
2014-01-08 09:06:32 +00:00
|
|
|
&& settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces
|
2013-12-19 08:14:11 +00:00
|
|
|
&& !mConnection.textBeforeCursorLooksLikeURL()) {
|
2013-12-20 06:00:44 +00:00
|
|
|
sendKeyCodePoint(settingsValues, Constants.CODE_SPACE);
|
2013-12-19 08:14:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-25 11:08:52 +00:00
|
|
|
/**
|
|
|
|
* Do the final processing after a batch input has ended. This commits the word to the editor.
|
|
|
|
* @param settingsValues the current values of the settings.
|
|
|
|
* @param suggestedWords suggestedWords to use.
|
|
|
|
*/
|
2014-03-06 07:10:15 +00:00
|
|
|
public void onUpdateTailBatchInputCompleted(final SettingsValues settingsValues,
|
2015-03-20 17:43:38 +00:00
|
|
|
final SuggestedWords suggestedWords, final KeyboardSwitcher keyboardSwitcher) {
|
2013-12-25 11:08:52 +00:00
|
|
|
final String batchInputText = suggestedWords.isEmpty() ? null : suggestedWords.getWord(0);
|
|
|
|
if (TextUtils.isEmpty(batchInputText)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mConnection.beginBatchEdit();
|
|
|
|
if (SpaceState.PHANTOM == mSpaceState) {
|
2014-11-18 02:49:45 +00:00
|
|
|
insertAutomaticSpaceIfOptionsAndTextAllow(settingsValues);
|
2013-12-25 11:08:52 +00:00
|
|
|
}
|
2015-03-12 00:09:44 +00:00
|
|
|
mWordComposer.setBatchInputWord(batchInputText);
|
|
|
|
setComposingTextInternal(batchInputText, 1);
|
2013-12-25 11:08:52 +00:00
|
|
|
mConnection.endBatchEdit();
|
|
|
|
// Space state must be updated before calling updateShiftState
|
|
|
|
mSpaceState = SpaceState.PHANTOM;
|
2014-03-27 09:37:16 +00:00
|
|
|
keyboardSwitcher.requestUpdatingShiftState(getCurrentAutoCapsState(settingsValues),
|
|
|
|
getCurrentRecapitalizeState());
|
2013-12-25 11:08:52 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Commit the typed string to the editor.
|
|
|
|
*
|
|
|
|
* This is typically called when we should commit the currently composing word without applying
|
|
|
|
* auto-correction to it. Typically, we come here upon pressing a separator when the keyboard
|
|
|
|
* is configured to not do auto-correction at all (because of the settings or the properties of
|
|
|
|
* the editor). In this case, `separatorString' is set to the separator that was pressed.
|
|
|
|
* We also come here in a variety of cases with external user action. For example, when the
|
|
|
|
* cursor is moved while there is a composition, or when the keyboard is closed, or when the
|
|
|
|
* user presses the Send button for an SMS, we don't auto-correct as that would be unexpected.
|
|
|
|
* In this case, `separatorString' is set to NOT_A_SEPARATOR.
|
|
|
|
*
|
2013-12-19 12:53:08 +00:00
|
|
|
* @param settingsValues the current values of the settings.
|
|
|
|
* @param separatorString the separator that's causing the commit, or NOT_A_SEPARATOR if none.
|
2013-12-19 10:29:05 +00:00
|
|
|
*/
|
2013-12-19 12:53:08 +00:00
|
|
|
public void commitTyped(final SettingsValues settingsValues, final String separatorString) {
|
2013-12-19 07:37:07 +00:00
|
|
|
if (!mWordComposer.isComposingWord()) return;
|
|
|
|
final String typedWord = mWordComposer.getTypedWord();
|
|
|
|
if (typedWord.length() > 0) {
|
2014-09-18 05:53:59 +00:00
|
|
|
final boolean isBatchMode = mWordComposer.isBatchMode();
|
2013-12-19 12:53:08 +00:00
|
|
|
commitChosenWord(settingsValues, typedWord,
|
|
|
|
LastComposedWord.COMMIT_TYPE_USER_TYPED_WORD, separatorString);
|
2014-09-18 05:53:59 +00:00
|
|
|
StatsUtils.onWordCommitUserTyped(typedWord, isBatchMode);
|
2013-12-19 07:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-19 09:18:18 +00:00
|
|
|
|
2013-12-19 10:29:05 +00:00
|
|
|
/**
|
|
|
|
* Commit the current auto-correction.
|
|
|
|
*
|
|
|
|
* This will commit the best guess of the keyboard regarding what the user meant by typing
|
|
|
|
* the currently composing word. The IME computes suggestions and assigns a confidence score
|
|
|
|
* to each of them; when it's confident enough in one suggestion, it replaces the typed string
|
|
|
|
* by this suggestion at commit time. When it's not confident enough, or when it has no
|
|
|
|
* suggestions, or when the settings or environment does not allow for auto-correction, then
|
|
|
|
* this method just commits the typed string.
|
|
|
|
* Note that if suggestions are currently being computed in the background, this method will
|
|
|
|
* block until the computation returns. This is necessary for consistency (it would be very
|
|
|
|
* strange if pressing space would commit a different word depending on how fast you press).
|
|
|
|
*
|
|
|
|
* @param settingsValues the current value of the settings.
|
|
|
|
* @param separator the separator that's causing the commit to happen.
|
|
|
|
*/
|
2013-12-25 12:32:44 +00:00
|
|
|
private void commitCurrentAutoCorrection(final SettingsValues settingsValues,
|
2015-03-20 17:43:38 +00:00
|
|
|
final String separator, final LatinIME.UIHandler handler) {
|
2013-12-19 09:18:18 +00:00
|
|
|
// Complete any pending suggestions query first
|
|
|
|
if (handler.hasPendingUpdateSuggestions()) {
|
2014-03-03 05:19:52 +00:00
|
|
|
handler.cancelUpdateSuggestionStrip();
|
2014-08-14 03:48:50 +00:00
|
|
|
// To know the input style here, we should retrieve the in-flight "update suggestions"
|
|
|
|
// message and read its arg1 member here. However, the Handler class does not let
|
|
|
|
// us retrieve this message, so we can't do that. But in fact, we notice that
|
|
|
|
// we only ever come here when the input style was typing. In the case of batch
|
|
|
|
// input, we update the suggestions synchronously when the tail batch comes. Likewise
|
|
|
|
// for application-specified completions. As for recorrections, we never auto-correct,
|
|
|
|
// so we don't come here either. Hence, the input style is necessarily
|
|
|
|
// INPUT_STYLE_TYPING.
|
|
|
|
performUpdateSuggestionStripSync(settingsValues, SuggestedWords.INPUT_STYLE_TYPING);
|
2013-12-19 09:18:18 +00:00
|
|
|
}
|
2014-10-07 10:29:05 +00:00
|
|
|
final SuggestedWordInfo autoCorrectionOrNull = mWordComposer.getAutoCorrectionOrNull();
|
2013-12-19 09:18:18 +00:00
|
|
|
final String typedWord = mWordComposer.getTypedWord();
|
2014-10-07 10:29:05 +00:00
|
|
|
final String stringToCommit = (autoCorrectionOrNull != null)
|
|
|
|
? autoCorrectionOrNull.mWord : typedWord;
|
|
|
|
if (stringToCommit != null) {
|
2013-12-19 09:18:18 +00:00
|
|
|
if (TextUtils.isEmpty(typedWord)) {
|
|
|
|
throw new RuntimeException("We have an auto-correction but the typed word "
|
|
|
|
+ "is empty? Impossible! I must commit suicide.");
|
|
|
|
}
|
2014-09-18 05:53:59 +00:00
|
|
|
final boolean isBatchMode = mWordComposer.isBatchMode();
|
2014-10-07 10:29:05 +00:00
|
|
|
commitChosenWord(settingsValues, stringToCommit,
|
2013-12-19 12:53:08 +00:00
|
|
|
LastComposedWord.COMMIT_TYPE_DECIDED_WORD, separator);
|
2014-10-07 10:29:05 +00:00
|
|
|
if (!typedWord.equals(stringToCommit)) {
|
2013-12-19 09:18:18 +00:00
|
|
|
// This will make the correction flash for a short while as a visual clue
|
|
|
|
// to the user that auto-correction happened. It has no other effect; in particular
|
|
|
|
// note that this won't affect the text inside the text field AT ALL: it only makes
|
|
|
|
// the segment of text starting at the supplied index and running for the length
|
|
|
|
// of the auto-correction flash. At this moment, the "typedWord" argument is
|
|
|
|
// ignored by TextView.
|
2014-02-05 09:33:41 +00:00
|
|
|
mConnection.commitCorrection(new CorrectionInfo(
|
2014-10-07 10:29:05 +00:00
|
|
|
mConnection.getExpectedSelectionEnd() - stringToCommit.length(),
|
|
|
|
typedWord, stringToCommit));
|
2015-03-19 23:07:04 +00:00
|
|
|
String prevWordsContext = (autoCorrectionOrNull != null)
|
|
|
|
? autoCorrectionOrNull.mPrevWordsContext
|
|
|
|
: "";
|
2014-10-07 10:29:05 +00:00
|
|
|
StatsUtils.onAutoCorrection(typedWord, stringToCommit, isBatchMode,
|
2015-03-19 23:07:04 +00:00
|
|
|
mDictionaryFacilitator, prevWordsContext);
|
2014-10-07 10:29:05 +00:00
|
|
|
StatsUtils.onWordCommitAutoCorrect(stringToCommit, isBatchMode);
|
2014-09-18 05:53:59 +00:00
|
|
|
} else {
|
2014-10-07 10:29:05 +00:00
|
|
|
StatsUtils.onWordCommitUserTyped(stringToCommit, isBatchMode);
|
2013-12-19 09:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-19 12:53:08 +00:00
|
|
|
|
|
|
|
/**
|
2014-09-14 07:53:33 +00:00
|
|
|
* Commits the chosen word to the text field and saves it for later retrieval.
|
2013-12-19 12:53:08 +00:00
|
|
|
*
|
|
|
|
* @param settingsValues the current values of the settings.
|
|
|
|
* @param chosenWord the word we want to commit.
|
|
|
|
* @param commitType the type of the commit, as one of LastComposedWord.COMMIT_TYPE_*
|
|
|
|
* @param separatorString the separator that's causing the commit, or NOT_A_SEPARATOR if none.
|
|
|
|
*/
|
2014-03-03 05:19:52 +00:00
|
|
|
private void commitChosenWord(final SettingsValues settingsValues, final String chosenWord,
|
2013-12-19 12:53:08 +00:00
|
|
|
final int commitType, final String separatorString) {
|
|
|
|
final SuggestedWords suggestedWords = mSuggestedWords;
|
2014-01-09 09:36:23 +00:00
|
|
|
final CharSequence chosenWordWithSuggestions =
|
|
|
|
SuggestionSpanUtils.getTextWithSuggestionSpan(mLatinIME, chosenWord,
|
|
|
|
suggestedWords);
|
2014-09-29 01:52:18 +00:00
|
|
|
// When we are composing word, get n-gram context from the 2nd previous word because the
|
|
|
|
// 1st previous word is the word to be committed. Otherwise get n-gram context from the 1st
|
|
|
|
// previous word.
|
|
|
|
final NgramContext ngramContext = mConnection.getNgramContextFromNthPreviousWord(
|
2014-06-27 08:59:21 +00:00
|
|
|
settingsValues.mSpacingAndPunctuations, mWordComposer.isComposingWord() ? 2 : 1);
|
2014-09-14 07:53:33 +00:00
|
|
|
mConnection.commitText(chosenWordWithSuggestions, 1);
|
2013-12-19 12:53:08 +00:00
|
|
|
// Add the word to the user history dictionary
|
2014-09-29 01:52:18 +00:00
|
|
|
performAdditionToUserHistoryDictionary(settingsValues, chosenWord, ngramContext);
|
2013-12-19 12:53:08 +00:00
|
|
|
// TODO: figure out here if this is an auto-correct or if the best word is actually
|
|
|
|
// what user typed. Note: currently this is done much later in
|
|
|
|
// LastComposedWord#didCommitTypedWord by string equality of the remembered
|
|
|
|
// strings.
|
|
|
|
mLastComposedWord = mWordComposer.commitWord(commitType,
|
2014-09-29 01:52:18 +00:00
|
|
|
chosenWordWithSuggestions, separatorString, ngramContext);
|
2013-12-19 12:53:08 +00:00
|
|
|
}
|
2013-12-20 11:07:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retry resetting caches in the rich input connection.
|
|
|
|
*
|
|
|
|
* When the editor can't be accessed we can't reset the caches, so we schedule a retry.
|
|
|
|
* This method handles the retry, and re-schedules a new retry if we still can't access.
|
|
|
|
* We only retry up to 5 times before giving up.
|
|
|
|
*
|
|
|
|
* @param tryResumeSuggestions Whether we should resume suggestions or not.
|
|
|
|
* @param remainingTries How many times we may try again before giving up.
|
2013-12-27 08:04:12 +00:00
|
|
|
* @return whether true if the caches were successfully reset, false otherwise.
|
2013-12-20 11:07:16 +00:00
|
|
|
*/
|
2014-08-04 13:07:15 +00:00
|
|
|
public boolean retryResetCachesAndReturnSuccess(final boolean tryResumeSuggestions,
|
2015-03-20 17:43:38 +00:00
|
|
|
final int remainingTries, final LatinIME.UIHandler handler) {
|
Fix a bug on rotation with selection.
The symptom : when text is selected and the device is rotated,
sometimes the keyboard sets the word as being composed around
the start of the selection. Upon the next rotation this ends up
with the keyboard committing some text in place of the selection.
The cause : another bug in the framework with rotation >.>
The keyboard receives a call to startInput with a wrong cursor
position, namely one that does not represent a selection. The
keyboard sets a composition according to this wrong data. When
the keyboard is rotated again, it commits the text, which takes
the place of the selection.
The solution : actually when restarting input the keyboard
realizes that the cursor position is wrong. We cancel composition
at that time.
For robustness, this change also implements two other defensive
changes : upon call to onUpdateSelection, we actually realize
that the previous values were wrong, so we also fix it at that
time, and in addition, when rotating, we finishComposingText()
instead of commitText() which is less dangerous. Implementing
this later change also allows us to let less internal variables
from InputLogic escape to LatinIME, so it's also a good change
for design.
Bug: 14140799
Change-Id: Ib10de18e53e376ac1bbc8487e13d969828483346
2014-06-05 08:48:10 +00:00
|
|
|
final boolean shouldFinishComposition = mConnection.hasSelection()
|
|
|
|
|| !mConnection.isCursorPositionKnown();
|
2013-12-20 11:07:16 +00:00
|
|
|
if (!mConnection.resetCachesUponCursorMoveAndReturnSuccess(
|
2014-01-09 05:09:26 +00:00
|
|
|
mConnection.getExpectedSelectionStart(), mConnection.getExpectedSelectionEnd(),
|
Fix a bug on rotation with selection.
The symptom : when text is selected and the device is rotated,
sometimes the keyboard sets the word as being composed around
the start of the selection. Upon the next rotation this ends up
with the keyboard committing some text in place of the selection.
The cause : another bug in the framework with rotation >.>
The keyboard receives a call to startInput with a wrong cursor
position, namely one that does not represent a selection. The
keyboard sets a composition according to this wrong data. When
the keyboard is rotated again, it commits the text, which takes
the place of the selection.
The solution : actually when restarting input the keyboard
realizes that the cursor position is wrong. We cancel composition
at that time.
For robustness, this change also implements two other defensive
changes : upon call to onUpdateSelection, we actually realize
that the previous values were wrong, so we also fix it at that
time, and in addition, when rotating, we finishComposingText()
instead of commitText() which is less dangerous. Implementing
this later change also allows us to let less internal variables
from InputLogic escape to LatinIME, so it's also a good change
for design.
Bug: 14140799
Change-Id: Ib10de18e53e376ac1bbc8487e13d969828483346
2014-06-05 08:48:10 +00:00
|
|
|
shouldFinishComposition)) {
|
2013-12-20 11:07:16 +00:00
|
|
|
if (0 < remainingTries) {
|
2014-08-06 09:34:26 +00:00
|
|
|
handler.postResetCaches(tryResumeSuggestions, remainingTries - 1);
|
2013-12-27 08:04:12 +00:00
|
|
|
return false;
|
2013-12-20 11:07:16 +00:00
|
|
|
}
|
2013-12-27 08:04:12 +00:00
|
|
|
// If remainingTries is 0, we should stop waiting for new tries, however we'll still
|
|
|
|
// return true as we need to perform other tasks (for example, loading the keyboard).
|
2013-12-20 11:07:16 +00:00
|
|
|
}
|
2014-01-09 05:09:26 +00:00
|
|
|
mConnection.tryFixLyingCursorPosition();
|
2013-12-20 11:07:16 +00:00
|
|
|
if (tryResumeSuggestions) {
|
2014-10-01 09:32:02 +00:00
|
|
|
handler.postResumeSuggestions(true /* shouldDelay */);
|
2013-12-20 11:07:16 +00:00
|
|
|
}
|
2013-12-27 08:04:12 +00:00
|
|
|
return true;
|
2013-12-20 11:07:16 +00:00
|
|
|
}
|
2014-06-13 07:50:05 +00:00
|
|
|
|
|
|
|
public void getSuggestedWords(final SettingsValues settingsValues,
|
2015-02-25 19:27:48 +00:00
|
|
|
final Keyboard keyboard, final int keyboardShiftMode, final int inputStyle,
|
|
|
|
final int sequenceNumber, final OnGetSuggestedWordsCallback callback) {
|
2014-06-13 07:50:05 +00:00
|
|
|
mWordComposer.adviseCapitalizedModeBeforeFetchingSuggestions(
|
|
|
|
getActualCapsMode(settingsValues, keyboardShiftMode));
|
|
|
|
mSuggest.getSuggestedWords(mWordComposer,
|
2014-09-29 01:52:18 +00:00
|
|
|
getNgramContextFromNthPreviousWordForSuggestion(
|
2014-06-13 07:50:05 +00:00
|
|
|
settingsValues.mSpacingAndPunctuations,
|
|
|
|
// Get the word on which we should search the bigrams. If we are composing
|
|
|
|
// a word, it's whatever is *before* the half-committed word in the buffer,
|
|
|
|
// hence 2; if we aren't, we should just skip whitespace if any, so 1.
|
|
|
|
mWordComposer.isComposingWord() ? 2 : 1),
|
2015-02-25 19:27:48 +00:00
|
|
|
keyboard,
|
2015-03-12 00:09:44 +00:00
|
|
|
new SettingsValuesForSuggestion(settingsValues.mBlockPotentiallyOffensive),
|
2014-07-22 17:18:38 +00:00
|
|
|
settingsValues.mAutoCorrectionEnabledPerUserSettings,
|
2015-02-25 19:27:48 +00:00
|
|
|
inputStyle, sequenceNumber, callback);
|
2014-06-13 07:50:05 +00:00
|
|
|
}
|
2014-08-25 03:08:55 +00:00
|
|
|
|
2014-08-27 04:55:48 +00:00
|
|
|
/**
|
|
|
|
* Used as an injection point for each call of
|
|
|
|
* {@link RichInputConnection#setComposingText(CharSequence, int)}.
|
|
|
|
*
|
|
|
|
* <p>Currently using this method is optional and you can still directly call
|
|
|
|
* {@link RichInputConnection#setComposingText(CharSequence, int)}, but it is recommended to
|
2015-02-03 18:18:06 +00:00
|
|
|
* use this method whenever possible.<p>
|
2014-08-27 04:55:48 +00:00
|
|
|
* <p>TODO: Should we move this mechanism to {@link RichInputConnection}?</p>
|
|
|
|
*
|
|
|
|
* @param newComposingText the composing text to be set
|
|
|
|
* @param newCursorPosition the new cursor position
|
|
|
|
*/
|
|
|
|
private void setComposingTextInternal(final CharSequence newComposingText,
|
|
|
|
final int newCursorPosition) {
|
2014-08-27 07:32:33 +00:00
|
|
|
setComposingTextInternalWithBackgroundColor(newComposingText, newCursorPosition,
|
2014-09-14 07:53:33 +00:00
|
|
|
Color.TRANSPARENT, newComposingText.length());
|
2014-08-27 07:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equivalent to {@link #setComposingTextInternal(CharSequence, int)} except that this method
|
|
|
|
* allows to set {@link BackgroundColorSpan} to the composing text with the given color.
|
|
|
|
*
|
|
|
|
* <p>TODO: Currently the background color is exclusive with the black underline, which is
|
|
|
|
* automatically added by the framework. We need to change the framework if we need to have both
|
|
|
|
* of them at the same time.</p>
|
|
|
|
* <p>TODO: Should we move this method to {@link RichInputConnection}?</p>
|
|
|
|
*
|
|
|
|
* @param newComposingText the composing text to be set
|
|
|
|
* @param newCursorPosition the new cursor position
|
|
|
|
* @param backgroundColor the background color to be set to the composing text. Set
|
|
|
|
* {@link Color#TRANSPARENT} to disable the background color.
|
2014-09-14 07:53:33 +00:00
|
|
|
* @param coloredTextLength the length of text, in Java chars, which should be rendered with
|
|
|
|
* the given background color.
|
2014-08-27 07:32:33 +00:00
|
|
|
*/
|
|
|
|
private void setComposingTextInternalWithBackgroundColor(final CharSequence newComposingText,
|
2014-09-14 07:53:33 +00:00
|
|
|
final int newCursorPosition, final int backgroundColor, final int coloredTextLength) {
|
2014-08-27 07:32:33 +00:00
|
|
|
final CharSequence composingTextToBeSet;
|
|
|
|
if (backgroundColor == Color.TRANSPARENT) {
|
|
|
|
composingTextToBeSet = newComposingText;
|
|
|
|
} else {
|
|
|
|
final SpannableString spannable = new SpannableString(newComposingText);
|
|
|
|
final BackgroundColorSpan backgroundColorSpan =
|
|
|
|
new BackgroundColorSpan(backgroundColor);
|
2014-09-14 07:53:33 +00:00
|
|
|
final int spanLength = Math.min(coloredTextLength, spannable.length());
|
|
|
|
spannable.setSpan(backgroundColorSpan, 0, spanLength,
|
2014-08-27 07:32:33 +00:00
|
|
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
|
|
|
|
composingTextToBeSet = spannable;
|
|
|
|
}
|
|
|
|
mConnection.setComposingText(composingTextToBeSet, newCursorPosition);
|
2014-08-27 04:55:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-04 20:42:10 +00:00
|
|
|
/**
|
|
|
|
* Gets an object allowing private IME commands to be sent to the
|
|
|
|
* underlying editor.
|
|
|
|
* @return An object for sending private commands to the underlying editor.
|
|
|
|
*/
|
|
|
|
public PrivateCommandPerformer getPrivateCommandPerformer() {
|
|
|
|
return mConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the expected index of the first char of the composing span within the editor's text.
|
|
|
|
* Returns a negative value in case there appears to be no valid composing span.
|
|
|
|
*
|
|
|
|
* @see #getComposingLength()
|
|
|
|
* @see RichInputConnection#hasSelection()
|
|
|
|
* @see RichInputConnection#isCursorPositionKnown()
|
|
|
|
* @see RichInputConnection#getExpectedSelectionStart()
|
|
|
|
* @see RichInputConnection#getExpectedSelectionEnd()
|
|
|
|
* @return The expected index in Java chars of the first char of the composing span.
|
|
|
|
*/
|
|
|
|
// TODO: try and see if we can get rid of this method. Ideally the users of this class should
|
|
|
|
// never need to know this.
|
|
|
|
public int getComposingStart() {
|
|
|
|
if (!mConnection.isCursorPositionKnown() || mConnection.hasSelection()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return mConnection.getExpectedSelectionStart() - mWordComposer.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the expected length in Java chars of the composing span.
|
|
|
|
* May be 0 if there is no valid composing span.
|
|
|
|
* @see #getComposingStart()
|
|
|
|
* @return The expected length of the composing span.
|
|
|
|
*/
|
|
|
|
// TODO: try and see if we can get rid of this method. Ideally the users of this class should
|
|
|
|
// never need to know this.
|
|
|
|
public int getComposingLength() {
|
|
|
|
return mWordComposer.size();
|
|
|
|
}
|
2013-12-18 09:08:35 +00:00
|
|
|
}
|