abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/Autocapitalisation.java
diff options
context:
space:
mode:
authorJules Aguillon2022-07-24 20:02:48 +0200
committerJules Aguillon2022-07-24 20:02:48 +0200
commit324756535e139aacfb9d828a5bc9a2a6fef634ea (patch)
tree717553aab3e9bd5a0536aa8b2e2a3a69d5e335ca /srcs/juloo.keyboard2/Autocapitalisation.java
parent2d8ed2d85849c95c7d39e0dfe11405bf70eeb395 (diff)
downloadunexpected-keyboard-324756535e139aacfb9d828a5bc9a2a6fef634ea.tar.gz
unexpected-keyboard-324756535e139aacfb9d828a5bc9a2a6fef634ea.zip
Automatic capitalisation at beginning of sentences
Keep track of end-of-sentence characters while typing and automatically enable shift when appropriate. The last few characters just before the cursor need to be queried in some cases: Begin of input, cursor has moved or text is deleted. This might have a performance cost. This normally only enable shift but it also needs to disable shift when the cursor moves.
Diffstat (limited to 'srcs/juloo.keyboard2/Autocapitalisation.java')
-rw-r--r--srcs/juloo.keyboard2/Autocapitalisation.java119
1 files changed, 119 insertions, 0 deletions
diff --git a/srcs/juloo.keyboard2/Autocapitalisation.java b/srcs/juloo.keyboard2/Autocapitalisation.java
new file mode 100644
index 0000000..1affdab
--- /dev/null
+++ b/srcs/juloo.keyboard2/Autocapitalisation.java
@@ -0,0 +1,119 @@
+package juloo.keyboard2;
+
+import android.text.InputType;
+import android.text.TextUtils;
+import android.view.inputmethod.EditorInfo;
+import android.view.inputmethod.InputConnection;
+import android.view.KeyEvent;
+
+final class Autocapitalisation
+{
+ private boolean _enabled = false;
+ private boolean _beginning_of_sentence = false;
+
+ /** Keep track of the cursor to differentiate 'selection_updated' events
+ corresponding to typing from cursor movement. */
+ private int _cursor = 0;
+
+ public boolean should_enable_shift()
+ {
+ return _enabled && _beginning_of_sentence;
+ }
+
+ /** Returns [true] if shift should be on initially. The input connection
+ isn't stored. */
+ public void started(EditorInfo info, InputConnection ic)
+ {
+ if ((info.inputType & InputType.TYPE_TEXT_FLAG_CAP_SENTENCES) == 0)
+ {
+ _enabled = false;
+ return;
+ }
+ _enabled = true;
+ _beginning_of_sentence = ((info.initialCapsMode & TextUtils.CAP_MODE_SENTENCES) != 0);
+ _cursor = 0; // Just a guess
+ scan_text_before_cursor(10, ic);
+ }
+
+ public void typed(CharSequence c)
+ {
+ for (int i = 0; i < c.length(); i++)
+ typed(c.charAt(i));
+ }
+
+ public void typed(char c)
+ {
+ _cursor++;
+ if (is_beginning_of_sentence(c))
+ _beginning_of_sentence = true;
+ else if (!ignore_at_beginning_of_sentence(c))
+ _beginning_of_sentence = false;
+ }
+
+ public void selection_updated(int old_cursor, int new_cursor, InputConnection ic)
+ {
+ if (new_cursor == _cursor)
+ return;
+ // Text has been inserted
+ if (old_cursor == _cursor && new_cursor > old_cursor)
+ {
+ scan_text_before_cursor(Math.min(new_cursor - old_cursor, 10), ic);
+ }
+ else
+ {
+ // Cursor has moved or [_cursor] wasn't uptodate
+ _beginning_of_sentence = false;
+ scan_text_before_cursor(10, ic);
+ }
+ _cursor = new_cursor;
+ }
+
+ /** Updates [_cursor]. */
+ private void scan_text_before_cursor(int range, InputConnection ic)
+ {
+ if (!_enabled) // Don't query characters if disabled
+ return;
+ CharSequence text_before = ic.getTextBeforeCursor(range, 0);
+ if (text_before == null)
+ {
+ _beginning_of_sentence = false;
+ }
+ else
+ {
+ _beginning_of_sentence = true;
+ typed(text_before);
+ }
+ }
+
+ private boolean ignore_at_beginning_of_sentence(char c)
+ {
+ switch (c)
+ {
+ case ' ':
+ case '"':
+ case '\'':
+ case '(':
+ case '«':
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ private boolean is_beginning_of_sentence(char c)
+ {
+ switch (c)
+ {
+ case '.':
+ case ';':
+ case '\n':
+ case '!':
+ case '?':
+ case '¿':
+ case '¡':
+ return true;
+ default:
+ return false;
+ }
+ }
+}