am a2632aab: Merge "Extract method to reloadTextCache()"

* commit 'a2632aabf7815b1e5ede374a120471b491fa4020':
  Extract method to reloadTextCache()
main
Kurt Partridge 2013-11-14 07:30:56 -08:00 committed by Android Git Automerger
commit 66028b33f9
1 changed files with 47 additions and 23 deletions

View File

@ -160,21 +160,12 @@ public final class RichInputConnection {
final boolean shouldFinishComposition) { final boolean shouldFinishComposition) {
mExpectedCursorPosition = newCursorPosition; mExpectedCursorPosition = newCursorPosition;
mComposingText.setLength(0); mComposingText.setLength(0);
mCommittedTextBeforeComposingText.setLength(0); final boolean didReloadTextSuccessfully = reloadTextCache();
mIC = mParent.getCurrentInputConnection(); if (!didReloadTextSuccessfully) {
// Call upon the inputconnection directly since our own method is using the cache, and Log.d(TAG, "Will try to retrieve text later.");
// we want to refresh it.
final CharSequence textBeforeCursor = null == mIC ? null :
mIC.getTextBeforeCursor(Constants.EDITOR_CONTENTS_CACHE_SIZE, 0);
if (null == textBeforeCursor) {
// For some reason the app thinks we are not connected to it. This looks like a
// framework bug... Fall back to ground state and return false.
mExpectedCursorPosition = INVALID_CURSOR_POSITION;
Log.e(TAG, "Unable to connect to the editor to retrieve text... will retry later");
return false; return false;
} }
mCommittedTextBeforeComposingText.append(textBeforeCursor); final int lengthOfTextBeforeCursor = mCommittedTextBeforeComposingText.length();
final int lengthOfTextBeforeCursor = textBeforeCursor.length();
if (lengthOfTextBeforeCursor > newCursorPosition if (lengthOfTextBeforeCursor > newCursorPosition
|| (lengthOfTextBeforeCursor < Constants.EDITOR_CONTENTS_CACHE_SIZE || (lengthOfTextBeforeCursor < Constants.EDITOR_CONTENTS_CACHE_SIZE
&& newCursorPosition < Constants.EDITOR_CONTENTS_CACHE_SIZE)) { && newCursorPosition < Constants.EDITOR_CONTENTS_CACHE_SIZE)) {
@ -193,6 +184,29 @@ public final class RichInputConnection {
return true; return true;
} }
/**
* Reload the cached text from the InputConnection.
*
* @return true if successful
*/
private boolean reloadTextCache() {
mCommittedTextBeforeComposingText.setLength(0);
mIC = mParent.getCurrentInputConnection();
// Call upon the inputconnection directly since our own method is using the cache, and
// we want to refresh it.
final CharSequence textBeforeCursor = null == mIC ? null :
mIC.getTextBeforeCursor(Constants.EDITOR_CONTENTS_CACHE_SIZE, 0);
if (null == textBeforeCursor) {
// For some reason the app thinks we are not connected to it. This looks like a
// framework bug... Fall back to ground state and return false.
mExpectedCursorPosition = INVALID_CURSOR_POSITION;
Log.e(TAG, "Unable to connect to the editor to retrieve text.");
return false;
}
mCommittedTextBeforeComposingText.append(textBeforeCursor);
return true;
}
private void checkBatchEdit() { private void checkBatchEdit() {
if (mNestLevel != 1) { if (mNestLevel != 1) {
// TODO: exception instead // TODO: exception instead
@ -269,10 +283,9 @@ public final class RichInputConnection {
// getCapsMode should be updated to be able to return a "not enough info" result so that // getCapsMode should be updated to be able to return a "not enough info" result so that
// we can get more context only when needed. // we can get more context only when needed.
if (TextUtils.isEmpty(mCommittedTextBeforeComposingText) && 0 != mExpectedCursorPosition) { if (TextUtils.isEmpty(mCommittedTextBeforeComposingText) && 0 != mExpectedCursorPosition) {
final CharSequence textBeforeCursor = getTextBeforeCursor( if (!reloadTextCache()) {
Constants.EDITOR_CONTENTS_CACHE_SIZE, 0); Log.w(TAG, "Unable to connect to the editor. "
if (!TextUtils.isEmpty(textBeforeCursor)) { + "Setting caps mode without knowing text.");
mCommittedTextBeforeComposingText.append(textBeforeCursor);
} }
} }
// 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
@ -443,19 +456,30 @@ public final class RichInputConnection {
if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
} }
public void setSelection(final int start, final int end) { /**
* Set the selection of the text editor.
*
* Calls through to {@link InputConnection#setSelection(int, int)}.
*
* @param start the character index where the selection should start.
* @param end the character index where the selection should end.
* @return Returns true on success, false if the input connection is no longer valid either when
* setting the selection or when retrieving the text cache at that point.
*/
public boolean setSelection(final int start, final int end) {
if (DEBUG_BATCH_NESTING) checkBatchEdit(); if (DEBUG_BATCH_NESTING) checkBatchEdit();
if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug(); if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
mExpectedCursorPosition = start;
if (null != mIC) { if (null != mIC) {
mIC.setSelection(start, end); final boolean isIcValid = mIC.setSelection(start, end);
if (!isIcValid) {
return false;
}
if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
ResearchLogger.richInputConnection_setSelection(start, end); ResearchLogger.richInputConnection_setSelection(start, end);
} }
} }
mExpectedCursorPosition = start; return reloadTextCache();
mCommittedTextBeforeComposingText.setLength(0);
mCommittedTextBeforeComposingText.append(
getTextBeforeCursor(Constants.EDITOR_CONTENTS_CACHE_SIZE, 0));
} }
public void commitCorrection(final CorrectionInfo correctionInfo) { public void commitCorrection(final CorrectionInfo correctionInfo) {