2009-03-13 22:11:42 +00:00
|
|
|
/*
|
2010-03-26 22:07:10 +00:00
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2009-03-13 22:11:42 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
|
|
* use this file except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations under
|
|
|
|
* the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
2011-07-12 01:29:29 +00:00
|
|
|
import com.android.inputmethod.keyboard.Keyboard;
|
2011-04-06 02:14:20 +00:00
|
|
|
import com.android.inputmethod.latin.Utils.RingCharBuffer;
|
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
public class TextEntryState {
|
2011-03-03 01:02:06 +00:00
|
|
|
private static final String TAG = TextEntryState.class.getSimpleName();
|
2011-03-08 20:11:47 +00:00
|
|
|
private static final boolean DEBUG = false;
|
2011-03-03 01:02:06 +00:00
|
|
|
|
|
|
|
private static final int UNKNOWN = 0;
|
|
|
|
private static final int START = 1;
|
|
|
|
private static final int IN_WORD = 2;
|
|
|
|
private static final int ACCEPTED_DEFAULT = 3;
|
|
|
|
private static final int PICKED_SUGGESTION = 4;
|
2011-10-25 08:12:51 +00:00
|
|
|
private static final int PUNCTUATION_AFTER_ACCEPTED = 5;
|
|
|
|
private static final int SPACE_AFTER_ACCEPTED = 6;
|
|
|
|
private static final int SPACE_AFTER_PICKED = 7;
|
|
|
|
private static final int UNDO_COMMIT = 8;
|
2011-03-03 01:02:06 +00:00
|
|
|
|
|
|
|
private static int sState = UNKNOWN;
|
|
|
|
private static int sPreviousState = UNKNOWN;
|
|
|
|
|
|
|
|
private static void setState(final int newState) {
|
|
|
|
sPreviousState = sState;
|
|
|
|
sState = newState;
|
2010-08-13 20:29:49 +00:00
|
|
|
}
|
|
|
|
|
2011-04-06 02:14:20 +00:00
|
|
|
public static void acceptedDefault(CharSequence typedWord, CharSequence actualWord,
|
|
|
|
int separatorCode) {
|
2010-01-30 04:09:49 +00:00
|
|
|
if (typedWord == null) return;
|
2011-03-03 01:02:06 +00:00
|
|
|
setState(ACCEPTED_DEFAULT);
|
2011-04-06 02:14:20 +00:00
|
|
|
LatinImeLogger.logOnAutoCorrection(
|
|
|
|
typedWord.toString(), actualWord.toString(), separatorCode);
|
2011-03-03 01:02:06 +00:00
|
|
|
if (DEBUG)
|
|
|
|
displayState("acceptedDefault", "typedWord", typedWord, "actualWord", actualWord);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-05-31 08:47:33 +00:00
|
|
|
|
2010-08-13 20:29:49 +00:00
|
|
|
// State.ACCEPTED_DEFAULT will be changed to other sub-states
|
|
|
|
// (see "case ACCEPTED_DEFAULT" in typedCharacter() below),
|
|
|
|
// and should be restored back to State.ACCEPTED_DEFAULT after processing for each sub-state.
|
2010-07-01 21:36:02 +00:00
|
|
|
public static void backToAcceptedDefault(CharSequence typedWord) {
|
|
|
|
if (typedWord == null) return;
|
2010-05-31 08:47:33 +00:00
|
|
|
switch (sState) {
|
2010-12-10 06:24:28 +00:00
|
|
|
case SPACE_AFTER_ACCEPTED:
|
|
|
|
case PUNCTUATION_AFTER_ACCEPTED:
|
|
|
|
case IN_WORD:
|
2011-03-03 01:02:06 +00:00
|
|
|
setState(ACCEPTED_DEFAULT);
|
2010-12-10 06:24:28 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2010-05-31 08:47:33 +00:00
|
|
|
}
|
2011-03-03 01:02:06 +00:00
|
|
|
if (DEBUG) displayState("backToAcceptedDefault", "typedWord", typedWord);
|
2010-05-31 08:47:33 +00:00
|
|
|
}
|
|
|
|
|
2011-03-03 01:02:06 +00:00
|
|
|
public static void acceptedTyped(CharSequence typedWord) {
|
|
|
|
setState(PICKED_SUGGESTION);
|
|
|
|
if (DEBUG) displayState("acceptedTyped", "typedWord", typedWord);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void acceptedSuggestion(CharSequence typedWord, CharSequence actualWord) {
|
2011-10-25 12:54:35 +00:00
|
|
|
setState(PICKED_SUGGESTION);
|
2011-03-03 01:02:06 +00:00
|
|
|
if (DEBUG)
|
|
|
|
displayState("acceptedSuggestion", "typedWord", typedWord, "actualWord", actualWord);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-02-27 15:53:56 +00:00
|
|
|
|
2011-04-06 02:14:20 +00:00
|
|
|
public static void typedCharacter(char c, boolean isSeparator, int x, int y) {
|
2011-07-12 01:29:29 +00:00
|
|
|
final boolean isSpace = (c == Keyboard.CODE_SPACE);
|
2009-03-13 22:11:42 +00:00
|
|
|
switch (sState) {
|
2011-03-03 01:02:06 +00:00
|
|
|
case IN_WORD:
|
|
|
|
if (isSpace || isSeparator) {
|
|
|
|
setState(START);
|
|
|
|
} else {
|
|
|
|
// State hasn't changed.
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ACCEPTED_DEFAULT:
|
|
|
|
case SPACE_AFTER_PICKED:
|
2011-03-03 05:14:53 +00:00
|
|
|
case PUNCTUATION_AFTER_ACCEPTED:
|
2011-03-03 01:02:06 +00:00
|
|
|
if (isSpace) {
|
|
|
|
setState(SPACE_AFTER_ACCEPTED);
|
|
|
|
} else if (isSeparator) {
|
|
|
|
// Swap
|
|
|
|
setState(PUNCTUATION_AFTER_ACCEPTED);
|
|
|
|
} else {
|
|
|
|
setState(IN_WORD);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PICKED_SUGGESTION:
|
|
|
|
if (isSpace) {
|
|
|
|
setState(SPACE_AFTER_PICKED);
|
|
|
|
} else if (isSeparator) {
|
|
|
|
// Swap
|
|
|
|
setState(PUNCTUATION_AFTER_ACCEPTED);
|
|
|
|
} else {
|
|
|
|
setState(IN_WORD);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case START:
|
|
|
|
case UNKNOWN:
|
|
|
|
case SPACE_AFTER_ACCEPTED:
|
|
|
|
if (!isSpace && !isSeparator) {
|
|
|
|
setState(IN_WORD);
|
|
|
|
} else {
|
|
|
|
setState(START);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case UNDO_COMMIT:
|
|
|
|
if (isSpace || isSeparator) {
|
2011-06-30 09:34:10 +00:00
|
|
|
setState(START);
|
2011-03-03 01:02:06 +00:00
|
|
|
} else {
|
|
|
|
setState(IN_WORD);
|
|
|
|
}
|
|
|
|
break;
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2011-04-06 02:14:20 +00:00
|
|
|
RingCharBuffer.getInstance().push(c, x, y);
|
|
|
|
if (isSeparator) {
|
|
|
|
LatinImeLogger.logOnInputSeparator();
|
|
|
|
} else {
|
|
|
|
LatinImeLogger.logOnInputChar();
|
|
|
|
}
|
2011-03-03 01:02:06 +00:00
|
|
|
if (DEBUG) displayState("typedCharacter", "char", c, "isSeparator", isSeparator);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void backspace() {
|
2011-03-03 01:02:06 +00:00
|
|
|
if (sState == ACCEPTED_DEFAULT) {
|
|
|
|
setState(UNDO_COMMIT);
|
2011-04-06 02:14:20 +00:00
|
|
|
LatinImeLogger.logOnAutoCorrectionCancelled();
|
2011-03-03 01:02:06 +00:00
|
|
|
} else if (sState == UNDO_COMMIT) {
|
|
|
|
setState(IN_WORD);
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2011-03-03 01:02:06 +00:00
|
|
|
if (DEBUG) displayState("backspace");
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-08-13 20:29:49 +00:00
|
|
|
|
2009-03-13 22:11:42 +00:00
|
|
|
public static void reset() {
|
2011-03-03 01:02:06 +00:00
|
|
|
setState(START);
|
|
|
|
if (DEBUG) displayState("reset");
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
2010-08-13 20:29:49 +00:00
|
|
|
|
2011-03-03 01:02:06 +00:00
|
|
|
public static boolean isUndoCommit() {
|
|
|
|
return sState == UNDO_COMMIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getState() {
|
|
|
|
return stateName(sState);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String stateName(int state) {
|
|
|
|
switch (state) {
|
|
|
|
case START: return "START";
|
|
|
|
case IN_WORD: return "IN_WORD";
|
|
|
|
case ACCEPTED_DEFAULT: return "ACCEPTED_DEFAULT";
|
|
|
|
case PICKED_SUGGESTION: return "PICKED_SUGGESTION";
|
|
|
|
case PUNCTUATION_AFTER_ACCEPTED: return "PUNCTUATION_AFTER_ACCEPTED";
|
|
|
|
case SPACE_AFTER_ACCEPTED: return "SPACE_AFTER_ACCEPTED";
|
|
|
|
case SPACE_AFTER_PICKED: return "SPACE_AFTER_PICKED";
|
|
|
|
case UNDO_COMMIT: return "UNDO_COMMIT";
|
|
|
|
default: return "UNKNOWN";
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-13 20:29:49 +00:00
|
|
|
|
2011-03-03 01:02:06 +00:00
|
|
|
private static void displayState(String title, Object ... args) {
|
|
|
|
final StringBuilder sb = new StringBuilder(title);
|
|
|
|
sb.append(':');
|
|
|
|
for (int i = 0; i < args.length; i += 2) {
|
|
|
|
sb.append(' ');
|
|
|
|
sb.append(args[i]);
|
|
|
|
sb.append('=');
|
|
|
|
sb.append(args[i+1].toString());
|
2010-08-13 20:29:49 +00:00
|
|
|
}
|
2011-03-03 01:02:06 +00:00
|
|
|
sb.append(" state=");
|
|
|
|
sb.append(stateName(sState));
|
|
|
|
sb.append(" previous=");
|
|
|
|
sb.append(stateName(sPreviousState));
|
|
|
|
Log.d(TAG, sb.toString());
|
2010-08-13 20:29:49 +00:00
|
|
|
}
|
2009-03-13 22:11:42 +00:00
|
|
|
}
|