MainActivity.java 5.0 KB

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