axmol/cocos2dx/platform/android/jni/JniHelper.cpp

195 lines
6.0 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2010 cocos2d-x.org
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>
#define LOG_TAG "JniHelper"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
extern "C"
{
2013-07-19 04:41:10 +08:00
static JNIEnv* _getJNIEnv(void) {
JavaVM* jvm = cocos2d::JniHelper::getJavaVM();
if (NULL == jvm) {
LOGD("Failed to get JNIEnv. JniHelper::getJavaVM() is NULL");
return NULL;
}
JNIEnv *env = NULL;
// get jni environment
jint ret = jvm->GetEnv((void**)&env, JNI_VERSION_1_4);
switch (ret) {
case JNI_OK :
// Success!
return env;
case JNI_EDETACHED :
// Thread not attached
// TODO : If calling AttachCurrentThread() on a native thread
// must call DetachCurrentThread() in future.
// see: http://developer.android.com/guide/practices/design/jni.html
if (jvm->AttachCurrentThread(&env, NULL) < 0)
{
LOGD("Failed to get the environment using AttachCurrentThread()");
return NULL;
} else {
// Success : Attached and obtained JNIEnv!
return env;
}
2013-07-19 04:41:10 +08:00
case JNI_EVERSION :
// Cannot recover from this error
LOGD("JNI interface version 1.4 not supported");
default :
LOGD("Failed to get the environment using GetEnv()");
return NULL;
}
}
2013-07-19 04:41:10 +08:00
jclass _getClassID(const char *className, JNIEnv *env) {
if (NULL == className) {
return NULL;
}
2013-07-19 04:41:10 +08:00
jclass ret = env->FindClass(className);
if (!ret) {
LOGD("Failed to find class of %s", className);
}
return ret;
}
2013-07-19 04:41:10 +08:00
}
2013-07-19 04:41:10 +08:00
namespace cocos2d {
2013-07-19 04:41:10 +08:00
JavaVM* JniHelper::_psJavaVM = NULL;
2013-07-19 04:41:10 +08:00
JavaVM* JniHelper::getJavaVM() {
pthread_t thisthread = pthread_self();
LOGD("JniHelper::getJavaVM(), pthread_self() = %X", thisthread);
return _psJavaVM;
}
2013-07-19 04:41:10 +08:00
void JniHelper::setJavaVM(JavaVM *javaVM) {
pthread_t thisthread = pthread_self();
LOGD("JniHelper::setJavaVM(%p), pthread_self() = %X", javaVM, thisthread);
_psJavaVM = javaVM;
}
2013-07-19 04:41:10 +08:00
bool JniHelper::getStaticMethodInfo(JniMethodInfo &methodinfo,
const char *className,
const char *methodName,
const char *paramCode) {
if ((NULL == className) ||
(NULL == methodName) ||
(NULL == paramCode)) {
return false;
}
2013-07-19 04:41:10 +08:00
JNIEnv *pEnv = _getJNIEnv();
if (!pEnv) {
LOGD("Failed to get JNIEnv");
return false;
}
jclass classID = _getClassID(className, pEnv);
if (! classID) {
LOGD("Failed to find class %s", className);
return false;
}
2013-07-19 04:41:10 +08:00
jmethodID methodID = pEnv->GetStaticMethodID(classID, methodName, paramCode);
if (! methodID) {
LOGD("Failed to find static method id of %s", methodName);
return false;
}
methodinfo.classID = classID;
methodinfo.env = pEnv;
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) {
if ((NULL == className) ||
(NULL == methodName) ||
(NULL == paramCode)) {
return false;
}
2013-07-19 04:41:10 +08:00
JNIEnv *pEnv = _getJNIEnv();
if (!pEnv) {
return false;
}
2013-07-19 04:41:10 +08:00
jclass classID = _getClassID(className, pEnv);
if (! classID) {
LOGD("Failed to find class %s", className);
return false;
}
2013-07-19 04:41:10 +08:00
jmethodID methodID = pEnv->GetMethodID(classID, methodName, paramCode);
if (! methodID) {
LOGD("Failed to find method id of %s", methodName);
return false;
}
2013-07-19 04:41:10 +08:00
methodinfo.classID = classID;
methodinfo.env = pEnv;
methodinfo.methodID = methodID;
2013-07-19 04:41:10 +08:00
return true;
}
2013-07-19 04:41:10 +08:00
std::string JniHelper::jstring2string(jstring jstr) {
if (jstr == NULL) {
return "";
}
2013-07-19 04:41:10 +08:00
JNIEnv *env = _getJNIEnv();
if (!env) {
return NULL;
}
2012-05-03 16:30:13 +08:00
const char* chars = env->GetStringUTFChars(jstr, NULL);
2013-07-19 04:41:10 +08:00
std::string ret(chars);
2012-05-03 16:30:13 +08:00
env->ReleaseStringUTFChars(jstr, chars);
return ret;
}
2013-07-19 04:41:10 +08:00
} //namespace cocos2d