summaryrefslogtreecommitdiff
path: root/platform/android/src/attach_env.cpp
blob: 0dc8069284a6fee3792565ef69b91e656e6973b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <android/log.h>
#include <mbgl/util/platform.hpp>
#include "attach_env.hpp"
#include "jni.hpp"

namespace mbgl {
namespace android {

UniqueEnv AttachEnv() {
    JNIEnv* env = nullptr;
    jint err = theJVM->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);

    switch (err) {
    case JNI_OK:
        __android_log_write(ANDROID_LOG_ERROR, "JNI_THREAD", ("is attached: " + mbgl::platform::getCurrentThreadName()).c_str());
        return UniqueEnv(env, JNIEnvDeleter(*theJVM, false));
    case JNI_EDETACHED:
        __android_log_write(ANDROID_LOG_ERROR, "JNI_THREAD", ("is detached: " + mbgl::platform::getCurrentThreadName()).c_str());
        return UniqueEnv(jni::AttachCurrentThread(*theJVM).release(), JNIEnvDeleter(*theJVM, true));
    default:
        throw std::system_error(err,  jni::ErrorCategory());
    }
}

}
}