Skip to content

Commit f84123d

Browse files
committed
Merge pull request godotengine#110611 from m4gr3d/fix_show_keyboard_crash
Fix the bug causing `java.lang.StringIndexOutOfBoundsException` crashes when showing the virtual keyboard
2 parents 42fbaba + ff3eee7 commit f84123d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

platform/android/java/lib/src/org/godotengine/godot/input/GodotEditText.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,25 +276,29 @@ public void showKeyboard(String p_existing_text, VirtualKeyboardType p_type, int
276276
return;
277277
}
278278

279+
int cursorStart = p_cursor_start;
280+
int cursorEnd = p_cursor_end;
279281
int maxInputLength = (p_max_input_length <= 0) ? Integer.MAX_VALUE : p_max_input_length;
280-
if (p_cursor_start == -1) { // cursor position not given
282+
if (cursorStart == -1) { // cursor position not given
281283
this.mOriginText = p_existing_text;
282284
this.mMaxInputLength = maxInputLength;
283-
} else if (p_cursor_end == -1) { // not text selection
284-
this.mOriginText = p_existing_text.substring(0, p_cursor_start);
285-
this.mMaxInputLength = maxInputLength - (p_existing_text.length() - p_cursor_start);
285+
} else if (cursorEnd == -1) { // not text selection
286+
cursorStart = Math.min(p_existing_text.length(), cursorStart);
287+
this.mOriginText = p_existing_text.substring(0, cursorStart);
288+
this.mMaxInputLength = maxInputLength - (p_existing_text.length() - cursorStart);
286289
} else {
287-
this.mOriginText = p_existing_text.substring(0, p_cursor_end);
288-
this.mMaxInputLength = maxInputLength - (p_existing_text.length() - p_cursor_end);
290+
cursorEnd = Math.min(p_existing_text.length(), cursorEnd);
291+
this.mOriginText = p_existing_text.substring(0, cursorEnd);
292+
this.mMaxInputLength = maxInputLength - (p_existing_text.length() - cursorEnd);
289293
}
290294

291295
this.mKeyboardType = p_type;
292296

293297
final Message msg = new Message();
294298
msg.what = HANDLER_OPEN_IME_KEYBOARD;
295299
msg.obj = this;
296-
msg.arg1 = p_cursor_start;
297-
msg.arg2 = p_cursor_end;
300+
msg.arg1 = cursorStart;
301+
msg.arg2 = cursorEnd;
298302
sHandler.sendMessage(msg);
299303
}
300304

0 commit comments

Comments
 (0)