java_godot_lib_jni.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /**************************************************************************/
  2. /* java_godot_lib_jni.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "java_godot_lib_jni.h"
  31. #include "android_input_handler.h"
  32. #include "api/java_class_wrapper.h"
  33. #include "api/jni_singleton.h"
  34. #include "dir_access_jandroid.h"
  35. #include "display_server_android.h"
  36. #include "file_access_android.h"
  37. #include "file_access_filesystem_jandroid.h"
  38. #include "java_godot_io_wrapper.h"
  39. #include "java_godot_wrapper.h"
  40. #include "jni_utils.h"
  41. #include "net_socket_android.h"
  42. #include "os_android.h"
  43. #include "plugin/godot_plugin_jni.h"
  44. #include "string_android.h"
  45. #include "thread_jandroid.h"
  46. #include "tts_android.h"
  47. #include "core/config/engine.h"
  48. #include "core/config/project_settings.h"
  49. #include "core/input/input.h"
  50. #include "main/main.h"
  51. #ifdef TOOLS_ENABLED
  52. #include "editor/editor_settings.h"
  53. #endif
  54. #include <android/asset_manager_jni.h>
  55. #include <android/input.h>
  56. #include <android/native_window_jni.h>
  57. #include <unistd.h>
  58. static JavaClassWrapper *java_class_wrapper = nullptr;
  59. static OS_Android *os_android = nullptr;
  60. static AndroidInputHandler *input_handler = nullptr;
  61. static GodotJavaWrapper *godot_java = nullptr;
  62. static GodotIOJavaWrapper *godot_io_java = nullptr;
  63. enum StartupStep {
  64. STEP_TERMINATED = -1,
  65. STEP_SETUP,
  66. STEP_SHOW_LOGO,
  67. STEP_STARTED
  68. };
  69. static SafeNumeric<int> step; // Shared between UI and render threads
  70. static Size2 new_size;
  71. static Vector3 accelerometer;
  72. static Vector3 gravity;
  73. static Vector3 magnetometer;
  74. static Vector3 gyroscope;
  75. static void _terminate(JNIEnv *env, bool p_restart = false) {
  76. step.set(STEP_TERMINATED); // Ensure no further steps are attempted and no further events are sent
  77. // lets cleanup
  78. // Unregister android plugins
  79. unregister_plugins_singletons();
  80. if (java_class_wrapper) {
  81. memdelete(java_class_wrapper);
  82. }
  83. if (input_handler) {
  84. delete input_handler;
  85. }
  86. // Whether restarting is handled by 'Main::cleanup()'
  87. bool restart_on_cleanup = false;
  88. if (os_android) {
  89. restart_on_cleanup = os_android->is_restart_on_exit_set();
  90. os_android->main_loop_end();
  91. Main::cleanup();
  92. delete os_android;
  93. }
  94. if (godot_io_java) {
  95. delete godot_io_java;
  96. }
  97. TTS_Android::terminate();
  98. FileAccessAndroid::terminate();
  99. DirAccessJAndroid::terminate();
  100. FileAccessFilesystemJAndroid::terminate();
  101. NetSocketAndroid::terminate();
  102. if (godot_java) {
  103. godot_java->on_godot_terminating(env);
  104. if (!restart_on_cleanup) {
  105. if (p_restart) {
  106. godot_java->restart(env);
  107. } else {
  108. godot_java->force_quit(env);
  109. }
  110. }
  111. delete godot_java;
  112. }
  113. }
  114. extern "C" {
  115. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight(JNIEnv *env, jclass clazz, jint p_height) {
  116. if (godot_io_java) {
  117. godot_io_java->set_vk_height(p_height);
  118. }
  119. }
  120. JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *env, jclass clazz, jobject p_activity, jobject p_godot_instance, jobject p_asset_manager, jobject p_godot_io, jobject p_net_utils, jobject p_directory_access_handler, jobject p_file_access_handler, jboolean p_use_apk_expansion) {
  121. JavaVM *jvm;
  122. env->GetJavaVM(&jvm);
  123. // create our wrapper classes
  124. godot_java = new GodotJavaWrapper(env, p_activity, p_godot_instance);
  125. godot_io_java = new GodotIOJavaWrapper(env, p_godot_io);
  126. init_thread_jandroid(jvm, env);
  127. FileAccessAndroid::setup(p_asset_manager);
  128. DirAccessJAndroid::setup(p_directory_access_handler);
  129. FileAccessFilesystemJAndroid::setup(p_file_access_handler);
  130. NetSocketAndroid::setup(p_net_utils);
  131. os_android = new OS_Android(godot_java, godot_io_java, p_use_apk_expansion);
  132. return true;
  133. }
  134. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ondestroy(JNIEnv *env, jclass clazz) {
  135. _terminate(env, false);
  136. }
  137. JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_setup(JNIEnv *env, jclass clazz, jobjectArray p_cmdline, jobject p_godot_tts) {
  138. setup_android_thread();
  139. const char **cmdline = nullptr;
  140. jstring *j_cmdline = nullptr;
  141. int cmdlen = 0;
  142. if (p_cmdline) {
  143. cmdlen = env->GetArrayLength(p_cmdline);
  144. if (cmdlen) {
  145. cmdline = (const char **)memalloc((cmdlen + 1) * sizeof(const char *));
  146. ERR_FAIL_NULL_V_MSG(cmdline, false, "Out of memory.");
  147. cmdline[cmdlen] = nullptr;
  148. j_cmdline = (jstring *)memalloc(cmdlen * sizeof(jstring));
  149. ERR_FAIL_NULL_V_MSG(j_cmdline, false, "Out of memory.");
  150. for (int i = 0; i < cmdlen; i++) {
  151. jstring string = (jstring)env->GetObjectArrayElement(p_cmdline, i);
  152. const char *rawString = env->GetStringUTFChars(string, nullptr);
  153. cmdline[i] = rawString;
  154. j_cmdline[i] = string;
  155. }
  156. }
  157. }
  158. Error err = Main::setup(OS_Android::ANDROID_EXEC_PATH, cmdlen, (char **)cmdline, false);
  159. if (cmdline) {
  160. if (j_cmdline) {
  161. for (int i = 0; i < cmdlen; ++i) {
  162. env->ReleaseStringUTFChars(j_cmdline[i], cmdline[i]);
  163. }
  164. memfree(j_cmdline);
  165. }
  166. memfree(cmdline);
  167. }
  168. // Note: --help and --version return ERR_HELP, but this should be translated to 0 if exit codes are propagated.
  169. if (err != OK) {
  170. return false;
  171. }
  172. TTS_Android::setup(p_godot_tts);
  173. java_class_wrapper = memnew(JavaClassWrapper(godot_java->get_activity()));
  174. GDREGISTER_CLASS(JNISingleton);
  175. return true;
  176. }
  177. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_resize(JNIEnv *env, jclass clazz, jobject p_surface, jint p_width, jint p_height) {
  178. if (os_android) {
  179. os_android->set_display_size(Size2i(p_width, p_height));
  180. // No need to reset the surface during startup
  181. if (step.get() > STEP_SETUP) {
  182. if (p_surface) {
  183. ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface);
  184. os_android->set_native_window(native_window);
  185. }
  186. DisplayServerAndroid::get_singleton()->reset_window();
  187. DisplayServerAndroid::get_singleton()->notify_surface_changed(p_width, p_height);
  188. }
  189. }
  190. }
  191. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_newcontext(JNIEnv *env, jclass clazz, jobject p_surface) {
  192. if (os_android) {
  193. if (step.get() == STEP_SETUP) {
  194. // During startup
  195. if (p_surface) {
  196. ANativeWindow *native_window = ANativeWindow_fromSurface(env, p_surface);
  197. os_android->set_native_window(native_window);
  198. }
  199. } else {
  200. // Rendering context recreated because it was lost; restart app to let it reload everything
  201. _terminate(env, true);
  202. }
  203. }
  204. }
  205. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_back(JNIEnv *env, jclass clazz) {
  206. if (step.get() <= STEP_SETUP) {
  207. return;
  208. }
  209. if (DisplayServerAndroid *dsa = Object::cast_to<DisplayServerAndroid>(DisplayServer::get_singleton())) {
  210. dsa->send_window_event(DisplayServer::WINDOW_EVENT_GO_BACK_REQUEST);
  211. }
  212. }
  213. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_ttsCallback(JNIEnv *env, jclass clazz, jint event, jint id, jint pos) {
  214. TTS_Android::_java_utterance_callback(event, id, pos);
  215. }
  216. JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_step(JNIEnv *env, jclass clazz) {
  217. if (step.get() == STEP_TERMINATED) {
  218. return true;
  219. }
  220. if (step.get() == STEP_SETUP) {
  221. // Since Godot is initialized on the UI thread, main_thread_id was set to that thread's id,
  222. // but for Godot purposes, the main thread is the one running the game loop
  223. Main::setup2(false); // The logo is shown in the next frame otherwise we run into rendering issues
  224. input_handler = new AndroidInputHandler();
  225. step.increment();
  226. return true;
  227. }
  228. if (step.get() == STEP_SHOW_LOGO) {
  229. Main::setup_boot_logo();
  230. step.increment();
  231. return true;
  232. }
  233. if (step.get() == STEP_STARTED) {
  234. if (Main::start() != EXIT_SUCCESS) {
  235. return true; // should exit instead and print the error
  236. }
  237. godot_java->on_godot_setup_completed(env);
  238. os_android->main_loop_begin();
  239. godot_java->on_godot_main_loop_started(env);
  240. step.increment();
  241. }
  242. DisplayServerAndroid::get_singleton()->process_accelerometer(accelerometer);
  243. DisplayServerAndroid::get_singleton()->process_gravity(gravity);
  244. DisplayServerAndroid::get_singleton()->process_magnetometer(magnetometer);
  245. DisplayServerAndroid::get_singleton()->process_gyroscope(gyroscope);
  246. bool should_swap_buffers = false;
  247. if (os_android->main_loop_iterate(&should_swap_buffers)) {
  248. _terminate(env, false);
  249. }
  250. return should_swap_buffers;
  251. }
  252. // Called on the UI thread
  253. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_dispatchMouseEvent(JNIEnv *env, jclass clazz, jint p_event_type, jint p_button_mask, jfloat p_x, jfloat p_y, jfloat p_delta_x, jfloat p_delta_y, jboolean p_double_click, jboolean p_source_mouse_relative, jfloat p_pressure, jfloat p_tilt_x, jfloat p_tilt_y) {
  254. if (step.get() <= STEP_SETUP) {
  255. return;
  256. }
  257. input_handler->process_mouse_event(p_event_type, p_button_mask, Point2(p_x, p_y), Vector2(p_delta_x, p_delta_y), p_double_click, p_source_mouse_relative, p_pressure, Vector2(p_tilt_x, p_tilt_y));
  258. }
  259. // Called on the UI thread
  260. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_dispatchTouchEvent(JNIEnv *env, jclass clazz, jint ev, jint pointer, jint pointer_count, jfloatArray position, jboolean p_double_tap) {
  261. if (step.get() <= STEP_SETUP) {
  262. return;
  263. }
  264. Vector<AndroidInputHandler::TouchPos> points;
  265. for (int i = 0; i < pointer_count; i++) {
  266. jfloat p[6];
  267. env->GetFloatArrayRegion(position, i * 6, 6, p);
  268. AndroidInputHandler::TouchPos tp;
  269. tp.id = (int)p[0];
  270. tp.pos = Point2(p[1], p[2]);
  271. tp.pressure = p[3];
  272. tp.tilt = Vector2(p[4], p[5]);
  273. points.push_back(tp);
  274. }
  275. input_handler->process_touch_event(ev, pointer, points, p_double_tap);
  276. }
  277. // Called on the UI thread
  278. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnify(JNIEnv *env, jclass clazz, jfloat p_x, jfloat p_y, jfloat p_factor) {
  279. if (step.get() <= STEP_SETUP) {
  280. return;
  281. }
  282. input_handler->process_magnify(Point2(p_x, p_y), p_factor);
  283. }
  284. // Called on the UI thread
  285. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_pan(JNIEnv *env, jclass clazz, jfloat p_x, jfloat p_y, jfloat p_delta_x, jfloat p_delta_y) {
  286. if (step.get() <= STEP_SETUP) {
  287. return;
  288. }
  289. input_handler->process_pan(Point2(p_x, p_y), Vector2(p_delta_x, p_delta_y));
  290. }
  291. // Called on the UI thread
  292. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joybutton(JNIEnv *env, jclass clazz, jint p_device, jint p_button, jboolean p_pressed) {
  293. if (step.get() <= STEP_SETUP) {
  294. return;
  295. }
  296. AndroidInputHandler::JoypadEvent jevent;
  297. jevent.device = p_device;
  298. jevent.type = AndroidInputHandler::JOY_EVENT_BUTTON;
  299. jevent.index = p_button;
  300. jevent.pressed = p_pressed;
  301. input_handler->process_joy_event(jevent);
  302. }
  303. // Called on the UI thread
  304. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyaxis(JNIEnv *env, jclass clazz, jint p_device, jint p_axis, jfloat p_value) {
  305. if (step.get() <= STEP_SETUP) {
  306. return;
  307. }
  308. AndroidInputHandler::JoypadEvent jevent;
  309. jevent.device = p_device;
  310. jevent.type = AndroidInputHandler::JOY_EVENT_AXIS;
  311. jevent.index = p_axis;
  312. jevent.value = p_value;
  313. input_handler->process_joy_event(jevent);
  314. }
  315. // Called on the UI thread
  316. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyhat(JNIEnv *env, jclass clazz, jint p_device, jint p_hat_x, jint p_hat_y) {
  317. if (step.get() <= STEP_SETUP) {
  318. return;
  319. }
  320. AndroidInputHandler::JoypadEvent jevent;
  321. jevent.device = p_device;
  322. jevent.type = AndroidInputHandler::JOY_EVENT_HAT;
  323. BitField<HatMask> hat;
  324. if (p_hat_x != 0) {
  325. if (p_hat_x < 0) {
  326. hat.set_flag(HatMask::LEFT);
  327. } else {
  328. hat.set_flag(HatMask::RIGHT);
  329. }
  330. }
  331. if (p_hat_y != 0) {
  332. if (p_hat_y < 0) {
  333. hat.set_flag(HatMask::UP);
  334. } else {
  335. hat.set_flag(HatMask::DOWN);
  336. }
  337. }
  338. jevent.hat = hat;
  339. input_handler->process_joy_event(jevent);
  340. }
  341. // Called on the UI thread
  342. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_joyconnectionchanged(JNIEnv *env, jclass clazz, jint p_device, jboolean p_connected, jstring p_name) {
  343. if (os_android) {
  344. String name = jstring_to_string(p_name, env);
  345. Input::get_singleton()->joy_connection_changed(p_device, p_connected, name);
  346. }
  347. }
  348. // Called on the UI thread
  349. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_key(JNIEnv *env, jclass clazz, jint p_physical_keycode, jint p_unicode, jint p_key_label, jboolean p_pressed, jboolean p_echo) {
  350. if (step.get() <= STEP_SETUP) {
  351. return;
  352. }
  353. input_handler->process_key_event(p_physical_keycode, p_unicode, p_key_label, p_pressed, p_echo);
  354. }
  355. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_accelerometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  356. accelerometer = Vector3(x, y, z);
  357. }
  358. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gravity(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  359. gravity = Vector3(x, y, z);
  360. }
  361. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_magnetometer(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  362. magnetometer = Vector3(x, y, z);
  363. }
  364. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_gyroscope(JNIEnv *env, jclass clazz, jfloat x, jfloat y, jfloat z) {
  365. gyroscope = Vector3(x, y, z);
  366. }
  367. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusin(JNIEnv *env, jclass clazz) {
  368. if (step.get() <= STEP_SETUP) {
  369. return;
  370. }
  371. os_android->main_loop_focusin();
  372. }
  373. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_focusout(JNIEnv *env, jclass clazz) {
  374. if (step.get() <= STEP_SETUP) {
  375. return;
  376. }
  377. os_android->main_loop_focusout();
  378. }
  379. JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getGlobal(JNIEnv *env, jclass clazz, jstring path) {
  380. String js = jstring_to_string(path, env);
  381. return env->NewStringUTF(GLOBAL_GET(js).operator String().utf8().get_data());
  382. }
  383. JNIEXPORT jstring JNICALL Java_org_godotengine_godot_GodotLib_getEditorSetting(JNIEnv *env, jclass clazz, jstring p_setting_key) {
  384. String editor_setting = "";
  385. #ifdef TOOLS_ENABLED
  386. String godot_setting_key = jstring_to_string(p_setting_key, env);
  387. editor_setting = EDITOR_GET(godot_setting_key).operator String();
  388. #else
  389. WARN_PRINT("Access to the Editor Settings in only available on Editor builds");
  390. #endif
  391. return env->NewStringUTF(editor_setting.utf8().get_data());
  392. }
  393. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_callobject(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
  394. Object *obj = ObjectDB::get_instance(ObjectID(ID));
  395. ERR_FAIL_NULL(obj);
  396. String str_method = jstring_to_string(method, env);
  397. int count = env->GetArrayLength(params);
  398. Variant *vlist = (Variant *)alloca(sizeof(Variant) * count);
  399. const Variant **vptr = (const Variant **)alloca(sizeof(Variant *) * count);
  400. for (int i = 0; i < count; i++) {
  401. jobject jobj = env->GetObjectArrayElement(params, i);
  402. ERR_FAIL_NULL(jobj);
  403. memnew_placement(&vlist[i], Variant(_jobject_to_variant(env, jobj)));
  404. vptr[i] = &vlist[i];
  405. env->DeleteLocalRef(jobj);
  406. }
  407. Callable::CallError err;
  408. obj->callp(str_method, vptr, count, err);
  409. }
  410. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_calldeferred(JNIEnv *env, jclass clazz, jlong ID, jstring method, jobjectArray params) {
  411. Object *obj = ObjectDB::get_instance(ObjectID(ID));
  412. ERR_FAIL_NULL(obj);
  413. String str_method = jstring_to_string(method, env);
  414. int count = env->GetArrayLength(params);
  415. Variant *args = (Variant *)alloca(sizeof(Variant) * count);
  416. const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * count);
  417. for (int i = 0; i < count; i++) {
  418. jobject jobj = env->GetObjectArrayElement(params, i);
  419. ERR_FAIL_NULL(jobj);
  420. memnew_placement(&args[i], Variant(_jobject_to_variant(env, jobj)));
  421. argptrs[i] = &args[i];
  422. env->DeleteLocalRef(jobj);
  423. }
  424. Callable(obj, str_method).call_deferredp(argptrs, count);
  425. }
  426. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onNightModeChanged(JNIEnv *env, jclass clazz) {
  427. DisplayServerAndroid *ds = (DisplayServerAndroid *)DisplayServer::get_singleton();
  428. if (ds) {
  429. ds->emit_system_theme_changed();
  430. }
  431. }
  432. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_requestPermissionResult(JNIEnv *env, jclass clazz, jstring p_permission, jboolean p_result) {
  433. String permission = jstring_to_string(p_permission, env);
  434. if (permission == "android.permission.RECORD_AUDIO" && p_result) {
  435. AudioDriver::get_singleton()->input_start();
  436. }
  437. if (os_android->get_main_loop()) {
  438. os_android->get_main_loop()->emit_signal(SNAME("on_request_permissions_result"), permission, p_result == JNI_TRUE);
  439. }
  440. }
  441. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererResumed(JNIEnv *env, jclass clazz) {
  442. if (step.get() <= STEP_SETUP) {
  443. return;
  444. }
  445. // We force redraw to ensure we render at least once when resuming the app.
  446. Main::force_redraw();
  447. if (os_android->get_main_loop()) {
  448. os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_RESUMED);
  449. }
  450. }
  451. JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_onRendererPaused(JNIEnv *env, jclass clazz) {
  452. if (step.get() <= STEP_SETUP) {
  453. return;
  454. }
  455. if (os_android->get_main_loop()) {
  456. os_android->get_main_loop()->notification(MainLoop::NOTIFICATION_APPLICATION_PAUSED);
  457. }
  458. }
  459. JNIEXPORT jboolean JNICALL Java_org_godotengine_godot_GodotLib_shouldDispatchInputToRenderThread(JNIEnv *env, jclass clazz) {
  460. Input *input = Input::get_singleton();
  461. if (input) {
  462. return !input->is_agile_input_event_flushing();
  463. }
  464. return false;
  465. }
  466. }