MainActivity.java 6.1 KB

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