Merge pull request #110611 from m4gr3d/fix_show_keyboard_crash

Fix the bug causing `java.lang.StringIndexOutOfBoundsException` crashes when showing the virtual keyboard
This commit is contained in:
Thaddeus Crews 2025-09-18 21:02:33 -05:00
commit f84123d20e
No known key found for this signature in database
GPG key ID: 8C6E5FEB5FC03CCC

View file

@ -276,16 +276,20 @@ public class GodotEditText extends EditText {
return; return;
} }
int cursorStart = p_cursor_start;
int cursorEnd = p_cursor_end;
int maxInputLength = (p_max_input_length <= 0) ? Integer.MAX_VALUE : p_max_input_length; int maxInputLength = (p_max_input_length <= 0) ? Integer.MAX_VALUE : p_max_input_length;
if (p_cursor_start == -1) { // cursor position not given if (cursorStart == -1) { // cursor position not given
this.mOriginText = p_existing_text; this.mOriginText = p_existing_text;
this.mMaxInputLength = maxInputLength; this.mMaxInputLength = maxInputLength;
} else if (p_cursor_end == -1) { // not text selection } else if (cursorEnd == -1) { // not text selection
this.mOriginText = p_existing_text.substring(0, p_cursor_start); cursorStart = Math.min(p_existing_text.length(), cursorStart);
this.mMaxInputLength = maxInputLength - (p_existing_text.length() - p_cursor_start); this.mOriginText = p_existing_text.substring(0, cursorStart);
this.mMaxInputLength = maxInputLength - (p_existing_text.length() - cursorStart);
} else { } else {
this.mOriginText = p_existing_text.substring(0, p_cursor_end); cursorEnd = Math.min(p_existing_text.length(), cursorEnd);
this.mMaxInputLength = maxInputLength - (p_existing_text.length() - p_cursor_end); this.mOriginText = p_existing_text.substring(0, cursorEnd);
this.mMaxInputLength = maxInputLength - (p_existing_text.length() - cursorEnd);
} }
this.mKeyboardType = p_type; this.mKeyboardType = p_type;
@ -293,8 +297,8 @@ public class GodotEditText extends EditText {
final Message msg = new Message(); final Message msg = new Message();
msg.what = HANDLER_OPEN_IME_KEYBOARD; msg.what = HANDLER_OPEN_IME_KEYBOARD;
msg.obj = this; msg.obj = this;
msg.arg1 = p_cursor_start; msg.arg1 = cursorStart;
msg.arg2 = p_cursor_end; msg.arg2 = cursorEnd;
sHandler.sendMessage(msg); sHandler.sendMessage(msg);
} }