src/core/android/SDL_android.cpp
changeset 6464 ab55284b389f
parent 6448 64a6297a8b93
child 6555 f2c03c06d987
equal deleted inserted replaced
6463:051403ca44cc 6464:ab55284b389f
   733 extern "C" int Android_JNI_FileClose(SDL_RWops* ctx)
   733 extern "C" int Android_JNI_FileClose(SDL_RWops* ctx)
   734 {
   734 {
   735     return Android_JNI_FileClose(ctx, true);
   735     return Android_JNI_FileClose(ctx, true);
   736 }
   736 }
   737 
   737 
       
   738 // returns a new global reference which needs to be released later
       
   739 static jobject Android_JNI_GetSystemServiceObject(const char* name)
       
   740 {
       
   741     LocalReferenceHolder refs;
       
   742     JNIEnv* env = Android_JNI_GetEnv();
       
   743     if (!refs.init(env)) {
       
   744         return NULL;
       
   745     }
       
   746 
       
   747     jstring service = env->NewStringUTF(name);
       
   748 
       
   749     jmethodID mid;
       
   750 
       
   751     mid = env->GetStaticMethodID(mActivityClass, "getContext", "()Landroid/content/Context;");
       
   752     jobject context = env->CallStaticObjectMethod(mActivityClass, mid);
       
   753 
       
   754     mid = env->GetMethodID(mActivityClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
       
   755     jobject manager = env->CallObjectMethod(context, mid, service);
       
   756 
       
   757     env->DeleteLocalRef(service);
       
   758 
       
   759     return manager ? env->NewGlobalRef(manager) : NULL;
       
   760 }
       
   761 
       
   762 #define SETUP_CLIPBOARD(error) \
       
   763     LocalReferenceHolder refs; \
       
   764     JNIEnv* env = Android_JNI_GetEnv(); \
       
   765     if (!refs.init(env)) { \
       
   766         return error; \
       
   767     } \
       
   768     jobject clipboard = Android_JNI_GetSystemServiceObject("clipboard"); \
       
   769     if (!clipboard) { \
       
   770         return error; \
       
   771     }
       
   772 
       
   773 extern "C" int Android_JNI_SetClipboardText(const char* text)
       
   774 {
       
   775     SETUP_CLIPBOARD(-1)
       
   776 
       
   777     jmethodID mid = env->GetMethodID(env->GetObjectClass(clipboard), "setText", "(Ljava/lang/CharSequence;)V");
       
   778     jstring string = env->NewStringUTF(text);
       
   779     env->CallVoidMethod(clipboard, mid, string);
       
   780     env->DeleteGlobalRef(clipboard);
       
   781     env->DeleteLocalRef(string);
       
   782     return 0;
       
   783 }
       
   784 
       
   785 extern "C" char* Android_JNI_GetClipboardText()
       
   786 {
       
   787     SETUP_CLIPBOARD(SDL_strdup(""))
       
   788 
       
   789     jmethodID mid = env->GetMethodID(env->GetObjectClass(clipboard), "getText", "()Ljava/lang/CharSequence;");
       
   790     jobject sequence = env->CallObjectMethod(clipboard, mid);
       
   791     env->DeleteGlobalRef(clipboard);
       
   792     if (sequence) {
       
   793         mid = env->GetMethodID(env->GetObjectClass(sequence), "toString", "()Ljava/lang/String;");
       
   794         jstring string = reinterpret_cast<jstring>(env->CallObjectMethod(sequence, mid));
       
   795         const char* utf = env->GetStringUTFChars(string, 0);
       
   796         if (utf) {
       
   797             char* text = SDL_strdup(utf);
       
   798             env->ReleaseStringUTFChars(string, utf);
       
   799             return text;
       
   800         }
       
   801     }
       
   802     return SDL_strdup("");
       
   803 }
       
   804 
       
   805 extern "C" SDL_bool Android_JNI_HasClipboardText()
       
   806 {
       
   807     SETUP_CLIPBOARD(SDL_FALSE)
       
   808 
       
   809     jmethodID mid = env->GetMethodID(env->GetObjectClass(clipboard), "hasText", "()Z");
       
   810     jboolean has = env->CallBooleanMethod(clipboard, mid);
       
   811     env->DeleteGlobalRef(clipboard);
       
   812     return has ? SDL_TRUE : SDL_FALSE;
       
   813 }
       
   814 
       
   815 
   738 // returns 0 on success or -1 on error (others undefined then)
   816 // returns 0 on success or -1 on error (others undefined then)
   739 // returns truthy or falsy value in plugged, charged and battery
   817 // returns truthy or falsy value in plugged, charged and battery
   740 // returns the value in seconds and percent or -1 if not available
   818 // returns the value in seconds and percent or -1 if not available
   741 extern "C" int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seconds, int* percent)
   819 extern "C" int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seconds, int* percent)
   742 {
   820 {