| @@ -0,0 +1,109 @@ |
| 1 | +#ifdef __ANDROID__ |
| 2 | + |
| 3 | +#include "chuck_android.h" |
| 4 | +#include <jni.h> |
| 5 | +#include <cassert> |
| 6 | +#include <memory> |
| 7 | +#include <functional> |
| 8 | + |
| 9 | +static JavaVM *jvm_instance = NULL; |
| 10 | + |
| 11 | +JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) |
| 12 | +{ |
| 13 | + jvm_instance = vm; |
| 14 | + |
| 15 | + return JNI_VERSION_1_6; |
| 16 | +} |
| 17 | + |
| 18 | +JavaVM *ChuckAndroid::getJVM() |
| 19 | +{ |
| 20 | + return jvm_instance; |
| 21 | +} |
| 22 | + |
| 23 | +FILE *ChuckAndroid::getTemporaryFile() |
| 24 | +{ |
| 25 | + JavaVM *jvm = getJVM(); |
| 26 | + if( !jvm ) |
| 27 | + { |
| 28 | + return NULL; |
| 29 | + } |
| 30 | + JNIEnv* env = NULL; |
| 31 | + bool needs_detaching = false; |
| 32 | + int reason = jvm->GetEnv((void **)&env, JNI_VERSION_1_6); |
| 33 | + if( reason != JNI_OK && reason != JNI_EDETACHED ) |
| 34 | + { |
| 35 | + return NULL; |
| 36 | + } |
| 37 | + if( reason == JNI_EDETACHED ) |
| 38 | + { |
| 39 | + if( jvm->AttachCurrentThread(&env, NULL) != 0 ) |
| 40 | + { |
| 41 | + return NULL; |
| 42 | + } |
| 43 | + needs_detaching = true; |
| 44 | + } |
| 45 | + |
| 46 | + std::unique_ptr<JavaVM, std::function<void(JavaVM *)>> guard(jvm, |
| 47 | + [needs_detaching](JavaVM *jvm) |
| 48 | + { |
| 49 | + if (needs_detaching) |
| 50 | + { |
| 51 | + jvm->DetachCurrentThread(); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + assert( env != NULL ); |
| 56 | + #define EXCEPTION_CHECK \ |
| 57 | + if (env->ExceptionCheck()) \ |
| 58 | + { \ |
| 59 | + env->ExceptionDescribe(); \ |
| 60 | + env->ExceptionClear(); \ |
| 61 | + return NULL; \ |
| 62 | + } |
| 63 | + |
| 64 | + // context = android.app.ActivityThread.currentActivityThread().getApplication() |
| 65 | + jclass thread_class = env->FindClass("android/app/ActivityThread"); |
| 66 | + EXCEPTION_CHECK; |
| 67 | + jmethodID get_thread = env->GetStaticMethodID( |
| 68 | + thread_class, "currentActivityThread", "()Landroid/app/ActivityThread;"); |
| 69 | + EXCEPTION_CHECK; |
| 70 | + jobject current_thread = env->CallStaticObjectMethod(thread_class, get_thread); |
| 71 | + EXCEPTION_CHECK; |
| 72 | + jmethodID get_application = env->GetMethodID( |
| 73 | + thread_class, "getApplication", "()Landroid/app/Application;"); |
| 74 | + EXCEPTION_CHECK; |
| 75 | + jobject context = env->CallObjectMethod(current_thread, get_application); |
| 76 | + EXCEPTION_CHECK; |
| 77 | + |
| 78 | + // cache_dir = context.getCacheDir() |
| 79 | + jclass context_class = env->FindClass("android/content/Context"); |
| 80 | + EXCEPTION_CHECK; |
| 81 | + jmethodID get_cache_dir = env->GetMethodID(context_class, "getCacheDir", "()Ljava/io/File;"); |
| 82 | + EXCEPTION_CHECK; |
| 83 | + jobject cache_dir = env->CallObjectMethod(context, get_cache_dir); |
| 84 | + EXCEPTION_CHECK; |
| 85 | + |
| 86 | + // temp_file = cache_dir.createTempFile("chuck_temp", "", cache_dir) |
| 87 | + jclass file_class = env->FindClass("java/io/File"); |
| 88 | + EXCEPTION_CHECK; |
| 89 | + jmethodID create_temp_file = env->GetStaticMethodID(file_class, "createTempFile", |
| 90 | + "(Ljava/lang/String;Ljava/lang/String;Ljava/io/File;)Ljava/io/File;"); |
| 91 | + EXCEPTION_CHECK; |
| 92 | + jobject temp_file = env->CallStaticObjectMethod(file_class, create_temp_file, |
| 93 | + env->NewStringUTF("chuck_temp"), env->NewStringUTF(""), cache_dir); |
| 94 | + EXCEPTION_CHECK; |
| 95 | + |
| 96 | + // temp_file_path = temp_file.getPath() |
| 97 | + jmethodID get_path = env->GetMethodID(file_class, "getPath", "()Ljava/lang/String;"); |
| 98 | + EXCEPTION_CHECK; |
| 99 | + jstring temp_file_path = (jstring)env->CallObjectMethod(temp_file, get_path); |
| 100 | + EXCEPTION_CHECK; |
| 101 | + |
| 102 | + const char *path_chars = env->GetStringUTFChars(temp_file_path, NULL); |
| 103 | + FILE *ret = fopen(path_chars, "wb+D"); |
| 104 | + env->ReleaseStringUTFChars(temp_file_path, path_chars); |
| 105 | + |
| 106 | + return ret; |
| 107 | +} |
| 108 | + |
| 109 | +#endif // __ANDROID__ |