Forráskód Böngészése

Merge pull request #110611 from m4gr3d/fix_show_keyboard_crash

Fix the bug causing `java.lang.StringIndexOutOfBoundsException` crashes when showing the virtual keyboard
Thaddeus Crews 3 hete
szülő
commit
f84123d20e

+ 12 - 8
platform/android/java/lib/src/org/godotengine/godot/input/GodotEditText.java

@@ -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
-			this.mOriginText = p_existing_text.substring(0, p_cursor_start);
-			this.mMaxInputLength = maxInputLength - (p_existing_text.length() - p_cursor_start);
+		} else if (cursorEnd == -1) { // not text selection
+			cursorStart = Math.min(p_existing_text.length(), cursorStart);
+			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);
-			this.mMaxInputLength = maxInputLength - (p_existing_text.length() - p_cursor_end);
+			cursorEnd = Math.min(p_existing_text.length(), cursorEnd);
+			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.arg2 = p_cursor_end;
+		msg.arg1 = cursorStart;
+		msg.arg2 = cursorEnd;
 		sHandler.sendMessage(msg);
 		sHandler.sendMessage(msg);
 	}
 	}