CustomInputConnection.java 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. package autosuggest;
  19. import android.util.Log;
  20. import android.view.KeyEvent;
  21. import android.view.View;
  22. import android.view.inputmethod.BaseInputConnection;
  23. import android.inputmethodservice.InputMethodService;
  24. // setComposingText() - change text being composed
  25. // See 30b299cb04b4ba2330ef61a8a24c1e58513a0af2
  26. // content/public/android/java/src/org/chromium/content/browser/input/AdapterInputConnection.java
  27. public class CustomInputConnection extends BaseInputConnection {
  28. native static void setup();
  29. native static void onCommitText(String text);
  30. native static void onEndEdit(String text);
  31. public CustomInputConnection(View view, boolean fullEditor) {
  32. super(view, fullEditor);
  33. setup();
  34. }
  35. @Override
  36. public boolean sendKeyEvent(KeyEvent event) {
  37. int action = event.getAction();
  38. int keycode = event.getKeyCode();
  39. // If this is backspace/del or if the key has a character representation,
  40. // need to update the underlying Editable (i.e. the local representation of the text
  41. // being edited). Some IMEs like Jellybean stock IME and Samsung IME mix in delete
  42. // KeyPress events instead of calling deleteSurroundingText.
  43. if (action == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_DEL) {
  44. deleteSurroundingText(1, 0);
  45. //String text = getTextBeforeCursor(100, 0).toString();
  46. //text = text.substring(0, text.length() - 1);
  47. //setComposingText(text, 1);
  48. } else if (action == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_FORWARD_DEL) {
  49. deleteSurroundingText(0, 1);
  50. } else if (action == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_ENTER) {
  51. setComposingText("", 0);
  52. // Chromium does this but the above seems to work too.
  53. //beginBatchEdit();
  54. //finishComposingText();
  55. //endBatchEdit();
  56. }
  57. return true;
  58. }
  59. @Override
  60. public boolean commitText(CharSequence text, int newCursorPosition) {
  61. //Log.i("darkfi", String.format("commitText(%s, %d)", text.toString(), newCursorPosition));
  62. onCommitText(text.toString());
  63. return super.commitText(text, newCursorPosition);
  64. }
  65. @Override
  66. public boolean endBatchEdit() {
  67. //Log.i("darkfi", "endBatchEdit: " + curr);
  68. String text = getTextBeforeCursor(100, 0).toString();
  69. onEndEdit(text);
  70. return super.endBatchEdit();
  71. }
  72. }