88import java .util .concurrent .ExecutorService ;
99import java .util .concurrent .RejectedExecutionException ;
1010
11+ import io .github .sspanak .tt9 .hacks .InputType ;
12+ import io .github .sspanak .tt9 .ime .helpers .TextField ;
1113import io .github .sspanak .tt9 .ime .modes .InputMode ;
1214import io .github .sspanak .tt9 .ime .modes .InputModeKind ;
15+ import io .github .sspanak .tt9 .ime .modes .helpers .AutoTextCase ;
16+ import io .github .sspanak .tt9 .ime .modes .helpers .Sequences ;
1317import io .github .sspanak .tt9 .languages .Language ;
1418import io .github .sspanak .tt9 .languages .LanguageKind ;
1519import io .github .sspanak .tt9 .languages .NaturalLanguage ;
20+ import io .github .sspanak .tt9 .languages .exceptions .InvalidLanguageCharactersException ;
1621import io .github .sspanak .tt9 .preferences .settings .SettingsStore ;
1722import io .github .sspanak .tt9 .util .Logger ;
1823import io .github .sspanak .tt9 .util .Text ;
@@ -23,6 +28,7 @@ public class MindReader {
2328 private static final String LOG_TAG = MindReader .class .getSimpleName ();
2429
2530 // @todo: move these constants to SettingsStatic
31+ // @todo: test and maybe set a maximum size for MindReaderNgramList?
2632 private static final int MAX_NGRAM_SIZE = 4 ;
2733 private static final int MAX_BIGRAM_SUGGESTIONS = 5 ;
2834 private static final int MAX_TRIGRAM_SUGGESTIONS = 4 ;
@@ -31,6 +37,7 @@ public class MindReader {
3137 static final int DICTIONARY_WORD_SIZE = 16 ; // in bytes
3238 private static final int MAX_DICTIONARY_WORDS = (int ) Math .pow (2 , DICTIONARY_WORD_SIZE );
3339
40+ @ Nullable private final AutoTextCase autoTextCase ;
3441 @ Nullable private final ExecutorService executor ;
3542 @ Nullable private final SettingsStore settings ;
3643
@@ -44,11 +51,12 @@ public class MindReader {
4451
4552
4653 public MindReader () {
47- this (null , null );
54+ this (null , null , null );
4855 }
4956
5057
51- public MindReader (@ Nullable SettingsStore settings , @ Nullable ExecutorService executor ) {
58+ public MindReader (@ Nullable SettingsStore settings , @ Nullable ExecutorService executor , @ Nullable InputType inputType ) {
59+ this .autoTextCase = settings != null ? new AutoTextCase (settings , new Sequences (), inputType ) : null ;
5260 this .executor = executor ;
5361 this .settings = settings ;
5462 }
@@ -63,10 +71,9 @@ public void clearContext() {
6371
6472
6573 @ NonNull
66- public ArrayList <String > getCurrentWords (int textCase ) {
67- // @todo: use AutoTextCase.adjustParagraphTextCase() here. "before" is the context.
74+ public ArrayList <String > getCurrentWords (@ Nullable InputMode inputMode , @ NonNull TextField textField , @ NonNull InputType inputType , int textCase ) {
6875 final ArrayList <String > copy = new ArrayList <>(words );
69- copy .replaceAll (text -> new Text ( wordContext . language , text ). toTextCase ( textCase ));
76+ copy .replaceAll (text -> adjustWordTextCase ( inputMode , text , inputType . determineTextCase (), textCase , textField ));
7077 return copy ;
7178 }
7279
@@ -81,6 +88,7 @@ public void guessNext(@NonNull InputMode inputMode, @NonNull Language language,
8188
8289 loadingId = 0 ;
8390
91+ // @todo: this fails for ABC. Fix it!
8492 if (setContextSync (inputMode , language , surroundingText , lastWord )) {
8593 runInThread (() -> {
8694 processContext (inputMode , language , saveContext );
@@ -181,6 +189,29 @@ public void processContext(@Nullable InputMode inputMode, @NonNull Language lang
181189 }
182190
183191
192+ private String adjustWordTextCase (@ Nullable InputMode inputMode , @ NonNull String word , int modeTextCase , int textFieldTextCase , @ NonNull TextField textField ) {
193+ if (autoTextCase == null || wordContext .language == null || settings == null || !settings .isAutoTextCaseOn (inputMode )) {
194+ return word ;
195+ }
196+
197+ String digitSequence ;
198+ try {
199+ digitSequence = wordContext .language .getDigitSequenceForWord (word );
200+ } catch (InvalidLanguageCharactersException e ) {
201+ return word ;
202+ }
203+
204+
205+ String context = wordContext .getRaw ();
206+ if (!new Text (wordContext .language , context ).endsWithLetter ()) {
207+ context += " " ;
208+ }
209+
210+ final int newTextCase = autoTextCase .determineNextWordTextCase (wordContext .language , modeTextCase , textFieldTextCase , textField , digitSequence , context );
211+ return autoTextCase .adjustSuggestionTextCase (new Text (wordContext .language , word ), newTextCase );
212+ }
213+
214+
184215 private void changeLanguage (@ NonNull Language language ) {
185216 if (!language .equals (wordContext .language )) {
186217 // @todo: save the current dictionary for the previous language
0 commit comments