Skip to content

Commit 480be4f

Browse files
committed
Fix crash during IME_CLEAR_TEXT
1 parent 2931f1c commit 480be4f

File tree

1 file changed

+16
-5
lines changed
  • keyboardservice/src/main/java/com/android/adbkeyboard

1 file changed

+16
-5
lines changed

keyboardservice/src/main/java/com/android/adbkeyboard/AdbIME.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,22 @@ public void onReceive(Context context, Intent intent) {
164164
if (intent.getAction().equals(IME_CLEAR_TEXT)) {
165165
InputConnection ic = getCurrentInputConnection();
166166
if (ic != null) {
167-
//REF: stackoverflow/33082004 author: Maxime Epain
168-
CharSequence curPos = ic.getExtractedText(new ExtractedTextRequest(), 0).text;
169-
CharSequence beforePos = ic.getTextBeforeCursor(curPos.length(), 0);
170-
CharSequence afterPos = ic.getTextAfterCursor(curPos.length(), 0);
171-
ic.deleteSurroundingText(beforePos.length(), afterPos.length());
167+
// Try to get extracted text first
168+
ExtractedTextRequest req = new ExtractedTextRequest();
169+
req.hintMaxChars = 100000;
170+
req.hintMaxLines = 10000;
171+
android.view.inputmethod.ExtractedText et = ic.getExtractedText(req, 0);
172+
if (et != null && et.text != null) {
173+
CharSequence beforePos = ic.getTextBeforeCursor(et.text.length(), 0);
174+
CharSequence afterPos = ic.getTextAfterCursor(et.text.length(), 0);
175+
if (beforePos != null && afterPos != null) {
176+
ic.deleteSurroundingText(beforePos.length(), afterPos.length());
177+
}
178+
} else {
179+
// Fallback: select all and delete
180+
ic.performContextMenuAction(android.R.id.selectAll);
181+
ic.commitText("", 1);
182+
}
172183
}
173184
}
174185

0 commit comments

Comments
 (0)