MainActivity.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //% IMPORTS
  2. import android.view.ViewGroup;
  3. import android.text.Editable;
  4. import android.text.Spannable;
  5. import android.text.SpanWatcher;
  6. import android.text.Spanned;
  7. import android.text.TextWatcher;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10. import android.view.inputmethod.BaseInputConnection;
  11. import java.util.HashMap;
  12. import autosuggest.InvisibleInputView;
  13. import autosuggest.CustomInputConnection;
  14. //% END
  15. //% MAIN_ACTIVITY_BODY
  16. private ViewGroup rootView;
  17. private HashMap<Integer, InvisibleInputView> editors;
  18. native static void onInitEdit(int id);
  19. public void createComposer(final int id) {
  20. Log.d("darkfi", "createComposer() -> " + id);
  21. final InvisibleInputView iv = new InvisibleInputView(this, id);
  22. editors.put(id, iv);
  23. runOnUiThread(new Runnable() {
  24. @Override
  25. public void run() {
  26. rootView.addView(iv);
  27. iv.clearFocus();
  28. onInitEdit(id);
  29. }
  30. });
  31. }
  32. public boolean focus(final int id) {
  33. final InvisibleInputView iv = editors.get(id);
  34. if (iv == null) {
  35. return false;
  36. }
  37. runOnUiThread(new Runnable() {
  38. @Override
  39. public void run() {
  40. boolean isFocused = iv.requestFocus();
  41. // Just Android things ;)
  42. if (!isFocused) {
  43. Log.w("darkfi", "error requesting focus for id=" + id + ": " + iv);
  44. }
  45. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  46. imm.showSoftInput(iv, InputMethodManager.SHOW_IMPLICIT);
  47. }
  48. });
  49. return true;
  50. }
  51. /*
  52. public CustomInputConnection getInputConnect(int id) {
  53. InvisibleInputView iv = editors.get(id);
  54. if (iv == null) {
  55. return null;
  56. }
  57. return iv.inputConnection;
  58. }
  59. */
  60. public InvisibleInputView getInputView(int id) {
  61. return editors.get(id);
  62. }
  63. public boolean setText(int id, String txt) {
  64. InvisibleInputView iv = editors.get(id);
  65. if (iv == null) {
  66. return false;
  67. }
  68. // If inputConnection is not yet ready, then setup the editable directly.
  69. if (iv.inputConnection == null) {
  70. iv.setEditableText(txt);
  71. return true;
  72. }
  73. // Maybe do this on the UI thread?
  74. iv.inputConnection.setEditableText(txt, txt.length(), txt.length(), 0, 0);
  75. return true;
  76. }
  77. public boolean setSelection(int id, int start, int end) {
  78. InvisibleInputView iv = editors.get(id);
  79. if (iv == null) {
  80. return false;
  81. }
  82. // If inputConnection is not yet ready, then setup the sel directly.
  83. if (iv.inputConnection == null) {
  84. iv.setSelection(start, end);
  85. return true;
  86. }
  87. iv.inputConnection.beginBatchEdit();
  88. iv.inputConnection.setSelection(start, end);
  89. iv.inputConnection.endBatchEdit();
  90. return true;
  91. }
  92. /*
  93. // Editable string with the spans displayed inline
  94. public String getDebugEditableStr() {
  95. String edit = view.inputConnection.debugEditableStr();
  96. Log.d("darkfi", "getDebugEditableStr() -> " + edit);
  97. return edit;
  98. }
  99. */
  100. public String getAppDataPath() {
  101. return getApplicationContext().getDataDir().getAbsolutePath();
  102. }
  103. public String getExternalStoragePath() {
  104. return getApplicationContext().getExternalFilesDir(null).getAbsolutePath();
  105. }
  106. public int getKeyboardHeight() {
  107. WindowInsets windowInsets = view.getRootWindowInsets();
  108. if (windowInsets == null) {
  109. return 0;
  110. }
  111. Insets imeInsets = windowInsets.getInsets(WindowInsets.Type.ime());
  112. Insets navInsets = windowInsets.getInsets(WindowInsets.Type.navigationBars());
  113. return Math.max(0, imeInsets.bottom - navInsets.bottom);
  114. }
  115. //% END
  116. //% MAIN_ACTIVITY_ON_CREATE
  117. rootView = layout;
  118. editors = new HashMap<>();
  119. view.setFocusable(false);
  120. view.setFocusableInTouchMode(false);
  121. view.clearFocus();
  122. //% END