diff options
author | 2012-12-03 22:00:35 +0900 | |
---|---|---|
committer | 2013-04-15 20:23:01 +0900 | |
commit | 2995abe7aadd483aa57a9b088740d46ac07bbe46 (patch) | |
tree | fcf2a3303cb3fc67b621adac0cbf235a26b405ab /java/src/com/android/inputmethod/latin/LatinIME.java | |
parent | 059e084e983ce4a1440dc065f5167d278d8939e7 (diff) | |
download | latinime-2995abe7aadd483aa57a9b088740d46ac07bbe46.tar.gz latinime-2995abe7aadd483aa57a9b088740d46ac07bbe46.tar.xz latinime-2995abe7aadd483aa57a9b088740d46ac07bbe46.zip |
Have Latin IME re-capitalize a selected string
Upon pressing Shift, if there is currently a selected string, have
Latin IME change its capitalization.
This does not yet have the keyboard mode follow the mode - the change
is complicated enough as is.
Bug: 7657025
Change-Id: I54fe8485f44e04efd72c71ac9feee5ce21ba06f2
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LatinIME.java')
-rw-r--r-- | java/src/com/android/inputmethod/latin/LatinIME.java | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 47d51c586..eaa095256 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -161,6 +161,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction mPositionalInfoForUserDictPendingAddition = null; private final WordComposer mWordComposer = new WordComposer(); private final RichInputConnection mConnection = new RichInputConnection(this); + private RecapitalizeStatus mRecapitalizeStatus = new RecapitalizeStatus(-1, -1, "", + Locale.getDefault(), ""); // Dummy object that will match no real recapitalize // 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; @@ -1387,8 +1389,13 @@ public final class LatinIME extends InputMethodService implements KeyboardAction LatinImeLogger.logOnDelete(x, y); break; case Constants.CODE_SHIFT: + // Note: calling back to the keyboard on Shift key is handled in onPressKey() + // and onReleaseKey(). + handleRecapitalize(); + break; case Constants.CODE_SWITCH_ALPHA_SYMBOL: - // Shift and symbol key is handled in onPressKey() and onReleaseKey(). + // Note: calling back to the keyboard on symbol key is handled in onPressKey() + // and onReleaseKey(). break; case Constants.CODE_SETTINGS: onSettingsKeyPressed(); @@ -1928,6 +1935,37 @@ public final class LatinIME extends InputMethodService implements KeyboardAction } } + private void handleRecapitalize() { + if (mLastSelectionStart == mLastSelectionEnd) return; // No selection + // If we have a recapitalize in progress, use it; otherwise, create a new one. + if (null == mRecapitalizeStatus + || !mRecapitalizeStatus.isSetAt(mLastSelectionStart, mLastSelectionEnd)) { + mRecapitalizeStatus = + new RecapitalizeStatus(mLastSelectionStart, mLastSelectionEnd, + mConnection.getSelectedText(0 /* flags, 0 for no styles */).toString(), + mSettings.getCurrentLocale(), mSettings.getWordSeparators()); + // We trim leading and trailing whitespace. + mRecapitalizeStatus.trim(); + // Trimming the object may have changed the length of the string, and we need to + // reposition the selection handles accordingly. As this result in an IPC call, + // only do it if it's actually necessary, in other words if the recapitalize status + // is not set at the same place as before. + if (!mRecapitalizeStatus.isSetAt(mLastSelectionStart, mLastSelectionEnd)) { + mLastSelectionStart = mRecapitalizeStatus.getNewCursorStart(); + mLastSelectionEnd = mRecapitalizeStatus.getNewCursorEnd(); + mConnection.setSelection(mLastSelectionStart, mLastSelectionEnd); + } + } + mRecapitalizeStatus.rotate(); + final int numCharsDeleted = mLastSelectionEnd - mLastSelectionStart; + mConnection.setSelection(mLastSelectionEnd, mLastSelectionEnd); + mConnection.deleteSurroundingText(numCharsDeleted, 0); + mConnection.commitText(mRecapitalizeStatus.getRecapitalizedString(), 0); + mLastSelectionStart = mRecapitalizeStatus.getNewCursorStart(); + mLastSelectionEnd = mRecapitalizeStatus.getNewCursorEnd(); + mConnection.setSelection(mLastSelectionStart, mLastSelectionEnd); + } + // Returns true if we did an autocorrection, false otherwise. private boolean handleSeparator(final int primaryCode, final int x, final int y, final int spaceState) { |