Merge pull request #17950 from xsellier/bugfix/clipboard-not-working-on-android

Add clipboard operation for android OS
This commit is contained in:
Rémi Verschelde 2018-04-04 12:39:58 +02:00 committed by GitHub
commit e5979f5772
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 48 deletions

View File

@ -57,6 +57,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;
@ -101,6 +104,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;
@ -396,6 +400,7 @@ public class Godot extends Activity implements SensorEventListener, IDownloaderC
_self = this;
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
if (true) {
boolean md5mismatch = false;
@ -563,6 +568,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

@ -622,6 +622,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;
@ -660,6 +662,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();
@ -770,6 +785,8 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *en
_on_video_init = env->GetMethodID(cls, "onVideoInit", "(Z)V");
_setKeepScreenOn = env->GetMethodID(cls, "setKeepScreenOn", "(Z)V");
_alertDialog = env->GetMethodID(cls, "alert", "(Ljava/lang/String;Ljava/lang/String;)V");
_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) {
@ -828,7 +845,7 @@ JNIEXPORT void JNICALL Java_org_godotengine_godot_GodotLib_initialize(JNIEnv *en
}
}
os_android = new OS_Android(_gfx_init_func, env, _open_uri, _get_data_dir, _get_locale, _get_model, _get_screen_dpi, _show_vk, _hide_vk, _set_screen_orient, _get_unique_id, _get_system_dir, _play_video, _is_video_playing, _pause_video, _stop_video, _set_keep_screen_on, _alert, use_apk_expansion);
os_android = new OS_Android(_gfx_init_func, env, _open_uri, _get_data_dir, _get_locale, _get_model, _get_screen_dpi, _show_vk, _hide_vk, _set_screen_orient, _get_unique_id, _get_system_dir, _play_video, _is_video_playing, _pause_video, _stop_video, _set_keep_screen_on, _alert, use_apk_expansion, _set_clipboard, _get_clipboard);
os_android->set_need_reload_hooks(p_need_reload_hook);
char wd[500];

View File

@ -177,43 +177,6 @@ void OS_Android::set_main_loop(MainLoop *p_main_loop) {
main_loop = p_main_loop;
input->set_main_loop(p_main_loop);
#if 0
print_line("preGS");
FileAccess *f=memnew( FileAccessAndroid );
print("made f %p\n",f);
Error err = f->open("AndroidManifest.xml",FileAccess::READ);
if (err) {
print("************NO FILE!!\n");
} else {
print("************YES FILE!!\n");
}
f->close();
print_line("end");
AAssetDir* aad = AAssetManager_openDir(FileAccessAndroid::asset_manager,".");
if (aad) {
print_line("DIR OPEN OK");
const char *fn= AAssetDir_getNextFileName(aad);
while(fn) {
print_line("FNAME: "+String(fn));
fn= AAssetDir_getNextFileName(aad);
}
AAssetDir_close(aad);
} else {
print_line("DIR NO OPEN");
}
#endif
}
void OS_Android::delete_main_loop() {
@ -271,13 +234,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) {
}
@ -324,7 +284,7 @@ void OS_Android::set_cursor_shape(CursorShape p_shape) {
//android really really really has no mouse.. how amazing..
}
void OS_Android::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
void OS_Android::set_custom_mouse_cursor(const RES &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) {
// Since android has no mouse, we do not need to change its texture (how amazing !)
}
@ -678,6 +638,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)
@ -791,7 +768,7 @@ String OS_Android::get_joy_guid(int p_device) const {
return input->get_joy_guid_remapped(p_device);
}
OS_Android::OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_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, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_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, GetDataDirFunc p_get_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, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_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, SetClipboardFunc p_set_clipboard_func, GetClipboardFunc p_get_clipboard_func) {
use_apk_expansion = p_use_apk_expansion;
default_videomode.width = 800;
@ -827,6 +804,9 @@ OS_Android::OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURI
set_keep_screen_on_func = p_set_keep_screen_on_func;
alert_func = p_alert_func;
use_reload_hooks = false;
set_clipboard_func = p_set_clipboard_func;
get_clipboard_func = p_get_clipboard_func;
}
OS_Android::~OS_Android() {

View File

@ -61,6 +61,8 @@ typedef void (*GFXInitFunc)(void *ud, bool gl2);
typedef int (*OpenURIFunc)(const String &);
typedef String (*GetDataDirFunc)();
typedef String (*GetLocaleFunc)();
typedef void (*SetClipboardFunc)(const String &);
typedef String (*GetClipboardFunc)();
typedef String (*GetModelFunc)();
typedef int (*GetScreenDPIFunc)();
typedef String (*GetUniqueIDFunc)();
@ -139,6 +141,8 @@ private:
OpenURIFunc open_uri_func;
GetDataDirFunc get_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;
@ -187,9 +191,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;
@ -232,6 +233,8 @@ public:
virtual String get_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;
@ -257,7 +260,7 @@ public:
virtual String get_joy_guid(int p_device) const;
void joy_connection_changed(int p_device, bool p_connected, String p_name);
OS_Android(GFXInitFunc p_gfx_init_func, void *p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_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, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_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, GetDataDirFunc p_get_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, SetScreenOrientationFunc p_screen_orient, GetUniqueIDFunc p_get_unique_id, GetSystemDirFunc p_get_sdir_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, SetClipboardFunc p_set_clipboard_func, GetClipboardFunc p_get_clipboard_func);
~OS_Android();
};