WordComposer.java revision be99616afa2243fe48dc406d0a3f442cb05453b4
1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.inputmethod.latin; 18 19import com.android.inputmethod.event.CombinerChain; 20import com.android.inputmethod.event.Event; 21import com.android.inputmethod.latin.utils.CollectionUtils; 22import com.android.inputmethod.latin.utils.CoordinateUtils; 23import com.android.inputmethod.latin.utils.StringUtils; 24 25import java.util.ArrayList; 26import java.util.Collections; 27 28/** 29 * A place to store the currently composing word with information such as adjacent key codes as well 30 */ 31public final class WordComposer { 32 private static final int MAX_WORD_LENGTH = Constants.DICTIONARY_MAX_WORD_LENGTH; 33 private static final boolean DBG = LatinImeLogger.sDBG; 34 35 public static final int CAPS_MODE_OFF = 0; 36 // 1 is shift bit, 2 is caps bit, 4 is auto bit but this is just a convention as these bits 37 // aren't used anywhere in the code 38 public static final int CAPS_MODE_MANUAL_SHIFTED = 0x1; 39 public static final int CAPS_MODE_MANUAL_SHIFT_LOCKED = 0x3; 40 public static final int CAPS_MODE_AUTO_SHIFTED = 0x5; 41 public static final int CAPS_MODE_AUTO_SHIFT_LOCKED = 0x7; 42 43 private CombinerChain mCombinerChain; 44 private String mCombiningSpec; // Memory so that we don't uselessly recreate the combiner chain 45 46 // The list of events that served to compose this string. 47 private final ArrayList<Event> mEvents; 48 private final InputPointers mInputPointers = new InputPointers(MAX_WORD_LENGTH); 49 // The previous word (before the composing word). Used as context for suggestions. May be null 50 // after resetting and before starting a new composing word, or when there is no context like 51 // at the start of text for example. It can also be set to null externally when the user 52 // enters a separator that does not let bigrams across, like a period or a comma. 53 private String mPreviousWordForSuggestion; 54 private String mAutoCorrection; 55 private boolean mIsResumed; 56 private boolean mIsBatchMode; 57 // A memory of the last rejected batch mode suggestion, if any. This goes like this: the user 58 // gestures a word, is displeased with the results and hits backspace, then gestures again. 59 // At the very least we should avoid re-suggesting the same thing, and to do that we memorize 60 // the rejected suggestion in this variable. 61 // TODO: this should be done in a comprehensive way by the User History feature instead of 62 // as an ad-hockery here. 63 private String mRejectedBatchModeSuggestion; 64 65 // Cache these values for performance 66 private CharSequence mTypedWordCache; 67 private int mCapsCount; 68 private int mDigitsCount; 69 private int mCapitalizedMode; 70 // This is the number of code points entered so far. This is not limited to MAX_WORD_LENGTH. 71 // In general, this contains the size of mPrimaryKeyCodes, except when this is greater than 72 // MAX_WORD_LENGTH in which case mPrimaryKeyCodes only contain the first MAX_WORD_LENGTH 73 // code points. 74 private int mCodePointSize; 75 private int mCursorPositionWithinWord; 76 77 /** 78 * Whether the user chose to capitalize the first char of the word. 79 */ 80 private boolean mIsFirstCharCapitalized; 81 82 public WordComposer() { 83 mCombinerChain = new CombinerChain(); 84 mEvents = CollectionUtils.newArrayList(); 85 mAutoCorrection = null; 86 mIsResumed = false; 87 mIsBatchMode = false; 88 mCursorPositionWithinWord = 0; 89 mRejectedBatchModeSuggestion = null; 90 mPreviousWordForSuggestion = null; 91 refreshTypedWordCache(); 92 } 93 94 /** 95 * Restart input with a new combining spec. 96 * @param combiningSpec The spec string for combining. This is found in the extra value. 97 */ 98 public void restart(final String combiningSpec) { 99 final String nonNullCombiningSpec = null == combiningSpec ? "" : combiningSpec; 100 if (nonNullCombiningSpec.equals(mCombiningSpec)) { 101 mCombinerChain.reset(); 102 } else { 103 mCombinerChain = new CombinerChain(CombinerChain.createCombiners(nonNullCombiningSpec)); 104 mCombiningSpec = nonNullCombiningSpec; 105 } 106 reset(); 107 } 108 109 /** 110 * Clear out the keys registered so far. 111 */ 112 public void reset() { 113 mCombinerChain.reset(); 114 mEvents.clear(); 115 mAutoCorrection = null; 116 mCapsCount = 0; 117 mDigitsCount = 0; 118 mIsFirstCharCapitalized = false; 119 mIsResumed = false; 120 mIsBatchMode = false; 121 mCursorPositionWithinWord = 0; 122 mRejectedBatchModeSuggestion = null; 123 mPreviousWordForSuggestion = null; 124 refreshTypedWordCache(); 125 } 126 127 private final void refreshTypedWordCache() { 128 mTypedWordCache = mCombinerChain.getComposingWordWithCombiningFeedback(); 129 mCodePointSize = Character.codePointCount(mTypedWordCache, 0, mTypedWordCache.length()); 130 } 131 132 /** 133 * Number of keystrokes in the composing word. 134 * @return the number of keystrokes 135 */ 136 // This may be made public if need be, but right now it's not used anywhere 137 /* package for tests */ int size() { 138 return mCodePointSize; 139 } 140 141 /** 142 * Copy the code points in the typed word to a destination array of ints. 143 * 144 * If the array is too small to hold the code points in the typed word, nothing is copied and 145 * -1 is returned. 146 * 147 * @param destination the array of ints. 148 * @return the number of copied code points. 149 */ 150 public int copyCodePointsExceptTrailingSingleQuotesAndReturnCodePointCount( 151 final int[] destination) { 152 // lastIndex is exclusive 153 final int lastIndex = mTypedWordCache.length() - trailingSingleQuotesCount(); 154 if (lastIndex <= 0) { 155 // The string is empty or contains only single quotes. 156 return 0; 157 } 158 159 // The following function counts the number of code points in the text range which begins 160 // at index 0 and extends to the character at lastIndex. 161 final int codePointSize = Character.codePointCount(mTypedWordCache, 0, lastIndex); 162 if (codePointSize > destination.length) { 163 return -1; 164 } 165 return StringUtils.copyCodePointsAndReturnCodePointCount(destination, mTypedWordCache, 0, 166 lastIndex, true /* downCase */); 167 } 168 169 public boolean isSingleLetter() { 170 return size() == 1; 171 } 172 173 public final boolean isComposingWord() { 174 return size() > 0; 175 } 176 177 public InputPointers getInputPointers() { 178 return mInputPointers; 179 } 180 181 private static boolean isFirstCharCapitalized(final int index, final int codePoint, 182 final boolean previous) { 183 if (index == 0) return Character.isUpperCase(codePoint); 184 return previous && !Character.isUpperCase(codePoint); 185 } 186 187 /** 188 * Process an input event. 189 * 190 * All input events should be supported, including software/hardware events, characters as well 191 * as deletions, multiple inputs and gestures. 192 * 193 * @param event the event to process. 194 */ 195 public void processEvent(final Event event) { 196 final int primaryCode = event.mCodePoint; 197 final int keyX = event.mX; 198 final int keyY = event.mY; 199 final int newIndex = size(); 200 mCombinerChain.processEvent(mEvents, event); 201 mEvents.add(event); 202 refreshTypedWordCache(); 203 mCursorPositionWithinWord = mCodePointSize; 204 // We may have deleted the last one. 205 if (0 == mCodePointSize) { 206 mIsFirstCharCapitalized = false; 207 } 208 if (Constants.CODE_DELETE != event.mKeyCode) { 209 if (newIndex < MAX_WORD_LENGTH) { 210 // In the batch input mode, the {@code mInputPointers} holds batch input points and 211 // shouldn't be overridden by the "typed key" coordinates 212 // (See {@link #setBatchInputWord}). 213 if (!mIsBatchMode) { 214 // TODO: Set correct pointer id and time 215 mInputPointers.addPointerAt(newIndex, keyX, keyY, 0, 0); 216 } 217 } 218 mIsFirstCharCapitalized = isFirstCharCapitalized( 219 newIndex, primaryCode, mIsFirstCharCapitalized); 220 if (Character.isUpperCase(primaryCode)) mCapsCount++; 221 if (Character.isDigit(primaryCode)) mDigitsCount++; 222 } 223 mAutoCorrection = null; 224 } 225 226 public void setCursorPositionWithinWord(final int posWithinWord) { 227 mCursorPositionWithinWord = posWithinWord; 228 // TODO: compute where that puts us inside the events 229 } 230 231 public boolean isCursorFrontOrMiddleOfComposingWord() { 232 if (DBG && mCursorPositionWithinWord > mCodePointSize) { 233 throw new RuntimeException("Wrong cursor position : " + mCursorPositionWithinWord 234 + "in a word of size " + mCodePointSize); 235 } 236 return mCursorPositionWithinWord != mCodePointSize; 237 } 238 239 /** 240 * When the cursor is moved by the user, we need to update its position. 241 * If it falls inside the currently composing word, we don't reset the composition, and 242 * only update the cursor position. 243 * 244 * @param expectedMoveAmount How many java chars to move the cursor. Negative values move 245 * the cursor backward, positive values move the cursor forward. 246 * @return true if the cursor is still inside the composing word, false otherwise. 247 */ 248 public boolean moveCursorByAndReturnIfInsideComposingWord(final int expectedMoveAmount) { 249 // TODO: should uncommit the composing feedback 250 mCombinerChain.reset(); 251 int actualMoveAmountWithinWord = 0; 252 int cursorPos = mCursorPositionWithinWord; 253 // TODO: Don't make that copy. We can do this directly from mTypedWordCache. 254 final int[] codePoints = StringUtils.toCodePointArray(mTypedWordCache); 255 if (expectedMoveAmount >= 0) { 256 // Moving the cursor forward for the expected amount or until the end of the word has 257 // been reached, whichever comes first. 258 while (actualMoveAmountWithinWord < expectedMoveAmount && cursorPos < mCodePointSize) { 259 actualMoveAmountWithinWord += Character.charCount(codePoints[cursorPos]); 260 ++cursorPos; 261 } 262 } else { 263 // Moving the cursor backward for the expected amount or until the start of the word 264 // has been reached, whichever comes first. 265 while (actualMoveAmountWithinWord > expectedMoveAmount && cursorPos > 0) { 266 --cursorPos; 267 actualMoveAmountWithinWord -= Character.charCount(codePoints[cursorPos]); 268 } 269 } 270 // If the actual and expected amounts differ, we crossed the start or the end of the word 271 // so the result would not be inside the composing word. 272 if (actualMoveAmountWithinWord != expectedMoveAmount) return false; 273 mCursorPositionWithinWord = cursorPos; 274 return true; 275 } 276 277 public void setBatchInputPointers(final InputPointers batchPointers) { 278 mInputPointers.set(batchPointers); 279 mIsBatchMode = true; 280 } 281 282 public void setBatchInputWord(final String word) { 283 reset(); 284 mIsBatchMode = true; 285 final int length = word.length(); 286 for (int i = 0; i < length; i = Character.offsetByCodePoints(word, i, 1)) { 287 final int codePoint = Character.codePointAt(word, i); 288 // We don't want to override the batch input points that are held in mInputPointers 289 // (See {@link #add(int,int,int)}). 290 processEvent(Event.createEventForCodePointFromUnknownSource(codePoint)); 291 } 292 } 293 294 /** 295 * Set the currently composing word to the one passed as an argument. 296 * This will register NOT_A_COORDINATE for X and Ys, and use the passed keyboard for proximity. 297 * @param codePoints the code points to set as the composing word. 298 * @param coordinates the x, y coordinates of the key in the CoordinateUtils format 299 * @param previousWord the previous word, to use as context for suggestions. Can be null if 300 * the context is nil (typically, at start of text). 301 */ 302 public void setComposingWord(final int[] codePoints, final int[] coordinates, 303 final CharSequence previousWord) { 304 reset(); 305 final int length = codePoints.length; 306 for (int i = 0; i < length; ++i) { 307 processEvent(Event.createEventForCodePointFromAlreadyTypedText(codePoints[i], 308 CoordinateUtils.xFromArray(coordinates, i), 309 CoordinateUtils.yFromArray(coordinates, i))); 310 } 311 mIsResumed = true; 312 mPreviousWordForSuggestion = null == previousWord ? null : previousWord.toString(); 313 } 314 315 /** 316 * Returns the word as it was typed, without any correction applied. 317 * @return the word that was typed so far. Never returns null. 318 */ 319 public String getTypedWord() { 320 return mTypedWordCache.toString(); 321 } 322 323 public String getPreviousWordForSuggestion() { 324 return mPreviousWordForSuggestion; 325 } 326 327 /** 328 * Whether or not the user typed a capital letter as the first letter in the word 329 * @return capitalization preference 330 */ 331 public boolean isFirstCharCapitalized() { 332 return mIsFirstCharCapitalized; 333 } 334 335 public int trailingSingleQuotesCount() { 336 final int lastIndex = mTypedWordCache.length() - 1; 337 int i = lastIndex; 338 while (i >= 0 && mTypedWordCache.charAt(i) == Constants.CODE_SINGLE_QUOTE) { 339 --i; 340 } 341 return lastIndex - i; 342 } 343 344 /** 345 * Whether or not all of the user typed chars are upper case 346 * @return true if all user typed chars are upper case, false otherwise 347 */ 348 public boolean isAllUpperCase() { 349 if (size() <= 1) { 350 return mCapitalizedMode == CAPS_MODE_AUTO_SHIFT_LOCKED 351 || mCapitalizedMode == CAPS_MODE_MANUAL_SHIFT_LOCKED; 352 } else { 353 return mCapsCount == size(); 354 } 355 } 356 357 public boolean wasShiftedNoLock() { 358 return mCapitalizedMode == CAPS_MODE_AUTO_SHIFTED 359 || mCapitalizedMode == CAPS_MODE_MANUAL_SHIFTED; 360 } 361 362 /** 363 * Returns true if more than one character is upper case, otherwise returns false. 364 */ 365 public boolean isMostlyCaps() { 366 return mCapsCount > 1; 367 } 368 369 /** 370 * Returns true if we have digits in the composing word. 371 */ 372 public boolean hasDigits() { 373 return mDigitsCount > 0; 374 } 375 376 /** 377 * Saves the caps mode and the previous word at the start of composing. 378 * 379 * WordComposer needs to know about the caps mode for several reasons. The first is, we need 380 * to know after the fact what the reason was, to register the correct form into the user 381 * history dictionary: if the word was automatically capitalized, we should insert it in 382 * all-lower case but if it's a manual pressing of shift, then it should be inserted as is. 383 * Also, batch input needs to know about the current caps mode to display correctly 384 * capitalized suggestions. 385 * @param mode the mode at the time of start 386 * @param previousWord the previous word as context for suggestions. May be null if none. 387 */ 388 public void setCapitalizedModeAndPreviousWordAtStartComposingTime(final int mode, 389 final CharSequence previousWord) { 390 mCapitalizedMode = mode; 391 mPreviousWordForSuggestion = null == previousWord ? null : previousWord.toString(); 392 } 393 394 /** 395 * Returns whether the word was automatically capitalized. 396 * @return whether the word was automatically capitalized 397 */ 398 public boolean wasAutoCapitalized() { 399 return mCapitalizedMode == CAPS_MODE_AUTO_SHIFT_LOCKED 400 || mCapitalizedMode == CAPS_MODE_AUTO_SHIFTED; 401 } 402 403 /** 404 * Sets the auto-correction for this word. 405 */ 406 public void setAutoCorrection(final String correction) { 407 mAutoCorrection = correction; 408 } 409 410 /** 411 * @return the auto-correction for this word, or null if none. 412 */ 413 public String getAutoCorrectionOrNull() { 414 return mAutoCorrection; 415 } 416 417 /** 418 * @return whether we started composing this word by resuming suggestion on an existing string 419 */ 420 public boolean isResumed() { 421 return mIsResumed; 422 } 423 424 // `type' should be one of the LastComposedWord.COMMIT_TYPE_* constants above. 425 // committedWord should contain suggestion spans if applicable. 426 public LastComposedWord commitWord(final int type, final CharSequence committedWord, 427 final String separatorString, final String prevWord) { 428 // Note: currently, we come here whenever we commit a word. If it's a MANUAL_PICK 429 // or a DECIDED_WORD we may cancel the commit later; otherwise, we should deactivate 430 // the last composed word to ensure this does not happen. 431 final LastComposedWord lastComposedWord = new LastComposedWord(mEvents, 432 mInputPointers, mTypedWordCache.toString(), committedWord, separatorString, 433 prevWord, mCapitalizedMode); 434 mInputPointers.reset(); 435 if (type != LastComposedWord.COMMIT_TYPE_DECIDED_WORD 436 && type != LastComposedWord.COMMIT_TYPE_MANUAL_PICK) { 437 lastComposedWord.deactivate(); 438 } 439 mCapsCount = 0; 440 mDigitsCount = 0; 441 mIsBatchMode = false; 442 mPreviousWordForSuggestion = committedWord.toString(); 443 mCombinerChain.reset(); 444 mEvents.clear(); 445 mCodePointSize = 0; 446 mIsFirstCharCapitalized = false; 447 mCapitalizedMode = CAPS_MODE_OFF; 448 refreshTypedWordCache(); 449 mAutoCorrection = null; 450 mCursorPositionWithinWord = 0; 451 mIsResumed = false; 452 mRejectedBatchModeSuggestion = null; 453 return lastComposedWord; 454 } 455 456 // Call this when the recorded previous word should be discarded. This is typically called 457 // when the user inputs a separator that's not whitespace (including the case of the 458 // double-space-to-period feature). 459 public void discardPreviousWordForSuggestion() { 460 mPreviousWordForSuggestion = null; 461 } 462 463 public void resumeSuggestionOnLastComposedWord(final LastComposedWord lastComposedWord, 464 final String previousWord) { 465 mEvents.clear(); 466 Collections.copy(mEvents, lastComposedWord.mEvents); 467 mInputPointers.set(lastComposedWord.mInputPointers); 468 mCombinerChain.reset(); 469 refreshTypedWordCache(); 470 mCapitalizedMode = lastComposedWord.mCapitalizedMode; 471 mAutoCorrection = null; // This will be filled by the next call to updateSuggestion. 472 mCursorPositionWithinWord = mCodePointSize; 473 mRejectedBatchModeSuggestion = null; 474 mIsResumed = true; 475 mPreviousWordForSuggestion = previousWord; 476 } 477 478 public boolean isBatchMode() { 479 return mIsBatchMode; 480 } 481 482 public void setRejectedBatchModeSuggestion(final String rejectedSuggestion) { 483 mRejectedBatchModeSuggestion = rejectedSuggestion; 484 } 485 486 public String getRejectedBatchModeSuggestion() { 487 return mRejectedBatchModeSuggestion; 488 } 489} 490