MainActivity.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. private InputMethodManager getIMM() {
  33. return (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  34. }
  35. public boolean focus(final int id) {
  36. final InvisibleInputView iv = editors.get(id);
  37. if (iv == null) {
  38. return false;
  39. }
  40. runOnUiThread(new Runnable() {
  41. @Override
  42. public void run() {
  43. boolean isFocused = iv.requestFocus();
  44. // Just Android things ;)
  45. if (!isFocused) {
  46. Log.w("darkfi", "error requesting focus for id=" + id + ": " + iv);
  47. }
  48. getIMM().showSoftInput(iv, InputMethodManager.SHOW_IMPLICIT);
  49. }
  50. });
  51. return true;
  52. }
  53. public boolean unfocus(final int id) {
  54. final InvisibleInputView iv = editors.get(id);
  55. if (iv == null) {
  56. return false;
  57. }
  58. runOnUiThread(new Runnable() {
  59. @Override
  60. public void run() {
  61. iv.clearFocus();
  62. getIMM().hideSoftInputFromWindow(iv.getWindowToken(), 0);
  63. }
  64. });
  65. return true;
  66. }
  67. /*
  68. public CustomInputConnection getInputConnect(int id) {
  69. InvisibleInputView iv = editors.get(id);
  70. if (iv == null) {
  71. return null;
  72. }
  73. return iv.inputConnection;
  74. }
  75. */
  76. public InvisibleInputView getInputView(int id) {
  77. return editors.get(id);
  78. }
  79. public boolean setText(int id, String txt) {
  80. InvisibleInputView iv = editors.get(id);
  81. if (iv == null) {
  82. return false;
  83. }
  84. // If inputConnection is not yet ready, then setup the editable directly.
  85. if (iv.inputConnection == null) {
  86. iv.setEditableText(txt);
  87. return true;
  88. }
  89. // Maybe do this on the UI thread?
  90. iv.inputConnection.setEditableText(txt, txt.length(), txt.length(), 0, 0);
  91. return true;
  92. }
  93. public boolean setSelection(int id, int start, int end) {
  94. InvisibleInputView iv = editors.get(id);
  95. if (iv == null) {
  96. return false;
  97. }
  98. // If inputConnection is not yet ready, then setup the sel directly.
  99. if (iv.inputConnection == null) {
  100. iv.setSelection(start, end);
  101. return true;
  102. }
  103. iv.inputConnection.beginBatchEdit();
  104. iv.inputConnection.setSelection(start, end);
  105. iv.inputConnection.endBatchEdit();
  106. return true;
  107. }
  108. /*
  109. // Editable string with the spans displayed inline
  110. public String getDebugEditableStr() {
  111. String edit = view.inputConnection.debugEditableStr();
  112. Log.d("darkfi", "getDebugEditableStr() -> " + edit);
  113. return edit;
  114. }
  115. */
  116. public String getAppDataPath() {
  117. return getApplicationContext().getDataDir().getAbsolutePath();
  118. }
  119. public String getExternalStoragePath() {
  120. return getApplicationContext().getExternalFilesDir(null).getAbsolutePath();
  121. }
  122. public int getKeyboardHeight() {
  123. WindowInsets windowInsets = view.getRootWindowInsets();
  124. if (windowInsets == null) {
  125. return 0;
  126. }
  127. Insets imeInsets = windowInsets.getInsets(WindowInsets.Type.ime());
  128. Insets navInsets = windowInsets.getInsets(WindowInsets.Type.navigationBars());
  129. return Math.max(0, imeInsets.bottom - navInsets.bottom);
  130. }
  131. public float getScreenDensity() {
  132. return getResources().getDisplayMetrics().density;
  133. }
  134. //% END
  135. //% MAIN_ACTIVITY_ON_CREATE
  136. rootView = layout;
  137. editors = new HashMap<>();
  138. view.setFocusable(false);
  139. view.setFocusableInTouchMode(false);
  140. view.clearFocus();
  141. //% END