InvisibleInputView.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.View;
  25. import android.view.ViewGroup;
  26. import android.view.inputmethod.BaseInputConnection;
  27. import android.view.inputmethod.EditorInfo;
  28. import android.view.inputmethod.InputConnection;
  29. import autosuggest.CustomInputConnection;
  30. public class InvisibleInputView extends View {
  31. public CustomInputConnection inputConnection;
  32. public int id = -1;
  33. public Editable editable;
  34. native static void onCreateInputConnect(int id);
  35. public InvisibleInputView(Context ctx, int id) {
  36. super(ctx);
  37. setFocusable(true);
  38. setFocusableInTouchMode(true);
  39. //setVisibility(INVISIBLE);
  40. setVisibility(VISIBLE);
  41. //setAlpha(0f);
  42. setLayoutParams(new ViewGroup.LayoutParams(400, 200));
  43. this.id = id;
  44. editable = Editable.Factory.getInstance().newEditable("");
  45. Selection.setSelection(editable, 0);
  46. }
  47. // Maybe move CustomInputConnection.setEditableText() to here?
  48. // For now this is called when the InputConnection is not yet available.
  49. public void setEditableText(String text) {
  50. editable.replace(0, editable.length(), text);
  51. Selection.setSelection(editable, text.length(), text.length());
  52. }
  53. // Same as above
  54. public void setSelection(int start, int end) {
  55. Selection.setSelection(editable, start, end);
  56. }
  57. @Override
  58. protected void onAttachedToWindow() {
  59. super.onAttachedToWindow();
  60. Log.d("darkfi", "InvisibleInputView " + id + " attached to window");
  61. }
  62. @Override
  63. public boolean onCheckIsTextEditor() {
  64. Log.d("darkfi", "onCheckIsTextEditor");
  65. return true;
  66. }
  67. @Override
  68. public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
  69. Log.d("darkfi", "Create InputConnection for view=" + this.toString());
  70. // Losing focus requires the inputConnection to be destroyed
  71. //if (inputConnection != null) {
  72. // Log.d("darkfi", " -> return existing InputConnection");
  73. // return inputConnection;
  74. //}
  75. outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT
  76. | EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT;
  77. //| EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT;
  78. outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN
  79. //| EditorInfo.IME_ACTION_NONE;
  80. | EditorInfo.IME_ACTION_GO;
  81. outAttrs.initialSelStart = getSelectionStart();
  82. outAttrs.initialSelEnd = getSelectionEnd();
  83. //if (outAttrs.initialSelStart != 0) {
  84. // Log.d("darkfi", " select: [" + outAttrs.initialSelStart + ", " +
  85. // outAttrs.initialSelEnd + "]");
  86. //}
  87. inputConnection = new CustomInputConnection(id, editable, this);
  88. onCreateInputConnect(id);
  89. return inputConnection;
  90. }
  91. @Override
  92. protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
  93. super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
  94. Log.d("darkfi", "onFocusChanged: " + gainFocus);
  95. }
  96. public String debugEditableStr() {
  97. return CustomInputConnection.editableToXml(editable);
  98. }
  99. public String rawText() {
  100. return editable.toString();
  101. }
  102. public int getSelectionStart() {
  103. return Selection.getSelectionStart(editable);
  104. }
  105. public int getSelectionEnd() {
  106. return Selection.getSelectionEnd(editable);
  107. }
  108. public int getComposeStart() {
  109. return BaseInputConnection.getComposingSpanStart(editable);
  110. }
  111. public int getComposeEnd() {
  112. return BaseInputConnection.getComposingSpanEnd(editable);
  113. }
  114. }