55 lines
1.5 KiB
C++
Executable File
55 lines
1.5 KiB
C++
Executable File
#include "jni.hpp"
|
|
|
|
#include <mbgl/util/logging.hpp>
|
|
|
|
namespace mbgl {
|
|
namespace android {
|
|
|
|
JavaVM* theJVM;
|
|
|
|
//TODO: remove
|
|
bool attach_jni_thread(JavaVM* vm, JNIEnv** env, std::string threadName) {
|
|
assert(vm != nullptr);
|
|
assert(env != nullptr);
|
|
|
|
JavaVMAttachArgs args = {JNI_VERSION_1_2, threadName.c_str(), NULL};
|
|
|
|
jint ret;
|
|
*env = nullptr;
|
|
bool detach = false;
|
|
ret = vm->GetEnv(reinterpret_cast<void **>(env), JNI_VERSION_1_6);
|
|
if (ret != JNI_OK) {
|
|
if (ret != JNI_EDETACHED) {
|
|
mbgl::Log::Error(mbgl::Event::JNI, "GetEnv() failed with %i", ret);
|
|
throw std::runtime_error("GetEnv() failed");
|
|
} else {
|
|
ret = vm->AttachCurrentThread(env, &args);
|
|
if (ret != JNI_OK) {
|
|
mbgl::Log::Error(mbgl::Event::JNI, "AttachCurrentThread() failed with %i", ret);
|
|
throw std::runtime_error("AttachCurrentThread() failed");
|
|
}
|
|
detach = true;
|
|
}
|
|
}
|
|
|
|
return detach;
|
|
}
|
|
|
|
//TODO: remove
|
|
void detach_jni_thread(JavaVM* vm, JNIEnv** env, bool detach) {
|
|
if (detach) {
|
|
assert(vm != nullptr);
|
|
assert(env != nullptr);
|
|
|
|
jint ret;
|
|
if ((ret = vm->DetachCurrentThread()) != JNI_OK) {
|
|
mbgl::Log::Error(mbgl::Event::JNI, "DetachCurrentThread() failed with %i", ret);
|
|
throw std::runtime_error("DetachCurrentThread() failed");
|
|
}
|
|
}
|
|
*env = nullptr;
|
|
}
|
|
|
|
} // namespace android
|
|
} // namespace mbgl
|