Add clipboard operation for android OS

This commit is contained in:
Xavier Sellier 2018-04-03 11:58:17 -04:00
parent 3a8f8381f2
commit f68e127079
4 changed files with 70 additions and 10 deletions

View file

@ -59,6 +59,9 @@ import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.media.MediaPlayer;
import android.content.ClipboardManager;
import android.content.ClipData;
import java.lang.reflect.Method;
import java.util.List;
import java.util.ArrayList;
@ -103,6 +106,7 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC
private TextView mAverageSpeed;
private TextView mTimeRemaining;
private ProgressBar mPB;
private ClipboardManager mClipboard;
private View mDashboard;
private View mCellMessage;
@ -441,6 +445,7 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC
Window window = getWindow();
//window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
mClipboard = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE);
//check for apk expansion API
if (true) {
@ -607,6 +612,24 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC
}
}
public String getClipboard() {
String copiedText = "";
if (mClipboard.getPrimaryClip() != null) {
ClipData.Item item = mClipboard.getPrimaryClip().getItemAt(0);
copiedText = item.getText().toString();
}
return copiedText;
}
public void setClipboard(String p_text) {
ClipData clip = ClipData.newPlainText("myLabel", p_text);
mClipboard.setPrimaryClip(clip);
}
@Override
protected void onResume() {
super.onResume();

View file

@ -607,6 +607,8 @@ static jobject _godot_instance;
static jmethodID _openURI = 0;
static jmethodID _getDataDir = 0;
static jmethodID _getLocale = 0;
static jmethodID _getClipboard = 0;
static jmethodID _setClipboard = 0;
static jmethodID _getModel = 0;
static jmethodID _getScreenDPI = 0;
static jmethodID _showKeyboard = 0;
@ -646,6 +648,19 @@ static String _get_locale() {
return String(env->GetStringUTFChars(s, NULL));
}
static String _get_clipboard() {
JNIEnv *env = ThreadAndroid::get_env();
jstring s = (jstring)env->CallObjectMethod(_godot_instance, _getClipboard);
return String(env->GetStringUTFChars(s, NULL));
}
static void _set_clipboard(const String &p_text) {
JNIEnv *env = ThreadAndroid::get_env();
jstring jStr = env->NewStringUTF(p_text.utf8().get_data());
env->CallVoidMethod(_godot_instance, _setClipboard, jStr);
}
static String _get_model() {
JNIEnv *env = ThreadAndroid::get_env();
@ -774,6 +789,8 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *en
_setKeepScreenOn = env->GetMethodID(cls, "setKeepScreenOn", "(Z)V");
_alertDialog = env->GetMethodID(cls, "alert", "(Ljava/lang/String;Ljava/lang/String;)V");
_getGLESVersionCode = env->GetMethodID(cls, "getGLESVersionCode", "()I");
_getClipboard = env->GetMethodID(cls, "getClipboard", "()Ljava/lang/String;");
_setClipboard = env->GetMethodID(cls, "setClipboard", "(Ljava/lang/String;)V");
jclass clsio = env->FindClass("org/godotengine/godot/Godot");
if (cls) {
@ -807,7 +824,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, p_use_apk_expansion);
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, p_use_apk_expansion);
os_android->set_need_reload_hooks(p_need_reload_hook);
char wd[500];

View file

@ -257,13 +257,10 @@ int OS_Android::get_mouse_button_state() const {
return 0;
}
void OS_Android::set_window_title(const String &p_title) {
}
//interesting byt not yet
//void set_clipboard(const String& p_text);
//String get_clipboard() const;
void OS_Android::set_video_mode(const VideoMode &p_video_mode, int p_screen) {
}
@ -599,6 +596,23 @@ String OS_Android::get_locale() const {
return OS_Unix::get_locale();
}
void OS_Android::set_clipboard(const String &p_text) {
if (set_clipboard_func) {
set_clipboard_func(p_text);
} else {
OS_Unix::set_clipboard(p_text);
}
}
String OS_Android::get_clipboard() const {
if (get_clipboard_func) {
return get_clipboard_func();
}
return OS_Unix::get_clipboard();
}
String OS_Android::get_model_name() const {
if (get_model_func)
@ -732,7 +746,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, bool p_use_apk_expansion) {
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, bool p_use_apk_expansion) {
use_apk_expansion = p_use_apk_expansion;
default_videomode.width = 800;
@ -765,6 +779,9 @@ OS_Android::OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURI
hide_virtual_keyboard_func = p_hide_vk;
get_virtual_keyboard_height_func = p_vk_height_func;
set_clipboard_func = p_set_clipboard_func;
get_clipboard_func = p_get_clipboard_func;
set_screen_orientation_func = p_screen_orient;
set_keep_screen_on_func = p_set_keep_screen_on_func;
alert_func = p_alert_func;

View file

@ -51,6 +51,8 @@ typedef void (*GFXInitFunc)(void *ud, bool gl2);
typedef int (*OpenURIFunc)(const String &);
typedef String (*GetUserDataDirFunc)();
typedef String (*GetLocaleFunc)();
typedef void (*SetClipboardFunc)(const String &);
typedef String (*GetClipboardFunc)();
typedef String (*GetModelFunc)();
typedef int (*GetScreenDPIFunc)();
typedef String (*GetUniqueIDFunc)();
@ -119,6 +121,8 @@ private:
OpenURIFunc open_uri_func;
GetUserDataDirFunc get_user_data_dir_func;
GetLocaleFunc get_locale_func;
SetClipboardFunc set_clipboard_func;
GetClipboardFunc get_clipboard_func;
GetModelFunc get_model_func;
GetScreenDPIFunc get_screen_dpi_func;
ShowVirtualKeyboardFunc show_virtual_keyboard_func;
@ -172,9 +176,6 @@ public:
virtual int get_mouse_button_state() const;
virtual void set_window_title(const String &p_title);
//virtual void set_clipboard(const String& p_text);
//virtual String get_clipboard() const;
virtual void set_video_mode(const VideoMode &p_video_mode, int p_screen = 0);
virtual VideoMode get_video_mode(int p_screen = 0) const;
virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;
@ -218,6 +219,8 @@ public:
virtual String get_user_data_dir() const;
virtual String get_resource_dir() const;
virtual String get_locale() const;
virtual void set_clipboard(const String &p_text);
virtual String get_clipboard() const;
virtual String get_model_name() const;
virtual int get_screen_dpi(int p_screen = 0) const;
@ -244,7 +247,7 @@ public:
void joy_connection_changed(int p_device, bool p_connected, String p_name);
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, bool p_use_apk_expansion);
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, bool p_use_apk_expansion);
~OS_Android();
};