|
15 | 15 | import android.text.TextWatcher;
|
16 | 16 | import android.util.AttributeSet;
|
17 | 17 | import android.util.Log;
|
| 18 | +import android.view.KeyCharacterMap; |
| 19 | +import android.view.KeyEvent; |
18 | 20 | import android.view.MotionEvent;
|
19 | 21 | import android.view.View;
|
20 | 22 | import android.view.ViewGroup;
|
21 | 23 | import android.view.inputmethod.EditorInfo;
|
22 | 24 | import android.view.inputmethod.ExtractedText;
|
23 | 25 | import android.view.inputmethod.ExtractedTextRequest;
|
24 | 26 | import android.view.inputmethod.InputConnection;
|
| 27 | +import android.widget.EditText; |
25 | 28 | import android.widget.ImageButton;
|
26 | 29 | import android.widget.ImageView;
|
27 | 30 | import android.widget.LinearLayout;
|
@@ -309,6 +312,10 @@ private void resetKeyboardLayout() {
|
309 | 312 | updateCandidates();
|
310 | 313 | }
|
311 | 314 |
|
| 315 | + private boolean isAttachToWindowWidget() { |
| 316 | + return mFocusedView instanceof WindowWidget; |
| 317 | + } |
| 318 | + |
312 | 319 | public void updateFocusedView(View aFocusedView) {
|
313 | 320 | if (mFocusedView != null && mFocusedView instanceof TextView) {
|
314 | 321 | ((TextView)mFocusedView).removeTextChangedListener(this);
|
@@ -910,6 +917,59 @@ private void displayComposingText(String aText, ComposingAction aAction) {
|
910 | 917 | }
|
911 | 918 | }
|
912 | 919 |
|
| 920 | + private void moveCursor(final int direction) { |
| 921 | + EditText textView; |
| 922 | + if (mFocusedView != null && mFocusedView instanceof EditText) { |
| 923 | + textView = (EditText)mFocusedView; |
| 924 | + final int cursor = textView.getSelectionStart() + direction; |
| 925 | + if ((cursor <= textView.length()) && (cursor >= 0)) { |
| 926 | + textView.setSelection(cursor); |
| 927 | + } |
| 928 | + } |
| 929 | + } |
| 930 | + |
| 931 | + @Override |
| 932 | + public boolean dispatchKeyEvent(final KeyEvent event) { |
| 933 | + final int keyCode = event.getKeyCode(); |
| 934 | + final InputConnection connection = mInputConnection; |
| 935 | + if (connection != null) { |
| 936 | + if (isAttachToWindowWidget()) { |
| 937 | + connection.sendKeyEvent(event); |
| 938 | + hide(UIWidget.KEEP_WIDGET); |
| 939 | + return true; |
| 940 | + } |
| 941 | + // Android Components do not support InputConnection.sendKeyEvent() |
| 942 | + if (event.getAction() == KeyEvent.ACTION_DOWN) { |
| 943 | + Log.e("reb", "key = " + KeyEvent.keyCodeToString(keyCode)); |
| 944 | + |
| 945 | + switch (keyCode) { |
| 946 | + case KeyEvent.KEYCODE_DEL: |
| 947 | + handleBackspace(event.isLongPress()); |
| 948 | + return true; |
| 949 | + case KeyEvent.KEYCODE_ENTER: |
| 950 | + case KeyEvent.KEYCODE_NUMPAD_ENTER: |
| 951 | + handleDone(); |
| 952 | + return true; |
| 953 | + case KeyEvent.KEYCODE_DPAD_LEFT: |
| 954 | + moveCursor(-1); |
| 955 | + return true; |
| 956 | + case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 957 | + moveCursor(1); |
| 958 | + return true; |
| 959 | + default: |
| 960 | + break; |
| 961 | + } |
| 962 | + if (event.getUnicodeChar() != 0) { |
| 963 | + KeyCharacterMap map = event.getKeyCharacterMap(); |
| 964 | + String value = String.valueOf((char) map.get(keyCode, event.getMetaState())); |
| 965 | + connection.commitText(value, 1); |
| 966 | + return true; |
| 967 | + } |
| 968 | + } |
| 969 | + } |
| 970 | + return false; |
| 971 | + } |
| 972 | + |
913 | 973 | // GeckoSession.TextInputDelegate
|
914 | 974 |
|
915 | 975 | @Override
|
|
0 commit comments