|
| 1 | +package juloo.keyboard2; |
| 2 | + |
| 3 | +import android.os.Handler; |
| 4 | +import android.view.KeyEvent; |
| 5 | +import android.view.inputmethod.EditorInfo; |
| 6 | +import android.view.inputmethod.InputConnection; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +/** Keep track of the word being typed. */ |
| 10 | +public final class CurrentlyTypedWord |
| 11 | +{ |
| 12 | + InputConnection _ic = null; |
| 13 | + Handler _handler; |
| 14 | + Callback _callback; |
| 15 | + |
| 16 | + /** The currently typed word. */ |
| 17 | + StringBuilder _w = new StringBuilder(); |
| 18 | + /** This can be disabled if the editor doesn't support looking at the text |
| 19 | + before the cursor. */ |
| 20 | + boolean _enabled = false; |
| 21 | + /** The current word is empty while the selection is ongoing. */ |
| 22 | + boolean _has_selection = false; |
| 23 | + /** Used to avoid concurrent refreshes in [delayed_refresh()]. */ |
| 24 | + boolean _refresh_pending = false; |
| 25 | + |
| 26 | + /** The estimated cursor position. Used to avoid expensive IPC calls when the |
| 27 | + typed word can be estimated locally with [typed]. When the cursor |
| 28 | + position gets out of sync, the text before the cursor is queried again to |
| 29 | + the editor. */ |
| 30 | + int _cursor; |
| 31 | + |
| 32 | + public CurrentlyTypedWord(Handler h, Callback cb) |
| 33 | + { |
| 34 | + _handler = h; |
| 35 | + _callback = cb; |
| 36 | + } |
| 37 | + |
| 38 | + public String get() |
| 39 | + { |
| 40 | + return _w.toString(); |
| 41 | + } |
| 42 | + |
| 43 | + public void started(Config conf, InputConnection ic) |
| 44 | + { |
| 45 | + _ic = ic; |
| 46 | + _enabled = true; |
| 47 | + EditorConfig e = conf.editor_config; |
| 48 | + _has_selection = e.initial_sel_start != e.initial_sel_end; |
| 49 | + _cursor = e.initial_sel_start; |
| 50 | + if (!_has_selection) |
| 51 | + set_current_word(e.initial_text_before_cursor); |
| 52 | + } |
| 53 | + |
| 54 | + public void typed(String s) |
| 55 | + { |
| 56 | + if (!_enabled) |
| 57 | + return; |
| 58 | + _has_selection = false; |
| 59 | + type_chars(s); |
| 60 | + callback(); |
| 61 | + } |
| 62 | + |
| 63 | + public void selection_updated(int oldSelStart, int newSelStart, int newSelEnd) |
| 64 | + { |
| 65 | + // Avoid the expensive [refresh_current_word] call when [typed] was called |
| 66 | + // before. |
| 67 | + boolean new_has_sel = newSelStart != newSelEnd; |
| 68 | + if (!_enabled || (newSelStart == _cursor && new_has_sel == _has_selection)) |
| 69 | + return; |
| 70 | + _has_selection = new_has_sel; |
| 71 | + _cursor = newSelStart; |
| 72 | + refresh_current_word(); |
| 73 | + } |
| 74 | + |
| 75 | + public void event_sent(int code, int meta) |
| 76 | + { |
| 77 | + if (!_enabled) |
| 78 | + return; |
| 79 | + delayed_refresh(); |
| 80 | + } |
| 81 | + |
| 82 | + void callback() |
| 83 | + { |
| 84 | + _callback.currently_typed_word(_w.toString()); |
| 85 | + } |
| 86 | + |
| 87 | + /** Estimate the currently typed word after [chars] has been typed. */ |
| 88 | + void type_chars(String s) |
| 89 | + { |
| 90 | + int len = s.length(); |
| 91 | + for (int i = 0; i < len;) |
| 92 | + { |
| 93 | + int c = s.codePointAt(i); |
| 94 | + if (Character.isLetter(c)) |
| 95 | + _w.appendCodePoint(c); |
| 96 | + else |
| 97 | + _w.setLength(0); |
| 98 | + _cursor++; |
| 99 | + i += Character.charCount(c); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** Refresh the current word by immediately querying the editor. */ |
| 104 | + void refresh_current_word() |
| 105 | + { |
| 106 | + _refresh_pending = false; |
| 107 | + if (_has_selection) |
| 108 | + set_current_word(""); |
| 109 | + else |
| 110 | + set_current_word(_ic.getTextBeforeCursor(10, 0)); |
| 111 | + } |
| 112 | + |
| 113 | + /** Refresh the current word by immediately querying the editor. */ |
| 114 | + void set_current_word(CharSequence text_before_cursor) |
| 115 | + { |
| 116 | + if (text_before_cursor == null) |
| 117 | + { |
| 118 | + _enabled = false; |
| 119 | + return; |
| 120 | + } |
| 121 | + _w.setLength(0); |
| 122 | + int saved_cursor = _cursor; |
| 123 | + type_chars(text_before_cursor.toString()); |
| 124 | + _cursor = saved_cursor; |
| 125 | + callback(); |
| 126 | + } |
| 127 | + |
| 128 | + /** Wait some time to let the editor finishes reacting to changes and call |
| 129 | + [refresh_current_word]. */ |
| 130 | + void delayed_refresh() |
| 131 | + { |
| 132 | + _refresh_pending = true; |
| 133 | + _handler.postDelayed(delayed_refresh_run, 50); |
| 134 | + } |
| 135 | + |
| 136 | + Runnable delayed_refresh_run = new Runnable() |
| 137 | + { |
| 138 | + public void run() |
| 139 | + { |
| 140 | + if (_refresh_pending) |
| 141 | + refresh_current_word(); |
| 142 | + } |
| 143 | + }; |
| 144 | + |
| 145 | + public static interface Callback |
| 146 | + { |
| 147 | + public void currently_typed_word(String word); |
| 148 | + } |
| 149 | +} |
0 commit comments