1616
1717package rkr .simplekeyboard .inputmethod .latin ;
1818
19+ import android .annotation .TargetApi ;
1920import android .os .Build ;
2021import android .util .Log ;
2122import android .view .KeyEvent ;
23+ import android .view .inputmethod .EditorInfo ;
2224import android .view .inputmethod .InputConnection ;
2325import 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