// // Platform-android.cpp // // Created by 杜家兑macbook on 17/2/11. // #include "RUPlatform.h" #include "../../cocos2d/cocos/platform/android/jni/JniHelper.h" static const char* APP_MAIN_CLASS = "org/cocos2dx/cpp/NativeUtils"; #include #include USING_NS_CC; NS_RU_BEGIN static Platform *g_Platform = nullptr; Platform* Platform::getInstance() { if (g_Platform == nullptr) { g_Platform = new (std::nothrow) Platform; } return g_Platform; } void Platform::reportLog(string log){ JniMethodInfo t; if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS , "Java_reportLog" , "(Ljava/lang/String;)V")) { jstring s1 = t.env->NewStringUTF(log.c_str()); t.env->CallStaticVoidMethod(t.classID, t.methodID,s1); t.env->DeleteLocalRef(s1); t.env->DeleteLocalRef(t.classID); } } void Platform::vibrate(int duration, int strength) { JniMethodInfo t; if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS , "vibrate" , "(II)V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID, duration, strength); t.env->DeleteLocalRef(t.classID); } } bool Platform::saveToSharedPreferences(const std::string& key, const std::string& base64Data) { JniMethodInfo t; if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS, "saveToSharedPreferences", "(Ljava/lang/String;Ljava/lang/String;)V")) { jstring jKey = t.env->NewStringUTF(key.c_str()); jstring jBase64Data = t.env->NewStringUTF(base64Data.c_str()); t.env->CallStaticVoidMethod(t.classID, t.methodID, jKey, jBase64Data); t.env->DeleteLocalRef(jKey); t.env->DeleteLocalRef(jBase64Data); t.env->DeleteLocalRef(t.classID); return true; } else { __android_log_print(ANDROID_LOG_ERROR, "RUPlatform", "Failed to find Java method saveToSharedPreferences"); return false; } } std::string Platform::readFromSharedPreferences(const std::string& key) { JniMethodInfo t; if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS, "readFromSharedPreferences", "(Ljava/lang/String;)Ljava/lang/String;")) { jstring jKey = t.env->NewStringUTF(key.c_str()); jstring jResult = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, jKey); t.env->DeleteLocalRef(jKey); t.env->DeleteLocalRef(t.classID); const char* cResult = t.env->GetStringUTFChars(jResult, 0); std::string result(cResult); t.env->ReleaseStringUTFChars(jResult, cResult); t.env->DeleteLocalRef(jResult); return result; } else { __android_log_print(ANDROID_LOG_ERROR, "RUPlatform", "Failed to find Java method readFromSharedPreferences"); return ""; } } double Platform::getSystemUpTime(){ JniMethodInfo t; if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS, "getSystemUpTime", "()D")) { double systemUpSec = JniHelper::callStaticDoubleMethod(APP_MAIN_CLASS, "getSystemUpTime"); return systemUpSec; } else { __android_log_print(ANDROID_LOG_ERROR, "RUPlatform", "Failed to find Java method getSystemUpTime"); return 0; } } int Platform::getServerTime() { return JniHelper::callStaticIntMethod(APP_MAIN_CLASS, "getServerTime"); } void Platform::showDbgView() { #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID JniMethodInfo t; if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS , "Java_showDbgView" , "()V")) { t.env->CallStaticVoidMethod(t.classID, t.methodID); t.env->DeleteLocalRef(t.classID); } #endif } NS_RU_END