2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "JniHelper.h"
|
|
|
|
#include <android/log.h>
|
|
|
|
#include <string.h>
|
2013-07-19 04:41:10 +08:00
|
|
|
#include <pthread.h>
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-08-04 11:30:30 +08:00
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#define LOG_TAG "JniHelper"
|
|
|
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
2014-05-20 18:52:21 +08:00
|
|
|
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-03-25 10:57:29 +08:00
|
|
|
static pthread_key_t g_key;
|
|
|
|
|
2013-07-20 06:01:42 +08:00
|
|
|
jclass _getClassID(const char *className) {
|
2014-05-28 17:41:34 +08:00
|
|
|
if (nullptr == className) {
|
|
|
|
return nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 07:07:34 +08:00
|
|
|
JNIEnv* env = cocos2d::JniHelper::getEnv();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-20 07:07:34 +08:00
|
|
|
jstring _jstrClassName = env->NewStringUTF(className);
|
2013-07-20 06:01:42 +08:00
|
|
|
|
2013-07-20 07:52:51 +08:00
|
|
|
jclass _clazz = (jclass) env->CallObjectMethod(cocos2d::JniHelper::classloader,
|
|
|
|
cocos2d::JniHelper::loadclassMethod_methodID,
|
|
|
|
_jstrClassName);
|
2013-07-20 06:01:42 +08:00
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
if (nullptr == _clazz) {
|
2014-05-20 18:52:21 +08:00
|
|
|
LOGE("Classloader failed to find class of %s", className);
|
|
|
|
env->ExceptionClear();
|
2013-07-20 06:01:42 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 07:07:34 +08:00
|
|
|
env->DeleteLocalRef(_jstrClassName);
|
2013-07-20 06:01:42 +08:00
|
|
|
|
|
|
|
return _clazz;
|
2013-07-19 04:41:10 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-10-08 17:34:06 +08:00
|
|
|
void _detachCurrentThread(void* a) {
|
|
|
|
cocos2d::JniHelper::getJavaVM()->DetachCurrentThread();
|
|
|
|
}
|
|
|
|
|
2013-07-19 04:41:10 +08:00
|
|
|
namespace cocos2d {
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
JavaVM* JniHelper::_psJavaVM = nullptr;
|
|
|
|
jmethodID JniHelper::loadclassMethod_methodID = nullptr;
|
|
|
|
jobject JniHelper::classloader = nullptr;
|
2015-11-11 04:39:16 +08:00
|
|
|
std::unordered_map<JNIEnv*, std::vector<jobject>> JniHelper::localRefs;
|
2013-10-09 18:09:24 +08:00
|
|
|
|
2013-07-19 04:41:10 +08:00
|
|
|
JavaVM* JniHelper::getJavaVM() {
|
|
|
|
pthread_t thisthread = pthread_self();
|
2014-03-25 10:57:29 +08:00
|
|
|
LOGD("JniHelper::getJavaVM(), pthread_self() = %ld", thisthread);
|
2013-07-19 04:41:10 +08:00
|
|
|
return _psJavaVM;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-20 06:18:32 +08:00
|
|
|
void JniHelper::setJavaVM(JavaVM *javaVM) {
|
2013-07-19 04:41:10 +08:00
|
|
|
pthread_t thisthread = pthread_self();
|
2014-03-25 10:57:29 +08:00
|
|
|
LOGD("JniHelper::setJavaVM(%p), pthread_self() = %ld", javaVM, thisthread);
|
2013-07-19 04:41:10 +08:00
|
|
|
_psJavaVM = javaVM;
|
2013-07-20 07:07:34 +08:00
|
|
|
|
2014-10-08 17:34:06 +08:00
|
|
|
pthread_key_create(&g_key, _detachCurrentThread);
|
2013-07-20 07:07:34 +08:00
|
|
|
}
|
|
|
|
|
2014-03-25 10:57:29 +08:00
|
|
|
JNIEnv* JniHelper::cacheEnv(JavaVM* jvm) {
|
2014-05-28 17:41:34 +08:00
|
|
|
JNIEnv* _env = nullptr;
|
2013-07-20 07:07:34 +08:00
|
|
|
// get jni environment
|
|
|
|
jint ret = jvm->GetEnv((void**)&_env, JNI_VERSION_1_4);
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case JNI_OK :
|
|
|
|
// Success!
|
2014-03-25 10:57:29 +08:00
|
|
|
pthread_setspecific(g_key, _env);
|
|
|
|
return _env;
|
2013-07-20 07:07:34 +08:00
|
|
|
|
|
|
|
case JNI_EDETACHED :
|
|
|
|
// Thread not attached
|
2014-05-28 17:41:34 +08:00
|
|
|
if (jvm->AttachCurrentThread(&_env, nullptr) < 0)
|
2013-07-20 07:07:34 +08:00
|
|
|
{
|
2014-05-20 18:52:21 +08:00
|
|
|
LOGE("Failed to get the environment using AttachCurrentThread()");
|
2013-07-20 07:07:34 +08:00
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
return nullptr;
|
2013-07-20 07:07:34 +08:00
|
|
|
} else {
|
|
|
|
// Success : Attached and obtained JNIEnv!
|
2014-03-25 10:57:29 +08:00
|
|
|
pthread_setspecific(g_key, _env);
|
|
|
|
return _env;
|
2013-07-20 07:07:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
case JNI_EVERSION :
|
|
|
|
// Cannot recover from this error
|
2014-05-20 18:52:21 +08:00
|
|
|
LOGE("JNI interface version 1.4 not supported");
|
2013-07-20 07:07:34 +08:00
|
|
|
default :
|
2014-05-20 18:52:21 +08:00
|
|
|
LOGE("Failed to get the environment using GetEnv()");
|
2014-05-28 17:41:34 +08:00
|
|
|
return nullptr;
|
2013-07-20 07:07:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEnv* JniHelper::getEnv() {
|
2014-03-25 10:57:29 +08:00
|
|
|
JNIEnv *_env = (JNIEnv *)pthread_getspecific(g_key);
|
2014-05-28 17:41:34 +08:00
|
|
|
if (_env == nullptr)
|
2014-03-25 10:57:29 +08:00
|
|
|
_env = JniHelper::cacheEnv(_psJavaVM);
|
|
|
|
return _env;
|
2013-07-20 06:01:42 +08:00
|
|
|
}
|
|
|
|
|
2014-03-25 10:57:29 +08:00
|
|
|
bool JniHelper::setClassLoaderFrom(jobject activityinstance) {
|
2013-07-20 06:01:42 +08:00
|
|
|
JniMethodInfo _getclassloaderMethod;
|
2013-07-20 07:26:02 +08:00
|
|
|
if (!JniHelper::getMethodInfo_DefaultClassLoader(_getclassloaderMethod,
|
2014-03-25 10:57:29 +08:00
|
|
|
"android/content/Context",
|
2013-07-20 07:26:02 +08:00
|
|
|
"getClassLoader",
|
|
|
|
"()Ljava/lang/ClassLoader;")) {
|
2013-07-20 06:01:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-25 10:57:29 +08:00
|
|
|
jobject _c = cocos2d::JniHelper::getEnv()->CallObjectMethod(activityinstance,
|
2013-07-20 07:07:34 +08:00
|
|
|
_getclassloaderMethod.methodID);
|
2013-07-20 06:18:32 +08:00
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
if (nullptr == _c) {
|
2013-07-20 06:18:32 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-20 06:01:42 +08:00
|
|
|
JniMethodInfo _m;
|
2013-07-20 07:26:02 +08:00
|
|
|
if (!JniHelper::getMethodInfo_DefaultClassLoader(_m,
|
|
|
|
"java/lang/ClassLoader",
|
|
|
|
"loadClass",
|
|
|
|
"(Ljava/lang/String;)Ljava/lang/Class;")) {
|
2013-07-20 06:01:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-01 19:14:25 +08:00
|
|
|
JniHelper::classloader = cocos2d::JniHelper::getEnv()->NewGlobalRef(_c);
|
2013-07-20 07:07:34 +08:00
|
|
|
JniHelper::loadclassMethod_methodID = _m.methodID;
|
2013-07-20 06:18:32 +08:00
|
|
|
|
|
|
|
return true;
|
2013-07-19 04:41:10 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-19 04:41:10 +08:00
|
|
|
bool JniHelper::getStaticMethodInfo(JniMethodInfo &methodinfo,
|
|
|
|
const char *className,
|
|
|
|
const char *methodName,
|
|
|
|
const char *paramCode) {
|
2014-05-28 17:41:34 +08:00
|
|
|
if ((nullptr == className) ||
|
|
|
|
(nullptr == methodName) ||
|
|
|
|
(nullptr == paramCode)) {
|
2013-07-19 04:41:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
JNIEnv *env = JniHelper::getEnv();
|
|
|
|
if (!env) {
|
2014-05-20 18:52:21 +08:00
|
|
|
LOGE("Failed to get JNIEnv");
|
2013-07-19 04:41:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-20 06:01:42 +08:00
|
|
|
jclass classID = _getClassID(className);
|
2013-07-19 04:41:10 +08:00
|
|
|
if (! classID) {
|
2014-05-20 18:52:21 +08:00
|
|
|
LOGE("Failed to find class %s", className);
|
2014-05-28 17:41:34 +08:00
|
|
|
env->ExceptionClear();
|
2013-07-19 04:41:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
jmethodID methodID = env->GetStaticMethodID(classID, methodName, paramCode);
|
2013-07-19 04:41:10 +08:00
|
|
|
if (! methodID) {
|
2014-05-20 18:52:21 +08:00
|
|
|
LOGE("Failed to find static method id of %s", methodName);
|
2014-05-28 17:41:34 +08:00
|
|
|
env->ExceptionClear();
|
2013-07-19 04:41:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
methodinfo.classID = classID;
|
2014-05-28 17:41:34 +08:00
|
|
|
methodinfo.env = env;
|
2013-07-19 04:41:10 +08:00
|
|
|
methodinfo.methodID = methodID;
|
|
|
|
return true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-20 07:26:02 +08:00
|
|
|
bool JniHelper::getMethodInfo_DefaultClassLoader(JniMethodInfo &methodinfo,
|
|
|
|
const char *className,
|
|
|
|
const char *methodName,
|
|
|
|
const char *paramCode) {
|
2014-05-28 17:41:34 +08:00
|
|
|
if ((nullptr == className) ||
|
|
|
|
(nullptr == methodName) ||
|
|
|
|
(nullptr == paramCode)) {
|
2013-07-20 07:26:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
JNIEnv *env = JniHelper::getEnv();
|
|
|
|
if (!env) {
|
2013-07-20 07:26:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
jclass classID = env->FindClass(className);
|
2013-07-20 07:26:02 +08:00
|
|
|
if (! classID) {
|
2014-05-20 18:52:21 +08:00
|
|
|
LOGE("Failed to find class %s", className);
|
2014-05-28 17:41:34 +08:00
|
|
|
env->ExceptionClear();
|
2013-07-20 07:26:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
jmethodID methodID = env->GetMethodID(classID, methodName, paramCode);
|
2013-07-20 07:26:02 +08:00
|
|
|
if (! methodID) {
|
2014-05-20 18:52:21 +08:00
|
|
|
LOGE("Failed to find method id of %s", methodName);
|
2014-05-28 17:41:34 +08:00
|
|
|
env->ExceptionClear();
|
2013-07-20 07:26:02 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
methodinfo.classID = classID;
|
2014-05-28 17:41:34 +08:00
|
|
|
methodinfo.env = env;
|
2013-07-20 07:26:02 +08:00
|
|
|
methodinfo.methodID = methodID;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-07-19 04:41:10 +08:00
|
|
|
bool JniHelper::getMethodInfo(JniMethodInfo &methodinfo,
|
|
|
|
const char *className,
|
|
|
|
const char *methodName,
|
|
|
|
const char *paramCode) {
|
2014-05-28 17:41:34 +08:00
|
|
|
if ((nullptr == className) ||
|
|
|
|
(nullptr == methodName) ||
|
|
|
|
(nullptr == paramCode)) {
|
2013-07-19 04:41:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
JNIEnv *env = JniHelper::getEnv();
|
|
|
|
if (!env) {
|
2013-07-19 04:41:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-20 06:01:42 +08:00
|
|
|
jclass classID = _getClassID(className);
|
2013-07-19 04:41:10 +08:00
|
|
|
if (! classID) {
|
2014-05-20 18:52:21 +08:00
|
|
|
LOGE("Failed to find class %s", className);
|
2014-05-28 17:41:34 +08:00
|
|
|
env->ExceptionClear();
|
2013-07-19 04:41:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
jmethodID methodID = env->GetMethodID(classID, methodName, paramCode);
|
2013-07-19 04:41:10 +08:00
|
|
|
if (! methodID) {
|
2014-05-20 18:52:21 +08:00
|
|
|
LOGE("Failed to find method id of %s", methodName);
|
2014-05-28 17:41:34 +08:00
|
|
|
env->ExceptionClear();
|
2013-07-19 04:41:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-19 04:41:10 +08:00
|
|
|
methodinfo.classID = classID;
|
2014-05-28 17:41:34 +08:00
|
|
|
methodinfo.env = env;
|
2013-07-19 04:41:10 +08:00
|
|
|
methodinfo.methodID = methodID;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-19 04:41:10 +08:00
|
|
|
return true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-07-19 04:41:10 +08:00
|
|
|
std::string JniHelper::jstring2string(jstring jstr) {
|
2014-05-28 17:41:34 +08:00
|
|
|
if (jstr == nullptr) {
|
2012-05-16 14:17:37 +08:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-05-28 17:41:34 +08:00
|
|
|
JNIEnv *env = JniHelper::getEnv();
|
|
|
|
if (!env) {
|
2015-08-04 16:24:08 +08:00
|
|
|
return "";
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-08-05 16:10:14 +08:00
|
|
|
std::string strValue = cocos2d::StringUtils::getStringUTFCharsJNI(env, jstr);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-08-04 16:24:08 +08:00
|
|
|
return strValue;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 04:39:16 +08:00
|
|
|
jstring JniHelper::convert(cocos2d::JniMethodInfo& t, const char* x) {
|
2015-11-27 17:00:33 +08:00
|
|
|
jstring ret = cocos2d::StringUtils::newStringUTFJNI(t.env, x ? x : "");
|
2015-11-11 04:39:16 +08:00
|
|
|
localRefs[t.env].push_back(ret);
|
|
|
|
return ret;
|
2015-11-08 04:57:05 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 04:39:16 +08:00
|
|
|
jstring JniHelper::convert(cocos2d::JniMethodInfo& t, const std::string& x) {
|
|
|
|
return convert(t, x.c_str());
|
2015-11-08 04:57:05 +08:00
|
|
|
}
|
|
|
|
|
2015-11-11 04:39:16 +08:00
|
|
|
void JniHelper::deleteLocalRefs(JNIEnv* env) {
|
|
|
|
if (!env) {
|
|
|
|
return;
|
|
|
|
}
|
2015-11-08 04:57:05 +08:00
|
|
|
|
2015-11-11 04:39:16 +08:00
|
|
|
for (const auto& ref : localRefs[env]) {
|
|
|
|
env->DeleteLocalRef(ref);
|
|
|
|
}
|
|
|
|
localRefs[env].clear();
|
2015-11-08 04:57:05 +08:00
|
|
|
}
|
|
|
|
|
2015-11-27 17:00:33 +08:00
|
|
|
void JniHelper::reportError(const std::string& className, const std::string& methodName, const std::string& signature) {
|
|
|
|
LOGE("Failed to find static java method. Class name: %s, method name: %s, signature: %s ", className.c_str(), methodName.c_str(), signature.c_str());
|
|
|
|
}
|
2015-11-08 04:57:05 +08:00
|
|
|
|
2013-07-19 04:41:10 +08:00
|
|
|
} //namespace cocos2d
|