Skip to content

Commit 8deaf35

Browse files
committed
Some apps send onStartInputView instead of onUpdateSelection
1 parent bcd8faf commit 8deaf35

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

app/src/main/java/rkr/simplekeyboard/inputmethod/latin/LatinIME.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ void onStartInputViewInternal(final EditorInfo editorInfo, final boolean restart
424424
// it can adjust its combiners if needed.
425425
mInputLogic.startInput();
426426

427-
mInputLogic.mConnection.reloadTextCache();
427+
// Some applications call onStartInputView without updating EditorInfo. In these cases
428+
// selection will be incorrect.
429+
mInputLogic.mConnection.reloadTextCache(editorInfo);
428430
}
429431

430432
if (isDifferentTextField ||
@@ -493,11 +495,7 @@ public void onUpdateSelection(final int oldSelStart, final int oldSelEnd,
493495
final int composingSpanStart, final int composingSpanEnd) {
494496
super.onUpdateSelection(oldSelStart, oldSelEnd, newSelStart, newSelEnd,
495497
composingSpanStart, composingSpanEnd);
496-
if (DebugFlags.DEBUG_ENABLED) {
497-
Log.i(TAG, "onUpdateSelection: oss=" + oldSelStart + ", ose=" + oldSelEnd
498-
+ ", nss=" + newSelStart + ", nse=" + newSelEnd
499-
+ ", cs=" + composingSpanStart + ", ce=" + composingSpanEnd);
500-
}
498+
Log.i(TAG, "Update Selection. Cursor position = " + newSelStart + "," + newSelEnd);
501499

502500
mInputLogic.onUpdateSelection(newSelStart, newSelEnd);
503501
if (isInputViewShown()) {

app/src/main/java/rkr/simplekeyboard/inputmethod/latin/RichInputConnection.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package rkr.simplekeyboard.inputmethod.latin;
1818

19+
import android.annotation.TargetApi;
1920
import android.os.Build;
2021
import android.util.Log;
2122
import android.view.KeyEvent;
23+
import android.view.inputmethod.EditorInfo;
2224
import android.view.inputmethod.InputConnection;
2325
import android.view.inputmethod.SurroundingText;
2426

@@ -100,6 +102,36 @@ public void updateSelection(final int newSelStart, final int newSelEnd) {
100102
mExpectedSelEnd = newSelEnd;
101103
}
102104

105+
@TargetApi(Build.VERSION_CODES.S)
106+
private void setTextAroundCursor(final SurroundingText textAroundCursor) {
107+
if (null == textAroundCursor) {
108+
Log.e(TAG, "Unable get text around cursor.");
109+
mTextBeforeCursor = "";
110+
mTextAfterCursor = "";
111+
mTextSelection = "";
112+
return;
113+
}
114+
final CharSequence text = textAroundCursor.getText();
115+
mTextBeforeCursor = text.subSequence(0, textAroundCursor.getSelectionStart()).toString();
116+
mTextSelection = text.subSequence(textAroundCursor.getSelectionStart(), textAroundCursor.getSelectionEnd()).toString();
117+
mTextAfterCursor = text.subSequence(textAroundCursor.getSelectionEnd(), text.length()).toString();
118+
}
119+
120+
/**
121+
* Reload the cached text from the EditorInfo.
122+
*/
123+
public void reloadTextCache(final EditorInfo editorInfo) {
124+
updateSelection(editorInfo.initialSelStart, editorInfo.initialSelEnd);
125+
126+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
127+
final SurroundingText textAroundCursor = editorInfo
128+
.getInitialSurroundingText(Constants.EDITOR_CONTENTS_CACHE_SIZE, Constants.EDITOR_CONTENTS_CACHE_SIZE, 0);
129+
setTextAroundCursor(textAroundCursor);
130+
} else {
131+
reloadTextCache();
132+
}
133+
}
134+
103135
/**
104136
* Reload the cached text from the InputConnection.
105137
*/
@@ -120,17 +152,7 @@ public void reloadTextCache() {
120152
Log.w(TAG, "Selection range modified before thread completion.");
121153
return;
122154
}
123-
if (null == textAroundCursor) {
124-
Log.e(TAG, "Unable get text around cursor.");
125-
mTextBeforeCursor = "";
126-
mTextAfterCursor = "";
127-
mTextSelection = "";
128-
return;
129-
}
130-
final CharSequence text = textAroundCursor.getText();
131-
mTextBeforeCursor = text.subSequence(0, textAroundCursor.getSelectionStart()).toString();
132-
mTextSelection = text.subSequence(textAroundCursor.getSelectionStart(), textAroundCursor.getSelectionEnd()).toString();
133-
mTextAfterCursor = text.subSequence(textAroundCursor.getSelectionEnd(), text.length()).toString();
155+
setTextAroundCursor(textAroundCursor);
134156

135157
// All callbacks that need text before cursor are here
136158
mLatinIME.mHandler.postUpdateShiftState();

0 commit comments

Comments
 (0)