Refactor RichInputConnection a bit

Change-Id: I866488a47ca04ca587e805663dfd597bb7b1ebce
main
Tadashi G. Takaoka 2014-01-17 13:14:29 +09:00
parent 2334bf6d8c
commit 6a18af634e
2 changed files with 32 additions and 32 deletions

View File

@ -27,7 +27,6 @@ import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.latin.settings.SettingsValues;
import com.android.inputmethod.latin.settings.SpacingAndPunctuations; import com.android.inputmethod.latin.settings.SpacingAndPunctuations;
import com.android.inputmethod.latin.utils.CapsModeUtils; import com.android.inputmethod.latin.utils.CapsModeUtils;
import com.android.inputmethod.latin.utils.DebugLogUtils; import com.android.inputmethod.latin.utils.DebugLogUtils;
@ -98,7 +97,7 @@ public final class RichInputConnection {
final ExtractedText et = mIC.getExtractedText(r, 0); final ExtractedText et = mIC.getExtractedText(r, 0);
final CharSequence beforeCursor = getTextBeforeCursor(Constants.EDITOR_CONTENTS_CACHE_SIZE, final CharSequence beforeCursor = getTextBeforeCursor(Constants.EDITOR_CONTENTS_CACHE_SIZE,
0); 0);
final StringBuilder internal = new StringBuilder().append(mCommittedTextBeforeComposingText) final StringBuilder internal = new StringBuilder(mCommittedTextBeforeComposingText)
.append(mComposingText); .append(mComposingText);
if (null == et || null == beforeCursor) return; if (null == et || null == beforeCursor) return;
final int actualLength = Math.min(beforeCursor.length(), internal.length()); final int actualLength = Math.min(beforeCursor.length(), internal.length());
@ -253,8 +252,7 @@ public final class RichInputConnection {
} }
public CharSequence getSelectedText(final int flags) { public CharSequence getSelectedText(final int flags) {
if (null == mIC) return null; return (null == mIC) ? null : mIC.getSelectedText(flags);
return mIC.getSelectedText(flags);
} }
public boolean canDeleteCharacters() { public boolean canDeleteCharacters() {
@ -272,12 +270,12 @@ public final class RichInputConnection {
* American English, it's just the most common set of rules for English). * American English, it's just the most common set of rules for English).
* *
* @param inputType a mask of the caps modes to test for. * @param inputType a mask of the caps modes to test for.
* @param settingsValues the values of the settings to use for locale and separators. * @param spacingAndPunctuations the values of the settings to use for locale and separators.
* @param hasSpaceBefore if we should consider there should be a space after the string. * @param hasSpaceBefore if we should consider there should be a space after the string.
* @return the caps modes that should be on as a set of bits * @return the caps modes that should be on as a set of bits
*/ */
public int getCursorCapsMode(final int inputType, final SettingsValues settingsValues, public int getCursorCapsMode(final int inputType,
final boolean hasSpaceBefore) { final SpacingAndPunctuations spacingAndPunctuations, final boolean hasSpaceBefore) {
mIC = mParent.getCurrentInputConnection(); mIC = mParent.getCurrentInputConnection();
if (null == mIC) return Constants.TextUtils.CAP_MODE_OFF; if (null == mIC) return Constants.TextUtils.CAP_MODE_OFF;
if (!TextUtils.isEmpty(mComposingText)) { if (!TextUtils.isEmpty(mComposingText)) {
@ -304,13 +302,13 @@ public final class RichInputConnection {
// This never calls InputConnection#getCapsMode - in fact, it's a static method that // This never calls InputConnection#getCapsMode - in fact, it's a static method that
// never blocks or initiates IPC. // never blocks or initiates IPC.
return CapsModeUtils.getCapsMode(mCommittedTextBeforeComposingText, inputType, return CapsModeUtils.getCapsMode(mCommittedTextBeforeComposingText, inputType,
settingsValues.mSpacingAndPunctuations, hasSpaceBefore); spacingAndPunctuations, hasSpaceBefore);
} }
public int getCodePointBeforeCursor() { public int getCodePointBeforeCursor() {
if (mCommittedTextBeforeComposingText.length() < 1) return Constants.NOT_A_CODE; final int length = mCommittedTextBeforeComposingText.length();
return Character.codePointBefore(mCommittedTextBeforeComposingText, if (length < 1) return Constants.NOT_A_CODE;
mCommittedTextBeforeComposingText.length()); return Character.codePointBefore(mCommittedTextBeforeComposingText, length);
} }
public CharSequence getTextBeforeCursor(final int n, final int flags) { public CharSequence getTextBeforeCursor(final int n, final int flags) {
@ -338,16 +336,12 @@ public final class RichInputConnection {
return s; return s;
} }
mIC = mParent.getCurrentInputConnection(); mIC = mParent.getCurrentInputConnection();
if (null != mIC) { return (null == mIC) ? null : mIC.getTextBeforeCursor(n, flags);
return mIC.getTextBeforeCursor(n, flags);
}
return null;
} }
public CharSequence getTextAfterCursor(final int n, final int flags) { public CharSequence getTextAfterCursor(final int n, final int flags) {
mIC = mParent.getCurrentInputConnection(); mIC = mParent.getCurrentInputConnection();
if (null != mIC) return mIC.getTextAfterCursor(n, flags); return (null == mIC) ? null : mIC.getTextAfterCursor(n, flags);
return null;
} }
public void deleteSurroundingText(final int beforeLength, final int afterLength) { public void deleteSurroundingText(final int beforeLength, final int afterLength) {
@ -681,24 +675,29 @@ public final class RichInputConnection {
startIndexInBefore, before.length() + endIndexInAfter, before.length()); startIndexInBefore, before.length() + endIndexInAfter, before.length());
} }
public boolean isCursorTouchingWord(final SettingsValues settingsValues) { public boolean isCursorTouchingWord(final SpacingAndPunctuations spacingAndPunctuations) {
final int codePointBeforeCursor = getCodePointBeforeCursor(); final int codePointBeforeCursor = getCodePointBeforeCursor();
if (Constants.NOT_A_CODE != codePointBeforeCursor if (Constants.NOT_A_CODE == codePointBeforeCursor
&& !settingsValues.isWordSeparator(codePointBeforeCursor) || spacingAndPunctuations.isWordSeparator(codePointBeforeCursor)
&& !settingsValues.isWordConnector(codePointBeforeCursor)) { || spacingAndPunctuations.isWordConnector(codePointBeforeCursor)) {
return true; return isCursorFollowedByWordCharacter(spacingAndPunctuations);
} }
return isCursorFollowedByWordCharacter(settingsValues); return true;
} }
public boolean isCursorFollowedByWordCharacter(final SettingsValues settingsValues) { public boolean isCursorFollowedByWordCharacter(
final SpacingAndPunctuations spacingAndPunctuations) {
final CharSequence after = getTextAfterCursor(1, 0); final CharSequence after = getTextAfterCursor(1, 0);
if (!TextUtils.isEmpty(after) && !settingsValues.isWordSeparator(after.charAt(0)) if (TextUtils.isEmpty(after)) {
&& !settingsValues.isWordConnector(after.charAt(0))) {
return true;
}
return false; return false;
} }
final int codePointAfterCursor = Character.codePointAt(after, 0);
if (spacingAndPunctuations.isWordSeparator(codePointAfterCursor)
|| spacingAndPunctuations.isWordConnector(codePointAfterCursor)) {
return false;
}
return true;
}
public void removeTrailingSpace() { public void removeTrailingSpace() {
if (DEBUG_BATCH_NESTING) checkBatchEdit(); if (DEBUG_BATCH_NESTING) checkBatchEdit();

View File

@ -530,7 +530,7 @@ public final class InputLogic {
&& settingsValues.isSuggestionsRequested() && && settingsValues.isSuggestionsRequested() &&
// In languages with spaces, we only start composing a word when we are not already // In languages with spaces, we only start composing a word when we are not already
// touching a word. In languages without spaces, the above conditions are sufficient. // touching a word. In languages without spaces, the above conditions are sufficient.
(!mConnection.isCursorTouchingWord(settingsValues) (!mConnection.isCursorTouchingWord(settingsValues.mSpacingAndPunctuations)
|| !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces)) { || !settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces)) {
// 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 or a dash. The idea here is, single quote and dash // the character is a single quote or a dash. The idea here is, single quote and dash
@ -816,7 +816,8 @@ public final class InputLogic {
} }
if (settingsValues.isSuggestionStripVisible() if (settingsValues.isSuggestionStripVisible()
&& settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces && settingsValues.mSpacingAndPunctuations.mCurrentLanguageHasSpaces
&& !mConnection.isCursorFollowedByWordCharacter(settingsValues)) { && !mConnection.isCursorFollowedByWordCharacter(
settingsValues.mSpacingAndPunctuations)) {
restartSuggestionsOnWordTouchedByCursor(settingsValues, restartSuggestionsOnWordTouchedByCursor(settingsValues,
true /* includeResumedWordInSuggestions */, keyboardSwitcher); true /* includeResumedWordInSuggestions */, keyboardSwitcher);
} }
@ -1069,7 +1070,7 @@ public final class InputLogic {
// If we don't know the cursor location, return. // If we don't know the cursor location, return.
if (mConnection.getExpectedSelectionStart() < 0) return; if (mConnection.getExpectedSelectionStart() < 0) return;
final int expectedCursorPosition = mConnection.getExpectedSelectionStart(); final int expectedCursorPosition = mConnection.getExpectedSelectionStart();
if (!mConnection.isCursorTouchingWord(settingsValues)) return; if (!mConnection.isCursorTouchingWord(settingsValues.mSpacingAndPunctuations)) return;
final TextRange range = mConnection.getWordRangeAtCursor( final TextRange range = mConnection.getWordRangeAtCursor(
settingsValues.mSpacingAndPunctuations.mWordSeparators, settingsValues.mSpacingAndPunctuations.mWordSeparators,
0 /* additionalPrecedingWordsCount */); 0 /* additionalPrecedingWordsCount */);
@ -1290,7 +1291,7 @@ public final class InputLogic {
final int inputType = ei.inputType; final int inputType = ei.inputType;
// Warning: this depends on mSpaceState, which may not be the most current value. If // Warning: this depends on mSpaceState, which may not be the most current value. If
// mSpaceState gets updated later, whoever called this may need to be told about it. // mSpaceState gets updated later, whoever called this may need to be told about it.
return mConnection.getCursorCapsMode(inputType, settingsValues, return mConnection.getCursorCapsMode(inputType, settingsValues.mSpacingAndPunctuations,
SpaceState.PHANTOM == mSpaceState); SpaceState.PHANTOM == mSpaceState);
} }