InvisibleInputView.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. if (inputConnection != null) {
  71. Log.d("darkfi", " -> return existing InputConnection");
  72. return inputConnection;
  73. }
  74. outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT
  75. | EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT;
  76. //| EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT;
  77. outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN
  78. //| EditorInfo.IME_ACTION_NONE;
  79. | EditorInfo.IME_ACTION_GO;
  80. outAttrs.initialSelStart = getSelectionStart();
  81. outAttrs.initialSelEnd = getSelectionEnd();
  82. inputConnection = new CustomInputConnection(id, editable, this);
  83. onCreateInputConnect(id);
  84. return inputConnection;
  85. }
  86. @Override
  87. protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
  88. super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
  89. Log.d("darkfi", "onFocusChanged: " + gainFocus);
  90. }
  91. public String debugEditableStr() {
  92. return CustomInputConnection.editableToXml(editable);
  93. }
  94. public String rawText() {
  95. return editable.toString();
  96. }
  97. public int getSelectionStart() {
  98. return Selection.getSelectionStart(editable);
  99. }
  100. public int getSelectionEnd() {
  101. return Selection.getSelectionEnd(editable);
  102. }
  103. public int getComposeStart() {
  104. return BaseInputConnection.getComposingSpanStart(editable);
  105. }
  106. public int getComposeEnd() {
  107. return BaseInputConnection.getComposingSpanEnd(editable);
  108. }
  109. }