diff --git a/platform/android/java_glue.cpp b/platform/android/java_glue.cpp index d08dc61d84fc..39589269516b 100644 --- a/platform/android/java_glue.cpp +++ b/platform/android/java_glue.cpp @@ -623,6 +623,7 @@ static jmethodID _setKeepScreenOn = 0; static jmethodID _alertDialog = 0; static jmethodID _requestPermission = 0; static jmethodID _vibrate = 0; +static jmethodID _getWindowInset = 0; static void _gfx_init_func(void *ud, bool gl2) { } @@ -767,6 +768,19 @@ static int _get_vk_height() { return virtual_keyboard_height; } +static Rect2 _get_window_inset() { + if (_getWindowInset) { + JNIEnv *env = ThreadAndroid::get_env(); + jintArray ret = (jintArray)env->CallObjectMethod(godot_io, _getWindowInset); + jint *inset_arr = env->GetIntArrayElements(ret, JNI_FALSE); + Rect2 inset_rect(inset_arr[0], inset_arr[1], inset_arr[2], inset_arr[3]); + env->ReleaseIntArrayElements(ret, inset_arr, JNI_ABORT); + return inset_rect; + } else { + return Rect2(); + } +} + JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_setVirtualKeyboardHeight(JNIEnv *env, jobject obj, jint p_height) { virtual_keyboard_height = p_height; } @@ -822,6 +836,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *en _isVideoPlaying = env->GetMethodID(c, "isVideoPlaying", "()Z"); _pauseVideo = env->GetMethodID(c, "pauseVideo", "()V"); _stopVideo = env->GetMethodID(c, "stopVideo", "()V"); + _getWindowInset = env->GetMethodID(c, "getWindowInset", "()[I"); } ThreadAndroid::make_default(jvm); @@ -837,7 +852,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *en AudioDriverAndroid::setup(gob); } - os_android = new OS_Android(_gfx_init_func, env, _open_uri, _get_user_data_dir, _get_locale, _get_model, _get_screen_dpi, _show_vk, _hide_vk, _get_vk_height, _set_screen_orient, _get_unique_id, _get_system_dir, _get_gles_version_code, _play_video, _is_video_playing, _pause_video, _stop_video, _set_keep_screen_on, _alert, _set_clipboard, _get_clipboard, _request_permission, p_use_apk_expansion, _request_vibrate); + os_android = new OS_Android(_gfx_init_func, env, _open_uri, _get_user_data_dir, _get_locale, _get_model, _get_screen_dpi, _show_vk, _hide_vk, _get_vk_height, _set_screen_orient, _get_unique_id, _get_system_dir, _get_gles_version_code, _play_video, _is_video_playing, _pause_video, _stop_video, _set_keep_screen_on, _alert, _set_clipboard, _get_clipboard, _request_permission, p_use_apk_expansion, _request_vibrate, _get_window_inset); char wd[500]; getcwd(wd, 500); diff --git a/platform/android/java_godot_io_wrapper.cpp b/platform/android/java_godot_io_wrapper.cpp deleted file mode 100644 index 88f288a03191..000000000000 --- a/platform/android/java_godot_io_wrapper.cpp +++ /dev/null @@ -1,221 +0,0 @@ -/*************************************************************************/ -/* java_godot_io_wrapper.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "java_godot_io_wrapper.h" -#include "core/error_list.h" - -// JNIEnv is only valid within the thread it belongs to, in a multi threading environment -// we can't cache it. -// For GodotIO we call all access methods from our thread and we thus get a valid JNIEnv -// from ThreadAndroid. - -GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instance) { - godot_io_instance = p_env->NewGlobalRef(p_godot_io_instance); - if (godot_io_instance) { - cls = p_env->GetObjectClass(godot_io_instance); - if (cls) { - cls = (jclass)p_env->NewGlobalRef(cls); - } else { - // this is a pretty serious fail.. bail... pointers will stay 0 - return; - } - - _open_URI = p_env->GetMethodID(cls, "openURI", "(Ljava/lang/String;)I"); - _get_data_dir = p_env->GetMethodID(cls, "getDataDir", "()Ljava/lang/String;"); - _get_locale = p_env->GetMethodID(cls, "getLocale", "()Ljava/lang/String;"); - _get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;"); - _get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I"); - _get_unique_id = p_env->GetMethodID(cls, "getUniqueID", "()Ljava/lang/String;"); - _show_keyboard = p_env->GetMethodID(cls, "showKeyboard", "(Ljava/lang/String;)V"); - _hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V"); - _set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V"); - _get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(I)Ljava/lang/String;"); - _play_video = p_env->GetMethodID(cls, "playVideo", "(Ljava/lang/String;)V"); - _is_video_playing = p_env->GetMethodID(cls, "isVideoPlaying", "()Z"); - _pause_video = p_env->GetMethodID(cls, "pauseVideo", "()V"); - _stop_video = p_env->GetMethodID(cls, "stopVideo", "()V"); - _get_window_inset = p_env->GetMethodID(cls, "getWindowInset", "()[I"); - } -} - -GodotIOJavaWrapper::~GodotIOJavaWrapper() { - // nothing to do here for now -} - -jobject GodotIOJavaWrapper::get_instance() { - return godot_io_instance; -} - -Error GodotIOJavaWrapper::open_uri(const String &p_uri) { - if (_open_URI) { - JNIEnv *env = ThreadAndroid::get_env(); - jstring jStr = env->NewStringUTF(p_uri.utf8().get_data()); - return env->CallIntMethod(godot_io_instance, _open_URI, jStr) ? ERR_CANT_OPEN : OK; - } else { - return ERR_UNAVAILABLE; - } -} - -String GodotIOJavaWrapper::get_user_data_dir() { - if (_get_data_dir) { - JNIEnv *env = ThreadAndroid::get_env(); - jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_data_dir); - return jstring_to_string(s, env); - } else { - return String(); - } -} - -String GodotIOJavaWrapper::get_locale() { - if (_get_locale) { - JNIEnv *env = ThreadAndroid::get_env(); - jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_locale); - return jstring_to_string(s, env); - } else { - return String(); - } -} - -String GodotIOJavaWrapper::get_model() { - if (_get_model) { - JNIEnv *env = ThreadAndroid::get_env(); - jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_model); - return jstring_to_string(s, env); - } else { - return String(); - } -} - -int GodotIOJavaWrapper::get_screen_dpi() { - if (_get_screen_DPI) { - JNIEnv *env = ThreadAndroid::get_env(); - return env->CallIntMethod(godot_io_instance, _get_screen_DPI); - } else { - return 160; - } -} - -String GodotIOJavaWrapper::get_unique_id() { - if (_get_unique_id) { - JNIEnv *env = ThreadAndroid::get_env(); - jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_unique_id); - return jstring_to_string(s, env); - } else { - return String(); - } -} - -bool GodotIOJavaWrapper::has_vk() { - return (_show_keyboard != 0) && (_hide_keyboard != 0); -} - -void GodotIOJavaWrapper::show_vk(const String &p_existing) { - if (_show_keyboard) { - JNIEnv *env = ThreadAndroid::get_env(); - jstring jStr = env->NewStringUTF(p_existing.utf8().get_data()); - env->CallVoidMethod(godot_io_instance, _show_keyboard, jStr); - } -} - -void GodotIOJavaWrapper::hide_vk() { - if (_hide_keyboard) { - JNIEnv *env = ThreadAndroid::get_env(); - env->CallVoidMethod(godot_io_instance, _hide_keyboard); - } -} - -void GodotIOJavaWrapper::set_screen_orientation(int p_orient) { - if (_set_screen_orientation) { - JNIEnv *env = ThreadAndroid::get_env(); - env->CallVoidMethod(godot_io_instance, _set_screen_orientation, p_orient); - } -} - -String GodotIOJavaWrapper::get_system_dir(int p_dir) { - if (_get_system_dir) { - JNIEnv *env = ThreadAndroid::get_env(); - jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_system_dir, p_dir); - return jstring_to_string(s, env); - } else { - return String("."); - } -} - -void GodotIOJavaWrapper::play_video(const String &p_path) { - // Why is this not here?!?! -} - -bool GodotIOJavaWrapper::is_video_playing() { - if (_is_video_playing) { - JNIEnv *env = ThreadAndroid::get_env(); - return env->CallBooleanMethod(godot_io_instance, _is_video_playing); - } else { - return false; - } -} - -void GodotIOJavaWrapper::pause_video() { - if (_pause_video) { - JNIEnv *env = ThreadAndroid::get_env(); - env->CallVoidMethod(godot_io_instance, _pause_video); - } -} - -void GodotIOJavaWrapper::stop_video() { - if (_stop_video) { - JNIEnv *env = ThreadAndroid::get_env(); - env->CallVoidMethod(godot_io_instance, _stop_video); - } -} - -// volatile because it can be changed from non-main thread and we need to -// ensure the change is immediately visible to other threads. -static volatile int virtual_keyboard_height; - -int GodotIOJavaWrapper::get_vk_height() { - return virtual_keyboard_height; -} - -void GodotIOJavaWrapper::set_vk_height(int p_height) { - virtual_keyboard_height = p_height; -} - -Rect2 GodotIOJavaWrapper::get_window_inset() { - if (_get_window_inset) { - JNIEnv *env = ThreadAndroid::get_env(); - jintArray ret = (jintArray)env->CallObjectMethod(godot_io_instance, _get_window_inset); - jint *inset_arr = env->GetIntArrayElements(ret, JNI_FALSE); - Rect2 inset_rect(inset_arr[0], inset_arr[1], inset_arr[2], inset_arr[3]); - env->ReleaseIntArrayElements(ret, inset_arr, JNI_ABORT); - return inset_rect; - } else { - return Rect2(); - } -} diff --git a/platform/android/java_godot_io_wrapper.h b/platform/android/java_godot_io_wrapper.h deleted file mode 100644 index 9205ad91565f..000000000000 --- a/platform/android/java_godot_io_wrapper.h +++ /dev/null @@ -1,91 +0,0 @@ -/*************************************************************************/ -/* java_godot_io_wrapper.h */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -// note, swapped java and godot around in the file name so all the java -// wrappers are together - -#ifndef JAVA_GODOT_IO_WRAPPER_H -#define JAVA_GODOT_IO_WRAPPER_H - -#include -#include - -#include "core/math/rect2.h" -#include "string_android.h" - -// Class that makes functions in java/src/org/godotengine/godot/GodotIO.java callable from C++ -class GodotIOJavaWrapper { -private: - jobject godot_io_instance; - jclass cls; - - jmethodID _open_URI = 0; - jmethodID _get_data_dir = 0; - jmethodID _get_locale = 0; - jmethodID _get_model = 0; - jmethodID _get_screen_DPI = 0; - jmethodID _get_unique_id = 0; - jmethodID _show_keyboard = 0; - jmethodID _hide_keyboard = 0; - jmethodID _set_screen_orientation = 0; - jmethodID _get_system_dir = 0; - jmethodID _play_video = 0; - jmethodID _is_video_playing = 0; - jmethodID _pause_video = 0; - jmethodID _stop_video = 0; - jmethodID _get_window_inset = 0; - -public: - GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instance); - ~GodotIOJavaWrapper(); - - jobject get_instance(); - - Error open_uri(const String &p_uri); - String get_user_data_dir(); - String get_locale(); - String get_model(); - int get_screen_dpi(); - String get_unique_id(); - bool has_vk(); - void show_vk(const String &p_existing); - void hide_vk(); - int get_vk_height(); - void set_vk_height(int p_height); - void set_screen_orientation(int p_orient); - String get_system_dir(int p_dir); - void play_video(const String &p_path); - bool is_video_playing(); - void pause_video(); - void stop_video(); - Rect2 get_window_inset(); -}; - -#endif /* !JAVA_GODOT_IO_WRAPPER_H */ diff --git a/platform/android/os_android.cpp b/platform/android/os_android.cpp index b0e266a0c9a2..d9db09bae862 100644 --- a/platform/android/os_android.cpp +++ b/platform/android/os_android.cpp @@ -273,7 +273,7 @@ Size2 OS_Android::get_window_size() const { } Rect2 OS_Android::get_window_safe_area() const { - Rect2 inset = godot_io_java->get_window_inset(); + Rect2 inset = get_window_inset(); Size2 window_size = get_window_size(); return Rect2( @@ -282,7 +282,7 @@ Rect2 OS_Android::get_window_safe_area() const { window_size.height - inset.size.height - inset.position.y); } -String OS_Android::get_name() const { +String OS_Android::get_name() { return "Android"; } @@ -523,6 +523,13 @@ int OS_Android::get_virtual_keyboard_height() const { return 0; } +Rect2 OS_Android::get_window_inset() const { + if (get_window_inset_func) { + return get_window_inset_func(); + } +} + + void OS_Android::show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect) { if (show_virtual_keyboard_func) { @@ -735,7 +742,7 @@ bool OS_Android::_check_internal_feature_support(const String &p_feature) { return false; } -OS_Android::OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetUserDataDirFunc p_get_user_data_dir_func, GetLocaleFunc p_get_locale_func, GetModelFunc p_get_model_func, GetScreenDPIFunc p_get_screen_dpi_func, ShowVirtualKeyboardFunc p_show_vk, HideVirtualKeyboardFunc p_hide_vk, VirtualKeyboardHeightFunc p_vk_height_func, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_func, GetGLVersionCodeFunc p_get_gl_version_func, VideoPlayFunc p_video_play_func, VideoIsPlayingFunc p_video_is_playing_func, VideoPauseFunc p_video_pause_func, VideoStopFunc p_video_stop_func, SetKeepScreenOnFunc p_set_keep_screen_on_func, AlertFunc p_alert_func, SetClipboardFunc p_set_clipboard_func, GetClipboardFunc p_get_clipboard_func, RequestPermissionFunc p_request_permission, bool p_use_apk_expansion, VibrateFunc p_vibrate_func) { +OS_Android::OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetUserDataDirFunc p_get_user_data_dir_func, GetLocaleFunc p_get_locale_func, GetModelFunc p_get_model_func, GetScreenDPIFunc p_get_screen_dpi_func, ShowVirtualKeyboardFunc p_show_vk, HideVirtualKeyboardFunc p_hide_vk, VirtualKeyboardHeightFunc p_vk_height_func, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_func, GetGLVersionCodeFunc p_get_gl_version_func, VideoPlayFunc p_video_play_func, VideoIsPlayingFunc p_video_is_playing_func, VideoPauseFunc p_video_pause_func, VideoStopFunc p_video_stop_func, SetKeepScreenOnFunc p_set_keep_screen_on_func, AlertFunc p_alert_func, SetClipboardFunc p_set_clipboard_func, GetClipboardFunc p_get_clipboard_func, RequestPermissionFunc p_request_permission, bool p_use_apk_expansion, VibrateFunc p_vibrate_func, GetWindowInsetFunc p_get_window_inset_func ) { use_apk_expansion = p_use_apk_expansion; default_videomode.width = 800; @@ -758,6 +765,7 @@ OS_Android::OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURI get_unique_id_func = p_get_unique_id; get_system_dir_func = p_get_sdir_func; get_gl_version_code_func = p_get_gl_version_func; + get_window_inset_func = p_get_window_inset_func; video_play_func = p_video_play_func; video_is_playing_func = p_video_is_playing_func; diff --git a/platform/android/os_android.h b/platform/android/os_android.h index f2e276af28bf..03cbb97c4079 100644 --- a/platform/android/os_android.h +++ b/platform/android/os_android.h @@ -55,6 +55,7 @@ typedef void (*HideVirtualKeyboardFunc)(); typedef void (*SetScreenOrientationFunc)(int); typedef String (*GetSystemDirFunc)(int); typedef int (*GetGLVersionCodeFunc)(); +typedef Rect2 (*GetWindowInsetFunc)(); typedef void (*VideoPlayFunc)(const String &); typedef bool (*VideoIsPlayingFunc)(); @@ -127,6 +128,7 @@ class OS_Android : public OS_Unix { GetUniqueIDFunc get_unique_id_func; GetSystemDirFunc get_system_dir_func; GetGLVersionCodeFunc get_gl_version_code_func; + GetWindowInsetFunc get_window_inset_func; VideoPlayFunc video_play_func; VideoIsPlayingFunc video_is_playing_func; @@ -204,6 +206,7 @@ class OS_Android : public OS_Unix { virtual void show_virtual_keyboard(const String &p_existing_text, const Rect2 &p_screen_rect = Rect2()); virtual void hide_virtual_keyboard(); virtual int get_virtual_keyboard_height() const; + virtual Rect2 get_window_inset() const; void set_opengl_extensions(const char *p_gl_extensions); void set_display_size(Size2 p_size); @@ -245,7 +248,7 @@ class OS_Android : public OS_Unix { void vibrate_handheld(int p_duration_ms); virtual bool _check_internal_feature_support(const String &p_feature); - OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetUserDataDirFunc p_get_user_data_dir_func, GetLocaleFunc p_get_locale_func, GetModelFunc p_get_model_func, GetScreenDPIFunc p_get_screen_dpi_func, ShowVirtualKeyboardFunc p_show_vk, HideVirtualKeyboardFunc p_hide_vk, VirtualKeyboardHeightFunc p_vk_height_func, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_func, GetGLVersionCodeFunc p_get_gl_version_func, VideoPlayFunc p_video_play_func, VideoIsPlayingFunc p_video_is_playing_func, VideoPauseFunc p_video_pause_func, VideoStopFunc p_video_stop_func, SetKeepScreenOnFunc p_set_keep_screen_on_func, AlertFunc p_alert_func, SetClipboardFunc p_set_clipboard, GetClipboardFunc p_get_clipboard, RequestPermissionFunc p_request_permission, bool p_use_apk_expansion, VibrateFunc vibrate_func); + OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetUserDataDirFunc p_get_user_data_dir_func, GetLocaleFunc p_get_locale_func, GetModelFunc p_get_model_func, GetScreenDPIFunc p_get_screen_dpi_func, ShowVirtualKeyboardFunc p_show_vk, HideVirtualKeyboardFunc p_hide_vk, VirtualKeyboardHeightFunc p_vk_height_func, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_func, GetGLVersionCodeFunc p_get_gl_version_func, VideoPlayFunc p_video_play_func, VideoIsPlayingFunc p_video_is_playing_func, VideoPauseFunc p_video_pause_func, VideoStopFunc p_video_stop_func, SetKeepScreenOnFunc p_set_keep_screen_on_func, AlertFunc p_alert_func, SetClipboardFunc p_set_clipboard, GetClipboardFunc p_get_clipboard, RequestPermissionFunc p_request_permission, bool p_use_apk_expansion, VibrateFunc vibrate_func, GetWindowInsetFunc p_get_window_inset_func); ~OS_Android(); };