Merge pull request #14340 from fnz/easyjni

easy jni
This commit is contained in:
zilongshanren 2016-01-12 11:50:16 +08:00
commit 161be986af
32 changed files with 695 additions and 1384 deletions

View File

@ -26,31 +26,22 @@ THE SOFTWARE.
#include "cddandroidAndroidJavaEngine.h"
#include <stdlib.h>
#include <android/log.h>
#include <jni.h>
#include <sys/system_properties.h>
#include "platform/android/jni/JniHelper.h"
#include "ccdandroidUtils.h"
#include "audio/include/AudioEngine.h"
#include "platform/android/jni/JniHelper.h"
// logging
#define LOG_TAG "cocosdenshion::android::AndroidJavaEngine"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
// Java class
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxHelper"
static const std::string helperClassName = "org/cocos2dx/lib/Cocos2dxHelper";
using namespace cocos2d;
using namespace cocos2d::experimental;
using namespace CocosDenshion::android;
static inline bool getJNIStaticMethodInfo(cocos2d::JniMethodInfo &methodinfo,
const char *methodName,
const char *paramCode) {
return cocos2d::JniHelper::getStaticMethodInfo(methodinfo,
CLASS_NAME,
methodName,
paramCode);
}
AndroidJavaEngine::AndroidJavaEngine()
: _implementBaseOnAudioEngine(false)
, _effectVolume(1.f)
@ -78,89 +69,35 @@ AndroidJavaEngine::~AndroidJavaEngine()
{
stopAllEffects();
}
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "end", "()V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
JniHelper::callStaticVoidMethod(helperClassName, "end");
}
void AndroidJavaEngine::preloadBackgroundMusic(const char* filePath) {
std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
// void playBackgroundMusic(String,boolean)
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "preloadBackgroundMusic", "(Ljava/lang/String;)V")) {
return;
}
jstring stringArg = methodInfo.env->NewStringUTF(fullPath.c_str());
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg);
methodInfo.env->DeleteLocalRef(stringArg);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
JniHelper::callStaticVoidMethod(helperClassName, "preloadBackgroundMusic", filePath);
}
void AndroidJavaEngine::playBackgroundMusic(const char* filePath, bool loop) {
std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "playBackgroundMusic", "(Ljava/lang/String;Z)V")) {
return;
}
jstring stringArg = methodInfo.env->NewStringUTF(fullPath.c_str());
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg, loop);
methodInfo.env->DeleteLocalRef(stringArg);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
JniHelper::callStaticVoidMethod(helperClassName, "playBackgroundMusic", filePath, loop);
}
void AndroidJavaEngine::stopBackgroundMusic(bool releaseData) {
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "stopBackgroundMusic", "()V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
JniHelper::callStaticVoidMethod(helperClassName, "stopBackgroundMusic");
}
void AndroidJavaEngine::pauseBackgroundMusic() {
cocos2d::JniMethodInfo methodInfo;
JniHelper::callStaticVoidMethod(helperClassName, "pauseBackgroundMusic");
if (!getJNIStaticMethodInfo(methodInfo, "pauseBackgroundMusic", "()V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
void AndroidJavaEngine::resumeBackgroundMusic() {
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "resumeBackgroundMusic", "()V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
JniHelper::callStaticVoidMethod(helperClassName, "resumeBackgroundMusic");
}
void AndroidJavaEngine::rewindBackgroundMusic() {
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "rewindBackgroundMusic", "()V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
JniHelper::callStaticVoidMethod(helperClassName, "rewindBackgroundMusic");
}
bool AndroidJavaEngine::willPlayBackgroundMusic() {
@ -168,181 +105,17 @@ bool AndroidJavaEngine::willPlayBackgroundMusic() {
}
bool AndroidJavaEngine::isBackgroundMusicPlaying() {
cocos2d::JniMethodInfo methodInfo;
jboolean ret = false;
if (!getJNIStaticMethodInfo(methodInfo, "isBackgroundMusicPlaying", "()Z")) {
return ret;
}
ret = methodInfo.env->CallStaticBooleanMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
return ret;
return JniHelper::callStaticBooleanMethod(helperClassName, "isBackgroundMusicPlaying");
}
float AndroidJavaEngine::getBackgroundMusicVolume() {
cocos2d::JniMethodInfo methodInfo;
jfloat ret = -1.0;
if (!getJNIStaticMethodInfo(methodInfo, "getBackgroundMusicVolume", "()F")) {
return ret;
}
ret = methodInfo.env->CallStaticFloatMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
return ret;
return JniHelper::callStaticFloatMethod(helperClassName, "getBackgroundMusicVolume");
}
void AndroidJavaEngine::setBackgroundMusicVolume(float volume) {
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "setBackgroundMusicVolume", "(F)V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, volume);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
JniHelper::callStaticVoidMethod(helperClassName, "setBackgroundMusicVolume", volume);
}
static float _jni_getEffectsVolume() {
cocos2d::JniMethodInfo methodInfo;
jfloat ret = -1.0;
if (!getJNIStaticMethodInfo(methodInfo, "getEffectsVolume", "()F")) {
return ret;
}
ret = methodInfo.env->CallStaticFloatMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
return ret;
}
static void _jni_setEffectsVolume(float volume) {
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "setEffectsVolume", "(F)V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, volume);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
static unsigned int _jni_playEffect(const char* filePath, bool loop, float pitch, float pan, float gain)
{
cocos2d::JniMethodInfo methodInfo;
int ret = 0;
std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
if (!getJNIStaticMethodInfo(methodInfo, "playEffect", "(Ljava/lang/String;ZFFF)I")) {
return ret;
}
jstring stringArg = methodInfo.env->NewStringUTF(fullPath.c_str());
ret = methodInfo.env->CallStaticIntMethod(methodInfo.classID,
methodInfo.methodID,
stringArg,
loop,
pitch, pan, gain);
methodInfo.env->DeleteLocalRef(stringArg);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
return (unsigned int)ret;
}
static void _jni_pauseEffect(unsigned int soundId) {
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "pauseEffect", "(I)V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, (int)soundId);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
static void _jni_pauseAllEffects() {
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "pauseAllEffects", "()V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
static void _jni_resumeEffect(unsigned int soundId) {
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "resumeEffect", "(I)V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, (int)soundId);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
static void _jni_resumeAllEffects() {
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "resumeAllEffects", "()V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
static void _jni_stopEffect(unsigned int soundId) {
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "stopEffect", "(I)V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, (int)soundId);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
static void _jni_stopAllEffects() {
cocos2d::JniMethodInfo methodInfo;
if (!getJNIStaticMethodInfo(methodInfo, "stopAllEffects", "()V")) {
return;
}
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
static void loadEffect(const char* filePath, char* loadEffectName) {
cocos2d::JniMethodInfo methodInfo;
std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
if (!cocos2d::JniHelper::getStaticMethodInfo(methodInfo, CLASS_NAME, loadEffectName, "(Ljava/lang/String;)V")) {
return;
}
jstring stringArg = methodInfo.env->NewStringUTF(fullPath.c_str());
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, stringArg);
methodInfo.env->DeleteLocalRef(stringArg);
methodInfo.env->DeleteLocalRef(methodInfo.classID);
}
static void _jni_preloadEffect(const char* filePath) {
loadEffect(filePath, "preloadEffect");
}
static void _jni_unloadEffect(const char* filePath) {
loadEffect(filePath, "unloadEffect");
}
float AndroidJavaEngine::getEffectsVolume()
{
if (_implementBaseOnAudioEngine)
@ -351,7 +124,7 @@ float AndroidJavaEngine::getEffectsVolume()
}
else
{
return _jni_getEffectsVolume();
return JniHelper::callStaticFloatMethod(helperClassName, "getEffectsVolume");
}
}
@ -379,7 +152,7 @@ void AndroidJavaEngine::setEffectsVolume(float volume)
}
else
{
_jni_setEffectsVolume(volume);
JniHelper::callStaticVoidMethod(helperClassName, "setEffectsVolume", volume);
}
}
@ -402,7 +175,9 @@ unsigned int AndroidJavaEngine::playEffect(const char* filePath, bool loop,
}
else
{
return _jni_playEffect(filePath, loop, pitch, pan, gain);
std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
int ret = JniHelper::callStaticIntMethod(helperClassName, "playEffect", fullPath, loop, pitch, pan, gain);
return (unsigned int)ret;
}
}
@ -414,7 +189,7 @@ void AndroidJavaEngine::pauseEffect(unsigned int soundID)
}
else
{
_jni_pauseEffect(soundID);
JniHelper::callStaticVoidMethod(helperClassName, "pauseEffect", (int)soundID);
}
}
@ -426,7 +201,7 @@ void AndroidJavaEngine::resumeEffect(unsigned int soundID)
}
else
{
_jni_resumeEffect(soundID);
JniHelper::callStaticVoidMethod(helperClassName, "resumeEffect", (int)soundID);
}
}
@ -439,7 +214,7 @@ void AndroidJavaEngine::stopEffect(unsigned int soundID)
}
else
{
_jni_stopEffect(soundID);
JniHelper::callStaticVoidMethod(helperClassName, "stopEffect", (int)soundID);
}
}
@ -454,7 +229,7 @@ void AndroidJavaEngine::pauseAllEffects()
}
else
{
_jni_pauseAllEffects();
JniHelper::callStaticVoidMethod(helperClassName, "pauseAllEffects");
}
}
@ -469,7 +244,7 @@ void AndroidJavaEngine::resumeAllEffects()
}
else
{
_jni_resumeAllEffects();
JniHelper::callStaticVoidMethod(helperClassName, "resumeAllEffects");
}
}
@ -485,7 +260,7 @@ void AndroidJavaEngine::stopAllEffects()
}
else
{
_jni_stopAllEffects();
JniHelper::callStaticVoidMethod(helperClassName, "stopAllEffects");
}
}
@ -493,7 +268,8 @@ void AndroidJavaEngine::preloadEffect(const char* filePath)
{
if (!_implementBaseOnAudioEngine)
{
_jni_preloadEffect(filePath);
std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
JniHelper::callStaticVoidMethod(helperClassName, "preloadEffect", fullPath);
}
}
@ -501,6 +277,7 @@ void AndroidJavaEngine::unloadEffect(const char* filePath)
{
if (!_implementBaseOnAudioEngine)
{
_jni_unloadEffect(filePath);
std::string fullPath = CocosDenshion::android::getFullPathWithoutAssetsPrefix(filePath);
JniHelper::callStaticVoidMethod(helperClassName, "unloadEffect", fullPath);
}
}

View File

@ -155,14 +155,8 @@ Controller::Controller()
init();
}
void Controller::receiveExternalKeyEvent(int externalKeyCode,bool receive)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/GameControllerHelper", "receiveExternalKeyEvent", "(IIZ)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, _deviceId, externalKeyCode, receive);
t.env->DeleteLocalRef(t.classID);
}
void Controller::receiveExternalKeyEvent(int externalKeyCode,bool receive) {
JniHelper::callStaticVoidMethod("org/cocos2dx/lib/GameControllerHelper", "receiveExternalKeyEvent", _deviceId, externalKeyCode, receive);
}
NS_CC_END

View File

@ -29,7 +29,7 @@ THE SOFTWARE.
#include "base/base64.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#include "platform/android/jni/JniHelper.h"
// root name of xml
#define USERDEFAULT_ROOT_NAME "userDefaultRoot"
@ -43,8 +43,9 @@ THE SOFTWARE.
#include "tinyxml2.h"
#endif
using namespace std;
static const std::string helperClassName = "org/cocos2dx/lib/Cocos2dxHelper";
using namespace std;
NS_CC_BEGIN
/**
@ -191,7 +192,7 @@ bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
}
#endif
return getBoolForKeyJNI(pKey, defaultValue);
return JniHelper::callStaticBooleanMethod(helperClassName, "getBoolForKey", pKey, defaultValue);
}
int UserDefault::getIntegerForKey(const char* pKey)
@ -227,7 +228,7 @@ int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
}
#endif
return getIntegerForKeyJNI(pKey, defaultValue);
return JniHelper::callStaticIntMethod(helperClassName, "getIntegerForKey", pKey, defaultValue);
}
float UserDefault::getFloatForKey(const char* pKey)
@ -263,7 +264,7 @@ float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
}
#endif
return getFloatForKeyJNI(pKey, defaultValue);
return JniHelper::callStaticFloatMethod(helperClassName, "getFloatForKey", pKey, defaultValue);
}
double UserDefault::getDoubleForKey(const char* pKey)
@ -299,7 +300,7 @@ double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
}
#endif
return getDoubleForKeyJNI(pKey, defaultValue);
return JniHelper::callStaticDoubleMethod(helperClassName, "getDoubleForKey", pKey, defaultValue);
}
std::string UserDefault::getStringForKey(const char* pKey)
@ -335,7 +336,7 @@ string UserDefault::getStringForKey(const char* pKey, const std::string & defaul
}
#endif
return getStringForKeyJNI(pKey, defaultValue.c_str());
return JniHelper::callStaticStringMethod(helperClassName, "getStringForKey", pKey, defaultValue);
}
Data UserDefault::getDataForKey(const char* pKey)
@ -383,7 +384,7 @@ Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
char * encodedDefaultData = NULL;
unsigned int encodedDefaultDataLen = !defaultValue.isNull() ? base64Encode(defaultValue.getBytes(), defaultValue.getSize(), &encodedDefaultData) : 0;
string encodedStr = getStringForKeyJNI(pKey, encodedDefaultData);
string encodedStr = JniHelper::callStaticStringMethod(helperClassName, "getStringForKey", pKey, (const char*)encodedDefaultData);
if (encodedDefaultData)
free(encodedDefaultData);
@ -411,7 +412,7 @@ void UserDefault::setBoolForKey(const char* pKey, bool value)
deleteNodeByKey(pKey);
#endif
return setBoolForKeyJNI(pKey, value);
JniHelper::callStaticVoidMethod(helperClassName, "setBoolForKey", pKey, value);
}
void UserDefault::setIntegerForKey(const char* pKey, int value)
@ -420,7 +421,7 @@ void UserDefault::setIntegerForKey(const char* pKey, int value)
deleteNodeByKey(pKey);
#endif
return setIntegerForKeyJNI(pKey, value);
JniHelper::callStaticVoidMethod(helperClassName, "setIntegerForKey", pKey, value);
}
void UserDefault::setFloatForKey(const char* pKey, float value)
@ -429,7 +430,7 @@ void UserDefault::setFloatForKey(const char* pKey, float value)
deleteNodeByKey(pKey);
#endif
return setFloatForKeyJNI(pKey, value);
JniHelper::callStaticVoidMethod(helperClassName, "setFloatForKey", pKey, value);
}
void UserDefault::setDoubleForKey(const char* pKey, double value)
@ -438,16 +439,16 @@ void UserDefault::setDoubleForKey(const char* pKey, double value)
deleteNodeByKey(pKey);
#endif
return setDoubleForKeyJNI(pKey, value);
JniHelper::callStaticVoidMethod(helperClassName, "setDoubleForKey", pKey, value);
}
void UserDefault::setStringForKey(const char* pKey, const std::string & value)
void UserDefault::setStringForKey(const char* pKey, const std::string& value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
return setStringForKeyJNI(pKey, value.c_str());
JniHelper::callStaticVoidMethod(helperClassName, "setStringForKey", pKey, value);
}
void UserDefault::setDataForKey(const char* pKey, const Data& value)
@ -462,7 +463,7 @@ void UserDefault::setDataForKey(const char* pKey, const Data& value)
CCLOG("SET DATA ENCODED: --%s", encodedData);
setStringForKeyJNI(pKey, encodedData);
JniHelper::callStaticVoidMethod(helperClassName, "setStringForKey", pKey, (const char*)encodedData);
if (encodedData)
free(encodedData);
@ -498,7 +499,8 @@ void UserDefault::initXMLFilePath()
if (! _isFilePathInitialized)
{
// UserDefault.xml is stored in /data/data/<package-path>/ before v2.1.2
_filePath += "/data/data/" + getPackageNameJNI() + "/" + XML_FILE_NAME;
std::string packageName = JniHelper::callStaticStringMethod(helperClassName, "getCocos2dxPackageName");
_filePath += "/data/data/" + packageName + "/" + XML_FILE_NAME;
_isFilePathInitialized = true;
}
#endif
@ -527,7 +529,7 @@ void UserDefault::deleteValueForKey(const char* key)
CCLOG("the key is invalid");
}
deleteValueForKeyJNI(key);
JniHelper::callStaticVoidMethod(helperClassName, "deleteValueForKey", key);
flush();
}

View File

@ -49,8 +49,6 @@ set(COCOS_PLATFORM_SPECIFIC_SRC
platform/android/CCGLViewImpl-android.cpp
platform/android/CCFileUtils-android.cpp
platform/android/javaactivity-android.cpp
platform/android/jni/DPIJni.cpp
platform/android/jni/IMEJni.cpp
platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxAccelerometer.cpp
platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp
platform/android/jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp

View File

@ -14,8 +14,6 @@ CCGLViewImpl-android.cpp \
CCFileUtils-android.cpp \
javaactivity-android.cpp \
CCEnhanceAPI-android.cpp \
jni/DPIJni.cpp \
jni/IMEJni.cpp \
jni/Java_org_cocos2dx_lib_Cocos2dxAccelerometer.cpp \
jni/Java_org_cocos2dx_lib_Cocos2dxBitmap.cpp \
jni/Java_org_cocos2dx_lib_Cocos2dxHelper.cpp \

View File

@ -26,8 +26,7 @@ THE SOFTWARE.
#include "platform/CCPlatformConfig.h"
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "jni/JniHelper.h"
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#include "platform/android/jni/JniHelper.h"
#include "CCApplication.h"
#include "base/CCDirector.h"
#include <android/log.h>
@ -45,6 +44,8 @@ extern "C" size_t __ctype_get_mb_cur_max(void) {
}
#endif
static const std::string helperClassName = "org/cocos2dx/lib/Cocos2dxHelper";
NS_CC_BEGIN
// sharedApplication pointer
@ -73,18 +74,8 @@ int Application::run()
return -1;
}
void Application::setAnimationInterval(float interval)
{
JniMethodInfo methodInfo;
if (! JniHelper::getStaticMethodInfo(methodInfo, "org/cocos2dx/lib/Cocos2dxRenderer", "setAnimationInterval",
"(F)V"))
{
CCLOG("%s %d: error to get methodInfo", __FILE__, __LINE__);
}
else
{
methodInfo.env->CallStaticVoidMethod(methodInfo.classID, methodInfo.methodID, interval);
}
void Application::setAnimationInterval(float interval) {
JniHelper::callStaticVoidMethod("org/cocos2dx/lib/Cocos2dxRenderer", "setAnimationInterval", interval);
}
//////////////////////////////////////////////////////////////////////////
@ -105,14 +96,15 @@ Application* Application::sharedApplication()
const char * Application::getCurrentLanguageCode()
{
static char code[3]={0};
strncpy(code,getCurrentLanguageJNI().c_str(),2);
std::string language = JniHelper::callStaticStringMethod(helperClassName, "getCurrentLanguage");
strncpy(code, language.c_str(), 2);
code[2]='\0';
return code;
}
LanguageType Application::getCurrentLanguage()
{
std::string languageName = getCurrentLanguageJNI();
std::string languageName = JniHelper::callStaticStringMethod(helperClassName, "getCurrentLanguage");
const char* pLanguageName = languageName.c_str();
LanguageType ret = LanguageType::ENGLISH;
@ -202,12 +194,12 @@ Application::Platform Application::getTargetPlatform()
std::string Application::getVersion()
{
return getVersionJNI();
return JniHelper::callStaticStringMethod(helperClassName, "getVersion");
}
bool Application::openURL(const std::string &url)
{
return openURLJNI(url.c_str());
return JniHelper::callStaticBooleanMethod(helperClassName, "openURL", url);
}
void Application::applicationScreenSizeChanged(int newWidth, int newHeight) {

View File

@ -27,7 +27,7 @@ THE SOFTWARE.
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "platform/CCCommon.h"
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#include "platform/android/jni/JniHelper.h"
#include <android/log.h>
#include <stdio.h>
#include <jni.h>
@ -38,7 +38,7 @@ NS_CC_BEGIN
void MessageBox(const char * pszMsg, const char * pszTitle)
{
showDialogJNI(pszMsg, pszTitle);
JniHelper::callStaticVoidMethod("org/cocos2dx/lib/Cocos2dxHelper", "showDialog", pszMsg, pszTitle);
}
void LuaLog(const char * pszFormat)

View File

@ -31,11 +31,11 @@ THE SOFTWARE.
#include <android/log.h>
#include <jni.h>
#include "base/ccTypes.h"
#include "jni/DPIJni.h"
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#include "jni/JniHelper.h"
#include "platform/android/jni/JniHelper.h"
#include "platform/CCFileUtils.h"
static const std::string helperClassName = "org/cocos2dx/lib/Cocos2dxHelper";
NS_CC_BEGIN
int Device::getDPI()
@ -43,7 +43,7 @@ int Device::getDPI()
static int dpi = -1;
if (dpi == -1)
{
dpi = (int)getDPIJNI();
dpi = JniHelper::callStaticIntMethod(helperClassName, "getDPI");
}
return dpi;
}
@ -52,17 +52,17 @@ void Device::setAccelerometerEnabled(bool isEnabled)
{
if (isEnabled)
{
enableAccelerometerJni();
JniHelper::callStaticVoidMethod(helperClassName, "enableAccelerometer");
}
else
{
disableAccelerometerJni();
JniHelper::callStaticVoidMethod(helperClassName, "disableAccelerometer");
}
}
void Device::setAccelerometerInterval(float interval)
{
setAccelerometerIntervalJni(interval);
JniHelper::callStaticVoidMethod(helperClassName, "setAccelerometerInterval", interval);
}
class BitmapDC
@ -170,12 +170,12 @@ Data Device::getTextureDataForText(const char * text, const FontDefinition& text
void Device::setKeepScreenOn(bool value)
{
setKeepScreenOnJni(value);
JniHelper::callStaticVoidMethod(helperClassName, "setKeepScreenOn", value);
}
void Device::vibrate(float duration)
{
vibrateJni(duration);
JniHelper::callStaticVoidMethod(helperClassName, "vibrate", duration);
}
NS_CC_END

View File

@ -28,10 +28,10 @@ THE SOFTWARE.
#include "CCFileUtils-android.h"
#include "platform/CCCommon.h"
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#include "platform/android/jni/JniHelper.h"
#include "platform/android/jni/CocosPlayClient.h"
#include "android/asset_manager.h"
#include "android/asset_manager_jni.h"
#include "jni/CocosPlayClient.h"
#include <stdlib.h>
#include <sys/stat.h>
@ -471,7 +471,7 @@ string FileUtilsAndroid::getWritablePath() const
// Fix for Nexus 10 (Android 4.2 multi-user environment)
// the path is retrieved through Java Context.getCacheDir() method
string dir("");
string tmp = getFileDirectoryJNI();
string tmp = JniHelper::callStaticStringMethod("org/cocos2dx/lib/Cocos2dxHelper", "getCocos2dxWritablePath");
if (tmp.length() > 0)
{

View File

@ -29,9 +29,7 @@ THE SOFTWARE.
#include "CCGLViewImpl-android.h"
#include "base/CCDirector.h"
#include "base/ccMacros.h"
#include "jni/IMEJni.h"
#include "jni/JniHelper.h"
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#include "platform/android/jni/JniHelper.h"
#include "CCGL.h"
#include <stdlib.h>
@ -112,7 +110,7 @@ bool GLViewImpl::isOpenGLReady()
void GLViewImpl::end()
{
terminateProcessJNI();
JniHelper::callStaticVoidMethod("org/cocos2dx/lib/Cocos2dxHelper", "terminateProcess");
}
void GLViewImpl::swapBuffers()
@ -121,7 +119,11 @@ void GLViewImpl::swapBuffers()
void GLViewImpl::setIMEKeyboardState(bool bOpen)
{
setKeyboardStateJNI((int)bOpen);
if (bOpen) {
JniHelper::callStaticVoidMethod("org/cocos2dx/lib/Cocos2dxGLSurfaceView", "openIMEKeyboard");
} else {
JniHelper::callStaticVoidMethod("org/cocos2dx/lib/Cocos2dxGLSurfaceView", "closeIMEKeyboard");
}
}
NS_CC_END

View File

@ -1,19 +0,0 @@
#include "DPIJni.h"
#include "jni/JniHelper.h"
USING_NS_CC;
extern "C" {
int getDPIJNI()
{
JniMethodInfo t;
jint ret = -1;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxHelper", "getDPI", "()I")) {
ret = t.env->CallStaticIntMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
return ret;
}
} // extern "C"

View File

@ -1,10 +0,0 @@
#ifndef __DPIJNI_H__
#define __DPIJNI_H__
extern "C" {
int getDPIJNI();
} // extern "C"
#endif /* __DPIJNI_H__ */

View File

@ -1,61 +0,0 @@
/****************************************************************************
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
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 "IMEJni.h"
#include "base/CCIMEDispatcher.h"
#include "JniHelper.h"
#include <android/log.h>
#include <string.h>
#include <jni.h>
using namespace cocos2d;
extern "C" {
void setKeyboardStateJNI(int bOpen) {
if (bOpen) {
openKeyboardJNI();
} else {
closeKeyboardJNI();
}
}
void openKeyboardJNI() {
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxGLSurfaceView", "openIMEKeyboard", "()V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
}
void closeKeyboardJNI() {
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxGLSurfaceView", "closeIMEKeyboard", "()V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
}
}

View File

@ -1,34 +0,0 @@
/****************************************************************************
Copyright (c) 2011-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
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.
****************************************************************************/
#ifndef __ANDROID_IME_JNI_H__
#define __ANDROID_IME_JNI_H__
extern "C" {
extern void setKeyboardStateJNI(int open);
extern void openKeyboardJNI();
extern void closeKeyboardJNI();
}
#endif // __ANDROID_IME_JNI_H__

View File

@ -23,48 +23,22 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "JniHelper.h"
#include "platform/android/jni/JniHelper.h"
#include <string.h>
#include "base/CCDirector.h"
#include "../CCApplication.h"
#include "platform/CCFileUtils.h"
#include <jni.h>
#include "base/ccUTF8.h"
static const std::string className = "org/cocos2dx/lib/Cocos2dxBitmap";
using namespace cocos2d;
int getFontSizeAccordingHeightJni(int height) {
int ret = 0;
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxBitmap", "getFontSizeAccordingHeight", "(I)I")) {
ret = t.env->CallStaticIntMethod(t.classID, t.methodID, height);
t.env->DeleteLocalRef(t.classID);
}
return ret;
return JniHelper::callStaticIntMethod(className, "getFontSizeAccordingHeight", height);
}
std::string getStringWithEllipsisJni(const char* text, float width, float fontSize) {
std::string ret;
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxBitmap", "getStringWithEllipsis", "(Ljava/lang/String;FF)Ljava/lang/String;")) {
jstring stringArg1;
if (!text) {
stringArg1 = t.env->NewStringUTF("");
} else {
stringArg1 = t.env->NewStringUTF(text);
}
jstring retFromJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, stringArg1, width, fontSize);
ret = cocos2d::StringUtils::getStringUTFCharsJNI(t.env, retFromJava);
t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(t.classID);
}
return ret;
return JniHelper::callStaticStringMethod(className, "getStringWithEllipsis", text, width, fontSize);
}

View File

@ -37,8 +37,7 @@ THE SOFTWARE.
#define LOG_TAG "Java_org_cocos2dx_lib_Cocos2dxHelper.cpp"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxHelper"
#define EDITBOX_CLASS_NAME "org/cocos2dx/lib/Cocos2dxEditBoxHelper"
static const std::string className = "org/cocos2dx/lib/Cocos2dxHelper";
static EditTextCallback s_editTextCallback = nullptr;
static void* s_ctx = nullptr;
@ -84,492 +83,15 @@ const char * getApkPath() {
return g_apkPath.c_str();
}
void showDialogJNI(const char * message, const char * title) {
if (!message) {
return;
}
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "showDialog", "(Ljava/lang/String;Ljava/lang/String;)V")) {
jstring stringArg1;
if (!title) {
stringArg1 = t.env->NewStringUTF("");
} else {
stringArg1 = t.env->NewStringUTF(title);
}
jstring stringArg2 = t.env->NewStringUTF(message);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1, stringArg2);
t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(stringArg2);
t.env->DeleteLocalRef(t.classID);
}
}
void terminateProcessJNI() {
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "terminateProcess", "()V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
}
std::string getPackageNameJNI() {
JniMethodInfo t;
std::string ret("");
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getCocos2dxPackageName", "()Ljava/lang/String;")) {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
ret = JniHelper::jstring2string(str);
t.env->DeleteLocalRef(str);
}
return ret;
}
std::string getFileDirectoryJNI() {
JniMethodInfo t;
std::string ret("");
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getCocos2dxWritablePath", "()Ljava/lang/String;")) {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
ret = JniHelper::jstring2string(str);
t.env->DeleteLocalRef(str);
}
return ret;
}
std::string getCurrentLanguageJNI() {
JniMethodInfo t;
std::string ret("");
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getCurrentLanguage", "()Ljava/lang/String;")) {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
ret = JniHelper::jstring2string(str);
t.env->DeleteLocalRef(str);
}
return ret;
}
void enableAccelerometerJni() {
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "enableAccelerometer", "()V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
}
void setAccelerometerIntervalJni(float interval) {
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setAccelerometerInterval", "(F)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, interval);
t.env->DeleteLocalRef(t.classID);
}
}
void disableAccelerometerJni() {
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "disableAccelerometer", "()V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
}
void setKeepScreenOnJni(bool value) {
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setKeepScreenOn", "(Z)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, value);
t.env->DeleteLocalRef(t.classID);
}
}
void vibrateJni(float duration) {
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "vibrate", "(F)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, duration);
t.env->DeleteLocalRef(t.classID);
}
}
std::string getVersionJNI() {
JniMethodInfo t;
std::string ret("");
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getVersion", "()Ljava/lang/String;")) {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
ret = JniHelper::jstring2string(str);
t.env->DeleteLocalRef(str);
}
return ret;
}
extern bool openURLJNI(const char* url) {
JniMethodInfo t;
bool ret = false;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "openURL", "(Ljava/lang/String;)Z")) {
jstring stringArg = t.env->NewStringUTF(url);
ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
}
return ret;
}
// functions for UserDefault
bool getBoolForKeyJNI(const char* key, bool defaultValue)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getBoolForKey", "(Ljava/lang/String;Z)Z")) {
jstring stringArg = t.env->NewStringUTF(key);
jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg, defaultValue);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
return ret;
}
return defaultValue;
}
int getIntegerForKeyJNI(const char* key, int defaultValue)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getIntegerForKey", "(Ljava/lang/String;I)I")) {
jstring stringArg = t.env->NewStringUTF(key);
jint ret = t.env->CallStaticIntMethod(t.classID, t.methodID, stringArg, defaultValue);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
return ret;
}
return defaultValue;
}
float getFloatForKeyJNI(const char* key, float defaultValue)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getFloatForKey", "(Ljava/lang/String;F)F")) {
jstring stringArg = t.env->NewStringUTF(key);
jfloat ret = t.env->CallStaticFloatMethod(t.classID, t.methodID, stringArg, defaultValue);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
return ret;
}
return defaultValue;
}
double getDoubleForKeyJNI(const char* key, double defaultValue)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getDoubleForKey", "(Ljava/lang/String;D)D")) {
jstring stringArg = t.env->NewStringUTF(key);
jdouble ret = t.env->CallStaticDoubleMethod(t.classID, t.methodID, stringArg, defaultValue);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
return ret;
}
return defaultValue;
}
std::string getStringForKeyJNI(const char* key, const char* defaultValue)
{
JniMethodInfo t;
std::string ret("");
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getStringForKey", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;")) {
jstring stringArg1 = t.env->NewStringUTF(key);
jstring stringArg2 = t.env->NewStringUTF(defaultValue);
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, stringArg1, stringArg2);
ret = JniHelper::jstring2string(str);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(stringArg2);
t.env->DeleteLocalRef(str);
return ret;
}
return defaultValue;
}
void setBoolForKeyJNI(const char* key, bool value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setBoolForKey", "(Ljava/lang/String;Z)V")) {
jstring stringArg = t.env->NewStringUTF(key);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
}
}
void setIntegerForKeyJNI(const char* key, int value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setIntegerForKey", "(Ljava/lang/String;I)V")) {
jstring stringArg = t.env->NewStringUTF(key);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
}
}
void setFloatForKeyJNI(const char* key, float value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setFloatForKey", "(Ljava/lang/String;F)V")) {
jstring stringArg = t.env->NewStringUTF(key);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
}
}
void setDoubleForKeyJNI(const char* key, double value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setDoubleForKey", "(Ljava/lang/String;D)V")) {
jstring stringArg = t.env->NewStringUTF(key);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
}
}
void setStringForKeyJNI(const char* key, const char* value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setStringForKey", "(Ljava/lang/String;Ljava/lang/String;)V")) {
jstring stringArg1 = t.env->NewStringUTF(key);
jstring stringArg2 = t.env->NewStringUTF(value);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1, stringArg2);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(stringArg2);
}
}
void deleteValueForKeyJNI(const char* key)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "deleteValueForKey", "(Ljava/lang/String;)V")) {
jstring stringArg1 = t.env->NewStringUTF(key);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg1);
}
}
int addEditBoxJNI(int left, int top, int width, int height, float scaleX){
JniMethodInfo t;
int ret = -1;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "createEditBox", "(IIIIF)I")) {
ret = t.env->CallStaticIntMethod(t.classID, t.methodID, left, top, width, height, scaleX);
t.env->DeleteLocalRef(t.classID);
}
return ret;
}
void removeEditBoxJNI(int index)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "removeEditBox", "(I)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index);
t.env->DeleteLocalRef(t.classID);
}
}
void setEditBoxViewRectJNI(int index, int left, int top, int width, int height)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "setEditBoxViewRect", "(IIIII)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, left, top, width, height);
t.env->DeleteLocalRef(t.classID);
}
}
void setMaxLengthJNI(int index, int maxLength)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "setMaxLength", "(II)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, maxLength);
t.env->DeleteLocalRef(t.classID);
}
}
void openEditBoxKeyboardJNI(int index)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "openKeyboard", "(I)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index);
t.env->DeleteLocalRef(t.classID);
}
}
void closeEditBoxKeyboardJNI(int index)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "closeKeyboard", "(I)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index);
t.env->DeleteLocalRef(t.classID);
}
}
void setVisibleEditBoxJNI(int index, bool visibility)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "setVisible", "(IZ)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, visibility);
t.env->DeleteLocalRef(t.classID);
}
}
void setReturnTypeEditBoxJNI(int index, int returnType)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "setReturnType", "(II)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, returnType);
t.env->DeleteLocalRef(t.classID);
}
}
void setInputFlagEditBoxJNI(int index, int returnType)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "setInputFlag", "(II)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, returnType);
t.env->DeleteLocalRef(t.classID);
}
}
void setInputModeEditBoxJNI(int index, int inputMode)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "setInputMode", "(II)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, inputMode);
t.env->DeleteLocalRef(t.classID);
}
}
void setTextEditBoxJNI(int index, const char* text)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "setText", "(ILjava/lang/String;)V")) {
jstring stringText = StringUtils::newStringUTFJNI(t.env,text);
t.env->CallStaticVoidMethod(t.classID, t.methodID,index, stringText);
t.env->DeleteLocalRef(stringText);
t.env->DeleteLocalRef(t.classID);
}
}
void setFontEditBoxJNI(int index, const char* fontName, float fontSize)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "setFont", "(ILjava/lang/String;F)V")) {
jstring stringText = StringUtils::newStringUTFJNI(t.env,fontName);
t.env->CallStaticVoidMethod(t.classID, t.methodID,index, stringText, fontSize);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringText);
}
}
void setFontColorEditBoxJNI(int index, int red, int green, int blue, int alpha)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "setFontColor", "(IIIII)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID,index, red, green, blue, alpha);
t.env->DeleteLocalRef(t.classID);
}
}
void setPlaceHolderTextEditBoxJNI(int index, const char* text)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "setPlaceHolderText", "(ILjava/lang/String;)V")) {
jstring stringText = StringUtils::newStringUTFJNI(t.env,text);
t.env->CallStaticVoidMethod(t.classID, t.methodID,index, stringText);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringText);
}
}
void setPlaceHolderTextColorEditBoxJNI(int index, int red, int green, int blue, int alpha)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, EDITBOX_CLASS_NAME, "setPlaceHolderTextColor", "(IIIII)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID,index, red, green, blue, alpha);
t.env->DeleteLocalRef(t.classID);
}
return JniHelper::callStaticStringMethod(className, "getCocos2dxPackageName");
}
void conversionEncodingJNI(const char* src, int byteSize, const char* fromCharset, char* dst, const char* newCharset)
{
JniMethodInfo methodInfo;
if (JniHelper::getStaticMethodInfo(methodInfo, CLASS_NAME, "conversionEncoding", "([BLjava/lang/String;Ljava/lang/String;)[B")) {
if (JniHelper::getStaticMethodInfo(methodInfo, className.c_str(), "conversionEncoding", "([BLjava/lang/String;Ljava/lang/String;)[B")) {
jbyteArray strArray = methodInfo.env->NewByteArray(byteSize);
methodInfo.env->SetByteArrayRegion(strArray, 0, byteSize, reinterpret_cast<const jbyte*>(src));

View File

@ -30,46 +30,7 @@ THE SOFTWARE.
typedef void (*EditTextCallback)(const char* text, void* ctx);
extern const char * getApkPath();
extern void showDialogJNI(const char * message, const char * title);
extern void terminateProcessJNI();
extern std::string getCurrentLanguageJNI();
extern std::string getPackageNameJNI();
extern std::string getFileDirectoryJNI();
extern void enableAccelerometerJni();
extern void disableAccelerometerJni();
extern void setAccelerometerIntervalJni(float interval);
extern void setKeepScreenOnJni(bool value);
extern void vibrateJni(float duration);
extern std::string getVersionJNI();
extern bool openURLJNI(const char* url);
// functions for UserDefault
extern bool getBoolForKeyJNI(const char* key, bool defaultValue);
extern int getIntegerForKeyJNI(const char* key, int defaultValue);
extern float getFloatForKeyJNI(const char* key, float defaultValue);
extern double getDoubleForKeyJNI(const char* key, double defaultValue);
extern std::string getStringForKeyJNI(const char* key, const char* defaultValue);
extern void setBoolForKeyJNI(const char* key, bool value);
extern void setIntegerForKeyJNI(const char* key, int value);
extern void setFloatForKeyJNI(const char* key, float value);
extern void setDoubleForKeyJNI(const char* key, double value);
extern void setStringForKeyJNI(const char* key, const char* value);
extern void deleteValueForKeyJNI(const char* key);
extern void conversionEncodingJNI(const char* src, int byteSize, const char* fromCharset, char* dst, const char* newCharset);
//Added for new Android EditBox
extern int addEditBoxJNI(int left, int top, int width, int height, float scaleX);
extern void removeEditBoxJNI(int index);
extern void setEditBoxViewRectJNI(int index, int left, int top, int width, int height);
extern void setMaxLengthJNI(int index, int maxLength);
extern void openEditBoxKeyboardJNI(int index);
extern void closeEditBoxKeyboardJNI(int index);
extern void setVisibleEditBoxJNI(int index, bool visibility);
extern void setReturnTypeEditBoxJNI(int index, int returnType);
extern void setInputFlagEditBoxJNI(int index, int inputFlag);
extern void setInputModeEditBoxJNI(int index, int inputMode);
extern void setTextEditBoxJNI(int index, const char* text);
extern void setFontEditBoxJNI(int index, const char* fontName, float fontSize);
extern void setFontColorEditBoxJNI(int index, int red, int green, int blue, int alpha);
extern void setPlaceHolderTextEditBoxJNI(int index, const char* text);
extern void setPlaceHolderTextColorEditBoxJNI(int index, int red, int green, int blue, int alpha);
#endif /* __Java_org_cocos2dx_lib_Cocos2dxHelper_H__ */

View File

@ -67,6 +67,7 @@ namespace cocos2d {
JavaVM* JniHelper::_psJavaVM = nullptr;
jmethodID JniHelper::loadclassMethod_methodID = nullptr;
jobject JniHelper::classloader = nullptr;
std::unordered_map<JNIEnv*, std::vector<jobject>> JniHelper::localRefs;
JavaVM* JniHelper::getJavaVM() {
pthread_t thisthread = pthread_self();
@ -275,4 +276,29 @@ namespace cocos2d {
return strValue;
}
jstring JniHelper::convert(cocos2d::JniMethodInfo& t, const char* x) {
jstring ret = cocos2d::StringUtils::newStringUTFJNI(t.env, x ? x : "");
localRefs[t.env].push_back(ret);
return ret;
}
jstring JniHelper::convert(cocos2d::JniMethodInfo& t, const std::string& x) {
return convert(t, x.c_str());
}
void JniHelper::deleteLocalRefs(JNIEnv* env) {
if (!env) {
return;
}
for (const auto& ref : localRefs[env]) {
env->DeleteLocalRef(ref);
}
localRefs[env].clear();
}
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());
}
} //namespace cocos2d

View File

@ -27,6 +27,8 @@ THE SOFTWARE.
#include <jni.h>
#include <string>
#include <vector>
#include <unordered_map>
#include "platform/CCPlatformMacros.h"
NS_CC_BEGIN
@ -60,6 +62,109 @@ public:
static jmethodID loadclassMethod_methodID;
static jobject classloader;
template <typename... Ts>
static void callStaticVoidMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")V";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, convert(t, xs)...);
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env);
} else {
reportError(className, methodName, signature);
}
}
template <typename... Ts>
static bool callStaticBooleanMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
jboolean jret = JNI_FALSE;
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")Z";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
jret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, convert(t, xs)...);
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env);
} else {
reportError(className, methodName, signature);
}
return (jret == JNI_TRUE);
}
template <typename... Ts>
static int callStaticIntMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
jint ret = 0;
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")I";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
ret = t.env->CallStaticIntMethod(t.classID, t.methodID, convert(t, xs)...);
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env);
} else {
reportError(className, methodName, signature);
}
return ret;
}
template <typename... Ts>
static float callStaticFloatMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
jfloat ret = 0.0;
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")F";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
ret = t.env->CallStaticFloatMethod(t.classID, t.methodID, convert(t, xs)...);
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env);
} else {
reportError(className, methodName, signature);
}
return ret;
}
template <typename... Ts>
static double callStaticDoubleMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
jdouble ret = 0.0;
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")D";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
ret = t.env->CallStaticDoubleMethod(t.classID, t.methodID, convert(t, xs)...);
t.env->DeleteLocalRef(t.classID);
deleteLocalRefs(t.env);
} else {
reportError(className, methodName, signature);
}
return ret;
}
template <typename... Ts>
static std::string callStaticStringMethod(const std::string& className,
const std::string& methodName,
Ts... xs) {
std::string ret;
cocos2d::JniMethodInfo t;
std::string signature = "(" + std::string(getJNISignature(xs...)) + ")Ljava/lang/String;";
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), methodName.c_str(), signature.c_str())) {
jstring jret = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, convert(t, xs)...);
ret = cocos2d::JniHelper::jstring2string(jret);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(jret);
deleteLocalRefs(t.env);
} else {
reportError(className, methodName, signature);
}
return ret;
}
private:
static JNIEnv* cacheEnv(JavaVM* jvm);
@ -69,6 +174,73 @@ private:
const char *paramCode);
static JavaVM* _psJavaVM;
static jstring convert(cocos2d::JniMethodInfo& t, const char* x);
static jstring convert(cocos2d::JniMethodInfo& t, const std::string& x);
template <typename T>
static T convert(cocos2d::JniMethodInfo&, T x) {
return x;
}
static std::unordered_map<JNIEnv*, std::vector<jobject>> localRefs;
static void deleteLocalRefs(JNIEnv* env);
static std::string getJNISignature() {
return "";
}
static std::string getJNISignature(bool) {
return "Z";
}
static std::string getJNISignature(char) {
return "C";
}
static std::string getJNISignature(short) {
return "S";
}
static std::string getJNISignature(int) {
return "I";
}
static std::string getJNISignature(long) {
return "J";
}
static std::string getJNISignature(float) {
return "F";
}
static std::string getJNISignature(double) {
return "D";
}
static std::string getJNISignature(const char*) {
return "Ljava/lang/String;";
}
static std::string getJNISignature(const std::string&) {
return "Ljava/lang/String;";
}
template <typename T>
static std::string getJNISignature(T x) {
// This template should never be instantiated
static_assert(sizeof(x) == 0, "Unsupported argument type");
return "";
}
template <typename T, typename... Ts>
static std::string getJNISignature(T x, Ts... xs) {
return getJNISignature(x) + getJNISignature(xs...);
}
static void reportError(const std::string& className, const std::string& methodName, const std::string& signature);
};
NS_CC_END

View File

@ -42,6 +42,8 @@
USING_NS_CC;
static int _initialized = 0;
static std::string className = "org/cocos2dx/lib/Cocos2dxLocalStorage";
static void splitFilename (std::string& str)
{
size_t found = 0;
@ -59,20 +61,10 @@ void localStorageInit( const std::string& fullpath)
if( ! _initialized )
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "init", "(Ljava/lang/String;Ljava/lang/String;)Z")) {
std::string strDBFilename = fullpath;
splitFilename(strDBFilename);
jstring jdbName = t.env->NewStringUTF(strDBFilename.c_str());
jstring jtableName = t.env->NewStringUTF("data");
jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, jdbName, jtableName);
t.env->DeleteLocalRef(jdbName);
t.env->DeleteLocalRef(jtableName);
t.env->DeleteLocalRef(t.classID);
if (ret) {
_initialized = 1;
}
std::string strDBFilename = fullpath;
splitFilename(strDBFilename);
if (JniHelper::callStaticBooleanMethod(className, "init", strDBFilename, "data")) {
_initialized = 1;
}
}
}
@ -80,15 +72,7 @@ void localStorageInit( const std::string& fullpath)
void localStorageFree()
{
if( _initialized ) {
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "destory", "()V"))
{
t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
JniHelper::callStaticVoidMethod(className, "destory");
_initialized = 0;
}
}
@ -97,17 +81,7 @@ void localStorageFree()
void localStorageSetItem( const std::string& key, const std::string& value)
{
assert( _initialized );
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "setItem", "(Ljava/lang/String;Ljava/lang/String;)V")) {
jstring jkey = t.env->NewStringUTF(key.c_str());
jstring jvalue = t.env->NewStringUTF(value.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, jkey, jvalue);
t.env->DeleteLocalRef(jkey);
t.env->DeleteLocalRef(jvalue);
t.env->DeleteLocalRef(t.classID);
}
JniHelper::callStaticVoidMethod(className, "setItem", key, value);
}
/** gets an item from the LS */
@ -136,14 +110,7 @@ bool localStorageGetItem( const std::string& key, std::string *outItem )
void localStorageRemoveItem( const std::string& key )
{
assert( _initialized );
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "removeItem", "(Ljava/lang/String;)V")) {
jstring jkey = t.env->NewStringUTF(key.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, jkey);
t.env->DeleteLocalRef(jkey);
t.env->DeleteLocalRef(t.classID);
}
JniHelper::callStaticVoidMethod(className, "removeItem", key);
}
@ -151,12 +118,7 @@ void localStorageRemoveItem( const std::string& key )
void localStorageClear()
{
assert( _initialized );
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "clear", "()V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
}
JniHelper::callStaticVoidMethod(className, "clear");
}
#endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

View File

@ -30,7 +30,7 @@
#include "UIEditBox.h"
#include <jni.h>
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#include "platform/android/jni/JniHelper.h"
#include "2d/CCLabel.h"
#include "base/ccUTF8.h"
#include "math/Vec2.h"
@ -39,6 +39,7 @@
NS_CC_BEGIN
static const std::string editBoxClassName = "org/cocos2dx/lib/Cocos2dxEditBoxHelper";
namespace ui {
@ -80,7 +81,8 @@ EditBoxImplAndroid::EditBoxImplAndroid(EditBox* pEditText)
EditBoxImplAndroid::~EditBoxImplAndroid()
{
s_allEditBoxes.erase(_editBoxIndex);
removeEditBoxJNI(_editBoxIndex);
JniHelper::callStaticVoidMethod(editBoxClassName, "removeEditBox", _editBoxIndex);
}
void EditBoxImplAndroid::createNativeControl(const Rect& frame)
@ -100,7 +102,9 @@ void EditBoxImplAndroid::createNativeControl(const Rect& frame)
auto uiWidth = (rightTop.x - leftBottom.x) * glView->getScaleX();
auto uiHeight = (rightTop.y - leftBottom.y) * glView->getScaleY();
LOGD("scaleX = %f", glView->getScaleX());
_editBoxIndex = addEditBoxJNI(uiLeft, uiTop, uiWidth, uiHeight, glView->getScaleX());
_editBoxIndex = JniHelper::callStaticIntMethod(editBoxClassName, "createEditBox",
(int)uiLeft, (int)uiTop, (int)uiWidth, (int)uiHeight,
(float)glView->getScaleX());
s_allEditBoxes[_editBoxIndex] = this;
}
@ -108,12 +112,15 @@ void EditBoxImplAndroid::setNativeFont(const char* pFontName, int fontSize)
{
auto director = cocos2d::Director::getInstance();
auto glView = director->getOpenGLView();
setFontEditBoxJNI(_editBoxIndex, pFontName, fontSize * glView->getScaleX());
JniHelper::callStaticVoidMethod(editBoxClassName, "setFont",
_editBoxIndex, pFontName,
(float)fontSize * glView->getScaleX());
}
void EditBoxImplAndroid::setNativeFontColor(const Color4B& color)
{
setFontColorEditBoxJNI(_editBoxIndex, color.r, color.g, color.b, color.a);
JniHelper::callStaticVoidMethod(editBoxClassName, "setFontColor", _editBoxIndex,
(int)color.r, (int)color.g, (int)color.b, (int)color.a);
}
void EditBoxImplAndroid::setNativePlaceholderFont(const char* pFontName, int fontSize)
@ -123,28 +130,31 @@ void EditBoxImplAndroid::setNativePlaceholderFont(const char* pFontName, int fon
void EditBoxImplAndroid::setNativePlaceholderFontColor(const Color4B& color)
{
setPlaceHolderTextColorEditBoxJNI(_editBoxIndex, color.r, color.g, color.b, color.a);
JniHelper::callStaticVoidMethod(editBoxClassName, "setPlaceHolderTextColor", _editBoxIndex,
(int)color.r, (int)color.g, (int)color.b, (int)color.a);
}
void EditBoxImplAndroid::setNativeInputMode(EditBox::InputMode inputMode)
{
setInputModeEditBoxJNI(_editBoxIndex, static_cast<int>(inputMode));
JniHelper::callStaticVoidMethod(editBoxClassName, "setInputMode",
_editBoxIndex, static_cast<int>(inputMode));
}
void EditBoxImplAndroid::setNativeMaxLength(int maxLength)
{
setMaxLengthJNI(_editBoxIndex, maxLength);
JniHelper::callStaticVoidMethod(editBoxClassName, "setMaxLength", _editBoxIndex, maxLength);
}
void EditBoxImplAndroid::setNativeInputFlag(EditBox::InputFlag inputFlag)
{
setInputFlagEditBoxJNI(_editBoxIndex, static_cast<int>(inputFlag));
JniHelper::callStaticVoidMethod(editBoxClassName, "setInputFlag",
_editBoxIndex, static_cast<int>(inputFlag));
}
void EditBoxImplAndroid::setNativeReturnType(EditBox::KeyboardReturnType returnType)
{
setReturnTypeEditBoxJNI(_editBoxIndex, static_cast<int>(returnType));
JniHelper::callStaticVoidMethod(editBoxClassName, "setReturnType",
_editBoxIndex, static_cast<int>(returnType));
}
bool EditBoxImplAndroid::isEditing()
@ -154,36 +164,37 @@ bool EditBoxImplAndroid::isEditing()
void EditBoxImplAndroid::setNativeText(const char* pText)
{
setTextEditBoxJNI(_editBoxIndex, pText);
JniHelper::callStaticVoidMethod(editBoxClassName, "setText", _editBoxIndex, pText);
}
void EditBoxImplAndroid::setNativePlaceHolder(const char* pText)
{
setPlaceHolderTextEditBoxJNI(_editBoxIndex, pText);
JniHelper::callStaticVoidMethod(editBoxClassName, "setPlaceHolderText", _editBoxIndex, pText);
}
void EditBoxImplAndroid::setNativeVisible(bool visible)
{ // don't need to be implemented on android platform.
setVisibleEditBoxJNI(_editBoxIndex, visible);
JniHelper::callStaticVoidMethod(editBoxClassName, "setVisible", _editBoxIndex, visible);
}
void EditBoxImplAndroid::updateNativeFrame(const Rect& rect)
{
setEditBoxViewRectJNI(_editBoxIndex, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
JniHelper::callStaticVoidMethod(editBoxClassName, "setEditBoxViewRect", _editBoxIndex,
(int)rect.origin.x, (int)rect.origin.y,
(int)rect.size.width, (int)rect.size.height);
}
void EditBoxImplAndroid::nativeOpenKeyboard()
{
//it will also open up the soft keyboard
setVisibleEditBoxJNI(_editBoxIndex,true);
JniHelper::callStaticVoidMethod(editBoxClassName, "setVisible", _editBoxIndex, true);
}
void EditBoxImplAndroid::nativeCloseKeyboard()
{
closeEditBoxKeyboardJNI(_editBoxIndex);
JniHelper::callStaticVoidMethod(editBoxClassName, "closeKeyboard", _editBoxIndex);
}
void editBoxEditingDidBegin(int index)

View File

@ -29,14 +29,15 @@
#include <stdlib.h>
#include <jni.h>
#include <string>
#include "jni/JniHelper.h"
#include "platform/android/jni/JniHelper.h"
#include "base/CCDirector.h"
#include "base/CCEventListenerKeyboard.h"
#include "platform/CCFileUtils.h"
#include "ui/UIHelper.h"
//-----------------------------------------------------------------------------------------------------------
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxVideoHelper"
static const std::string videoHelperClassName = "org/cocos2dx/lib/Cocos2dxVideoHelper";
USING_NS_CC;
@ -54,7 +55,7 @@ int createVideoWidgetJNI()
{
JniMethodInfo t;
int ret = -1;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "createVideoWidget", "()I")) {
if (JniHelper::getStaticMethodInfo(t, videoHelperClassName.c_str(), "createVideoWidget", "()I")) {
ret = t.env->CallStaticIntMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
@ -63,108 +64,6 @@ int createVideoWidgetJNI()
return ret;
}
void callVideoNonParameterFun(int index,const char* funName)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, funName, "(I)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index);
t.env->DeleteLocalRef(t.classID);
}
}
void removeVideoWidgetJNI(int index)
{
callVideoNonParameterFun(index,"removeVideoWidget");
}
void setVideoRectJNI(int index,int left,int top,int width,int height)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setVideoRect", "(IIIII)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, left, top, width, height);
t.env->DeleteLocalRef(t.classID);
}
}
void setFullScreenEnabledJni(int index,bool enabled, int width, int height)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setFullScreenEnabled", "(IZII)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, enabled, width, height);
t.env->DeleteLocalRef(t.classID);
}
}
void setVideoURLJNI(int index,int videoSource,const std::string& videoUrl)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setVideoUrl", "(IILjava/lang/String;)V")) {
jstring stringArg = t.env->NewStringUTF(videoUrl.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, videoSource,stringArg);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
}
}
void startVideoJNI(int index)
{
callVideoNonParameterFun(index,"startVideo");
}
void pauseVideoJNI(int index)
{
callVideoNonParameterFun(index,"pauseVideo");
}
void resumeVideoJNI(int index)
{
callVideoNonParameterFun(index,"resumeVideo");
}
void stopVideoJNI(int index)
{
callVideoNonParameterFun(index,"stopVideo");
}
void seekVideoToJNI(int index,int msec)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "seekVideoTo", "(II)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, msec);
t.env->DeleteLocalRef(t.classID);
}
}
void setVideoVisible(int index,bool visible)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setVideoVisible", "(IZ)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, visible);
t.env->DeleteLocalRef(t.classID);
}
}
void setVideoKeepRatioEnabled(int index,bool enabled)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setVideoKeepRatioEnabled", "(IZ)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, enabled);
t.env->DeleteLocalRef(t.classID);
}
}
//-----------------------------------------------------------------------------------------------------------
using namespace cocos2d::experimental::ui;
@ -190,21 +89,23 @@ VideoPlayer::VideoPlayer()
VideoPlayer::~VideoPlayer()
{
s_allVideoPlayers.erase(_videoPlayerIndex);
removeVideoWidgetJNI(_videoPlayerIndex);
JniHelper::callStaticVoidMethod(videoHelperClassName, "removeVideoWidget", _videoPlayerIndex);
}
void VideoPlayer::setFileName(const std::string& fileName)
{
_videoURL = FileUtils::getInstance()->fullPathForFilename(fileName);
_videoSource = VideoPlayer::Source::FILENAME;
setVideoURLJNI(_videoPlayerIndex, (int)Source::FILENAME,_videoURL);
JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoUrl", _videoPlayerIndex,
(int)Source::FILENAME,_videoURL);
}
void VideoPlayer::setURL(const std::string& videoUrl)
{
_videoURL = videoUrl;
_videoSource = VideoPlayer::Source::URL;
setVideoURLJNI(_videoPlayerIndex,(int)Source::URL,_videoURL);
JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoUrl", _videoPlayerIndex,
(int)Source::URL,_videoURL);
}
void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags)
@ -214,9 +115,9 @@ void VideoPlayer::draw(Renderer* renderer, const Mat4 &transform, uint32_t flags
if (flags & FLAGS_TRANSFORM_DIRTY)
{
auto uiRect = cocos2d::ui::Helper::convertBoundingBoxToScreen(this);
setVideoRectJNI(_videoPlayerIndex, uiRect.origin.x, uiRect.origin.y,
uiRect.size.width, uiRect.size.height);
JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoRect", _videoPlayerIndex,
(int)uiRect.origin.x, (int)uiRect.origin.y,
(int)uiRect.size.width, (int)uiRect.size.height);
}
#if CC_VIDEOPLAYER_DEBUG_DRAW
@ -240,7 +141,8 @@ void VideoPlayer::setFullScreenEnabled(bool enabled)
_fullScreenEnabled = enabled;
auto frameSize = Director::getInstance()->getOpenGLView()->getFrameSize();
setFullScreenEnabledJni(_videoPlayerIndex, enabled, frameSize.width, frameSize.height);
JniHelper::callStaticVoidMethod(videoHelperClassName, "setFullScreenEnabled", _videoPlayerIndex,
enabled, (int)frameSize.width, (int)frameSize.height);
}
}
@ -254,7 +156,7 @@ void VideoPlayer::setKeepAspectRatioEnabled(bool enable)
if (_keepAspectRatioEnabled != enable)
{
_keepAspectRatioEnabled = enable;
setVideoKeepRatioEnabled(_videoPlayerIndex,enable);
JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoKeepRatioEnabled", _videoPlayerIndex, enable);
}
}
@ -287,7 +189,7 @@ void VideoPlayer::play()
{
if (! _videoURL.empty())
{
startVideoJNI(_videoPlayerIndex);
JniHelper::callStaticVoidMethod(videoHelperClassName, "startVideo", _videoPlayerIndex);
}
}
@ -295,7 +197,7 @@ void VideoPlayer::pause()
{
if (! _videoURL.empty())
{
pauseVideoJNI(_videoPlayerIndex);
JniHelper::callStaticVoidMethod(videoHelperClassName, "pauseVideo", _videoPlayerIndex);
}
}
@ -303,7 +205,7 @@ void VideoPlayer::resume()
{
if (! _videoURL.empty())
{
resumeVideoJNI(_videoPlayerIndex);
JniHelper::callStaticVoidMethod(videoHelperClassName, "resumeVideo", _videoPlayerIndex);
}
}
@ -311,7 +213,7 @@ void VideoPlayer::stop()
{
if (! _videoURL.empty())
{
stopVideoJNI(_videoPlayerIndex);
JniHelper::callStaticVoidMethod(videoHelperClassName, "stopVideo", _videoPlayerIndex);
}
}
@ -319,7 +221,7 @@ void VideoPlayer::seekTo(float sec)
{
if (! _videoURL.empty())
{
seekVideoToJNI(_videoPlayerIndex,int(sec * 1000));
JniHelper::callStaticVoidMethod(videoHelperClassName, "seekVideoTo", _videoPlayerIndex, int(sec * 1000));
}
}
@ -334,7 +236,7 @@ void VideoPlayer::setVisible(bool visible)
if (! _videoURL.empty())
{
setVideoVisible(_videoPlayerIndex,visible);
JniHelper::callStaticVoidMethod(videoHelperClassName, "setVideoVisible", _videoPlayerIndex, visible);
}
}

View File

@ -29,8 +29,7 @@
#include <unordered_map>
#include <stdlib.h>
#include <string>
#include "jni/JniHelper.h"
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#include "UIWebView.h"
#include "platform/CCGLView.h"
@ -38,7 +37,7 @@
#include "platform/CCFileUtils.h"
#include "ui/UIHelper.h"
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxWebViewHelper"
static const std::string className = "org/cocos2dx/lib/Cocos2dxWebViewHelper";
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,"",__VA_ARGS__)
@ -132,7 +131,7 @@ namespace {
int createWebViewJNI() {
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "createWebView", "()I")) {
if (cocos2d::JniHelper::getStaticMethodInfo(t, className.c_str(), "createWebView", "()I")) {
// LOGD("error: %s,%d",__func__,__LINE__);
jint viewTag = t.env->CallStaticIntMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
@ -141,179 +140,6 @@ int createWebViewJNI() {
return -1;
}
void removeWebViewJNI(const int index) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "removeWebView", "(I)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index);
t.env->DeleteLocalRef(t.classID);
}
}
void setWebViewRectJNI(const int index, const int left, const int top, const int width, const int height) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setWebViewRect", "(IIIII)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, left, top, width, height);
t.env->DeleteLocalRef(t.classID);
}
}
void setJavascriptInterfaceSchemeJNI(const int index, const std::string &scheme) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setJavascriptInterfaceScheme", "(ILjava/lang/String;)V")) {
jstring jScheme = t.env->NewStringUTF(scheme.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jScheme);
t.env->DeleteLocalRef(jScheme);
t.env->DeleteLocalRef(t.classID);
}
}
void loadDataJNI(const int index, const std::string &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadData", "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V")) {
jstring jData = t.env->NewStringUTF(data.c_str());
jstring jMIMEType = t.env->NewStringUTF(MIMEType.c_str());
jstring jEncoding = t.env->NewStringUTF(encoding.c_str());
jstring jBaseURL = t.env->NewStringUTF(getFixedBaseUrl(baseURL).c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jData, jMIMEType, jEncoding, jBaseURL);
t.env->DeleteLocalRef(jData);
t.env->DeleteLocalRef(jMIMEType);
t.env->DeleteLocalRef(jEncoding);
t.env->DeleteLocalRef(jBaseURL);
t.env->DeleteLocalRef(t.classID);
}
}
void loadHTMLStringJNI(const int index, const std::string &string, const std::string &baseURL) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadHTMLString", "(ILjava/lang/String;Ljava/lang/String;)V")) {
jstring jString = t.env->NewStringUTF(string.c_str());
jstring jBaseURL = t.env->NewStringUTF(getFixedBaseUrl(baseURL).c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jString, jBaseURL);
t.env->DeleteLocalRef(jString);
t.env->DeleteLocalRef(jBaseURL);
t.env->DeleteLocalRef(t.classID);
}
}
void loadUrlJNI(const int index, const std::string &url) {
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadUrl", "(ILjava/lang/String;)V")) {
jstring jUrl = t.env->NewStringUTF(url.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jUrl);
t.env->DeleteLocalRef(jUrl);
t.env->DeleteLocalRef(t.classID);
}
}
void loadFileJNI(const int index, const std::string &filePath) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "loadFile", "(ILjava/lang/String;)V")) {
jstring jFilePath = t.env->NewStringUTF(filePath.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jFilePath);
t.env->DeleteLocalRef(jFilePath);
t.env->DeleteLocalRef(t.classID);
}
}
void stopLoadingJNI(const int index) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "stopLoading", "(I)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index);
t.env->DeleteLocalRef(t.classID);
}
}
void reloadJNI(const int index) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "reload", "(I)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index);
t.env->DeleteLocalRef(t.classID);
}
}
bool canGoBackJNI(const int index) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "canGoBack", "(I)Z")) {
jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, index);
t.env->DeleteLocalRef(t.classID);
return ret;
}
return false;
}
bool canGoForwardJNI(const int index) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "canGoForward", "(I)Z")) {
jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, index);
t.env->DeleteLocalRef(t.classID);
return ret;
}
return false;
}
void goBackJNI(const int index) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "goBack", "(I)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index);
t.env->DeleteLocalRef(t.classID);
}
}
void goForwardJNI(const int index) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "goForward", "(I)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index);
t.env->DeleteLocalRef(t.classID);
}
}
void evaluateJSJNI(const int index, const std::string &js) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "evaluateJS", "(ILjava/lang/String;)V")) {
jstring jjs = t.env->NewStringUTF(js.c_str());
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, jjs);
t.env->DeleteLocalRef(jjs);
t.env->DeleteLocalRef(t.classID);
}
}
void setScalesPageToFitJNI(const int index, const bool scalesPageToFit) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setScalesPageToFit", "(IZ)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, scalesPageToFit);
t.env->DeleteLocalRef(t.classID);
}
}
void setWebViewVisibleJNI(const int index, const bool visible) {
// LOGD("error: %s,%d",__func__,__LINE__);
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setVisible", "(IZ)V")) {
t.env->CallStaticVoidMethod(t.classID, t.methodID, index, visible);
t.env->DeleteLocalRef(t.classID);
}
}
std::string getUrlStringByFileName(const std::string &fileName) {
// LOGD("error: %s,%d",__func__,__LINE__);
const std::string basePath("file:///android_asset/");
@ -343,62 +169,62 @@ namespace cocos2d {
}
WebViewImpl::~WebViewImpl() {
removeWebViewJNI(_viewTag);
JniHelper::callStaticVoidMethod(className, "removeWebView", _viewTag);
s_WebViewImpls.erase(_viewTag);
}
void WebViewImpl::loadData(const Data &data, const std::string &MIMEType, const std::string &encoding, const std::string &baseURL) {
std::string dataString(reinterpret_cast<char *>(data.getBytes()), static_cast<unsigned int>(data.getSize()));
loadDataJNI(_viewTag, dataString, MIMEType, encoding, baseURL);
JniHelper::callStaticVoidMethod(className, "setJavascriptInterfaceScheme", _viewTag, dataString, MIMEType, encoding, baseURL);
}
void WebViewImpl::loadHTMLString(const std::string &string, const std::string &baseURL) {
loadHTMLStringJNI(_viewTag, string, baseURL);
JniHelper::callStaticVoidMethod(className, "loadHTMLString", _viewTag, string, baseURL);
}
void WebViewImpl::loadURL(const std::string &url) {
loadUrlJNI(_viewTag, url);
JniHelper::callStaticVoidMethod(className, "loadUrl", _viewTag, url);
}
void WebViewImpl::loadFile(const std::string &fileName) {
auto fullPath = getUrlStringByFileName(fileName);
loadFileJNI(_viewTag, fullPath);
JniHelper::callStaticVoidMethod(className, "loadFile", _viewTag, fullPath);
}
void WebViewImpl::stopLoading() {
stopLoadingJNI(_viewTag);
JniHelper::callStaticVoidMethod(className, "stopLoading", _viewTag);
}
void WebViewImpl::reload() {
reloadJNI(_viewTag);
JniHelper::callStaticVoidMethod(className, "reload", _viewTag);
}
bool WebViewImpl::canGoBack() {
return canGoBackJNI(_viewTag);
return JniHelper::callStaticBooleanMethod(className, "canGoBack", _viewTag);
}
bool WebViewImpl::canGoForward() {
return canGoForwardJNI(_viewTag);
return JniHelper::callStaticBooleanMethod(className, "canGoForward", _viewTag);
}
void WebViewImpl::goBack() {
goBackJNI(_viewTag);
JniHelper::callStaticVoidMethod(className, "goBack", _viewTag);
}
void WebViewImpl::goForward() {
goForwardJNI(_viewTag);
JniHelper::callStaticVoidMethod(className, "goForward", _viewTag);
}
void WebViewImpl::setJavascriptInterfaceScheme(const std::string &scheme) {
setJavascriptInterfaceSchemeJNI(_viewTag, scheme);
JniHelper::callStaticVoidMethod(className, "setJavascriptInterfaceScheme", _viewTag, scheme);
}
void WebViewImpl::evaluateJS(const std::string &js) {
evaluateJSJNI(_viewTag, js);
JniHelper::callStaticVoidMethod(className, "evaluateJS", _viewTag, js);
}
void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit) {
setScalesPageToFitJNI(_viewTag, scalesPageToFit);
JniHelper::callStaticVoidMethod(className, "setScalesPageToFit", _viewTag, scalesPageToFit);
}
bool WebViewImpl::shouldStartLoading(const int viewTag, const std::string &url) {
@ -446,14 +272,14 @@ namespace cocos2d {
void WebViewImpl::draw(cocos2d::Renderer *renderer, cocos2d::Mat4 const &transform, uint32_t flags) {
if (flags & cocos2d::Node::FLAGS_TRANSFORM_DIRTY) {
auto uiRect = cocos2d::ui::Helper::convertBoundingBoxToScreen(_webView);
setWebViewRectJNI(_viewTag, uiRect.origin.x, uiRect.origin.y,
uiRect.size.width, uiRect.size.height);
JniHelper::callStaticVoidMethod(className, "setWebViewRect", _viewTag,
(int)uiRect.origin.x, (int)uiRect.origin.y,
(int)uiRect.size.width, (int)uiRect.size.height);
}
}
void WebViewImpl::setVisible(bool visible) {
setWebViewVisibleJNI(_viewTag, visible);
JniHelper::callStaticVoidMethod(className, "setVisible", _viewTag, visible);
}
} // namespace ui
} // namespace experimental

View File

@ -0,0 +1,62 @@
#include "JNITest.h"
#include "platform/android/jni/JniHelper.h"
#include <string>
USING_NS_CC;
JNITests::JNITests()
{
ADD_TEST_CASE(JNITest);
}
JNITest::JNITest()
{
auto nameLabel = Label::createWithTTF("JNI Test", "fonts/arial.ttf", 28);
nameLabel->setPosition(VisibleRect::center().x, VisibleRect::top().y - 50);
addChild(nameLabel);
auto checkLabel = Label::createWithTTF("Please check console output", "fonts/arial.ttf", 22);
checkLabel->setPosition(VisibleRect::center());
addChild(checkLabel);
const std::string classPath = "org/cocos2dx/cpp_tests/JNITest";
JniHelper::callStaticVoidMethod(classPath, "voidMethod1");
JniHelper::callStaticVoidMethod(classPath, "voidMethod2", "JNI is easy");
JniHelper::callStaticVoidMethod(classPath, "voidMethod3", int(4), float(2.5), "JNI is really easy");
bool b1 = JniHelper::callStaticBooleanMethod(classPath, "booleanMethod", int(5));
CC_ASSERT(b1 == true);
bool b2 = JniHelper::callStaticBooleanMethod(classPath, "booleanMethod", int(-3));
CC_ASSERT(b2 == false);
int i = JniHelper::callStaticIntMethod(classPath, "intMethod", int(10), int(10));
CC_ASSERT(i == 20);
float f = JniHelper::callStaticFloatMethod(classPath, "floatMethod", float(2.35), float(7.65));
CC_ASSERT(f == 10.0);
double d = JniHelper::callStaticDoubleMethod(classPath, "doubleMethod", double(2.5), int(4));
CC_ASSERT(d == 10.0);
std::string str = "ABCDEF";
std::string s1 = JniHelper::callStaticStringMethod(classPath, "stringMethod", str, true);
CC_ASSERT(s1 == "FEDCBA");
std::string s2 = JniHelper::callStaticStringMethod(classPath, "stringMethod", str, false);
CC_ASSERT(s2 == "ABCDEF");
const char* cstr = "XYZ";
std::string s3 = JniHelper::callStaticStringMethod(classPath, "stringMethod", cstr, true);
CC_ASSERT(s3 == "ZYX");
// should not crash
for (int i = 0; i < 10000; i++) {
JniHelper::callStaticVoidMethod(classPath, "voidMethod4", "ABCDEF");
}
// should not compile
// JniHelper::callStaticVoidMethod(classPath, "voidMethod4", std::vector<int>());
}

View File

@ -0,0 +1,17 @@
#ifndef _JNI_TEST_H_
#define _JNI_TEST_H_
#include "cocos2d.h"
#include "../BaseTest.h"
DEFINE_TEST_SUITE(JNITests);
class JNITest : public TestCase
{
public:
CREATE_FUNC(JNITest);
JNITest();
};
#endif // _JNI_TEST_H_

View File

@ -50,6 +50,9 @@ public:
addTest("FileUtils", []() { return new FileUtilsTests(); });
addTest("Fonts", []() { return new FontTests(); });
addTest("Interval", [](){return new IntervalTests(); });
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
addTest("JNIHelper", []() { return new JNITests(); });
#endif
addTest("Material System", [](){return new MaterialSystemTest(); });
addTest("Navigation Mesh", [](){return new NavMeshTests(); });
addTest("Node: BillBoard Test", [](){ return new BillBoardTests(); });

View File

@ -18,6 +18,9 @@
#endif
#endif
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "JNITest/JNITest.h"
#endif
// sort them alphabetically. thanks
#include "ActionManagerTest/ActionManagerTest.h"

View File

@ -88,6 +88,7 @@ LOCAL_SRC_FILES := main.cpp \
../../../Classes/FontTest/FontTest.cpp \
../../../Classes/InputTest/MouseTest.cpp \
../../../Classes/IntervalTest/IntervalTest.cpp \
../../../Classes/JNITest/JNITest.cpp \
../../../Classes/LabelTest/LabelTest.cpp \
../../../Classes/LabelTest/LabelTestNew.cpp \
../../../Classes/LayerTest/LayerTest.cpp \

View File

@ -0,0 +1,118 @@
/****************************************************************************
Copyright (c) 2010-2012 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.
****************************************************************************/
package org.cocos2dx.cpp_tests;
import android.util.Log;
import java.lang.StringBuilder;
public class JNITest {
private static final String TAG = "JNITest";
public static void voidMethod1() {
Log.v(TAG, "Called voidMethod1");
}
public static void voidMethod2(final String str) {
StringBuilder message = new StringBuilder();
message.append("Called voidMethod2 with str = ");
message.append(str);
Log.v(TAG, message.toString());
}
public static void voidMethod3(int n, float x, final String str) {
StringBuilder message = new StringBuilder();
message.append("Called voidMethod3 with n = ");
message.append(n);
message.append(", x = ");
message.append(x);
message.append(", str = ");
message.append(str);
Log.v(TAG, message.toString());
}
public static void voidMethod4(final String str) {
// Used to test garbage collection
}
public static boolean booleanMethod(int n) {
boolean ret = n > 0;
StringBuilder message = new StringBuilder();
message.append("Called booleanMethod with n = ");
message.append(n);
message.append("\nReturning ");
message.append(ret);
Log.v(TAG, message.toString());
return ret;
}
public static int intMethod(int a, int b) {
int ret = a + b;
StringBuilder message = new StringBuilder();
message.append("Called intMethod with a = ");
message.append(a);
message.append(", b = ");
message.append(b);
message.append("\nReturning ");
message.append(ret);
Log.v(TAG, message.toString());
return ret;
}
public static float floatMethod(float x, float y) {
float ret = x + y;
StringBuilder message = new StringBuilder();
message.append("Called floatMethod with x = ");
message.append(x);
message.append(", y = ");
message.append(y);
message.append("\nReturning ");
message.append(ret);
Log.v(TAG, message.toString());
return ret;
}
public static double doubleMethod(double x, int n) {
double ret = x*n;
StringBuilder message = new StringBuilder();
message.append("Called doubleMethod with x = ");
message.append(x);
message.append(", n = ");
message.append(n);
message.append("\nReturning ");
message.append(ret);
Log.v(TAG, message.toString());
return ret;
}
public static String stringMethod(final String str, boolean reverse) {
String ret = reverse ? new StringBuilder(str).reverse().toString() : str;
StringBuilder message = new StringBuilder();
message.append("Called stringMethod with str = ");
message.append(str);
message.append("\nReturning ");
message.append(ret);
Log.v(TAG, message.toString());
return ret;
}
}

View File

@ -88,6 +88,7 @@ LOCAL_SRC_FILES := main.cpp \
../../Classes/FontTest/FontTest.cpp \
../../Classes/InputTest/MouseTest.cpp \
../../Classes/IntervalTest/IntervalTest.cpp \
../../Classes/JNITest/JNITest.cpp \
../../Classes/LabelTest/LabelTest.cpp \
../../Classes/LabelTest/LabelTestNew.cpp \
../../Classes/LayerTest/LayerTest.cpp \

View File

@ -0,0 +1,120 @@
/****************************************************************************
Copyright (c) 2010-2012 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.
****************************************************************************/
package org.cocos2dx.cpp_tests;
import android.util.Log;
import java.lang.StringBuilder;
public class JNITest {
private static final String TAG = "JNITest";
public static void voidMethod1() {
Log.v(TAG, "Called voidMethod1");
}
public static void voidMethod2(final String str) {
StringBuilder message = new StringBuilder();
message.append("Called voidMethod2 with str = ");
message.append(str);
Log.v(TAG, message.toString());
}
public static void voidMethod3(int n, float x, final String str) {
StringBuilder message = new StringBuilder();
message.append("Called voidMethod3 with n = ");
message.append(n);
message.append(", x = ");
message.append(x);
message.append(", str = ");
message.append(str);
Log.v(TAG, message.toString());
}
public static void voidMethod4(final String str) {
// Used to test garbage collection
}
public static boolean booleanMethod(int n) {
boolean ret = n > 0;
StringBuilder message = new StringBuilder();
message.append("Called booleanMethod with n = ");
message.append(n);
message.append("\nReturning ");
message.append(ret);
Log.v(TAG, message.toString());
return ret;
}
public static int intMethod(int a, int b) {
int ret = a + b;
StringBuilder message = new StringBuilder();
message.append("Called intMethod with a = ");
message.append(a);
message.append(", b = ");
message.append(b);
message.append("\nReturning ");
message.append(ret);
Log.v(TAG, message.toString());
return ret;
}
public static float floatMethod(float x, float y) {
float ret = x + y;
StringBuilder message = new StringBuilder();
message.append("Called floatMethod with x = ");
message.append(x);
message.append(", y = ");
message.append(y);
message.append("\nReturning ");
message.append(ret);
Log.v(TAG, message.toString());
return ret;
}
public static double doubleMethod(double x, int n) {
double ret = x*n;
StringBuilder message = new StringBuilder();
message.append("Called doubleMethod with x = ");
message.append(x);
message.append(", n = ");
message.append(n);
message.append("\nReturning ");
message.append(ret);
Log.v(TAG, message.toString());
return ret;
}
public static String stringMethod(final String str, boolean reverse) {
String ret = reverse ? new StringBuilder(str).reverse().toString() : str;
StringBuilder message = new StringBuilder();
message.append("Called stringMethod with str = ");
message.append(str);
message.append(", reverse = ");
message.append(reverse);
message.append("\nReturning ");
message.append(ret);
Log.v(TAG, message.toString());
return ret;
}
}

View File

@ -6,23 +6,14 @@
using namespace std;
using namespace cocos2d;
static std::string ACTIVITY_PATH("org/cocos2dx/lua/AppActivity");
static std::string className = "org/cocos2dx/lua/AppActivity";
void setActivityPathForAndroid(const std::string& path)
{
ACTIVITY_PATH = path;
className = path;
}
string getIPAddress()
{
JniMethodInfo t;
string IPAddress("");
if (JniHelper::getStaticMethodInfo(t, ACTIVITY_PATH.c_str(), "getLocalIpAddress", "()Ljava/lang/String;")) {
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
t.env->DeleteLocalRef(t.classID);
IPAddress = JniHelper::jstring2string(str);
t.env->DeleteLocalRef(str);
}
return IPAddress;
return JniHelper::callStaticStringMethod(className, "getLocalIpAddress");
}