MainActivity.java 6.0 KB

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