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