InvisibleInputView.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 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.content.Context;
  20. import android.graphics.Rect;
  21. import android.text.Editable;
  22. import android.text.Selection;
  23. import android.util.Log;
  24. import android.view.KeyEvent;
  25. import android.view.View;
  26. import android.view.ViewGroup;
  27. import android.view.inputmethod.BaseInputConnection;
  28. import android.view.inputmethod.EditorInfo;
  29. import android.view.inputmethod.InputConnection;
  30. import autosuggest.CustomInputConnection;
  31. public class InvisibleInputView extends View {
  32. public CustomInputConnection inputConnection;
  33. public int id = -1;
  34. public Editable editable;
  35. native static void onCreateInputConnect(int id);
  36. public InvisibleInputView(Context ctx, int id) {
  37. super(ctx);
  38. setFocusable(true);
  39. setFocusableInTouchMode(true);
  40. //setVisibility(INVISIBLE);
  41. setVisibility(VISIBLE);
  42. //setAlpha(0f);
  43. setLayoutParams(new ViewGroup.LayoutParams(400, 200));
  44. this.id = id;
  45. editable = Editable.Factory.getInstance().newEditable("");
  46. Selection.setSelection(editable, 0);
  47. }
  48. // Maybe move CustomInputConnection.setEditableText() to here?
  49. // For now this is called when the InputConnection is not yet available.
  50. public void setEditableText(String text) {
  51. editable.replace(0, editable.length(), text);
  52. Selection.setSelection(editable, text.length(), text.length());
  53. }
  54. // Same as above
  55. public void setSelection(int start, int end) {
  56. Selection.setSelection(editable, start, end);
  57. }
  58. @Override
  59. protected void onAttachedToWindow() {
  60. super.onAttachedToWindow();
  61. Log.d("darkfi", "InvisibleInputView " + id + " attached to window");
  62. }
  63. @Override
  64. public boolean onCheckIsTextEditor() {
  65. Log.d("darkfi", "onCheckIsTextEditor");
  66. return true;
  67. }
  68. @Override
  69. public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
  70. Log.d("darkfi", "Create InputConnection for view=" + this.toString());
  71. // Losing focus requires the inputConnection to be destroyed
  72. //if (inputConnection != null) {
  73. // Log.d("darkfi", " -> return existing InputConnection");
  74. // return inputConnection;
  75. //}
  76. outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT
  77. | EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT;
  78. //| EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT;
  79. outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN
  80. //| EditorInfo.IME_ACTION_NONE;
  81. | EditorInfo.IME_ACTION_GO;
  82. outAttrs.initialSelStart = getSelectionStart();
  83. outAttrs.initialSelEnd = getSelectionEnd();
  84. //if (outAttrs.initialSelStart != 0) {
  85. // Log.d("darkfi", " select: [" + outAttrs.initialSelStart + ", " +
  86. // outAttrs.initialSelEnd + "]");
  87. //}
  88. inputConnection = new CustomInputConnection(id, editable, this);
  89. onCreateInputConnect(id);
  90. return inputConnection;
  91. }
  92. @Override
  93. protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
  94. super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
  95. Log.d("darkfi", "onFocusChanged: " + gainFocus);
  96. }
  97. @Override
  98. public boolean onKeyDown(int keyCode, KeyEvent event) {
  99. Log.d("darkfi", "onKeyDown(" + keyCode + ", " + event + ")");
  100. // Copied from CustomInputConnection
  101. // Seems only the down event is sent.
  102. int selectionStart = Selection.getSelectionStart(editable);
  103. if (event.getAction() == KeyEvent.ACTION_DOWN) {
  104. if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
  105. if (selectionStart > 0) {
  106. editable.delete(selectionStart - 1, selectionStart);
  107. CustomInputConnection.onDeleteSurroundingText(id, 1, 0);
  108. }
  109. } else if (event.getKeyCode() == KeyEvent.KEYCODE_FORWARD_DEL) {
  110. if (selectionStart < editable.length()) {
  111. editable.delete(selectionStart, selectionStart + 1);
  112. CustomInputConnection.onDeleteSurroundingText(id, 0, 1);
  113. }
  114. } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
  115. if (selectionStart > 0) {
  116. Selection.setSelection(editable, selectionStart - 1);
  117. CustomInputConnection.onSetComposeRegion(
  118. id, selectionStart - 1, selectionStart);
  119. }
  120. } else if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
  121. if (selectionStart < editable.length()) {
  122. Selection.setSelection(editable, selectionStart + 1);
  123. CustomInputConnection.onSetComposeRegion(
  124. id, selectionStart + 1, selectionStart + 2);
  125. }
  126. } else {
  127. int unicodeChar = event.getUnicodeChar();
  128. if (unicodeChar != 0) {
  129. int selectionEnd = Selection.getSelectionEnd(editable);
  130. if (selectionStart > selectionEnd) {
  131. int temp = selectionStart;
  132. selectionStart = selectionEnd;
  133. selectionEnd = temp;
  134. }
  135. String inputChar = Character.toString((char)unicodeChar);
  136. Log.d("darkfi", "-> " + inputChar + " [" + selectionStart + ", " + selectionEnd + "]");
  137. editable.replace(selectionStart, selectionEnd, inputChar);
  138. CustomInputConnection.onCompose(
  139. id, inputChar, selectionStart, true);
  140. }
  141. }
  142. }
  143. return super.onKeyDown(keyCode, event);
  144. }
  145. public String debugEditableStr() {
  146. return CustomInputConnection.editableToXml(editable);
  147. }
  148. public String rawText() {
  149. return editable.toString();
  150. }
  151. public int getSelectionStart() {
  152. return Selection.getSelectionStart(editable);
  153. }
  154. public int getSelectionEnd() {
  155. return Selection.getSelectionEnd(editable);
  156. }
  157. public int getComposeStart() {
  158. return BaseInputConnection.getComposingSpanStart(editable);
  159. }
  160. public int getComposeEnd() {
  161. return BaseInputConnection.getComposingSpanEnd(editable);
  162. }
  163. }