Move some editing methods to RichInputConnection.

Change-Id: I8527776a4bd8236a85bb77f9791f9b76977ad181
main
Jean Chalard 2012-06-08 20:23:13 +09:00
parent e091982868
commit bbbdab12be
2 changed files with 39 additions and 35 deletions

View File

@ -1076,7 +1076,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
return mConnection.getCursorCapsMode(inputType); return mConnection.getCursorCapsMode(inputType);
} }
private void swapSwapperAndSpaceWhileInBatchEdit() { private void swapSwapperAndSpace() {
CharSequence lastTwo = mConnection.getTextBeforeCursor(2, 0); CharSequence lastTwo = mConnection.getTextBeforeCursor(2, 0);
// It is guaranteed lastTwo.charAt(1) is a swapper - else this method is not called. // It is guaranteed lastTwo.charAt(1) is a swapper - else this method is not called.
if (lastTwo != null && lastTwo.length() == 2 if (lastTwo != null && lastTwo.length() == 2
@ -1093,7 +1093,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} }
} }
private boolean maybeDoubleSpaceWhileInBatchEdit() { private boolean maybeDoubleSpace() {
if (mCorrectionMode == Suggest.CORRECTION_NONE) return false; if (mCorrectionMode == Suggest.CORRECTION_NONE) return false;
final CharSequence lastThree = mConnection.getTextBeforeCursor(3, 0); final CharSequence lastThree = mConnection.getTextBeforeCursor(3, 0);
if (lastThree != null && lastThree.length() == 3 if (lastThree != null && lastThree.length() == 3
@ -1125,17 +1125,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
|| codePoint == Keyboard.CODE_CLOSING_ANGLE_BRACKET; || codePoint == Keyboard.CODE_CLOSING_ANGLE_BRACKET;
} }
private void removeTrailingSpace() {
final CharSequence lastOne = mConnection.getTextBeforeCursor(1, 0);
if (lastOne != null && lastOne.length() == 1
&& lastOne.charAt(0) == Keyboard.CODE_SPACE) {
mConnection.deleteSurroundingText(1, 0);
if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.latinIME_deleteSurroundingText(1);
}
}
}
@Override @Override
public boolean addWordToDictionary(String word) { public boolean addWordToDictionary(String word) {
mUserDictionary.addWordToUserDictionary(word, 128); mUserDictionary.addWordToUserDictionary(word, 128);
@ -1487,7 +1476,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
private boolean maybeStripSpace(final int code, private boolean maybeStripSpace(final int code,
final int spaceState, final boolean isFromSuggestionStrip) { final int spaceState, final boolean isFromSuggestionStrip) {
if (Keyboard.CODE_ENTER == code && SPACE_STATE_SWAP_PUNCTUATION == spaceState) { if (Keyboard.CODE_ENTER == code && SPACE_STATE_SWAP_PUNCTUATION == spaceState) {
removeTrailingSpace(); mConnection.removeTrailingSpace();
return false; return false;
} else if ((SPACE_STATE_WEAK == spaceState } else if ((SPACE_STATE_WEAK == spaceState
|| SPACE_STATE_SWAP_PUNCTUATION == spaceState) || SPACE_STATE_SWAP_PUNCTUATION == spaceState)
@ -1496,7 +1485,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
return true; return true;
} else { } else {
if (mSettingsValues.isWeakSpaceStripper(code)) { if (mSettingsValues.isWeakSpaceStripper(code)) {
removeTrailingSpace(); mConnection.removeTrailingSpace();
} }
return false; return false;
} }
@ -1523,7 +1512,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// thread here. // thread here.
if (!isComposingWord && (isAlphabet(primaryCode) if (!isComposingWord && (isAlphabet(primaryCode)
|| mSettingsValues.isSymbolExcludedFromWordSeparators(primaryCode)) || mSettingsValues.isSymbolExcludedFromWordSeparators(primaryCode))
&& isSuggestionsRequested() && !isCursorTouchingWord()) { && isSuggestionsRequested() && !mConnection.isCursorTouchingWord(mSettingsValues)) {
// Reset entirely the composing state anyway, then start composing a new word unless // Reset entirely the composing state anyway, then start composing a new word unless
// the character is a single quote. The idea here is, single quote is not a // the character is a single quote. The idea here is, single quote is not a
// separator and it should be treated as a normal character, except in the first // separator and it should be treated as a normal character, except in the first
@ -1553,7 +1542,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
sendKeyCodePoint(primaryCode); sendKeyCodePoint(primaryCode);
if (swapWeakSpace) { if (swapWeakSpace) {
swapSwapperAndSpaceWhileInBatchEdit(); swapSwapperAndSpace();
mSpaceState = SPACE_STATE_WEAK; mSpaceState = SPACE_STATE_WEAK;
} }
// Some characters are not word separators, yet they don't start a new // Some characters are not word separators, yet they don't start a new
@ -1606,7 +1595,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (Keyboard.CODE_SPACE == primaryCode) { if (Keyboard.CODE_SPACE == primaryCode) {
if (isSuggestionsRequested()) { if (isSuggestionsRequested()) {
if (maybeDoubleSpaceWhileInBatchEdit()) { if (maybeDoubleSpace()) {
mSpaceState = SPACE_STATE_DOUBLE; mSpaceState = SPACE_STATE_DOUBLE;
} else if (!isShowingPunctuationList()) { } else if (!isShowingPunctuationList()) {
mSpaceState = SPACE_STATE_WEAK; mSpaceState = SPACE_STATE_WEAK;
@ -1614,13 +1603,13 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} }
mHandler.startDoubleSpacesTimer(); mHandler.startDoubleSpacesTimer();
if (!isCursorTouchingWord()) { if (!mConnection.isCursorTouchingWord(mSettingsValues)) {
mHandler.cancelUpdateSuggestions(); mHandler.cancelUpdateSuggestions();
mHandler.postUpdateBigramPredictions(); mHandler.postUpdateBigramPredictions();
} }
} else { } else {
if (swapWeakSpace) { if (swapWeakSpace) {
swapSwapperAndSpaceWhileInBatchEdit(); swapSwapperAndSpace();
mSpaceState = SPACE_STATE_SWAP_PUNCTUATION; mSpaceState = SPACE_STATE_SWAP_PUNCTUATION;
} else if (SPACE_STATE_PHANTOM == spaceState) { } else if (SPACE_STATE_PHANTOM == spaceState) {
// If we are in phantom space state, and the user presses a separator, we want to // If we are in phantom space state, and the user presses a separator, we want to
@ -2033,20 +2022,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
return null; return null;
} }
public boolean isCursorTouchingWord() {
CharSequence before = mConnection.getTextBeforeCursor(1, 0);
CharSequence after = mConnection.getTextAfterCursor(1, 0);
if (!TextUtils.isEmpty(before) && !mSettingsValues.isWordSeparator(before.charAt(0))
&& !mSettingsValues.isSymbolExcludedFromWordSeparators(before.charAt(0))) {
return true;
}
if (!TextUtils.isEmpty(after) && !mSettingsValues.isWordSeparator(after.charAt(0))
&& !mSettingsValues.isSymbolExcludedFromWordSeparators(after.charAt(0))) {
return true;
}
return false;
}
private boolean sameAsTextBeforeCursor(final CharSequence text) { private boolean sameAsTextBeforeCursor(final CharSequence text) {
final CharSequence beforeText = mConnection.getTextBeforeCursor(text.length(), 0); final CharSequence beforeText = mConnection.getTextBeforeCursor(text.length(), 0);
return TextUtils.equals(text, beforeText); return TextUtils.equals(text, beforeText);
@ -2230,7 +2205,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
// Since we just changed languages, we should re-evaluate suggestions with whatever word // Since we just changed languages, we should re-evaluate suggestions with whatever word
// we are currently composing. If we are not composing anything, we may want to display // we are currently composing. If we are not composing anything, we may want to display
// predictions or punctuation signs (which is done by updateBigramPredictions anyway). // predictions or punctuation signs (which is done by updateBigramPredictions anyway).
if (isCursorTouchingWord()) { if (mConnection.isCursorTouchingWord(mSettingsValues)) {
mHandler.postUpdateSuggestions(); mHandler.postUpdateSuggestions();
} else { } else {
mHandler.postUpdateBigramPredictions(); mHandler.postUpdateBigramPredictions();

View File

@ -16,6 +16,7 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.CompletionInfo;
@ -24,6 +25,9 @@ import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest; import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.latin.define.ProductionFlag;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -297,4 +301,29 @@ public class RichInputConnection {
return null; return null;
} }
public boolean isCursorTouchingWord(final SettingsValues settingsValues) {
CharSequence before = getTextBeforeCursor(1, 0);
CharSequence after = getTextAfterCursor(1, 0);
if (!TextUtils.isEmpty(before) && !settingsValues.isWordSeparator(before.charAt(0))
&& !settingsValues.isSymbolExcludedFromWordSeparators(before.charAt(0))) {
return true;
}
if (!TextUtils.isEmpty(after) && !settingsValues.isWordSeparator(after.charAt(0))
&& !settingsValues.isSymbolExcludedFromWordSeparators(after.charAt(0))) {
return true;
}
return false;
}
public void removeTrailingSpace() {
final CharSequence lastOne = getTextBeforeCursor(1, 0);
if (lastOne != null && lastOne.length() == 1
&& lastOne.charAt(0) == Keyboard.CODE_SPACE) {
deleteSurroundingText(1, 0);
if (ProductionFlag.IS_EXPERIMENTAL) {
ResearchLogger.latinIME_deleteSurroundingText(1);
}
}
}
} }