summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/javispedro/vndroid/KeyEventOutput.java
blob: 3ec4319c90fb5e585eff80fae16045ba0405d68c (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.javispedro.vndroid;

import android.os.Bundle;
import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.Nullable;

import com.javispedro.vndroid.keymaps.KeyActionHandler;
import com.javispedro.vndroid.keymaps.KeyHandler;

import java.util.ArrayList;
import java.util.List;

public class KeyEventOutput implements KeyActionHandler {
    private final String TAG = KeyEventOutput.class.getSimpleName();

    private final List<KeyHandler> handlers = new ArrayList<KeyHandler>();

    @Nullable
    private AccessibilityNodeInfo getFocusNode() {
        ControlService service = ControlService.getInstance();
        if (service != null) {
            return service.findFocus(AccessibilityNodeInfo.FOCUS_INPUT);
        }
        return null;
    }

    public void addHandler(KeyHandler handler) {
        handler.setActionHandler(this);
        handlers.add(handler);
    }

    public void postKeyEvent(int key, boolean state) {
        if (state) Log.d(TAG, "keysym pressed: " + Integer.toHexString(key));
        for (KeyHandler handler : handlers) {
            boolean handled;
            if (state) {
                handled = handler.keyDown(key);
            } else {
                handled = handler.keyUp(key);
            }
            if (handled) {
                break;
            }
        }
    }

    @Override
    public void postText(CharSequence text) {
        AccessibilityNodeInfo node = getFocusNode();
        if (node == null || !node.isEditable()) {
            Log.d(TAG, "no input focus or not editable");
            return;
        }

        final CharSequence curText = node.getText();
        final int curTextLen = curText != null ? curText.length() : 0;
        final int textLen = text.length();
        int selStart = node.getTextSelectionStart();
        int selEnd = node.getTextSelectionEnd();
        StringBuilder builder = new StringBuilder(curTextLen + textLen);

        Log.d(TAG, " cur text: " + curText + " start=" + selStart + " end=" + selEnd);

        if (selStart == -1 || selEnd == -1 || curTextLen == 0) {
            // No selection, no cursor
            if (curTextLen > 0) builder.append(curText);
            builder.append(text);
        } else {
            builder.append(curText.subSequence(0, selStart));
            builder.append(text);
            builder.append(curText.subSequence(selEnd, curText.length()));

            selStart = selEnd + textLen;
            selEnd = selStart;
        }

        Log.d(TAG, " new text: " + builder.toString() + " start=" + selStart + " end=" + selEnd);

        Bundle args = new Bundle();
        args.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
                builder.toString());
        node.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, args);

        if (selStart != -1 && selEnd != -1 && selStart != builder.length()) {
            args = new Bundle();
            args.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT, selStart);
            args.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT, selEnd);
            node.performAction(AccessibilityNodeInfo.ACTION_SET_SELECTION, args);
        }
    }

    @Override
    public void postChar(char c) {
        AccessibilityNodeInfo node = getFocusNode();
        if (node == null || !node.isEditable()) {
            Log.d(TAG, "no input focus or not editable");
            return;
        }

        final CharSequence curText = node.getText();
        final int curTextLen = curText != null ? curText.length() : 0;
        int selStart = node.getTextSelectionStart();
        int selEnd = node.getTextSelectionEnd();
        StringBuilder builder = new StringBuilder(curTextLen + 1);

        Log.d(TAG, " cur text: " + curText + " start=" + selStart + " end=" + selEnd);

        if (selStart == -1 || selEnd == -1 || curTextLen == 0) {
            // No selection, no cursor
            if (curTextLen > 0) builder.append(curText);
            builder.append(c);
        } else {
            builder.append(curText.subSequence(0, selStart));
            builder.append(c);
            builder.append(curText.subSequence(selEnd, curText.length()));

            selStart = selEnd + 1;
            selEnd = selStart;
        }

        Log.d(TAG, " new text: " + builder.toString() + " start=" + selStart + " end=" + selEnd);

        Bundle args = new Bundle();
        args.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
                builder.toString());
        node.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, args);

        if (selStart != -1 && selEnd != -1 && selStart != builder.length()) {
            args = new Bundle();
            args.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT, selStart);
            args.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT, selEnd);
            node.performAction(AccessibilityNodeInfo.ACTION_SET_SELECTION, args);
        }
    }

    @Override
    public void postLocalAction(int action) {
        AccessibilityNodeInfo node = getFocusNode();
        if (node == null || !node.isEditable()) {
            Log.d(TAG, "no input focus or not editable");
            return;
        }

        final CharSequence curText = node.getText();
        final int curTextLen = curText != null ? curText.length() : 0;
        int selStart = node.getTextSelectionStart();
        int selEnd = node.getTextSelectionEnd();
        StringBuilder builder = null;

        Log.d(TAG, " cur text: " + curText + " start=" + selStart + " end=" + selEnd);

        switch (action) {
            case KeyActionHandler.ACTION_BACKSPACE:
                if (selStart == -1 || selEnd == -1 || curTextLen == 0) {
                    return;
                }
                builder = new StringBuilder(curTextLen - 1);
                if (selStart == selEnd && selStart > 0) {
                    builder.append(curText.subSequence(0, selStart - 1));
                    builder.append(curText.subSequence(selEnd, curTextLen));
                    selStart = selStart - 1;
                    selEnd = selStart;
                } else if (selStart != selEnd) {
                    builder.append(curText.subSequence(0, selStart));
                    builder.append(curText.subSequence(selEnd, curTextLen));
                    selEnd = selStart;
                } else {
                    return;
                }
                break;
            case KeyActionHandler.ACTION_DEL:
                if (selStart == -1 || selEnd == -1 || curTextLen == 0) {
                    return;
                }
                builder = new StringBuilder(curTextLen - 1);
                if (selStart == selEnd && selStart < curTextLen) {
                    builder.append(curText.subSequence(0, selStart));
                    builder.append(curText.subSequence(selEnd + 1, curTextLen));
                    selStart = selStart;
                    selEnd = selStart;
                } else if (selStart != selEnd) {
                    builder.append(curText.subSequence(0, selStart));
                    builder.append(curText.subSequence(selEnd, curTextLen));
                    selEnd = selStart;
                } else {
                    return;
                }
                break;
            case KeyActionHandler.ACTION_MOVE_LEFT:
                if (selStart == -1 || selEnd == -1 || curTextLen == 0) {
                    return;
                }
                if (selStart > 0) {
                    selStart = selStart - 1;
                    selEnd = selStart;
                } else {
                    return;
                }
                break;
            case KeyActionHandler.ACTION_MOVE_RIGHT:
                if (selStart == -1 || selEnd == -1 || curTextLen == 0) {
                    return;
                }
                if (selEnd < curTextLen) {
                    selStart = selEnd + 1;
                    selEnd = selStart;
                } else {
                    return;
                }
                break;
            case KeyActionHandler.ACTION_MOVE_LEFTMOST:
                selStart = 0;
                selEnd = 0;
                break;
            case KeyActionHandler.ACTION_MOVE_RIGHTMOST:
                if (curTextLen == 0) {
                    return;
                }
                selStart = curTextLen;
                selEnd = curTextLen;
                break;
            case KeyActionHandler.ACTION_SELECT_LEFT:
                if (selStart == -1 || selEnd == -1 || curTextLen == 0) {
                    return;
                }
                if (selStart > 0) {
                    selStart = selStart - 1;
                } else {
                    return;
                }
                break;
            case KeyActionHandler.ACTION_SELECT_RIGHT:
                if (selStart == -1 || selEnd == -1 || curTextLen == 0) {
                    return;
                }
                if (selEnd < curTextLen) {
                    selEnd = selEnd + 1;
                } else {
                    return;
                }
                break;
            case KeyActionHandler.ACTION_SELECT_LEFTMOST:
                selStart = 0;
                break;
            case KeyActionHandler.ACTION_SELECT_RIGHTMOST:
                if (curTextLen == 0) {
                    return;
                }
                selEnd = curTextLen;
                break;
            default:
                return;
        }

        if (builder != null) {
            Log.d(TAG, " new text: " + builder.toString() + " start=" + selStart + " end=" + selEnd);
        } else {
            Log.d(TAG, " new  start=" + selStart + " end=" + selEnd);
        }

        if (builder != null && builder.toString() != curText) {
            Bundle args = new Bundle();
            args.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,
                    builder.toString());
            node.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, args);
        }

        if (selStart != -1 && selEnd != -1 && (builder == null || selStart != builder.length())) {
            Bundle args = new Bundle();
            args.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT, selStart);
            args.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT, selEnd);
            node.performAction(AccessibilityNodeInfo.ACTION_SET_SELECTION, args);
        }
    }

    @Override
    public void postGlobalAction(int action) {
        ControlService service = ControlService.getInstance();
        if (service != null) {
            if (!service.performGlobalAction(action)) {
                Log.e(TAG, "could not perform global action: " + action);
            }
        }
    }
}