MainActivity.java 4.7 KB

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