MainActivity.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 float getScreenDensity() {
  117. return getResources().getDisplayMetrics().density;
  118. }
  119. public boolean isImeVisible() {
  120. View decorView = getWindow().getDecorView();
  121. WindowInsets insets = decorView.getRootWindowInsets();
  122. Insets imeInsets = insets.getInsets(WindowInsets.Type.ime());
  123. if (imeInsets == null)
  124. return false;
  125. return insets.isVisible(Type.ime());
  126. }
  127. public void requestApplyInsets() {
  128. runOnUiThread(new Runnable() {
  129. @Override
  130. public void run() {
  131. view.requestApplyInsets();
  132. }
  133. });
  134. }
  135. public VideoDecoder createVideoDecoder() {
  136. VideoDecoder decoder = new VideoDecoder();
  137. decoder.setContext(this);
  138. return decoder;
  139. }
  140. public void openUrl(String url) {
  141. try {
  142. Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
  143. startActivity(intent);
  144. } catch (Exception e) {
  145. Log.e("darkfi", "Failed to open URL " + url + ": " + e.getMessage());
  146. }
  147. }
  148. //% END
  149. //% MAIN_ACTIVITY_ON_CREATE
  150. // Start a foreground service so the app stays awake
  151. Intent serviceIntent = new Intent(this, ForegroundService.class);
  152. startForegroundService(serviceIntent);
  153. final Thread.UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
  154. Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
  155. @Override
  156. public void uncaughtException(Thread thread, Throwable throwable) {
  157. try {
  158. File logFile = new File(getExternalFilesDir(null), "darkfi-app.log");
  159. PrintWriter writer = new PrintWriter(new FileWriter(logFile, true));
  160. writer.println("Uncaught exception on thread " + thread.getName() + ":");
  161. writer.println(Log.getStackTraceString(throwable));
  162. writer.close();
  163. } catch (Exception ignored) {
  164. }
  165. if (defaultHandler != null) {
  166. defaultHandler.uncaughtException(thread, throwable);
  167. }
  168. }
  169. });
  170. //% END