LastComposedWord.java revision 05c46f59fdc6065aad833d905fc9759ba907fc82
1/*
2 * Copyright (C) 2012 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 android.text.TextUtils;
20
21import com.android.inputmethod.event.Event;
22
23import java.util.ArrayList;
24
25/**
26 * This class encapsulates data about a word previously composed, but that has been
27 * committed already. This is used for resuming suggestion, and cancel auto-correction.
28 */
29public final class LastComposedWord {
30    // COMMIT_TYPE_USER_TYPED_WORD is used when the word committed is the exact typed word, with
31    // no hinting from the IME. It happens when some external event happens (rotating the device,
32    // for example) or when auto-correction is off by settings or editor attributes.
33    public static final int COMMIT_TYPE_USER_TYPED_WORD = 0;
34    // COMMIT_TYPE_MANUAL_PICK is used when the user pressed a field in the suggestion strip.
35    public static final int COMMIT_TYPE_MANUAL_PICK = 1;
36    // COMMIT_TYPE_DECIDED_WORD is used when the IME commits the word it decided was best
37    // for the current user input. It may be different from what the user typed (true auto-correct)
38    // or it may be exactly what the user typed if it's in the dictionary or the IME does not have
39    // enough confidence in any suggestion to auto-correct (auto-correct to typed word).
40    public static final int COMMIT_TYPE_DECIDED_WORD = 2;
41
42    public static final String NOT_A_SEPARATOR = "";
43
44    public final String mTypedWord;
45    public final CharSequence mCommittedWord;
46    public final String mSeparatorString;
47    public final String mPrevWord;
48    public final int mCapitalizedMode;
49
50    private boolean mActive;
51
52    public static final LastComposedWord NOT_A_COMPOSED_WORD =
53            new LastComposedWord("", "", NOT_A_SEPARATOR, null, WordComposer.CAPS_MODE_OFF);
54
55    // Warning: this is using the passed objects as is and fully expects them to be
56    // immutable. Do not fiddle with their contents after you passed them to this constructor.
57    public LastComposedWord(final String typedWord, final CharSequence committedWord,
58            final String separatorString, final String prevWord, final int capitalizedMode) {
59        mTypedWord = typedWord;
60        mCommittedWord = committedWord;
61        mSeparatorString = separatorString;
62        mActive = true;
63        mPrevWord = prevWord;
64        mCapitalizedMode = capitalizedMode;
65    }
66
67    public void deactivate() {
68        mActive = false;
69    }
70
71    public boolean canRevertCommit() {
72        return mActive && !TextUtils.isEmpty(mCommittedWord) && !didCommitTypedWord();
73    }
74
75    private boolean didCommitTypedWord() {
76        return TextUtils.equals(mTypedWord, mCommittedWord);
77    }
78}
79