CustomInputConnection.java 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // Android is sending commit("foo") then edit("foo") events which is confusing.
  32. // We use this to skip edit("foo") when proceeded by the commit.
  33. private String lastCommitText;
  34. public CustomInputConnection(View view, boolean fullEditor) {
  35. super(view, fullEditor);
  36. lastCommitText = null;
  37. setup();
  38. }
  39. @Override
  40. public boolean sendKeyEvent(KeyEvent event) {
  41. int action = event.getAction();
  42. int keycode = event.getKeyCode();
  43. // If this is backspace/del or if the key has a character representation,
  44. // need to update the underlying Editable (i.e. the local representation of the text
  45. // being edited). Some IMEs like Jellybean stock IME and Samsung IME mix in delete
  46. // KeyPress events instead of calling deleteSurroundingText.
  47. if (action == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_DEL) {
  48. deleteSurroundingText(1, 0);
  49. //String text = getTextBeforeCursor(100, 0).toString();
  50. //text = text.substring(0, text.length() - 1);
  51. //setComposingText(text, 1);
  52. } else if (action == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_FORWARD_DEL) {
  53. deleteSurroundingText(0, 1);
  54. } else if (action == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_ENTER) {
  55. reset();
  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. lastCommitText = text.toString();
  63. onCommitText(lastCommitText);
  64. return super.commitText(text, newCursorPosition);
  65. }
  66. @Override
  67. public boolean endBatchEdit() {
  68. //Log.i("darkfi", "endBatchEdit: " + curr);
  69. String text = getTextBeforeCursor(100, 0).toString();
  70. if (!text.equals(lastCommitText))
  71. onEndEdit(text);
  72. lastCommitText = null;
  73. return super.endBatchEdit();
  74. }
  75. public void reset() {
  76. setComposingText("", 0);
  77. // Chromium does this but the above seems to work too.
  78. //beginBatchEdit();
  79. //finishComposingText();
  80. //endBatchEdit();
  81. }
  82. }