abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/Autocapitalisation.java
blob: c23509cf29d1dc67a43c7d4fa1ecf1bb8fcb6ca7 (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
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 _should_enable_shift = false;

  private InputConnection _ic;
  private int _caps_mode;

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

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

  public boolean should_enable_shift()
  {
    return _should_enable_shift;
  }

  /** Returns [true] if shift should be on initially. The input connection
      isn't stored. */
  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);
  }

  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_trigger_character(c))
      update_caps_mode();
    else
      _should_enable_shift = false;
  }

  public void event_sent(int code)
  {
    switch (code)
    {
      case KeyEvent.KEYCODE_DEL:
        _cursor--;
        update_caps_mode();
        break;
    }
  }

  /** Returns [true] if shift might be disabled. */
  public boolean selection_updated(int old_cursor, int new_cursor)
  {
    if (new_cursor == _cursor) // Just typing
      return false;
    _cursor = new_cursor;
    _should_enable_shift = false;
    return true;
  }

  private void update_caps_mode()
  {
    _should_enable_shift = _enabled && (_ic.getCursorCapsMode(_caps_mode) != 0);
  }

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