mirror of https://github.com/axmolengine/axmol.git
Merge pull request #442 from minggo/iss652
[android] fixed #652: refactor JniHelper::jstringtochar()
This commit is contained in:
commit
6f33190f3b
|
@ -31,6 +31,7 @@ THE SOFTWARE.
|
|||
#include "CCStdC.h"
|
||||
|
||||
#include <vector>
|
||||
#include <stdarg.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
@ -79,7 +80,7 @@ namespace cocos2d{
|
|||
|
||||
bool CCMenu::init()
|
||||
{
|
||||
va_list args = NULL;
|
||||
va_list args;
|
||||
return initWithItems(0, args);
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,8 @@ THE SOFTWARE.
|
|||
|
||||
#define JAVAVM cocos2d::JniHelper::getJavaVM()
|
||||
|
||||
using namespace std;
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
|
@ -161,34 +163,24 @@ extern "C"
|
|||
return bRet;
|
||||
}
|
||||
|
||||
static char* jstringToChar_(jstring jstr)
|
||||
static string jstring2string_(jstring jstr)
|
||||
{
|
||||
char* rtn = 0;
|
||||
JNIEnv *env = 0;
|
||||
|
||||
jboolean isCopy;
|
||||
if (! getEnv(&env))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// convert jstring to byte array
|
||||
jclass clsstring = env->FindClass("java/lang/String");
|
||||
jstring strencode = env->NewStringUTF("utf-8");
|
||||
jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
|
||||
jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
|
||||
jsize alen = env->GetArrayLength(barr);
|
||||
jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
|
||||
|
||||
// copy byte array into char[]
|
||||
if (alen > 0)
|
||||
const char* chars = env->GetStringUTFChars(jstr, &isCopy);
|
||||
string ret(chars);
|
||||
if (isCopy)
|
||||
{
|
||||
rtn = new char[alen + 1];
|
||||
memcpy(rtn, ba, alen);
|
||||
rtn[alen] = 0;
|
||||
env->ReleaseStringUTFChars(jstr, chars);
|
||||
}
|
||||
env->ReleaseByteArrayElements(barr, ba, 0);
|
||||
|
||||
return rtn;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -221,8 +213,8 @@ namespace cocos2d {
|
|||
return getMethodInfo_(methodinfo, className, methodName, paramCode);
|
||||
}
|
||||
|
||||
char* JniHelper::jstringToChar(jstring str)
|
||||
string JniHelper::jstring2string(jstring str)
|
||||
{
|
||||
return jstringToChar_(str);
|
||||
return jstring2string_(str);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ THE SOFTWARE.
|
|||
#define __ANDROID_JNI_HELPER_H__
|
||||
|
||||
#include <jni.h>
|
||||
#include <string>
|
||||
#include "CCPlatformMacros.h"
|
||||
|
||||
namespace cocos2d {
|
||||
|
@ -44,7 +45,7 @@ namespace cocos2d {
|
|||
static jclass getClassID(const char *className, JNIEnv *env=0);
|
||||
static bool getStaticMethodInfo(JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode);
|
||||
static bool getMethodInfo(JniMethodInfo &methodinfo, const char *className, const char *methodName, const char *paramCode);
|
||||
static char* jstringToChar(jstring str);
|
||||
static std::string jstring2string(jstring str);
|
||||
|
||||
private:
|
||||
static JavaVM *m_psJavaVM;
|
||||
|
|
|
@ -25,6 +25,7 @@ THE SOFTWARE.
|
|||
#include "CCDirector.h"
|
||||
#include "JniHelper.h"
|
||||
#include "CCApplication.h"
|
||||
#include "CCFileUtils.h"
|
||||
|
||||
#include <android/log.h>
|
||||
#include <jni.h>
|
||||
|
@ -111,4 +112,18 @@ extern "C"
|
|||
t.env->CallStaticObjectMethod(t.classID, t.methodID);
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// set apk path
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void Java_org_cocos2dx_lib_Cocos2dxActivity_nativeSetPaths(JNIEnv* env, jobject thiz, jstring apkPath)
|
||||
{
|
||||
const char* str;
|
||||
jboolean isCopy;
|
||||
str = env->GetStringUTFChars(apkPath, &isCopy);
|
||||
if (isCopy) {
|
||||
cocos2d::CCFileUtils::setResourcePath(str);
|
||||
env->ReleaseStringUTFChars(apkPath, str);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,6 @@ THE SOFTWARE.
|
|||
#include "CCAccelerometer.h"
|
||||
#include "platform/android/CCAccelerometer_android.h"
|
||||
#include "CCEGLView.h"
|
||||
#include "CCFileUtils.h"
|
||||
#include "JniHelper.h"
|
||||
#include <android/log.h>
|
||||
#include <jni.h>
|
||||
|
@ -57,19 +56,7 @@ extern "C"
|
|||
(y - rcRect.origin.y) / fScreenScaleFactor,
|
||||
z,
|
||||
timeStamp);
|
||||
}
|
||||
|
||||
void Java_org_cocos2dx_lib_Cocos2dxActivity_nativeSetPaths(JNIEnv* env, jobject thiz, jstring apkPath)
|
||||
{
|
||||
const char* str;
|
||||
jboolean isCopy;
|
||||
str = env->GetStringUTFChars(apkPath, &isCopy);
|
||||
if (isCopy) {
|
||||
cocos2d::CCFileUtils::setResourcePath(str);
|
||||
env->ReleaseStringUTFChars(apkPath, str);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void enableAccelerometerJNI()
|
||||
{
|
||||
|
|
|
@ -50,7 +50,7 @@ extern "C"
|
|||
"()Ljava/lang/String;"))
|
||||
{
|
||||
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
|
||||
ret = JniHelper::jstringToChar(str);
|
||||
ret = (char*)JniHelper::jstring2string(str).c_str();
|
||||
|
||||
LOGD("package name %s", ret);
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ extern "C"
|
|||
, "()Ljava/lang/String;"))
|
||||
{
|
||||
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
|
||||
ret = JniHelper::jstringToChar(str);
|
||||
ret = (char*)JniHelper::jstring2string(str).c_str();
|
||||
|
||||
LOGD("language name %s", ret);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue