abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/Autocapitalisation.java
blob: ec730d54d9c5a968415a04c7e7e48e87898eca61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package juloo.keyboard2;

import android.os.Handler;
import android.text.InputType;
import android.text.TextUtils;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.KeyEvent;

public final class Autocapitalisation
{
  boolean _enabled = false;
  boolean _should_enable_shift = false;
  boolean _should_disable_shift = false;
  boolean _should_update_caps_mode = false;

  Handler _handler;
  InputConnection _ic;
  Callback _callback;
  int _caps_mode;

  /** Keep track of the cursor to recognize cursor movements from typing. */
  int _cursor;

  static int SUPPORTED_CAPS_MODES =
    InputType.TYPE_TEXT_FLAG_CAP_SENTENCES |
    InputType.TYPE_TEXT_FLAG_CAP_WORDS;

  public Autocapitalisation(Handler h, Callback cb)
  {
    _handler = h;
    _callback = cb;
  }

  /**
   * The events are: started, typed, event sent, selection updated
   * [started] does initialisation work and must be called before any other
   * event.
   */
  public void started(EditorInfo info, InputConnection ic)
  {
    _ic = ic;
    _caps_mode = info.inputType & TextUtils.CAP_MODE_SENTENCES;
    if (!Config.globalConfig().autocapitalisation || _caps_mode == 0)
    {
      _enabled = false;
      return;
    }
    _enabled = true;
    _should_enable_shift = (info.initialCapsMode != 0);
    _should_update_caps_mode = started_should_update_state(info.inputType);
    callback_now(true);
  }

  public void typed(CharSequence c)
  {
    for (int i = 0; i < c.length(); i++)
      type_one_char(c.charAt(i));
    callback(false);
  }

  public void event_sent(int code, int meta)
  {
    if (meta != 0)
    {
      _should_enable_shift = false;
      _should_update_caps_mode = false;
      return;
    }
    switch (code)
    {
      case KeyEvent.KEYCODE_DEL:
        if (_cursor > 0) _cursor--;
        _should_update_caps_mode = true;
        break;
      case KeyEvent.KEYCODE_ENTER:
        _should_update_caps_mode = true;
        break;
    }
    callback(true);
  }

  public void stop()
  {
    _should_enable_shift = false;
    _should_update_caps_mode = false;
    callback_now(true);
  }

  /** Pause auto capitalisation until [unpause()] is called. */
  public boolean pause()
  {
    boolean was_enabled = _enabled;
    stop();
    _enabled = false;
    return was_enabled;
  }

  /** Continue auto capitalisation after [pause()] was called. Argument is the
      output of [pause()]. */
  public void unpause(boolean was_enabled)
  {
    _enabled = was_enabled;
    _should_update_caps_mode = true;
    callback_now(true);
  }

  public static interface Callback
  {
    public void update_shift_state(boolean should_enable, boolean should_disable);
  }

  /** Returns [true] if shift might be disabled. */
  public void selection_updated(int old_cursor, int new_cursor)
  {
    if (new_cursor == _cursor) // Just typing
      return;
    if (new_cursor == 0 && _ic != null)
    {
      // Detect whether the input box has been cleared
      CharSequence t = _ic.getTextAfterCursor(1, 0);
      if (t != null && t.equals(""))
        _should_update_caps_mode = true;
    }
    _cursor = new_cursor;
    _should_enable_shift = false;
    callback(true);
  }

  Runnable delayed_callback = new Runnable()
  {
    public void run()
    {
      if (_should_update_caps_mode && _ic != null)
      {
        _should_enable_shift = _enabled && (_ic.getCursorCapsMode(_caps_mode) != 0);
        _should_update_caps_mode = false;
      }
      _callback.update_shift_state(_should_enable_shift, _should_disable_shift);
    }
  };

  /** Update the shift state if [_should_update_caps_mode] is true, then call
      [_callback.update_shift_state]. This is done after a short delay to wait
      for the editor to handle the events, as this might be called before the
      corresponding event is sent. */
  void callback(boolean might_disable)
  {
    _should_disable_shift = might_disable;
    // The callback must be delayed because [getCursorCapsMode] would sometimes
    // be called before the editor finished handling the previous event.
    _handler.postDelayed(delayed_callback, 50);
  }

  /** Like [callback] but runs immediately. */
  void callback_now(boolean might_disable)
  {
    _should_disable_shift = might_disable;
    delayed_callback.run();
  }

  void type_one_char(char c)
  {
    _cursor++;
    if (is_trigger_character(c))
      _should_update_caps_mode = true;
    else
      _should_enable_shift = false;
  }

  boolean is_trigger_character(char c)
  {
    switch (c)
    {
      case ' ':
        return true;
      default:
        return false;
    }
  }

  /** Whether the caps state should be updated when input starts. [inputType]
      is the field from the editor info object. */
  boolean started_should_update_state(int inputType)
  {
    int class_ = inputType & InputType.TYPE_MASK_CLASS;
    int variation = inputType & InputType.TYPE_MASK_VARIATION;
    if (class_ != InputType.TYPE_CLASS_TEXT)
      return false;
    switch (variation)
    {
      case InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE:
      case InputType.TYPE_TEXT_VARIATION_NORMAL:
      case InputType.TYPE_TEXT_VARIATION_PERSON_NAME:
      case InputType.TYPE_TEXT_VARIATION_SHORT_MESSAGE:
      case InputType.TYPE_TEXT_VARIATION_EMAIL_SUBJECT:
      case InputType.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT:
        return true;
      default:
        return false;
    }
  }
}