LastComposedWord.java revision 193d23f40e1556074f323b7bd9695759f4798efe
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import android.text.TextUtils;
20
21import java.util.ArrayList;
22
23/**
24 * This class encapsulates data about a word previously composed, but that has been
25 * committed already. This is used for resuming suggestion, and cancel auto-correction.
26 */
27public class LastComposedWord {
28    // COMMIT_TYPE_USER_TYPED_WORD is used when the word committed is the exact typed word, with
29    // no hinting from the IME. It happens when some external event happens (rotating the device,
30    // for example) or when auto-correction is off by settings or editor attributes.
31    public static final int COMMIT_TYPE_USER_TYPED_WORD = 0;
32    // COMMIT_TYPE_MANUAL_PICK is used when the user pressed a field in the suggestion strip.
33    public static final int COMMIT_TYPE_MANUAL_PICK = 1;
34    // COMMIT_TYPE_DECIDED_WORD is used when the IME commits the word it decided was best
35    // for the current user input. It may be different from what the user typed (true auto-correct)
36    // or it may be exactly what the user typed if it's in the dictionary or the IME does not have
37    // enough confidence in any suggestion to auto-correct (auto-correct to typed word).
38    public static final int COMMIT_TYPE_DECIDED_WORD = 2;
39    // COMMIT_TYPE_CANCEL_AUTO_CORRECT is used upon committing back the old word upon cancelling
40    // an auto-correction.
41    public static final int COMMIT_TYPE_CANCEL_AUTO_CORRECT = 3;
42
43    public static final int NOT_A_SEPARATOR = -1;
44
45    public final ArrayList<int[]> mCodes;
46    public final int[] mXCoordinates;
47    public final int[] mYCoordinates;
48    public final String mTypedWord;
49    public final String mCommittedWord;
50    public final int mSeparatorCode;
51
52    private boolean mActive;
53
54    public static final LastComposedWord NOT_A_COMPOSED_WORD =
55            new LastComposedWord(null, null, null, "", "", NOT_A_SEPARATOR);
56
57    // Warning: this is using the passed objects as is and fully expects them to be
58    // immutable. Do not fiddle with their contents after you passed them to this constructor.
59    public LastComposedWord(final ArrayList<int[]> codes, final int[] xCoordinates,
60            final int[] yCoordinates, final String typedWord, final String committedWord,
61            final int separatorCode) {
62        mCodes = codes;
63        mXCoordinates = xCoordinates;
64        mYCoordinates = yCoordinates;
65        mTypedWord = typedWord;
66        mCommittedWord = committedWord;
67        mSeparatorCode = separatorCode;
68        mActive = true;
69    }
70
71    public void deactivate() {
72        mActive = false;
73    }
74
75    public boolean canCancelAutoCorrect() {
76        return mActive && !TextUtils.isEmpty(mCommittedWord)
77                && !TextUtils.equals(mTypedWord, mCommittedWord);
78    }
79
80    public boolean didCommitTypedWord() {
81        return TextUtils.equals(mTypedWord, mCommittedWord);
82    }
83
84    public static int getSeparatorLength(final int separatorCode) {
85        return NOT_A_SEPARATOR == separatorCode ? 0 : 1;
86    }
87}
88