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 
31 #include "java_godot_io_wrapper.h"
32 #include "core/error_list.h"
33 
34 // JNIEnv is only valid within the thread it belongs to, in a multi threading environment
35 // we can't cache it.
36 // For GodotIO we call all access methods from our thread and we thus get a valid JNIEnv
37 // from ThreadAndroid.
38 
GodotIOJavaWrapper(JNIEnv * p_env,jobject p_godot_io_instance)39 GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instance) {
40 	godot_io_instance = p_env->NewGlobalRef(p_godot_io_instance);
41 	if (godot_io_instance) {
42 		cls = p_env->GetObjectClass(godot_io_instance);
43 		if (cls) {
44 			cls = (jclass)p_env->NewGlobalRef(cls);
45 		} else {
46 			// this is a pretty serious fail.. bail... pointers will stay 0
47 			return;
48 		}
49 
50 		_open_URI = p_env->GetMethodID(cls, "openURI", "(Ljava/lang/String;)I");
51 		_get_data_dir = p_env->GetMethodID(cls, "getDataDir", "()Ljava/lang/String;");
52 		_get_locale = p_env->GetMethodID(cls, "getLocale", "()Ljava/lang/String;");
53 		_get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;");
54 		_get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I");
55 		_get_unique_id = p_env->GetMethodID(cls, "getUniqueID", "()Ljava/lang/String;");
56 		_show_keyboard = p_env->GetMethodID(cls, "showKeyboard", "(Ljava/lang/String;ZIII)V");
57 		_hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V");
58 		_set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V");
59 		_get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(I)Ljava/lang/String;");
60 		_play_video = p_env->GetMethodID(cls, "playVideo", "(Ljava/lang/String;)V");
61 		_is_video_playing = p_env->GetMethodID(cls, "isVideoPlaying", "()Z");
62 		_pause_video = p_env->GetMethodID(cls, "pauseVideo", "()V");
63 		_stop_video = p_env->GetMethodID(cls, "stopVideo", "()V");
64 	}
65 }
66 
~GodotIOJavaWrapper()67 GodotIOJavaWrapper::~GodotIOJavaWrapper() {
68 	// nothing to do here for now
69 }
70 
get_instance()71 jobject GodotIOJavaWrapper::get_instance() {
72 	return godot_io_instance;
73 }
74 
open_uri(const String & p_uri)75 Error GodotIOJavaWrapper::open_uri(const String &p_uri) {
76 	if (_open_URI) {
77 		JNIEnv *env = ThreadAndroid::get_env();
78 		jstring jStr = env->NewStringUTF(p_uri.utf8().get_data());
79 		return env->CallIntMethod(godot_io_instance, _open_URI, jStr) ? ERR_CANT_OPEN : OK;
80 	} else {
81 		return ERR_UNAVAILABLE;
82 	}
83 }
84 
get_user_data_dir()85 String GodotIOJavaWrapper::get_user_data_dir() {
86 	if (_get_data_dir) {
87 		JNIEnv *env = ThreadAndroid::get_env();
88 		jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_data_dir);
89 		return jstring_to_string(s, env);
90 	} else {
91 		return String();
92 	}
93 }
94 
get_locale()95 String GodotIOJavaWrapper::get_locale() {
96 	if (_get_locale) {
97 		JNIEnv *env = ThreadAndroid::get_env();
98 		jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_locale);
99 		return jstring_to_string(s, env);
100 	} else {
101 		return String();
102 	}
103 }
104 
get_model()105 String GodotIOJavaWrapper::get_model() {
106 	if (_get_model) {
107 		JNIEnv *env = ThreadAndroid::get_env();
108 		jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_model);
109 		return jstring_to_string(s, env);
110 	} else {
111 		return String();
112 	}
113 }
114 
get_screen_dpi()115 int GodotIOJavaWrapper::get_screen_dpi() {
116 	if (_get_screen_DPI) {
117 		JNIEnv *env = ThreadAndroid::get_env();
118 		return env->CallIntMethod(godot_io_instance, _get_screen_DPI);
119 	} else {
120 		return 160;
121 	}
122 }
123 
get_unique_id()124 String GodotIOJavaWrapper::get_unique_id() {
125 	if (_get_unique_id) {
126 		JNIEnv *env = ThreadAndroid::get_env();
127 		jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_unique_id);
128 		return jstring_to_string(s, env);
129 	} else {
130 		return String();
131 	}
132 }
133 
has_vk()134 bool GodotIOJavaWrapper::has_vk() {
135 	return (_show_keyboard != 0) && (_hide_keyboard != 0);
136 }
137 
show_vk(const String & p_existing,bool p_multiline,int p_max_input_length,int p_cursor_start,int p_cursor_end)138 void GodotIOJavaWrapper::show_vk(const String &p_existing, bool p_multiline, int p_max_input_length, int p_cursor_start, int p_cursor_end) {
139 	if (_show_keyboard) {
140 		JNIEnv *env = ThreadAndroid::get_env();
141 		jstring jStr = env->NewStringUTF(p_existing.utf8().get_data());
142 		env->CallVoidMethod(godot_io_instance, _show_keyboard, jStr, p_multiline, p_max_input_length, p_cursor_start, p_cursor_end);
143 	}
144 }
145 
hide_vk()146 void GodotIOJavaWrapper::hide_vk() {
147 	if (_hide_keyboard) {
148 		JNIEnv *env = ThreadAndroid::get_env();
149 		env->CallVoidMethod(godot_io_instance, _hide_keyboard);
150 	}
151 }
152 
set_screen_orientation(int p_orient)153 void GodotIOJavaWrapper::set_screen_orientation(int p_orient) {
154 	if (_set_screen_orientation) {
155 		JNIEnv *env = ThreadAndroid::get_env();
156 		env->CallVoidMethod(godot_io_instance, _set_screen_orientation, p_orient);
157 	}
158 }
159 
get_system_dir(int p_dir)160 String GodotIOJavaWrapper::get_system_dir(int p_dir) {
161 	if (_get_system_dir) {
162 		JNIEnv *env = ThreadAndroid::get_env();
163 		jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_system_dir, p_dir);
164 		return jstring_to_string(s, env);
165 	} else {
166 		return String(".");
167 	}
168 }
169 
play_video(const String & p_path)170 void GodotIOJavaWrapper::play_video(const String &p_path) {
171 	// Why is this not here?!?!
172 }
173 
is_video_playing()174 bool GodotIOJavaWrapper::is_video_playing() {
175 	if (_is_video_playing) {
176 		JNIEnv *env = ThreadAndroid::get_env();
177 		return env->CallBooleanMethod(godot_io_instance, _is_video_playing);
178 	} else {
179 		return false;
180 	}
181 }
182 
pause_video()183 void GodotIOJavaWrapper::pause_video() {
184 	if (_pause_video) {
185 		JNIEnv *env = ThreadAndroid::get_env();
186 		env->CallVoidMethod(godot_io_instance, _pause_video);
187 	}
188 }
189 
stop_video()190 void GodotIOJavaWrapper::stop_video() {
191 	if (_stop_video) {
192 		JNIEnv *env = ThreadAndroid::get_env();
193 		env->CallVoidMethod(godot_io_instance, _stop_video);
194 	}
195 }
196 
197 // volatile because it can be changed from non-main thread and we need to
198 // ensure the change is immediately visible to other threads.
199 static volatile int virtual_keyboard_height;
200 
get_vk_height()201 int GodotIOJavaWrapper::get_vk_height() {
202 	return virtual_keyboard_height;
203 }
204 
set_vk_height(int p_height)205 void GodotIOJavaWrapper::set_vk_height(int p_height) {
206 	virtual_keyboard_height = p_height;
207 }
208