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