summary refs log tree commit diff
path: root/android/app/src/main/java/com/classicube/CCView.java
blob: b42d924c06043711e1adef25d608838d60166650 (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
package com.classicube;
import android.text.Editable;
import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;

public class CCView extends SurfaceView {
    SpannableStringBuilder kbText;
    MainActivity activity;

    public CCView(MainActivity activity) {
        // setFocusable, setFocusableInTouchMode - API level 1
        super(activity);
        this.activity = activity;
        setFocusable(true);
        setFocusableInTouchMode(true);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return activity.handleTouchEvent(ev) || super.dispatchTouchEvent(ev);
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo attrs) {
        // BaseInputConnection, IME_ACTION_GO, IME_FLAG_NO_EXTRACT_UI - API level 3
        attrs.actionLabel = null;
        attrs.inputType   = MainActivity.calcKeyboardType(activity.keyboardType);
        attrs.imeOptions  = MainActivity.calcKeyboardOptions(activity.keyboardType);

        kbText = new SpannableStringBuilder(activity.keyboardText);

        InputConnection ic = new BaseInputConnection(this, true) {
            boolean inited;

            void updateText() {
                activity.pushCmd(MainActivity.CMD_KEY_TEXT, kbText.toString());
            }

            @Override
            public Editable getEditable() {
                if (!inited) {
                    // needed to set selection, otherwise random crashes later with backspacing
                    // set selection to end, so backspacing after opening keyboard with text still works
                    Selection.setSelection(kbText, kbText.toString().length());
                    inited = true;
                }
                return kbText;
            }

            @Override
            public boolean setComposingText(CharSequence text, int newCursorPosition) {
                boolean success = super.setComposingText(text, newCursorPosition);
                updateText();
                return success;
            }

            @Override
            public boolean deleteSurroundingText(int beforeLength, int afterLength) {
                boolean success = super.deleteSurroundingText(beforeLength, afterLength);
                updateText();
                return success;
            }

            @Override
            public boolean commitText(CharSequence text, int newCursorPosition) {
                boolean success = super.commitText(text, newCursorPosition);
                updateText();
                return success;
            }

            @Override
            public boolean sendKeyEvent(KeyEvent ev) {
                // getSelectionStart - API level 1
                if (ev.getAction() != KeyEvent.ACTION_DOWN) return super.sendKeyEvent(ev);
                int code = ev.getKeyCode();
                int uni = ev.getUnicodeChar();

                // start is -1 sometimes, and trying to insert/delete there crashes
                int start = Selection.getSelectionStart(kbText);
                if (start == -1) start = kbText.toString().length();

                if (code == KeyEvent.KEYCODE_ENTER) {
                    // enter maps to \n but that should not be intercepted
                } else if (code == KeyEvent.KEYCODE_DEL) {
                    if (start <= 0) return false;
                    kbText.delete(start - 1, start);
                    updateText();
                    return false;
                } else if (uni != 0) {
                    kbText.insert(start, String.valueOf((char) uni));
                    updateText();
                    return false;
                }
                return super.sendKeyEvent(ev);
            }

        };
        //String text = MainActivity.this.keyboardText;
        //if (text != null) ic.setComposingText(text, 0);
        return ic;
    }
}