summaryrefslogtreecommitdiff
path: root/platform/android/src/attach_env.cpp
blob: 6da075ee3e1e69693af2107cc4c1b286def5b172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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:
        return UniqueEnv(env, JNIEnvDeleter(*theJVM, false));
    case JNI_EDETACHED:
        return UniqueEnv(jni::AttachCurrentThread(*theJVM).release(), JNIEnvDeleter(*theJVM, true));
    default:
        throw std::system_error(err,  jni::ErrorCategory());
    }
}

}
}