RUPlatform-android.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // Platform-android.cpp
  3. //
  4. // Created by 杜家兑macbook on 17/2/11.
  5. //
  6. #include "RUPlatform.h"
  7. #include "../../cocos2d/cocos/platform/android/jni/JniHelper.h"
  8. static const char* APP_MAIN_CLASS = "org/cocos2dx/cpp/NativeUtils";
  9. #include <android/log.h>
  10. #include <jni.h>
  11. USING_NS_CC;
  12. NS_RU_BEGIN
  13. static Platform *g_Platform = nullptr;
  14. Platform* Platform::getInstance()
  15. {
  16. if (g_Platform == nullptr)
  17. {
  18. g_Platform = new (std::nothrow) Platform;
  19. }
  20. return g_Platform;
  21. }
  22. void Platform::reportLog(string log){
  23. JniMethodInfo t;
  24. if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS
  25. , "Java_reportLog"
  26. , "(Ljava/lang/String;)V"))
  27. {
  28. jstring s1 = t.env->NewStringUTF(log.c_str());
  29. t.env->CallStaticVoidMethod(t.classID, t.methodID,s1);
  30. t.env->DeleteLocalRef(s1);
  31. t.env->DeleteLocalRef(t.classID);
  32. }
  33. }
  34. void Platform::vibrate(int duration, int strength) {
  35. JniMethodInfo t;
  36. if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS
  37. , "vibrate"
  38. , "(II)V"))
  39. {
  40. t.env->CallStaticVoidMethod(t.classID, t.methodID, duration, strength);
  41. t.env->DeleteLocalRef(t.classID);
  42. }
  43. }
  44. bool Platform::saveToSharedPreferences(const std::string& key, const std::string& base64Data) {
  45. JniMethodInfo t;
  46. if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS, "saveToSharedPreferences", "(Ljava/lang/String;Ljava/lang/String;)V")) {
  47. jstring jKey = t.env->NewStringUTF(key.c_str());
  48. jstring jBase64Data = t.env->NewStringUTF(base64Data.c_str());
  49. t.env->CallStaticVoidMethod(t.classID, t.methodID, jKey, jBase64Data);
  50. t.env->DeleteLocalRef(jKey);
  51. t.env->DeleteLocalRef(jBase64Data);
  52. t.env->DeleteLocalRef(t.classID);
  53. return true;
  54. } else {
  55. __android_log_print(ANDROID_LOG_ERROR, "RUPlatform", "Failed to find Java method saveToSharedPreferences");
  56. return false;
  57. }
  58. }
  59. std::string Platform::readFromSharedPreferences(const std::string& key) {
  60. JniMethodInfo t;
  61. if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS, "readFromSharedPreferences", "(Ljava/lang/String;)Ljava/lang/String;")) {
  62. jstring jKey = t.env->NewStringUTF(key.c_str());
  63. jstring jResult = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, jKey);
  64. t.env->DeleteLocalRef(jKey);
  65. t.env->DeleteLocalRef(t.classID);
  66. const char* cResult = t.env->GetStringUTFChars(jResult, 0);
  67. std::string result(cResult);
  68. t.env->ReleaseStringUTFChars(jResult, cResult);
  69. t.env->DeleteLocalRef(jResult);
  70. return result;
  71. } else {
  72. __android_log_print(ANDROID_LOG_ERROR, "RUPlatform", "Failed to find Java method readFromSharedPreferences");
  73. return "";
  74. }
  75. }
  76. double Platform::getSystemUpTime(){
  77. JniMethodInfo t;
  78. if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS, "getSystemUpTime", "()D")) {
  79. double systemUpSec = JniHelper::callStaticDoubleMethod(APP_MAIN_CLASS, "getSystemUpTime");
  80. return systemUpSec;
  81. } else {
  82. __android_log_print(ANDROID_LOG_ERROR, "RUPlatform", "Failed to find Java method getSystemUpTime");
  83. return 0;
  84. }
  85. }
  86. int Platform::getServerTime() {
  87. return JniHelper::callStaticIntMethod(APP_MAIN_CLASS, "getServerTime");
  88. }
  89. void Platform::showDbgView() {
  90. #if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
  91. JniMethodInfo t;
  92. if (JniHelper::getStaticMethodInfo(t, APP_MAIN_CLASS
  93. , "Java_showDbgView"
  94. , "()V"))
  95. {
  96. t.env->CallStaticVoidMethod(t.classID, t.methodID);
  97. t.env->DeleteLocalRef(t.classID);
  98. }
  99. #endif
  100. }
  101. NS_RU_END