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