From 0421578517641120e93e70a8b3f6b2afb0ecdc10 Mon Sep 17 00:00:00 2001 From: Dimo Karaivanov Date: Thu, 9 Feb 2023 11:01:13 +0200 Subject: [PATCH] fixed a rare problem where Predictive mode would crash while determining the text case at the beginning of a text field --- .../github/sspanak/tt9/ime/TraditionalT9.java | 2 +- .../tt9/ime/helpers/InputFieldHelper.java | 48 ++++++++++++++----- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/io/github/sspanak/tt9/ime/TraditionalT9.java b/src/io/github/sspanak/tt9/ime/TraditionalT9.java index 7da8fdfd..55da0bba 100644 --- a/src/io/github/sspanak/tt9/ime/TraditionalT9.java +++ b/src/io/github/sspanak/tt9/ime/TraditionalT9.java @@ -581,7 +581,7 @@ public class TraditionalT9 extends KeyPadHandler { mInputMode.determineNextWordTextCase( settings, InputFieldHelper.isThereText(currentInputConnection), - (String) currentInputConnection.getTextBeforeCursor(50, 0) + InputFieldHelper.getTextBeforeCursor(currentInputConnection) ); } diff --git a/src/io/github/sspanak/tt9/ime/helpers/InputFieldHelper.java b/src/io/github/sspanak/tt9/ime/helpers/InputFieldHelper.java index 07b16f6d..01f312c4 100644 --- a/src/io/github/sspanak/tt9/ime/helpers/InputFieldHelper.java +++ b/src/io/github/sspanak/tt9/ime/helpers/InputFieldHelper.java @@ -200,21 +200,43 @@ public class InputFieldHelper { } + /** + * getTextBeforeCursor + * A simplified helper that return up to 50 characters before the cursor and "just works". + */ + public static String getTextBeforeCursor(InputConnection inputConnection) { + if (inputConnection == null) { + return ""; + } + + CharSequence before = inputConnection.getTextBeforeCursor(50, 0); + return before != null ? before.toString() : ""; + } + + + /** + * getTextBeforeCursor + * A simplified helper that return up to 50 characters after the cursor and "just works". + */ + public static String getTextAfterCursor(InputConnection inputConnection) { + if (inputConnection == null) { + return ""; + } + + CharSequence before = inputConnection.getTextAfterCursor(50, 0); + return before != null ? before.toString() : ""; + } + + + /** + * getSurroundingWord + * Returns the word next or around the cursor. Scanning length is up to 50 chars in each direction. + */ public static String getSurroundingWord(InputConnection currentInputConnection) { - if (currentInputConnection == null) { - return ""; - } + Matcher before = beforeCursorWordRegex.matcher(getTextBeforeCursor(currentInputConnection)); + Matcher after = afterCursorWordRegex.matcher(getTextAfterCursor(currentInputConnection)); - CharSequence before = currentInputConnection.getTextBeforeCursor(50, 0); - CharSequence after = currentInputConnection.getTextAfterCursor(50, 0); - if (before == null || after == null) { - return ""; - } - - Matcher beforeMatch = beforeCursorWordRegex.matcher(before); - Matcher afterMatch = afterCursorWordRegex.matcher(after); - - return (beforeMatch.find() ? beforeMatch.group(1) : "") + (afterMatch.find() ? afterMatch.group(1) : ""); + return (before.find() ? before.group(1) : "") + (after.find() ? after.group(1) : ""); }