blob: a055d3d8b3a0c76051068ea3d4d686279021a489 (
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
|
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;
/** Used to avoid enabling shift after an arrow key is pressed. */
private boolean _skip_next_selection_update = 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 event_sent(int code)
{
switch (code)
{
// Disable temporarily after a keyboard cursor movement
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_PAGE_UP:
case KeyEvent.KEYCODE_PAGE_DOWN:
case KeyEvent.KEYCODE_MOVE_HOME:
case KeyEvent.KEYCODE_MOVE_END:
_skip_next_selection_update = true;
_beginning_of_sentence = false;
break;
}
}
/** Returns [true] if shift might be disabled. */
public boolean selection_updated(int old_cursor, int new_cursor, InputConnection ic)
{
if (_skip_next_selection_update)
{
_cursor = new_cursor;
_skip_next_selection_update = false;
return false;
}
if (new_cursor == _cursor)
return false;
// Text has been inserted or cursor moved forward
if (old_cursor == _cursor && new_cursor > old_cursor)
{
scan_text_before_cursor(Math.min(new_cursor - old_cursor, 10), ic);
return true;
}
else
{
// Cursor has moved backward or text deleted
_beginning_of_sentence = false;
scan_text_before_cursor(10, ic);
_cursor = new_cursor;
return true;
}
}
/** 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;
}
}
}
|