java_godot_io_wrapper.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************/
  2. /* java_godot_io_wrapper.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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_io_wrapper.h"
  31. #include "core/error_list.h"
  32. // JNIEnv is only valid within the thread it belongs to, in a multi threading environment
  33. // we can't cache it.
  34. // For GodotIO we call all access methods from our thread and we thus get a valid JNIEnv
  35. // from ThreadAndroid.
  36. GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instance) {
  37. godot_io_instance = p_env->NewGlobalRef(p_godot_io_instance);
  38. if (godot_io_instance) {
  39. cls = p_env->GetObjectClass(godot_io_instance);
  40. if (cls) {
  41. cls = (jclass)p_env->NewGlobalRef(cls);
  42. } else {
  43. // this is a pretty serious fail.. bail... pointers will stay 0
  44. return;
  45. }
  46. _open_URI = p_env->GetMethodID(cls, "openURI", "(Ljava/lang/String;)I");
  47. _get_data_dir = p_env->GetMethodID(cls, "getDataDir", "()Ljava/lang/String;");
  48. _get_locale = p_env->GetMethodID(cls, "getLocale", "()Ljava/lang/String;");
  49. _get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;");
  50. _get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I");
  51. _get_unique_id = p_env->GetMethodID(cls, "getUniqueID", "()Ljava/lang/String;");
  52. _show_keyboard = p_env->GetMethodID(cls, "showKeyboard", "(Ljava/lang/String;I)V");
  53. _hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V");
  54. _set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V");
  55. _get_screen_orientation = p_env->GetMethodID(cls, "getScreenOrientation", "()I");
  56. _get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(I)Ljava/lang/String;");
  57. }
  58. }
  59. GodotIOJavaWrapper::~GodotIOJavaWrapper() {
  60. // nothing to do here for now
  61. }
  62. jobject GodotIOJavaWrapper::get_instance() {
  63. return godot_io_instance;
  64. }
  65. Error GodotIOJavaWrapper::open_uri(const String &p_uri) {
  66. if (_open_URI) {
  67. JNIEnv *env = ThreadAndroid::get_env();
  68. jstring jStr = env->NewStringUTF(p_uri.utf8().get_data());
  69. return env->CallIntMethod(godot_io_instance, _open_URI, jStr) ? ERR_CANT_OPEN : OK;
  70. } else {
  71. return ERR_UNAVAILABLE;
  72. }
  73. }
  74. String GodotIOJavaWrapper::get_user_data_dir() {
  75. if (_get_data_dir) {
  76. JNIEnv *env = ThreadAndroid::get_env();
  77. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_data_dir);
  78. return jstring_to_string(s, env);
  79. } else {
  80. return String();
  81. }
  82. }
  83. String GodotIOJavaWrapper::get_locale() {
  84. if (_get_locale) {
  85. JNIEnv *env = ThreadAndroid::get_env();
  86. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_locale);
  87. return jstring_to_string(s, env);
  88. } else {
  89. return String();
  90. }
  91. }
  92. String GodotIOJavaWrapper::get_model() {
  93. if (_get_model) {
  94. JNIEnv *env = ThreadAndroid::get_env();
  95. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_model);
  96. return jstring_to_string(s, env);
  97. } else {
  98. return String();
  99. }
  100. }
  101. int GodotIOJavaWrapper::get_screen_dpi() {
  102. if (_get_screen_DPI) {
  103. JNIEnv *env = ThreadAndroid::get_env();
  104. return env->CallIntMethod(godot_io_instance, _get_screen_DPI);
  105. } else {
  106. return 160;
  107. }
  108. }
  109. String GodotIOJavaWrapper::get_unique_id() {
  110. if (_get_unique_id) {
  111. JNIEnv *env = ThreadAndroid::get_env();
  112. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_unique_id);
  113. return jstring_to_string(s, env);
  114. } else {
  115. return String();
  116. }
  117. }
  118. bool GodotIOJavaWrapper::has_vk() {
  119. return (_show_keyboard != 0) && (_hide_keyboard != 0);
  120. }
  121. void GodotIOJavaWrapper::show_vk(const String &p_existing, int p_max_input_length) {
  122. if (_show_keyboard) {
  123. JNIEnv *env = ThreadAndroid::get_env();
  124. jstring jStr = env->NewStringUTF(p_existing.utf8().get_data());
  125. env->CallVoidMethod(godot_io_instance, _show_keyboard, jStr, p_max_input_length);
  126. }
  127. }
  128. void GodotIOJavaWrapper::hide_vk() {
  129. if (_hide_keyboard) {
  130. JNIEnv *env = ThreadAndroid::get_env();
  131. env->CallVoidMethod(godot_io_instance, _hide_keyboard);
  132. }
  133. }
  134. void GodotIOJavaWrapper::set_screen_orientation(int p_orient) {
  135. if (_set_screen_orientation) {
  136. JNIEnv *env = ThreadAndroid::get_env();
  137. env->CallVoidMethod(godot_io_instance, _set_screen_orientation, p_orient);
  138. }
  139. }
  140. int GodotIOJavaWrapper::get_screen_orientation() {
  141. if (_get_screen_orientation) {
  142. JNIEnv *env = ThreadAndroid::get_env();
  143. return env->CallIntMethod(godot_io_instance, _get_screen_orientation);
  144. } else {
  145. return 0;
  146. }
  147. }
  148. String GodotIOJavaWrapper::get_system_dir(int p_dir) {
  149. if (_get_system_dir) {
  150. JNIEnv *env = ThreadAndroid::get_env();
  151. jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_system_dir, p_dir);
  152. return jstring_to_string(s, env);
  153. } else {
  154. return String(".");
  155. }
  156. }
  157. // volatile because it can be changed from non-main thread and we need to
  158. // ensure the change is immediately visible to other threads.
  159. static volatile int virtual_keyboard_height;
  160. int GodotIOJavaWrapper::get_vk_height() {
  161. return virtual_keyboard_height;
  162. }
  163. void GodotIOJavaWrapper::set_vk_height(int p_height) {
  164. virtual_keyboard_height = p_height;
  165. }