diff --git a/src/SFML/Window/Android/ClipboardImpl.cpp b/src/SFML/Window/Android/ClipboardImpl.cpp index d48ef2d9..47ed608f 100644 --- a/src/SFML/Window/Android/ClipboardImpl.cpp +++ b/src/SFML/Window/Android/ClipboardImpl.cpp @@ -26,8 +26,10 @@ // Headers //////////////////////////////////////////////////////////// #include +#include #include - +#include +#include namespace sf { @@ -36,8 +38,81 @@ namespace priv //////////////////////////////////////////////////////////// String ClipboardImpl::getString() { - sf::err() << "Clipboard API not implemented for Android.\n"; - return String(); + String content; + + ActivityStates* states = getActivity(NULL); + Lock lock(states->mutex); + + // Initializes JNI + jint lResult; + jint lFlags = 0; + + JavaVM* lJavaVM = states->activity->vm; + JNIEnv* lJNIEnv = states->activity->env; + + JavaVMAttachArgs lJavaVMAttachArgs; + lJavaVMAttachArgs.version = JNI_VERSION_1_6; + lJavaVMAttachArgs.name = "NativeThread"; + lJavaVMAttachArgs.group = NULL; + + lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs); + + if (lResult == JNI_ERR) + err() << "Failed to initialize JNI, couldn't switch the keyboard visibility" << std::endl; + + // Retrieves NativeActivity + jobject lNativeActivity = states->activity->clazz; + jclass ClassNativeActivity = lJNIEnv->GetObjectClass(lNativeActivity); + + // Retrieves Context.CLIPBOARD_SERVICE + jclass ClassContext = lJNIEnv->FindClass("android/content/Context"); + jfieldID FieldCLIPBOARD_SERVICE = lJNIEnv->GetStaticFieldID(ClassContext, + "CLIPBOARD_SERVICE", "Ljava/lang/String;"); + jobject CLIPBOARD_SERVICE = lJNIEnv->GetStaticObjectField(ClassContext, + FieldCLIPBOARD_SERVICE); + lJNIEnv->DeleteLocalRef(ClassContext); + + // Runs getSystemService(Context.CLIPBOARD_SERVICE) + jclass ClassInputMethodManager = + lJNIEnv->FindClass("android/view/inputmethod/InputMethodManager"); + jmethodID MethodGetSystemService = lJNIEnv->GetMethodID(ClassNativeActivity, + "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"); + jobject lInputMethodManager = lJNIEnv->CallObjectMethod(lNativeActivity, + MethodGetSystemService, CLIPBOARD_SERVICE); + lJNIEnv->DeleteLocalRef(CLIPBOARD_SERVICE); + + /*if (visible) + { + // Runs lInputMethodManager.showSoftInput(...) + jmethodID MethodShowSoftInput = lJNIEnv->GetMethodID(ClassInputMethodManager, + "showSoftInput", "(Landroid/view/View;I)Z"); + jboolean lResult = lJNIEnv->CallBooleanMethod(lInputMethodManager, + MethodShowSoftInput, lDecorView, lFlags); + } + else + { + // Runs lWindow.getViewToken() + jclass ClassView = lJNIEnv->FindClass("android/view/View"); + jmethodID MethodGetWindowToken = lJNIEnv->GetMethodID(ClassView, + "getWindowToken", "()Landroid/os/IBinder;"); + jobject lBinder = lJNIEnv->CallObjectMethod(lDecorView, + MethodGetWindowToken); + lJNIEnv->DeleteLocalRef(ClassView); + + // lInputMethodManager.hideSoftInput(...) + jmethodID MethodHideSoftInput = lJNIEnv->GetMethodID(ClassInputMethodManager, + "hideSoftInputFromWindow", "(Landroid/os/IBinder;I)Z"); + jboolean lRes = lJNIEnv->CallBooleanMethod(lInputMethodManager, + MethodHideSoftInput, lBinder, lFlags); + lJNIEnv->DeleteLocalRef(lBinder); + }*/ + lJNIEnv->DeleteLocalRef(ClassNativeActivity); + lJNIEnv->DeleteLocalRef(ClassInputMethodManager); + + // Finished with the JVM + lJavaVM->DetachCurrentThread(); + + return content; }