MainActivity.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //% IMPORTS
  2. import android.view.ViewGroup;
  3. import android.view.WindowInsets.Type;
  4. import android.view.inputmethod.EditorInfo;
  5. import android.text.InputType;
  6. import android.util.Log;
  7. import java.util.HashMap;
  8. import videodecode.VideoDecoder;
  9. import textinput.Settings;
  10. import textinput.Listener;
  11. import textinput.State;
  12. import textinput.GameTextInput;
  13. //% END
  14. //% QUAD_SURFACE_ON_CREATE_INPUT_CONNECTION
  15. MainActivity main = (MainActivity)getContext();
  16. // Create InputConnection if it doesn't exist yet
  17. if (main.inpcon == null) {
  18. EditorInfo editorInfo = new EditorInfo();
  19. editorInfo.inputType = InputType.TYPE_CLASS_TEXT |
  20. InputType.TYPE_TEXT_FLAG_AUTO_CORRECT;
  21. editorInfo.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN;
  22. main.inpcon = new textinput.InputConnection(
  23. getContext(),
  24. this,
  25. new Settings(editorInfo, true)
  26. );
  27. // Pass the InputConnection to native GameTextInput library
  28. main.setInputConnectionNative(main.inpcon);
  29. }
  30. // Set the listener to receive IME state changes
  31. main.inpcon.setListener(new Listener() {
  32. @Override
  33. public void stateChanged(State newState, boolean dismissed) {
  34. // Called when the IME sends new text state
  35. // Forward to native code which triggers Rust callback
  36. Log.d("darkfi", "stateChanged: text=" + newState.toString());
  37. main.onTextInputEventNative(newState);
  38. }
  39. @Override
  40. public void onImeInsetsChanged(androidx.core.graphics.Insets insets) {
  41. // Called when IME insets change (e.g., keyboard height changes)
  42. // Optional: can be used for dynamic layout adjustment
  43. }
  44. @Override
  45. public void onSoftwareKeyboardVisibilityChanged(boolean visible) {
  46. // Called when keyboard is shown or hidden
  47. }
  48. @Override
  49. public void onEditorAction(int actionCode) {
  50. // Called when user presses action button (Done, Next, etc.)
  51. // Optional: handle specific editor actions
  52. }
  53. });
  54. // Copy EditorInfo from GameTextInput to configure IME
  55. if (outAttrs != null) {
  56. GameTextInput.copyEditorInfo(
  57. main.inpcon.getEditorInfo(),
  58. outAttrs
  59. );
  60. }
  61. // Return the GameTextInput InputConnection to IME
  62. if (true) return main.inpcon;
  63. //% END
  64. //% RESIZING_LAYOUT_BODY
  65. native static void onApplyInsets(
  66. int sys_left, int sys_top, int sys_right, int sys_bottom,
  67. int ime_left, int ime_top, int ime_right, int ime_bottom
  68. );
  69. //% END
  70. //% RESIZING_LAYOUT_ON_APPLY_WINDOW_INSETS
  71. {
  72. Insets imeInsets = insets.getInsets(WindowInsets.Type.ime());
  73. Insets sysInsets = insets.getInsets(WindowInsets.Type.systemBars());
  74. // Screen: (1440, 3064)
  75. // IME height: 1056
  76. // Sys insets: (0, 152, 0, 135)
  77. onApplyInsets(
  78. sysInsets.left, sysInsets.top, sysInsets.right, sysInsets.bottom,
  79. imeInsets.left, imeInsets.top, imeInsets.right, imeInsets.bottom
  80. );
  81. }
  82. // Workaround for Java error due to remaining body.
  83. // We handle the insets in our app directly.
  84. if (true)
  85. return insets;
  86. //% END
  87. //% MAIN_ACTIVITY_BODY
  88. // GameTextInput native bridge functions (following official Android pattern)
  89. native void setInputConnectionNative(textinput.InputConnection c);
  90. native void onTextInputEventNative(textinput.State softKeyboardEvent);
  91. // GameTextInput InputConnection reference (public for QuadSurface access)
  92. public textinput.InputConnection inpcon;
  93. public String getAppDataPath() {
  94. return getApplicationContext().getDataDir().getAbsolutePath();
  95. }
  96. public String getExternalStoragePath() {
  97. return getApplicationContext().getExternalFilesDir(null).getAbsolutePath();
  98. }
  99. public int getKeyboardHeight() {
  100. WindowInsets windowInsets = view.getRootWindowInsets();
  101. if (windowInsets == null) {
  102. return 0;
  103. }
  104. Insets imeInsets = windowInsets.getInsets(WindowInsets.Type.ime());
  105. Insets navInsets = windowInsets.getInsets(WindowInsets.Type.navigationBars());
  106. return Math.max(0, imeInsets.bottom - navInsets.bottom);
  107. }
  108. public float getScreenDensity() {
  109. return getResources().getDisplayMetrics().density;
  110. }
  111. public boolean isImeVisible() {
  112. View decorView = getWindow().getDecorView();
  113. WindowInsets insets = decorView.getRootWindowInsets();
  114. Insets imeInsets = insets.getInsets(WindowInsets.Type.ime());
  115. if (imeInsets == null)
  116. return false;
  117. return insets.isVisible(Type.ime());
  118. }
  119. public VideoDecoder createVideoDecoder() {
  120. VideoDecoder decoder = new VideoDecoder();
  121. decoder.setContext(this);
  122. return decoder;
  123. }
  124. //% END
  125. //% MAIN_ACTIVITY_ON_CREATE
  126. // Start a foreground service so the app stays awake
  127. Intent serviceIntent = new Intent(this, ForegroundService.class);
  128. startForegroundService(serviceIntent);
  129. //% END