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;
|
|
|
|
|
2015-02-04 20:50:31 +00:00
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
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;
|
2014-10-07 10:29:05 +00:00
|
|
|
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
|
2014-10-24 08:12:30 +00:00
|
|
|
import com.android.inputmethod.latin.common.ComposedData;
|
2014-10-23 09:37:32 +00:00
|
|
|
import com.android.inputmethod.latin.common.Constants;
|
2014-11-06 11:29:29 +00:00
|
|
|
import com.android.inputmethod.latin.common.CoordinateUtils;
|
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;
|
2015-02-05 21:27:36 +00:00
|
|
|
import com.android.inputmethod.latin.define.DecoderSpecificConstants;
|
2011-02-10 11:53:58 +00:00
|
|
|
|
2014-03-13 08:37:16 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2014-07-30 04:01:39 +00:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
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 {
|
2015-02-05 21:27:36 +00:00
|
|
|
private static final int MAX_WORD_LENGTH = DecoderSpecificConstants.DICTIONARY_MAX_WORD_LENGTH;
|
2014-07-17 01:41:46 +00:00
|
|
|
private static final boolean DBG = DebugFlags.DEBUG_ENABLED;
|
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;
|
2014-05-08 05:53:56 +00:00
|
|
|
private String mCombiningSpec; // Memory so that we don't uselessly recreate the combiner chain
|
2014-03-14 13:50:20 +00:00
|
|
|
|
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);
|
2014-10-07 10:29:05 +00:00
|
|
|
private SuggestedWordInfo 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
|
2014-03-25 12:59:53 +00:00
|
|
|
private CharSequence mTypedWordCache;
|
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;
|
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
|
|
|
/**
|
2014-06-09 10:31:41 +00:00
|
|
|
* Whether the composing word has the only first char capitalized.
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
2014-06-09 10:31:41 +00:00
|
|
|
private boolean mIsOnlyFirstCharCapitalized;
|
2009-03-13 22:11:42 +00:00
|
|
|
|
2010-08-20 05:35:02 +00:00
|
|
|
public WordComposer() {
|
2014-05-14 11:19:16 +00:00
|
|
|
mCombinerChain = new CombinerChain("");
|
2014-05-23 11:18:17 +00:00
|
|
|
mEvents = new ArrayList<>();
|
2012-01-26 08:29:57 +00:00
|
|
|
mAutoCorrection = null;
|
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;
|
2014-03-25 12:59:53 +00:00
|
|
|
refreshTypedWordCache();
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2014-10-24 08:12:30 +00:00
|
|
|
public ComposedData getComposedDataSnapshot() {
|
|
|
|
return new ComposedData(getInputPointers(), isBatchMode(), mTypedWordCache.toString());
|
|
|
|
}
|
|
|
|
|
2014-05-08 05:53:56 +00:00
|
|
|
/**
|
2014-05-14 11:19:16 +00:00
|
|
|
* Restart the combiners, possibly with a new spec.
|
2014-05-08 05:53:56 +00:00
|
|
|
* @param combiningSpec The spec string for combining. This is found in the extra value.
|
|
|
|
*/
|
2014-05-14 11:19:16 +00:00
|
|
|
public void restartCombining(final String combiningSpec) {
|
2014-05-08 05:53:56 +00:00
|
|
|
final String nonNullCombiningSpec = null == combiningSpec ? "" : combiningSpec;
|
2014-05-14 11:19:16 +00:00
|
|
|
if (!nonNullCombiningSpec.equals(mCombiningSpec)) {
|
|
|
|
mCombinerChain = new CombinerChain(
|
2015-01-29 18:17:33 +00:00
|
|
|
mCombinerChain.getComposingWordWithCombiningFeedback().toString());
|
2014-05-08 05:53:56 +00:00
|
|
|
mCombiningSpec = nonNullCombiningSpec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
|
|
|
* Clear out the keys registered so far.
|
|
|
|
*/
|
|
|
|
public void reset() {
|
2014-03-20 08:55:30 +00:00
|
|
|
mCombinerChain.reset();
|
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;
|
2014-06-09 10:31:41 +00:00
|
|
|
mIsOnlyFirstCharCapitalized = false;
|
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;
|
2014-03-25 12:59:53 +00:00
|
|
|
refreshTypedWordCache();
|
2012-03-27 06:21:54 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 12:59:53 +00:00
|
|
|
private final void refreshTypedWordCache() {
|
|
|
|
mTypedWordCache = mCombinerChain.getComposingWordWithCombiningFeedback();
|
|
|
|
mCodePointSize = Character.codePointCount(mTypedWordCache, 0, mTypedWordCache.length());
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of keystrokes in the composing word.
|
|
|
|
* @return the number of keystrokes
|
|
|
|
*/
|
2014-09-04 20:42:10 +00:00
|
|
|
public int size() {
|
2012-03-27 06:21:54 +00:00
|
|
|
return mCodePointSize;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
2014-04-07 14:41:29 +00:00
|
|
|
public boolean isSingleLetter() {
|
|
|
|
return size() == 1;
|
2014-03-28 04:25:04 +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-06-29 09:42:15 +00:00
|
|
|
public InputPointers getInputPointers() {
|
|
|
|
return mInputPointers;
|
2011-02-22 08:28:55 +00:00
|
|
|
}
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
/**
|
2014-06-30 14:22:28 +00:00
|
|
|
* Process an event and return an event, and return a processed event to apply.
|
|
|
|
* @param event the unprocessed event.
|
2014-07-01 04:07:47 +00:00
|
|
|
* @return the processed event. Never null, but may be marked as consumed.
|
2014-06-30 14:22:28 +00:00
|
|
|
*/
|
2014-07-30 04:01:39 +00:00
|
|
|
@Nonnull
|
2014-10-22 05:04:07 +00:00
|
|
|
public Event processEvent(@Nonnull final Event event) {
|
2014-06-30 14:22:28 +00:00
|
|
|
final Event processedEvent = mCombinerChain.processEvent(mEvents, event);
|
2014-08-01 05:15:33 +00:00
|
|
|
// The retained state of the combiner chain may have changed while processing the event,
|
|
|
|
// so we need to update our cache.
|
|
|
|
refreshTypedWordCache();
|
2014-06-30 14:22:28 +00:00
|
|
|
mEvents.add(event);
|
|
|
|
return processedEvent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply a processed input event.
|
2014-03-27 07:12:58 +00:00
|
|
|
*
|
|
|
|
* All input events should be supported, including software/hardware events, characters as well
|
|
|
|
* as deletions, multiple inputs and gestures.
|
|
|
|
*
|
2014-07-01 04:07:47 +00:00
|
|
|
* @param event the event to apply. Must not be null.
|
2009-03-13 22:11:42 +00:00
|
|
|
*/
|
2014-06-30 14:22:28 +00:00
|
|
|
public void applyProcessedEvent(final Event event) {
|
2014-07-01 04:07:47 +00:00
|
|
|
mCombinerChain.applyProcessedEvent(event);
|
2014-03-25 13:44:59 +00:00
|
|
|
final int primaryCode = event.mCodePoint;
|
|
|
|
final int keyX = event.mX;
|
|
|
|
final int keyY = event.mY;
|
|
|
|
final int newIndex = size();
|
2014-03-25 13:43:13 +00:00
|
|
|
refreshTypedWordCache();
|
2014-03-25 13:44:59 +00:00
|
|
|
mCursorPositionWithinWord = mCodePointSize;
|
|
|
|
// We may have deleted the last one.
|
|
|
|
if (0 == mCodePointSize) {
|
2014-06-09 10:31:41 +00:00
|
|
|
mIsOnlyFirstCharCapitalized = false;
|
2014-03-25 13:44:59 +00:00
|
|
|
}
|
2014-04-08 07:42:24 +00:00
|
|
|
if (Constants.CODE_DELETE != event.mKeyCode) {
|
2014-03-27 05:34:45 +00:00
|
|
|
if (newIndex < MAX_WORD_LENGTH) {
|
|
|
|
// 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
|
|
|
|
mInputPointers.addPointerAt(newIndex, keyX, keyY, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
2014-06-09 10:31:41 +00:00
|
|
|
if (0 == newIndex) {
|
|
|
|
mIsOnlyFirstCharCapitalized = Character.isUpperCase(primaryCode);
|
|
|
|
} else {
|
|
|
|
mIsOnlyFirstCharCapitalized = mIsOnlyFirstCharCapitalized
|
|
|
|
&& !Character.isUpperCase(primaryCode);
|
|
|
|
}
|
2014-03-27 05:34:45 +00:00
|
|
|
if (Character.isUpperCase(primaryCode)) mCapsCount++;
|
|
|
|
if (Character.isDigit(primaryCode)) mDigitsCount++;
|
2014-03-25 13:47:05 +00:00
|
|
|
}
|
2014-03-25 13:44:59 +00:00
|
|
|
mAutoCorrection = null;
|
2014-03-25 13:43:13 +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) {
|
2015-02-27 18:13:06 +00:00
|
|
|
int actualMoveAmount = 0;
|
2013-06-25 10:26:30 +00:00
|
|
|
int cursorPos = mCursorPositionWithinWord;
|
2014-04-08 07:42:24 +00:00
|
|
|
// TODO: Don't make that copy. We can do this directly from mTypedWordCache.
|
|
|
|
final int[] codePoints = StringUtils.toCodePointArray(mTypedWordCache);
|
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.
|
2015-02-27 18:13:06 +00:00
|
|
|
while (actualMoveAmount < expectedMoveAmount && cursorPos < codePoints.length) {
|
|
|
|
actualMoveAmount += 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.
|
2015-02-27 18:13:06 +00:00
|
|
|
while (actualMoveAmount > expectedMoveAmount && cursorPos > 0) {
|
2013-06-25 10:26:30 +00:00
|
|
|
--cursorPos;
|
2015-02-27 18:13:06 +00:00
|
|
|
actualMoveAmount -= 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.
|
2015-02-27 18:13:06 +00:00
|
|
|
if (actualMoveAmount != expectedMoveAmount) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-25 10:26:30 +00:00
|
|
|
mCursorPositionWithinWord = cursorPos;
|
2015-02-27 18:13:06 +00:00
|
|
|
mCombinerChain.applyProcessedEvent(mCombinerChain.processEvent(
|
|
|
|
mEvents, Event.createCursorMovedEvent(cursorPos)));
|
2013-06-25 10:26:30 +00:00
|
|
|
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-06-30 14:22:28 +00:00
|
|
|
final Event processedEvent =
|
|
|
|
processEvent(Event.createEventForCodePointFromUnknownSource(codePoint));
|
|
|
|
applyProcessedEvent(processedEvent);
|
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
|
2011-11-22 02:35:40 +00:00
|
|
|
*/
|
2014-06-09 10:31:41 +00:00
|
|
|
public void setComposingWord(final int[] codePoints, final int[] coordinates) {
|
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-06-30 14:22:28 +00:00
|
|
|
final Event processedEvent =
|
|
|
|
processEvent(Event.createEventForCodePointFromAlreadyTypedText(codePoints[i],
|
2014-03-13 08:37:16 +00:00
|
|
|
CoordinateUtils.xFromArray(coordinates, i),
|
|
|
|
CoordinateUtils.yFromArray(coordinates, i)));
|
2014-06-30 14:22:28 +00:00
|
|
|
applyProcessedEvent(processedEvent);
|
2011-11-22 02:35:40 +00:00
|
|
|
}
|
2012-04-26 09:01:13 +00:00
|
|
|
mIsResumed = true;
|
2011-11-22 02:35:40 +00:00
|
|
|
}
|
|
|
|
|
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() {
|
2014-03-25 12:59:53 +00:00
|
|
|
return mTypedWordCache.toString();
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-13 07:50:05 +00:00
|
|
|
* Whether this composer is composing or about to compose a word in which only the first letter
|
|
|
|
* is a capital.
|
|
|
|
*
|
|
|
|
* If we do have a composing word, we just return whether the word has indeed only its first
|
|
|
|
* character capitalized. If we don't, then we return a value based on the capitalized mode,
|
|
|
|
* which tell us what is likely to happen for the next composing word.
|
|
|
|
*
|
2009-03-13 22:11:42 +00:00
|
|
|
* @return capitalization preference
|
|
|
|
*/
|
2014-06-13 07:50:05 +00:00
|
|
|
public boolean isOrWillBeOnlyFirstCharCapitalized() {
|
|
|
|
return isComposingWord() ? mIsOnlyFirstCharCapitalized
|
|
|
|
: (CAPS_MODE_OFF != mCapitalizedMode);
|
2009-03-13 22:11:42 +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;
|
|
|
|
}
|
2014-10-20 05:48:56 +00:00
|
|
|
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
|
|
|
/**
|
2014-06-09 10:31:41 +00:00
|
|
|
* Saves the caps mode 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
|
2010-01-24 15:34:07 +00:00
|
|
|
*/
|
2014-06-09 10:31:41 +00:00
|
|
|
public void setCapitalizedModeAtStartComposingTime(final int mode) {
|
2012-08-21 10:49:25 +00:00
|
|
|
mCapitalizedMode = mode;
|
2010-01-24 15:34:07 +00:00
|
|
|
}
|
|
|
|
|
2014-06-13 07:50:05 +00:00
|
|
|
/**
|
|
|
|
* Before fetching suggestions, we don't necessarily know about the capitalized mode yet.
|
|
|
|
*
|
|
|
|
* If we don't have a composing word yet, we take a note of this mode so that we can then
|
|
|
|
* supply this information to the suggestion process. If we have a composing word, then
|
|
|
|
* the previous mode has priority over this.
|
|
|
|
* @param mode the mode just before fetching suggestions
|
|
|
|
*/
|
|
|
|
public void adviseCapitalizedModeBeforeFetchingSuggestions(final int mode) {
|
|
|
|
if (!isComposingWord()) {
|
|
|
|
mCapitalizedMode = mode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2014-10-07 10:29:05 +00:00
|
|
|
public void setAutoCorrection(final SuggestedWordInfo autoCorrection) {
|
|
|
|
mAutoCorrection = autoCorrection;
|
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
|
|
|
*/
|
2014-10-07 10:29:05 +00:00
|
|
|
public SuggestedWordInfo 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,
|
2014-09-29 01:52:18 +00:00
|
|
|
final String separatorString, final NgramContext ngramContext) {
|
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.
|
2014-04-08 07:42:24 +00:00
|
|
|
final LastComposedWord lastComposedWord = new LastComposedWord(mEvents,
|
2014-03-25 12:59:53 +00:00
|
|
|
mInputPointers, mTypedWordCache.toString(), committedWord, separatorString,
|
2014-09-29 01:52:18 +00:00
|
|
|
ngramContext, 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-03-20 08:55:30 +00:00
|
|
|
mCombinerChain.reset();
|
2014-03-13 08:37:16 +00:00
|
|
|
mEvents.clear();
|
2013-01-17 11:39:09 +00:00
|
|
|
mCodePointSize = 0;
|
2014-06-09 10:31:41 +00:00
|
|
|
mIsOnlyFirstCharCapitalized = false;
|
2013-01-17 11:39:09 +00:00
|
|
|
mCapitalizedMode = CAPS_MODE_OFF;
|
2014-03-25 12:59:53 +00:00
|
|
|
refreshTypedWordCache();
|
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
|
|
|
|
2014-06-09 10:31:41 +00:00
|
|
|
public void resumeSuggestionOnLastComposedWord(final LastComposedWord lastComposedWord) {
|
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);
|
2014-03-20 08:55:30 +00:00
|
|
|
mCombinerChain.reset();
|
2014-03-25 12:59:53 +00:00
|
|
|
refreshTypedWordCache();
|
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;
|
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;
|
|
|
|
}
|
2015-02-04 20:50:31 +00:00
|
|
|
|
|
|
|
@UsedForTesting
|
|
|
|
void addInputPointerForTest(int index, int keyX, int keyY) {
|
|
|
|
mInputPointers.addPointerAt(index, keyX, keyY, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@UsedForTesting
|
|
|
|
void setTypedWordCacheForTests(String typedWordCacheForTests) {
|
|
|
|
mTypedWordCache = typedWordCacheForTests;
|
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|