Merge "Clean up RecapitalizeStatus"

main
Jean Chalard 2013-04-16 04:57:35 +00:00 committed by Android (Google) Code Review
commit e259b9f57d
3 changed files with 59 additions and 56 deletions

View File

@ -161,7 +161,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
mPositionalInfoForUserDictPendingAddition = null; mPositionalInfoForUserDictPendingAddition = null;
private final WordComposer mWordComposer = new WordComposer(); private final WordComposer mWordComposer = new WordComposer();
private final RichInputConnection mConnection = new RichInputConnection(this); private final RichInputConnection mConnection = new RichInputConnection(this);
private RecapitalizeStatus mRecapitalizeStatus = null; private final RecapitalizeStatus mRecapitalizeStatus = new RecapitalizeStatus();
// Keep track of the last selection range to decide if we need to show word alternatives // Keep track of the last selection range to decide if we need to show word alternatives
private static final int NOT_A_CURSOR_POSITION = -1; private static final int NOT_A_CURSOR_POSITION = -1;
@ -742,6 +742,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
resetComposingState(true /* alsoResetLastComposedWord */); resetComposingState(true /* alsoResetLastComposedWord */);
mDeleteCount = 0; mDeleteCount = 0;
mSpaceState = SPACE_STATE_NONE; mSpaceState = SPACE_STATE_NONE;
mRecapitalizeStatus.deactivate();
mCurrentlyPressedHardwareKeys.clear(); mCurrentlyPressedHardwareKeys.clear();
if (mSuggestionStripView != null) { if (mSuggestionStripView != null) {
@ -925,7 +926,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
// We moved the cursor. If we are touching a word, we need to resume suggestion. // We moved the cursor. If we are touching a word, we need to resume suggestion.
mHandler.postResumeSuggestions(); mHandler.postResumeSuggestions();
// Reset the last recapitalization. // Reset the last recapitalization.
mRecapitalizeStatus = null; mRecapitalizeStatus.deactivate();
mKeyboardSwitcher.updateShiftState(); mKeyboardSwitcher.updateShiftState();
} }
mExpectingUpdateSelection = false; mExpectingUpdateSelection = false;
@ -1953,10 +1954,9 @@ public final class LatinIME extends InputMethodService implements KeyboardAction
private void handleRecapitalize() { private void handleRecapitalize() {
if (mLastSelectionStart == mLastSelectionEnd) return; // No selection if (mLastSelectionStart == mLastSelectionEnd) return; // No selection
// If we have a recapitalize in progress, use it; otherwise, create a new one. // If we have a recapitalize in progress, use it; otherwise, create a new one.
if (null == mRecapitalizeStatus if (!mRecapitalizeStatus.isActive()
|| !mRecapitalizeStatus.isSetAt(mLastSelectionStart, mLastSelectionEnd)) { || !mRecapitalizeStatus.isSetAt(mLastSelectionStart, mLastSelectionEnd)) {
mRecapitalizeStatus = mRecapitalizeStatus.initialize(mLastSelectionStart, mLastSelectionEnd,
new RecapitalizeStatus(mLastSelectionStart, mLastSelectionEnd,
mConnection.getSelectedText(0 /* flags, 0 for no styles */).toString(), mConnection.getSelectedText(0 /* flags, 0 for no styles */).toString(),
mSettings.getCurrentLocale(), mSettings.getWordSeparators()); mSettings.getCurrentLocale(), mSettings.getWordSeparators());
// We trim leading and trailing whitespace. // We trim leading and trailing whitespace.

View File

@ -37,6 +37,7 @@ public class RecapitalizeStatus {
CAPS_MODE_FIRST_WORD_UPPER, CAPS_MODE_FIRST_WORD_UPPER,
CAPS_MODE_ALL_UPPER CAPS_MODE_ALL_UPPER
}; };
private static final int getStringMode(final String string, final String separators) { private static final int getStringMode(final String string, final String separators) {
if (StringUtils.isIdenticalAfterUpcase(string)) { if (StringUtils.isIdenticalAfterUpcase(string)) {
return CAPS_MODE_ALL_UPPER; return CAPS_MODE_ALL_UPPER;
@ -50,24 +51,29 @@ public class RecapitalizeStatus {
} }
/** /**
* We store the location of the cursor and the string that was there before the undoable * We store the location of the cursor and the string that was there before the recapitalize
* action was done, and the location of the cursor and the string that was there after. * action was done, and the location of the cursor and the string that was there after.
*/ */
private int mCursorStartBefore; private int mCursorStartBefore;
private int mCursorEndBefore;
private String mStringBefore; private String mStringBefore;
private int mCursorStartAfter; private int mCursorStartAfter;
private int mCursorEndAfter; private int mCursorEndAfter;
private int mRotationStyleCurrentIndex; private int mRotationStyleCurrentIndex;
private final boolean mSkipOriginalMixedCaseMode; private boolean mSkipOriginalMixedCaseMode;
private final Locale mLocale; private Locale mLocale;
private final String mSeparators; private String mSeparators;
private String mStringAfter; private String mStringAfter;
private boolean mIsActive;
public RecapitalizeStatus(final int cursorStart, final int cursorEnd, final String string, public RecapitalizeStatus() {
// By default, initialize with dummy values that won't match any real recapitalize.
initialize(-1, -1, "", Locale.getDefault(), "");
deactivate();
}
public void initialize(final int cursorStart, final int cursorEnd, final String string,
final Locale locale, final String separators) { final Locale locale, final String separators) {
mCursorStartBefore = cursorStart; mCursorStartBefore = cursorStart;
mCursorEndBefore = cursorEnd;
mStringBefore = string; mStringBefore = string;
mCursorStartAfter = cursorStart; mCursorStartAfter = cursorStart;
mCursorEndAfter = cursorEnd; mCursorEndAfter = cursorEnd;
@ -89,6 +95,15 @@ public class RecapitalizeStatus {
mRotationStyleCurrentIndex = currentMode; mRotationStyleCurrentIndex = currentMode;
mSkipOriginalMixedCaseMode = true; mSkipOriginalMixedCaseMode = true;
} }
mIsActive = true;
}
public void deactivate() {
mIsActive = false;
}
public boolean isActive() {
return mIsActive;
} }
public boolean isSetAt(final int cursorStart, final int cursorEnd) { public boolean isSetAt(final int cursorStart, final int cursorEnd) {
@ -110,23 +125,23 @@ public class RecapitalizeStatus {
} }
++count; ++count;
switch (ROTATION_STYLE[mRotationStyleCurrentIndex]) { switch (ROTATION_STYLE[mRotationStyleCurrentIndex]) {
case CAPS_MODE_ORIGINAL_MIXED_CASE: case CAPS_MODE_ORIGINAL_MIXED_CASE:
mStringAfter = mStringBefore; mStringAfter = mStringBefore;
break; break;
case CAPS_MODE_ALL_LOWER: case CAPS_MODE_ALL_LOWER:
mStringAfter = mStringBefore.toLowerCase(mLocale); mStringAfter = mStringBefore.toLowerCase(mLocale);
break; break;
case CAPS_MODE_FIRST_WORD_UPPER: case CAPS_MODE_FIRST_WORD_UPPER:
mStringAfter = StringUtils.capitalizeEachWord(mStringBefore, mSeparators, mStringAfter = StringUtils.capitalizeEachWord(mStringBefore, mSeparators,
mLocale); mLocale);
break; break;
case CAPS_MODE_ALL_UPPER: case CAPS_MODE_ALL_UPPER:
mStringAfter = mStringBefore.toUpperCase(mLocale); mStringAfter = mStringBefore.toUpperCase(mLocale);
break; break;
default: default:
mStringAfter = mStringBefore; mStringAfter = mStringBefore;
} }
} while (mStringAfter.equals(oldResult) && count < 5); } while (mStringAfter.equals(oldResult) && count < ROTATION_STYLE.length + 1);
mCursorEndAfter = mCursorStartAfter + mStringAfter.length(); mCursorEndAfter = mCursorStartAfter + mStringAfter.length();
} }
@ -148,7 +163,7 @@ public class RecapitalizeStatus {
if (!Character.isWhitespace(codePoint)) break; if (!Character.isWhitespace(codePoint)) break;
} }
if (0 != nonWhitespaceStart || len != nonWhitespaceEnd) { if (0 != nonWhitespaceStart || len != nonWhitespaceEnd) {
mCursorEndBefore = mCursorEndAfter = mCursorStartBefore + nonWhitespaceEnd; mCursorEndAfter = mCursorStartBefore + nonWhitespaceEnd;
mCursorStartBefore = mCursorStartAfter = mCursorStartBefore + nonWhitespaceStart; mCursorStartBefore = mCursorStartAfter = mCursorStartBefore + nonWhitespaceStart;
mStringAfter = mStringBefore = mStringAfter = mStringBefore =
mStringBefore.substring(nonWhitespaceStart, nonWhitespaceEnd); mStringBefore.substring(nonWhitespaceStart, nonWhitespaceEnd);

View File

@ -24,29 +24,26 @@ import java.util.Locale;
@SmallTest @SmallTest
public class RecapitalizeStatusTests extends AndroidTestCase { public class RecapitalizeStatusTests extends AndroidTestCase {
public void testTrim() { public void testTrim() {
RecapitalizeStatus status = new RecapitalizeStatus(30, 40, "abcdefghij", final RecapitalizeStatus status = new RecapitalizeStatus();
Locale.ENGLISH, " "); status.initialize(30, 40, "abcdefghij", Locale.ENGLISH, " ");
status.trim(); status.trim();
assertEquals("abcdefghij", status.getRecapitalizedString()); assertEquals("abcdefghij", status.getRecapitalizedString());
assertEquals(30, status.getNewCursorStart()); assertEquals(30, status.getNewCursorStart());
assertEquals(40, status.getNewCursorEnd()); assertEquals(40, status.getNewCursorEnd());
status = new RecapitalizeStatus(30, 44, " abcdefghij", status.initialize(30, 44, " abcdefghij", Locale.ENGLISH, " ");
Locale.ENGLISH, " ");
status.trim(); status.trim();
assertEquals("abcdefghij", status.getRecapitalizedString()); assertEquals("abcdefghij", status.getRecapitalizedString());
assertEquals(34, status.getNewCursorStart()); assertEquals(34, status.getNewCursorStart());
assertEquals(44, status.getNewCursorEnd()); assertEquals(44, status.getNewCursorEnd());
status = new RecapitalizeStatus(30, 40, "abcdefgh ", status.initialize(30, 40, "abcdefgh ", Locale.ENGLISH, " ");
Locale.ENGLISH, " ");
status.trim(); status.trim();
assertEquals("abcdefgh", status.getRecapitalizedString()); assertEquals("abcdefgh", status.getRecapitalizedString());
assertEquals(30, status.getNewCursorStart()); assertEquals(30, status.getNewCursorStart());
assertEquals(38, status.getNewCursorEnd()); assertEquals(38, status.getNewCursorEnd());
status = new RecapitalizeStatus(30, 45, " abcdefghij ", status.initialize(30, 45, " abcdefghij ", Locale.ENGLISH, " ");
Locale.ENGLISH, " ");
status.trim(); status.trim();
assertEquals("abcdefghij", status.getRecapitalizedString()); assertEquals("abcdefghij", status.getRecapitalizedString());
assertEquals(33, status.getNewCursorStart()); assertEquals(33, status.getNewCursorStart());
@ -54,8 +51,8 @@ public class RecapitalizeStatusTests extends AndroidTestCase {
} }
public void testRotate() { public void testRotate() {
RecapitalizeStatus status = new RecapitalizeStatus(29, 40, "abcd efghij", final RecapitalizeStatus status = new RecapitalizeStatus();
Locale.ENGLISH, " "); status.initialize(29, 40, "abcd efghij", Locale.ENGLISH, " ");
status.rotate(); status.rotate();
assertEquals("Abcd Efghij", status.getRecapitalizedString()); assertEquals("Abcd Efghij", status.getRecapitalizedString());
assertEquals(29, status.getNewCursorStart()); assertEquals(29, status.getNewCursorStart());
@ -67,8 +64,7 @@ public class RecapitalizeStatusTests extends AndroidTestCase {
status.rotate(); status.rotate();
assertEquals("Abcd Efghij", status.getRecapitalizedString()); assertEquals("Abcd Efghij", status.getRecapitalizedString());
status = new RecapitalizeStatus(29, 40, "Abcd Efghij", status.initialize(29, 40, "Abcd Efghij", Locale.ENGLISH, " ");
Locale.ENGLISH, " ");
status.rotate(); status.rotate();
assertEquals("ABCD EFGHIJ", status.getRecapitalizedString()); assertEquals("ABCD EFGHIJ", status.getRecapitalizedString());
assertEquals(29, status.getNewCursorStart()); assertEquals(29, status.getNewCursorStart());
@ -80,8 +76,7 @@ public class RecapitalizeStatusTests extends AndroidTestCase {
status.rotate(); status.rotate();
assertEquals("ABCD EFGHIJ", status.getRecapitalizedString()); assertEquals("ABCD EFGHIJ", status.getRecapitalizedString());
status = new RecapitalizeStatus(29, 40, "ABCD EFGHIJ", status.initialize(29, 40, "ABCD EFGHIJ", Locale.ENGLISH, " ");
Locale.ENGLISH, " ");
status.rotate(); status.rotate();
assertEquals("abcd efghij", status.getRecapitalizedString()); assertEquals("abcd efghij", status.getRecapitalizedString());
assertEquals(29, status.getNewCursorStart()); assertEquals(29, status.getNewCursorStart());
@ -93,8 +88,7 @@ public class RecapitalizeStatusTests extends AndroidTestCase {
status.rotate(); status.rotate();
assertEquals("abcd efghij", status.getRecapitalizedString()); assertEquals("abcd efghij", status.getRecapitalizedString());
status = new RecapitalizeStatus(29, 39, "AbCDefghij", status.initialize(29, 39, "AbCDefghij", Locale.ENGLISH, " ");
Locale.ENGLISH, " ");
status.rotate(); status.rotate();
assertEquals("abcdefghij", status.getRecapitalizedString()); assertEquals("abcdefghij", status.getRecapitalizedString());
assertEquals(29, status.getNewCursorStart()); assertEquals(29, status.getNewCursorStart());
@ -108,8 +102,7 @@ public class RecapitalizeStatusTests extends AndroidTestCase {
status.rotate(); status.rotate();
assertEquals("abcdefghij", status.getRecapitalizedString()); assertEquals("abcdefghij", status.getRecapitalizedString());
status = new RecapitalizeStatus(29, 40, "Abcd efghij", status.initialize(29, 40, "Abcd efghij", Locale.ENGLISH, " ");
Locale.ENGLISH, " ");
status.rotate(); status.rotate();
assertEquals("abcd efghij", status.getRecapitalizedString()); assertEquals("abcd efghij", status.getRecapitalizedString());
assertEquals(29, status.getNewCursorStart()); assertEquals(29, status.getNewCursorStart());
@ -123,8 +116,7 @@ public class RecapitalizeStatusTests extends AndroidTestCase {
status.rotate(); status.rotate();
assertEquals("abcd efghij", status.getRecapitalizedString()); assertEquals("abcd efghij", status.getRecapitalizedString());
status = new RecapitalizeStatus(30, 34, "grüß", Locale.GERMAN, " "); status.initialize(30, 34, "grüß", Locale.GERMAN, " "); status.rotate();
status.rotate();
assertEquals("Grüß", status.getRecapitalizedString()); assertEquals("Grüß", status.getRecapitalizedString());
assertEquals(30, status.getNewCursorStart()); assertEquals(30, status.getNewCursorStart());
assertEquals(34, status.getNewCursorEnd()); assertEquals(34, status.getNewCursorEnd());
@ -141,9 +133,7 @@ public class RecapitalizeStatusTests extends AndroidTestCase {
assertEquals(30, status.getNewCursorStart()); assertEquals(30, status.getNewCursorStart());
assertEquals(34, status.getNewCursorEnd()); assertEquals(34, status.getNewCursorEnd());
status.initialize(30, 33, "œuf", Locale.FRENCH, " "); status.rotate();
status = new RecapitalizeStatus(30, 33, "œuf", Locale.FRENCH, " ");
status.rotate();
assertEquals("Œuf", status.getRecapitalizedString()); assertEquals("Œuf", status.getRecapitalizedString());
assertEquals(30, status.getNewCursorStart()); assertEquals(30, status.getNewCursorStart());
assertEquals(33, status.getNewCursorEnd()); assertEquals(33, status.getNewCursorEnd());
@ -160,8 +150,7 @@ public class RecapitalizeStatusTests extends AndroidTestCase {
assertEquals(30, status.getNewCursorStart()); assertEquals(30, status.getNewCursorStart());
assertEquals(33, status.getNewCursorEnd()); assertEquals(33, status.getNewCursorEnd());
status = new RecapitalizeStatus(30, 33, "œUf", Locale.FRENCH, " "); status.initialize(30, 33, "œUf", Locale.FRENCH, " "); status.rotate();
status.rotate();
assertEquals("œuf", status.getRecapitalizedString()); assertEquals("œuf", status.getRecapitalizedString());
assertEquals(30, status.getNewCursorStart()); assertEquals(30, status.getNewCursorStart());
assertEquals(33, status.getNewCursorEnd()); assertEquals(33, status.getNewCursorEnd());
@ -182,8 +171,7 @@ public class RecapitalizeStatusTests extends AndroidTestCase {
assertEquals(30, status.getNewCursorStart()); assertEquals(30, status.getNewCursorStart());
assertEquals(33, status.getNewCursorEnd()); assertEquals(33, status.getNewCursorEnd());
status = new RecapitalizeStatus(30, 35, "école", Locale.FRENCH, " "); status.initialize(30, 35, "école", Locale.FRENCH, " "); status.rotate();
status.rotate();
assertEquals("École", status.getRecapitalizedString()); assertEquals("École", status.getRecapitalizedString());
assertEquals(30, status.getNewCursorStart()); assertEquals(30, status.getNewCursorStart());
assertEquals(35, status.getNewCursorEnd()); assertEquals(35, status.getNewCursorEnd());