src/core/android/SDL_android.cpp
changeset 6464 ab55284b389f
parent 6448 64a6297a8b93
child 6555 f2c03c06d987
     1.1 --- a/src/core/android/SDL_android.cpp	Wed Sep 26 15:57:28 2012 -0300
     1.2 +++ b/src/core/android/SDL_android.cpp	Wed Sep 26 20:14:37 2012 -0700
     1.3 @@ -735,6 +735,84 @@
     1.4      return Android_JNI_FileClose(ctx, true);
     1.5  }
     1.6  
     1.7 +// returns a new global reference which needs to be released later
     1.8 +static jobject Android_JNI_GetSystemServiceObject(const char* name)
     1.9 +{
    1.10 +    LocalReferenceHolder refs;
    1.11 +    JNIEnv* env = Android_JNI_GetEnv();
    1.12 +    if (!refs.init(env)) {
    1.13 +        return NULL;
    1.14 +    }
    1.15 +
    1.16 +    jstring service = env->NewStringUTF(name);
    1.17 +
    1.18 +    jmethodID mid;
    1.19 +
    1.20 +    mid = env->GetStaticMethodID(mActivityClass, "getContext", "()Landroid/content/Context;");
    1.21 +    jobject context = env->CallStaticObjectMethod(mActivityClass, mid);
    1.22 +
    1.23 +    mid = env->GetMethodID(mActivityClass, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
    1.24 +    jobject manager = env->CallObjectMethod(context, mid, service);
    1.25 +
    1.26 +    env->DeleteLocalRef(service);
    1.27 +
    1.28 +    return manager ? env->NewGlobalRef(manager) : NULL;
    1.29 +}
    1.30 +
    1.31 +#define SETUP_CLIPBOARD(error) \
    1.32 +    LocalReferenceHolder refs; \
    1.33 +    JNIEnv* env = Android_JNI_GetEnv(); \
    1.34 +    if (!refs.init(env)) { \
    1.35 +        return error; \
    1.36 +    } \
    1.37 +    jobject clipboard = Android_JNI_GetSystemServiceObject("clipboard"); \
    1.38 +    if (!clipboard) { \
    1.39 +        return error; \
    1.40 +    }
    1.41 +
    1.42 +extern "C" int Android_JNI_SetClipboardText(const char* text)
    1.43 +{
    1.44 +    SETUP_CLIPBOARD(-1)
    1.45 +
    1.46 +    jmethodID mid = env->GetMethodID(env->GetObjectClass(clipboard), "setText", "(Ljava/lang/CharSequence;)V");
    1.47 +    jstring string = env->NewStringUTF(text);
    1.48 +    env->CallVoidMethod(clipboard, mid, string);
    1.49 +    env->DeleteGlobalRef(clipboard);
    1.50 +    env->DeleteLocalRef(string);
    1.51 +    return 0;
    1.52 +}
    1.53 +
    1.54 +extern "C" char* Android_JNI_GetClipboardText()
    1.55 +{
    1.56 +    SETUP_CLIPBOARD(SDL_strdup(""))
    1.57 +
    1.58 +    jmethodID mid = env->GetMethodID(env->GetObjectClass(clipboard), "getText", "()Ljava/lang/CharSequence;");
    1.59 +    jobject sequence = env->CallObjectMethod(clipboard, mid);
    1.60 +    env->DeleteGlobalRef(clipboard);
    1.61 +    if (sequence) {
    1.62 +        mid = env->GetMethodID(env->GetObjectClass(sequence), "toString", "()Ljava/lang/String;");
    1.63 +        jstring string = reinterpret_cast<jstring>(env->CallObjectMethod(sequence, mid));
    1.64 +        const char* utf = env->GetStringUTFChars(string, 0);
    1.65 +        if (utf) {
    1.66 +            char* text = SDL_strdup(utf);
    1.67 +            env->ReleaseStringUTFChars(string, utf);
    1.68 +            return text;
    1.69 +        }
    1.70 +    }
    1.71 +    return SDL_strdup("");
    1.72 +}
    1.73 +
    1.74 +extern "C" SDL_bool Android_JNI_HasClipboardText()
    1.75 +{
    1.76 +    SETUP_CLIPBOARD(SDL_FALSE)
    1.77 +
    1.78 +    jmethodID mid = env->GetMethodID(env->GetObjectClass(clipboard), "hasText", "()Z");
    1.79 +    jboolean has = env->CallBooleanMethod(clipboard, mid);
    1.80 +    env->DeleteGlobalRef(clipboard);
    1.81 +    return has ? SDL_TRUE : SDL_FALSE;
    1.82 +}
    1.83 +
    1.84 +
    1.85  // returns 0 on success or -1 on error (others undefined then)
    1.86  // returns truthy or falsy value in plugged, charged and battery
    1.87  // returns the value in seconds and percent or -1 if not available