2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2010-03-26 22:07:10 +00:00
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2011-12-20 08:52:29 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2011-12-20 08:52:29 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-12-20 08:52:29 +00:00
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
2014-03-14 13:50:20 +00:00
|
|
|
import com.android.inputmethod.event.CombinerChain;
|
2014-03-13 08:37:16 +00:00
|
|
|
import com.android.inputmethod.event.Event;
|
|
|
|
import com.android.inputmethod.latin.utils.CollectionUtils;
|
2013-12-27 11:58:32 +00:00
|
|
|
import com.android.inputmethod.latin.utils.CoordinateUtils;
|
2013-07-17 09:20:03 +00:00
|
|
|
import com.android.inputmethod.latin.utils.StringUtils;
|
2011-02-10 11:53:58 +00:00
|
|
|
|
2014-03-13 08:37:16 +00:00
|
|
|
import java.util.ArrayList;
|
2011-11-18 11:03:38 +00:00
|
|
|
import java.util.Arrays;
|
2014-03-13 08:37:16 +00:00
|
|
|
import java.util.Collections;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A place to store the currently composing word with information such as adjacent key codes as well
|
|
|
|
*/
|
2012-09-27 09:16:16 +00:00
|
|
|
public final class WordComposer {
|
2013-07-26 03:35:11 +00:00
|
|
|
private static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH;
|
2013-04-10 09:30:11 +00:00
|
|
|
private static final boolean DBG = LatinImeLogger.sDBG;
|
2011-02-22 08:28:55 +00:00
|
|
|
|
2012-08-21 10:49:25 +00:00
|
|
|
public static final int CAPS_MODE_OFF = 0;
|
|
|
|
// 1 is shift bit, 2 is caps bit, 4 is auto bit but this is just a convention as these bits
|
|
|
|
// aren't used anywhere in the code
|
|
|
|
public static final int CAPS_MODE_MANUAL_SHIFTED = 0x1;
|
|
|
|
public static final int CAPS_MODE_MANUAL_SHIFT_LOCKED = 0x3;
|
|
|
|
public static final int CAPS_MODE_AUTO_SHIFTED = 0x5;
|
|
|
|
public static final int CAPS_MODE_AUTO_SHIFT_LOCKED = 0x7;
|
|
|
|
|
2014-03-14 13:50:20 +00:00
|
|
|
private CombinerChain mCombinerChain;
|
|
|
|
|
2013-07-17 09:20:03 +00:00
|
|
|
// An array of code points representing the characters typed so far.
|
|
|
|
// The array is limited to MAX_WORD_LENGTH code points, but mTypedWord extends past that
|
|
|
|
// and mCodePointSize can go past that. If mCodePointSize is greater than MAX_WORD_LENGTH,
|
|
|
|
// this just does not contain the associated code points past MAX_WORD_LENGTH.
|
2012-03-27 06:21:54 +00:00
|
|
|
private int[] mPrimaryKeyCodes;
|
2014-03-13 08:37:16 +00:00
|
|
|
// The list of events that served to compose this string.
|
|
|
|
private final ArrayList<Event> mEvents;
|
2012-12-17 08:43:09 +00:00
|
|
|
private final InputPointers mInputPointers = new InputPointers(MAX_WORD_LENGTH);
|
2013-07-17 09:20:03 +00:00
|
|
|
// This is the typed word, as a StringBuilder. This has the same contents as mPrimaryKeyCodes
|
|
|
|
// but under a StringBuilder representation for ease of use, depending on what is more useful
|
|
|
|
// at any given time. However this is not limited in size, while mPrimaryKeyCodes is limited
|
|
|
|
// to MAX_WORD_LENGTH code points.
|
2012-07-06 11:00:22 +00:00
|
|
|
private final StringBuilder mTypedWord;
|
2013-12-13 08:09:16 +00:00
|
|
|
// The previous word (before the composing word). Used as context for suggestions. May be null
|
|
|
|
// after resetting and before starting a new composing word, or when there is no context like
|
2013-12-16 12:41:03 +00:00
|
|
|
// at the start of text for example. It can also be set to null externally when the user
|
|
|
|
// enters a separator that does not let bigrams across, like a period or a comma.
|
|
|
|
private String mPreviousWordForSuggestion;
|
2012-10-03 06:19:43 +00:00
|
|
|
private String mAutoCorrection;
|
2012-04-26 09:01:13 +00:00
|
|
|
private boolean mIsResumed;
|
2012-07-10 01:46:13 +00:00
|
|
|
private boolean mIsBatchMode;
|
2013-04-12 11:45:18 +00:00
|
|
|
// A memory of the last rejected batch mode suggestion, if any. This goes like this: the user
|
|
|
|
// gestures a word, is displeased with the results and hits backspace, then gestures again.
|
|
|
|
// At the very least we should avoid re-suggesting the same thing, and to do that we memorize
|
|
|
|
// the rejected suggestion in this variable.
|
|
|
|
// TODO: this should be done in a comprehensive way by the User History feature instead of
|
|
|
|
// as an ad-hockery here.
|
|
|
|
private String mRejectedBatchModeSuggestion;
|
2009-07-23 19:17:48 +00:00
|
|
|
|
2012-01-26 08:29:57 +00:00
|
|
|
// Cache these values for performance
|
2009-07-23 19:17:48 +00:00
|
|
|
private int mCapsCount;
|
2012-07-27 14:13:28 +00:00
|
|
|
private int mDigitsCount;
|
2012-08-21 10:49:25 +00:00
|
|
|
private int mCapitalizedMode;
|
2011-11-29 05:15:41 +00:00
|
|
|
private int mTrailingSingleQuotesCount;
|
2013-07-17 09:20:03 +00:00
|
|
|
// This is the number of code points entered so far. This is not limited to MAX_WORD_LENGTH.
|
|
|
|
// In general, this contains the size of mPrimaryKeyCodes, except when this is greater than
|
|
|
|
// MAX_WORD_LENGTH in which case mPrimaryKeyCodes only contain the first MAX_WORD_LENGTH
|
|
|
|
// code points.
|
2012-03-27 06:21:54 +00:00
|
|
|
private int mCodePointSize;
|
2013-04-10 07:38:37 +00:00
|
|
|
private int mCursorPositionWithinWord;
|
2011-11-18 11:03:38 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
2010-09-27 15:32:35 +00:00
|
|
|
* Whether the user chose to capitalize the first char of the word.
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
2010-09-27 15:32:35 +00:00
|
|
|
private boolean mIsFirstCharCapitalized;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
public WordComposer() {
|
2014-03-14 13:50:20 +00:00
|
|
|
mCombinerChain = new CombinerChain();
|
2012-12-17 08:43:09 +00:00
|
|
|
mPrimaryKeyCodes = new int[MAX_WORD_LENGTH];
|
2014-03-13 08:37:16 +00:00
|
|
|
mEvents = CollectionUtils.newArrayList();
|
2012-12-17 08:43:09 +00:00
|
|
|
mTypedWord = new StringBuilder(MAX_WORD_LENGTH);
|
2012-01-26 08:29:57 +00:00
|
|
|
mAutoCorrection = null;
|
2011-11-29 05:15:41 +00:00
|
|
|
mTrailingSingleQuotesCount = 0;
|
2012-04-26 09:01:13 +00:00
|
|
|
mIsResumed = false;
|
2012-07-10 01:46:13 +00:00
|
|
|
mIsBatchMode = false;
|
2013-04-10 07:38:37 +00:00
|
|
|
mCursorPositionWithinWord = 0;
|
2013-04-12 11:45:18 +00:00
|
|
|
mRejectedBatchModeSuggestion = null;
|
2013-12-16 12:41:03 +00:00
|
|
|
mPreviousWordForSuggestion = null;
|
2012-03-27 06:21:54 +00:00
|
|
|
refreshSize();
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
public WordComposer(final WordComposer source) {
|
2014-03-14 13:50:20 +00:00
|
|
|
mCombinerChain = source.mCombinerChain;
|
2012-03-27 06:21:54 +00:00
|
|
|
mPrimaryKeyCodes = Arrays.copyOf(source.mPrimaryKeyCodes, source.mPrimaryKeyCodes.length);
|
2014-03-13 08:37:16 +00:00
|
|
|
mEvents = new ArrayList<Event>(source.mEvents);
|
2012-01-26 08:29:57 +00:00
|
|
|
mTypedWord = new StringBuilder(source.mTypedWord);
|
2012-06-29 09:42:15 +00:00
|
|
|
mInputPointers.copy(source.mInputPointers);
|
2011-09-13 08:46:23 +00:00
|
|
|
mCapsCount = source.mCapsCount;
|
2012-07-27 14:13:28 +00:00
|
|
|
mDigitsCount = source.mDigitsCount;
|
2011-09-13 08:46:23 +00:00
|
|
|
mIsFirstCharCapitalized = source.mIsFirstCharCapitalized;
|
2012-08-21 10:49:25 +00:00
|
|
|
mCapitalizedMode = source.mCapitalizedMode;
|
2011-11-29 05:15:41 +00:00
|
|
|
mTrailingSingleQuotesCount = source.mTrailingSingleQuotesCount;
|
2012-04-26 09:01:13 +00:00
|
|
|
mIsResumed = source.mIsResumed;
|
2012-07-10 01:46:13 +00:00
|
|
|
mIsBatchMode = source.mIsBatchMode;
|
2013-04-10 07:38:37 +00:00
|
|
|
mCursorPositionWithinWord = source.mCursorPositionWithinWord;
|
2013-04-12 11:45:18 +00:00
|
|
|
mRejectedBatchModeSuggestion = source.mRejectedBatchModeSuggestion;
|
2013-12-16 12:41:03 +00:00
|
|
|
mPreviousWordForSuggestion = source.mPreviousWordForSuggestion;
|
2012-03-27 06:21:54 +00:00
|
|
|
refreshSize();
|
2010-08-20 05:35:02 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Clear out the keys registered so far.
|
|
|
|
*/
|
|
|
|
public void reset() {
|
2012-01-26 08:29:57 +00:00
|
|
|
mTypedWord.setLength(0);
|
2014-03-13 08:37:16 +00:00
|
|
|
mEvents.clear();
|
2012-01-26 08:29:57 +00:00
|
|
|
mAutoCorrection = null;
|
2009-07-23 19:17:48 +00:00
|
|
|
mCapsCount = 0;
|
2012-07-27 14:13:28 +00:00
|
|
|
mDigitsCount = 0;
|
2011-09-13 08:46:23 +00:00
|
|
|
mIsFirstCharCapitalized = false;
|
2011-11-29 05:15:41 +00:00
|
|
|
mTrailingSingleQuotesCount = 0;
|
2012-04-26 09:01:13 +00:00
|
|
|
mIsResumed = false;
|
2012-07-10 01:46:13 +00:00
|
|
|
mIsBatchMode = false;
|
2013-04-10 07:38:37 +00:00
|
|
|
mCursorPositionWithinWord = 0;
|
2013-04-12 11:45:18 +00:00
|
|
|
mRejectedBatchModeSuggestion = null;
|
2013-12-16 12:41:03 +00:00
|
|
|
mPreviousWordForSuggestion = null;
|
2012-03-27 06:21:54 +00:00
|
|
|
refreshSize();
|
|
|
|
}
|
|
|
|
|
2012-06-27 05:35:24 +00:00
|
|
|
private final void refreshSize() {
|
2012-03-27 06:21:54 +00:00
|
|
|
mCodePointSize = mTypedWord.codePointCount(0, mTypedWord.length());
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of keystrokes in the composing word.
|
|
|
|
* @return the number of keystrokes
|
|
|
|
*/
|
2011-09-13 08:46:23 +00:00
|
|
|
public final int size() {
|
2012-03-27 06:21:54 +00:00
|
|
|
return mCodePointSize;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 14:24:37 +00:00
|
|
|
public final boolean isComposingWord() {
|
2012-03-27 06:21:54 +00:00
|
|
|
return size() > 0;
|
2011-12-13 14:24:37 +00:00
|
|
|
}
|
|
|
|
|
2012-03-28 02:32:11 +00:00
|
|
|
// TODO: make sure that the index should not exceed MAX_WORD_LENGTH
|
2012-03-27 06:21:54 +00:00
|
|
|
public int getCodeAt(int index) {
|
2012-12-17 08:43:09 +00:00
|
|
|
if (index >= MAX_WORD_LENGTH) {
|
2012-03-28 02:32:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-03-27 06:21:54 +00:00
|
|
|
return mPrimaryKeyCodes[index];
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 09:30:11 +00:00
|
|
|
public int getCodeBeforeCursor() {
|
|
|
|
if (mCursorPositionWithinWord < 1 || mCursorPositionWithinWord > mPrimaryKeyCodes.length) {
|
|
|
|
return Constants.NOT_A_CODE;
|
|
|
|
}
|
|
|
|
return mPrimaryKeyCodes[mCursorPositionWithinWord - 1];
|
|
|
|
}
|
|
|
|
|
2012-06-29 09:42:15 +00:00
|
|
|
public InputPointers getInputPointers() {
|
|
|
|
return mInputPointers;
|
2011-02-22 08:28:55 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
private static boolean isFirstCharCapitalized(final int index, final int codePoint,
|
|
|
|
final boolean previous) {
|
2011-09-13 08:46:23 +00:00
|
|
|
if (index == 0) return Character.isUpperCase(codePoint);
|
2011-09-16 03:28:13 +00:00
|
|
|
return previous && !Character.isUpperCase(codePoint);
|
2011-09-13 08:46:23 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
2014-03-13 08:37:16 +00:00
|
|
|
* Add a new event for a key stroke, with the pressed key's code point with the touch point
|
|
|
|
* coordinates.
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
2014-03-13 08:37:16 +00:00
|
|
|
public void add(final Event event) {
|
|
|
|
final int primaryCode = event.mCodePoint;
|
|
|
|
final int keyX = event.mX;
|
|
|
|
final int keyY = event.mY;
|
2012-03-27 06:21:54 +00:00
|
|
|
final int newIndex = size();
|
2012-02-03 07:05:48 +00:00
|
|
|
mTypedWord.appendCodePoint(primaryCode);
|
2014-03-13 08:37:16 +00:00
|
|
|
mEvents.add(event);
|
2012-03-27 06:21:54 +00:00
|
|
|
refreshSize();
|
2013-04-10 07:38:37 +00:00
|
|
|
mCursorPositionWithinWord = mCodePointSize;
|
2012-12-17 08:43:09 +00:00
|
|
|
if (newIndex < MAX_WORD_LENGTH) {
|
2012-10-29 05:46:34 +00:00
|
|
|
mPrimaryKeyCodes[newIndex] = primaryCode >= Constants.CODE_SPACE
|
2012-03-28 07:30:52 +00:00
|
|
|
? Character.toLowerCase(primaryCode) : primaryCode;
|
2012-06-12 10:40:37 +00:00
|
|
|
// In the batch input mode, the {@code mInputPointers} holds batch input points and
|
|
|
|
// shouldn't be overridden by the "typed key" coordinates
|
|
|
|
// (See {@link #setBatchInputWord}).
|
|
|
|
if (!mIsBatchMode) {
|
|
|
|
// TODO: Set correct pointer id and time
|
2014-01-08 05:11:56 +00:00
|
|
|
mInputPointers.addPointerAt(newIndex, keyX, keyY, 0, 0);
|
2012-06-12 10:40:37 +00:00
|
|
|
}
|
2011-02-22 08:28:55 +00:00
|
|
|
}
|
2011-09-13 08:46:23 +00:00
|
|
|
mIsFirstCharCapitalized = isFirstCharCapitalized(
|
|
|
|
newIndex, primaryCode, mIsFirstCharCapitalized);
|
|
|
|
if (Character.isUpperCase(primaryCode)) mCapsCount++;
|
2012-07-27 14:13:28 +00:00
|
|
|
if (Character.isDigit(primaryCode)) mDigitsCount++;
|
2012-10-29 05:46:34 +00:00
|
|
|
if (Constants.CODE_SINGLE_QUOTE == primaryCode) {
|
2011-11-29 05:15:41 +00:00
|
|
|
++mTrailingSingleQuotesCount;
|
|
|
|
} else {
|
|
|
|
mTrailingSingleQuotesCount = 0;
|
|
|
|
}
|
2012-01-26 08:29:57 +00:00
|
|
|
mAutoCorrection = null;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 07:38:37 +00:00
|
|
|
public void setCursorPositionWithinWord(final int posWithinWord) {
|
|
|
|
mCursorPositionWithinWord = posWithinWord;
|
2014-03-13 08:37:16 +00:00
|
|
|
// TODO: compute where that puts us inside the events
|
2013-04-10 07:38:37 +00:00
|
|
|
}
|
|
|
|
|
2013-04-10 09:30:11 +00:00
|
|
|
public boolean isCursorFrontOrMiddleOfComposingWord() {
|
|
|
|
if (DBG && mCursorPositionWithinWord > mCodePointSize) {
|
|
|
|
throw new RuntimeException("Wrong cursor position : " + mCursorPositionWithinWord
|
|
|
|
+ "in a word of size " + mCodePointSize);
|
|
|
|
}
|
|
|
|
return mCursorPositionWithinWord != mCodePointSize;
|
2013-04-10 07:38:37 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 10:26:30 +00:00
|
|
|
/**
|
|
|
|
* When the cursor is moved by the user, we need to update its position.
|
|
|
|
* If it falls inside the currently composing word, we don't reset the composition, and
|
|
|
|
* only update the cursor position.
|
|
|
|
*
|
|
|
|
* @param expectedMoveAmount How many java chars to move the cursor. Negative values move
|
|
|
|
* the cursor backward, positive values move the cursor forward.
|
|
|
|
* @return true if the cursor is still inside the composing word, false otherwise.
|
|
|
|
*/
|
|
|
|
public boolean moveCursorByAndReturnIfInsideComposingWord(final int expectedMoveAmount) {
|
|
|
|
int actualMoveAmountWithinWord = 0;
|
|
|
|
int cursorPos = mCursorPositionWithinWord;
|
2013-07-17 09:20:03 +00:00
|
|
|
final int[] codePoints;
|
|
|
|
if (mCodePointSize >= MAX_WORD_LENGTH) {
|
|
|
|
// If we have more than MAX_WORD_LENGTH characters, we don't have everything inside
|
|
|
|
// mPrimaryKeyCodes. This should be rare enough that we can afford to just compute
|
|
|
|
// the array on the fly when this happens.
|
|
|
|
codePoints = StringUtils.toCodePointArray(mTypedWord.toString());
|
|
|
|
} else {
|
|
|
|
codePoints = mPrimaryKeyCodes;
|
|
|
|
}
|
2013-06-25 10:26:30 +00:00
|
|
|
if (expectedMoveAmount >= 0) {
|
|
|
|
// Moving the cursor forward for the expected amount or until the end of the word has
|
|
|
|
// been reached, whichever comes first.
|
|
|
|
while (actualMoveAmountWithinWord < expectedMoveAmount && cursorPos < mCodePointSize) {
|
2013-07-17 09:20:03 +00:00
|
|
|
actualMoveAmountWithinWord += Character.charCount(codePoints[cursorPos]);
|
2013-06-25 10:26:30 +00:00
|
|
|
++cursorPos;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Moving the cursor backward for the expected amount or until the start of the word
|
|
|
|
// has been reached, whichever comes first.
|
|
|
|
while (actualMoveAmountWithinWord > expectedMoveAmount && cursorPos > 0) {
|
|
|
|
--cursorPos;
|
2013-07-17 09:20:03 +00:00
|
|
|
actualMoveAmountWithinWord -= Character.charCount(codePoints[cursorPos]);
|
2013-06-25 10:26:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If the actual and expected amounts differ, we crossed the start or the end of the word
|
|
|
|
// so the result would not be inside the composing word.
|
|
|
|
if (actualMoveAmountWithinWord != expectedMoveAmount) return false;
|
|
|
|
mCursorPositionWithinWord = cursorPos;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
public void setBatchInputPointers(final InputPointers batchPointers) {
|
2012-06-12 10:40:37 +00:00
|
|
|
mInputPointers.set(batchPointers);
|
2012-07-10 01:46:13 +00:00
|
|
|
mIsBatchMode = true;
|
|
|
|
}
|
|
|
|
|
2012-10-03 06:19:43 +00:00
|
|
|
public void setBatchInputWord(final String word) {
|
2012-06-12 10:40:37 +00:00
|
|
|
reset();
|
|
|
|
mIsBatchMode = true;
|
|
|
|
final int length = word.length();
|
|
|
|
for (int i = 0; i < length; i = Character.offsetByCodePoints(word, i, 1)) {
|
|
|
|
final int codePoint = Character.codePointAt(word, i);
|
|
|
|
// We don't want to override the batch input points that are held in mInputPointers
|
|
|
|
// (See {@link #add(int,int,int)}).
|
2014-03-13 08:37:16 +00:00
|
|
|
add(Event.createEventForCodePointFromUnknownSource(codePoint));
|
2012-06-12 10:40:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-22 02:35:40 +00:00
|
|
|
/**
|
|
|
|
* Set the currently composing word to the one passed as an argument.
|
|
|
|
* This will register NOT_A_COORDINATE for X and Ys, and use the passed keyboard for proximity.
|
2013-12-27 11:58:32 +00:00
|
|
|
* @param codePoints the code points to set as the composing word.
|
|
|
|
* @param coordinates the x, y coordinates of the key in the CoordinateUtils format
|
2013-12-13 08:09:16 +00:00
|
|
|
* @param previousWord the previous word, to use as context for suggestions. Can be null if
|
|
|
|
* the context is nil (typically, at start of text).
|
2011-11-22 02:35:40 +00:00
|
|
|
*/
|
2013-12-27 11:58:32 +00:00
|
|
|
public void setComposingWord(final int[] codePoints, final int[] coordinates,
|
2013-12-27 12:00:28 +00:00
|
|
|
final CharSequence previousWord) {
|
2011-11-22 02:35:40 +00:00
|
|
|
reset();
|
2013-12-27 11:58:32 +00:00
|
|
|
final int length = codePoints.length;
|
|
|
|
for (int i = 0; i < length; ++i) {
|
2014-03-13 08:37:16 +00:00
|
|
|
add(Event.createEventForCodePointFromAlreadyTypedText(codePoints[i],
|
|
|
|
CoordinateUtils.xFromArray(coordinates, i),
|
|
|
|
CoordinateUtils.yFromArray(coordinates, i)));
|
2011-11-22 02:35:40 +00:00
|
|
|
}
|
2012-04-26 09:01:13 +00:00
|
|
|
mIsResumed = true;
|
2014-01-09 09:36:23 +00:00
|
|
|
mPreviousWordForSuggestion = null == previousWord ? null : previousWord.toString();
|
2011-11-22 02:35:40 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Delete the last keystroke as a result of hitting backspace.
|
|
|
|
*/
|
|
|
|
public void deleteLast() {
|
2012-03-27 06:21:54 +00:00
|
|
|
final int size = size();
|
2011-09-13 08:46:23 +00:00
|
|
|
if (size > 0) {
|
2012-02-03 07:05:48 +00:00
|
|
|
// Note: mTypedWord.length() and mCodes.length differ when there are surrogate pairs
|
|
|
|
final int stringBuilderLength = mTypedWord.length();
|
|
|
|
if (stringBuilderLength < size) {
|
|
|
|
throw new RuntimeException(
|
|
|
|
"In WordComposer: mCodes and mTypedWords have non-matching lengths");
|
|
|
|
}
|
|
|
|
final int lastChar = mTypedWord.codePointBefore(stringBuilderLength);
|
2014-03-13 08:37:16 +00:00
|
|
|
// TODO: with events and composition, this is absolutely not necessarily true.
|
|
|
|
mEvents.remove(mEvents.size() - 1);
|
2012-02-03 07:05:48 +00:00
|
|
|
if (Character.isSupplementaryCodePoint(lastChar)) {
|
|
|
|
mTypedWord.delete(stringBuilderLength - 2, stringBuilderLength);
|
|
|
|
} else {
|
|
|
|
mTypedWord.deleteCharAt(stringBuilderLength - 1);
|
|
|
|
}
|
2011-09-13 08:46:23 +00:00
|
|
|
if (Character.isUpperCase(lastChar)) mCapsCount--;
|
2012-07-27 14:13:28 +00:00
|
|
|
if (Character.isDigit(lastChar)) mDigitsCount--;
|
2012-03-27 06:21:54 +00:00
|
|
|
refreshSize();
|
2010-09-23 03:31:22 +00:00
|
|
|
}
|
2012-02-03 07:05:48 +00:00
|
|
|
// We may have deleted the last one.
|
2012-03-27 06:21:54 +00:00
|
|
|
if (0 == size()) {
|
2011-09-13 08:46:23 +00:00
|
|
|
mIsFirstCharCapitalized = false;
|
2011-11-29 05:15:41 +00:00
|
|
|
}
|
|
|
|
if (mTrailingSingleQuotesCount > 0) {
|
|
|
|
--mTrailingSingleQuotesCount;
|
2011-11-18 11:03:38 +00:00
|
|
|
} else {
|
2012-02-03 03:22:02 +00:00
|
|
|
int i = mTypedWord.length();
|
|
|
|
while (i > 0) {
|
|
|
|
i = mTypedWord.offsetByCodePoints(i, -1);
|
2012-10-29 05:46:34 +00:00
|
|
|
if (Constants.CODE_SINGLE_QUOTE != mTypedWord.codePointAt(i)) break;
|
2011-11-29 05:15:41 +00:00
|
|
|
++mTrailingSingleQuotesCount;
|
|
|
|
}
|
2011-02-22 08:28:55 +00:00
|
|
|
}
|
2013-04-10 07:38:37 +00:00
|
|
|
mCursorPositionWithinWord = mCodePointSize;
|
2012-01-26 08:29:57 +00:00
|
|
|
mAutoCorrection = null;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the word as it was typed, without any correction applied.
|
2011-12-13 10:38:36 +00:00
|
|
|
* @return the word that was typed so far. Never returns null.
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
2011-09-15 06:42:21 +00:00
|
|
|
public String getTypedWord() {
|
2012-01-26 08:29:57 +00:00
|
|
|
return mTypedWord.toString();
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2013-12-16 12:41:03 +00:00
|
|
|
public String getPreviousWordForSuggestion() {
|
|
|
|
return mPreviousWordForSuggestion;
|
2013-12-13 08:09:16 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the user typed a capital letter as the first letter in the word
|
|
|
|
* @return capitalization preference
|
|
|
|
*/
|
2010-09-27 15:32:35 +00:00
|
|
|
public boolean isFirstCharCapitalized() {
|
|
|
|
return mIsFirstCharCapitalized;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-09-27 15:32:35 +00:00
|
|
|
|
2011-11-29 05:15:41 +00:00
|
|
|
public int trailingSingleQuotesCount() {
|
|
|
|
return mTrailingSingleQuotesCount;
|
2011-11-18 11:03:38 +00:00
|
|
|
}
|
|
|
|
|
2010-09-27 15:32:35 +00:00
|
|
|
/**
|
|
|
|
* Whether or not all of the user typed chars are upper case
|
|
|
|
* @return true if all user typed chars are upper case, false otherwise
|
|
|
|
*/
|
|
|
|
public boolean isAllUpperCase() {
|
2012-09-19 03:52:06 +00:00
|
|
|
if (size() <= 1) {
|
|
|
|
return mCapitalizedMode == CAPS_MODE_AUTO_SHIFT_LOCKED
|
|
|
|
|| mCapitalizedMode == CAPS_MODE_MANUAL_SHIFT_LOCKED;
|
|
|
|
} else {
|
|
|
|
return mCapsCount == size();
|
|
|
|
}
|
2012-08-21 10:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean wasShiftedNoLock() {
|
|
|
|
return mCapitalizedMode == CAPS_MODE_AUTO_SHIFTED
|
|
|
|
|| mCapitalizedMode == CAPS_MODE_MANUAL_SHIFTED;
|
2010-09-27 15:32:35 +00:00
|
|
|
}
|
|
|
|
|
2009-07-23 19:17:48 +00:00
|
|
|
/**
|
|
|
|
* Returns true if more than one character is upper case, otherwise returns false.
|
|
|
|
*/
|
|
|
|
public boolean isMostlyCaps() {
|
|
|
|
return mCapsCount > 1;
|
|
|
|
}
|
2010-01-24 15:34:07 +00:00
|
|
|
|
2012-07-27 14:13:28 +00:00
|
|
|
/**
|
|
|
|
* Returns true if we have digits in the composing word.
|
|
|
|
*/
|
|
|
|
public boolean hasDigits() {
|
|
|
|
return mDigitsCount > 0;
|
|
|
|
}
|
|
|
|
|
2011-12-20 08:52:29 +00:00
|
|
|
/**
|
2013-12-13 08:09:16 +00:00
|
|
|
* Saves the caps mode and the previous word at the start of composing.
|
2012-08-21 10:49:25 +00:00
|
|
|
*
|
2013-12-13 08:09:16 +00:00
|
|
|
* WordComposer needs to know about the caps mode for several reasons. The first is, we need
|
|
|
|
* to know after the fact what the reason was, to register the correct form into the user
|
|
|
|
* history dictionary: if the word was automatically capitalized, we should insert it in
|
|
|
|
* all-lower case but if it's a manual pressing of shift, then it should be inserted as is.
|
2012-08-21 10:49:25 +00:00
|
|
|
* Also, batch input needs to know about the current caps mode to display correctly
|
|
|
|
* capitalized suggestions.
|
|
|
|
* @param mode the mode at the time of start
|
2013-12-13 08:09:16 +00:00
|
|
|
* @param previousWord the previous word as context for suggestions. May be null if none.
|
2010-01-24 15:34:07 +00:00
|
|
|
*/
|
2013-12-13 08:09:16 +00:00
|
|
|
public void setCapitalizedModeAndPreviousWordAtStartComposingTime(final int mode,
|
2014-01-09 09:36:23 +00:00
|
|
|
final CharSequence previousWord) {
|
2012-08-21 10:49:25 +00:00
|
|
|
mCapitalizedMode = mode;
|
2014-01-09 09:36:23 +00:00
|
|
|
mPreviousWordForSuggestion = null == previousWord ? null : previousWord.toString();
|
2010-01-24 15:34:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the word was automatically capitalized.
|
|
|
|
* @return whether the word was automatically capitalized
|
|
|
|
*/
|
2012-08-21 10:49:25 +00:00
|
|
|
public boolean wasAutoCapitalized() {
|
|
|
|
return mCapitalizedMode == CAPS_MODE_AUTO_SHIFT_LOCKED
|
|
|
|
|| mCapitalizedMode == CAPS_MODE_AUTO_SHIFTED;
|
2010-01-24 15:34:07 +00:00
|
|
|
}
|
2011-12-13 10:38:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the auto-correction for this word.
|
|
|
|
*/
|
2012-10-03 06:19:43 +00:00
|
|
|
public void setAutoCorrection(final String correction) {
|
2012-01-26 08:29:57 +00:00
|
|
|
mAutoCorrection = correction;
|
2011-12-13 10:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-13 14:08:12 +00:00
|
|
|
* @return the auto-correction for this word, or null if none.
|
2011-12-13 10:38:36 +00:00
|
|
|
*/
|
2012-10-03 06:19:43 +00:00
|
|
|
public String getAutoCorrectionOrNull() {
|
2012-01-26 08:29:57 +00:00
|
|
|
return mAutoCorrection;
|
2011-12-13 10:38:36 +00:00
|
|
|
}
|
2011-12-13 14:12:22 +00:00
|
|
|
|
2012-04-26 09:01:13 +00:00
|
|
|
/**
|
|
|
|
* @return whether we started composing this word by resuming suggestion on an existing string
|
|
|
|
*/
|
|
|
|
public boolean isResumed() {
|
|
|
|
return mIsResumed;
|
|
|
|
}
|
|
|
|
|
2012-01-26 06:52:55 +00:00
|
|
|
// `type' should be one of the LastComposedWord.COMMIT_TYPE_* constants above.
|
2014-01-09 09:36:23 +00:00
|
|
|
// committedWord should contain suggestion spans if applicable.
|
|
|
|
public LastComposedWord commitWord(final int type, final CharSequence committedWord,
|
2012-10-03 06:19:43 +00:00
|
|
|
final String separatorString, final String prevWord) {
|
2012-02-22 08:05:19 +00:00
|
|
|
// Note: currently, we come here whenever we commit a word. If it's a MANUAL_PICK
|
|
|
|
// or a DECIDED_WORD we may cancel the commit later; otherwise, we should deactivate
|
|
|
|
// the last composed word to ensure this does not happen.
|
2012-03-27 06:21:54 +00:00
|
|
|
final int[] primaryKeyCodes = mPrimaryKeyCodes;
|
2012-12-17 08:43:09 +00:00
|
|
|
mPrimaryKeyCodes = new int[MAX_WORD_LENGTH];
|
2014-03-13 08:37:16 +00:00
|
|
|
final LastComposedWord lastComposedWord = new LastComposedWord(primaryKeyCodes, mEvents,
|
2012-08-27 11:09:46 +00:00
|
|
|
mInputPointers, mTypedWord.toString(), committedWord, separatorString,
|
2013-01-16 10:39:04 +00:00
|
|
|
prevWord, mCapitalizedMode);
|
2012-06-29 09:42:15 +00:00
|
|
|
mInputPointers.reset();
|
2012-02-22 08:05:19 +00:00
|
|
|
if (type != LastComposedWord.COMMIT_TYPE_DECIDED_WORD
|
|
|
|
&& type != LastComposedWord.COMMIT_TYPE_MANUAL_PICK) {
|
2012-01-26 10:05:59 +00:00
|
|
|
lastComposedWord.deactivate();
|
|
|
|
}
|
2012-07-13 04:31:27 +00:00
|
|
|
mCapsCount = 0;
|
2012-07-27 14:13:28 +00:00
|
|
|
mDigitsCount = 0;
|
2012-07-13 04:31:27 +00:00
|
|
|
mIsBatchMode = false;
|
2014-01-09 09:36:23 +00:00
|
|
|
mPreviousWordForSuggestion = committedWord.toString();
|
2012-01-26 08:29:57 +00:00
|
|
|
mTypedWord.setLength(0);
|
2014-03-13 08:37:16 +00:00
|
|
|
mEvents.clear();
|
2013-01-17 11:39:09 +00:00
|
|
|
mCodePointSize = 0;
|
2012-07-10 09:43:42 +00:00
|
|
|
mTrailingSingleQuotesCount = 0;
|
2012-07-13 04:31:27 +00:00
|
|
|
mIsFirstCharCapitalized = false;
|
2013-01-17 11:39:09 +00:00
|
|
|
mCapitalizedMode = CAPS_MODE_OFF;
|
2012-03-27 06:21:54 +00:00
|
|
|
refreshSize();
|
2012-01-26 08:29:57 +00:00
|
|
|
mAutoCorrection = null;
|
2013-04-10 07:38:37 +00:00
|
|
|
mCursorPositionWithinWord = 0;
|
2012-04-26 09:01:13 +00:00
|
|
|
mIsResumed = false;
|
2013-04-12 11:45:18 +00:00
|
|
|
mRejectedBatchModeSuggestion = null;
|
2012-01-26 07:05:09 +00:00
|
|
|
return lastComposedWord;
|
2011-12-13 14:12:22 +00:00
|
|
|
}
|
2013-12-13 11:42:29 +00:00
|
|
|
|
2013-12-16 12:41:03 +00:00
|
|
|
// Call this when the recorded previous word should be discarded. This is typically called
|
|
|
|
// when the user inputs a separator that's not whitespace (including the case of the
|
|
|
|
// double-space-to-period feature).
|
|
|
|
public void discardPreviousWordForSuggestion() {
|
|
|
|
mPreviousWordForSuggestion = null;
|
2013-12-13 11:42:29 +00:00
|
|
|
}
|
2011-12-13 14:12:22 +00:00
|
|
|
|
2013-12-13 08:09:16 +00:00
|
|
|
public void resumeSuggestionOnLastComposedWord(final LastComposedWord lastComposedWord,
|
|
|
|
final String previousWord) {
|
2012-03-27 06:21:54 +00:00
|
|
|
mPrimaryKeyCodes = lastComposedWord.mPrimaryKeyCodes;
|
2014-03-13 08:37:16 +00:00
|
|
|
mEvents.clear();
|
|
|
|
Collections.copy(mEvents, lastComposedWord.mEvents);
|
2012-06-29 09:42:15 +00:00
|
|
|
mInputPointers.set(lastComposedWord.mInputPointers);
|
2012-01-26 08:29:57 +00:00
|
|
|
mTypedWord.setLength(0);
|
|
|
|
mTypedWord.append(lastComposedWord.mTypedWord);
|
2012-03-27 06:21:54 +00:00
|
|
|
refreshSize();
|
2013-01-16 10:39:04 +00:00
|
|
|
mCapitalizedMode = lastComposedWord.mCapitalizedMode;
|
2012-02-22 07:11:07 +00:00
|
|
|
mAutoCorrection = null; // This will be filled by the next call to updateSuggestion.
|
2013-04-10 07:38:37 +00:00
|
|
|
mCursorPositionWithinWord = mCodePointSize;
|
2013-04-12 11:45:18 +00:00
|
|
|
mRejectedBatchModeSuggestion = null;
|
2012-04-26 09:01:13 +00:00
|
|
|
mIsResumed = true;
|
2013-12-16 12:41:03 +00:00
|
|
|
mPreviousWordForSuggestion = previousWord;
|
2011-12-14 11:13:16 +00:00
|
|
|
}
|
2012-07-10 01:46:13 +00:00
|
|
|
|
|
|
|
public boolean isBatchMode() {
|
|
|
|
return mIsBatchMode;
|
|
|
|
}
|
2013-04-12 11:45:18 +00:00
|
|
|
|
|
|
|
public void setRejectedBatchModeSuggestion(final String rejectedSuggestion) {
|
|
|
|
mRejectedBatchModeSuggestion = rejectedSuggestion;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getRejectedBatchModeSuggestion() {
|
|
|
|
return mRejectedBatchModeSuggestion;
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|