MainActivity.java 6.7 KB

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