ProtocolSocial.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "ProtocolSocial.h"
  21. #include "PluginJniHelper.h"
  22. #include <android/log.h>
  23. #include "PluginUtils.h"
  24. #include "PluginJavaData.h"
  25. namespace cocos2d { namespace plugin {
  26. extern "C" {
  27. JNIEXPORT void JNICALL Java_org_cocos2dx_plugin_SocialWrapper_nativeOnSocialResult(JNIEnv* env, jobject thiz, jstring className, jint ret, jstring msg)
  28. {
  29. std::string strMsg = PluginJniHelper::jstring2string(msg);
  30. std::string strClassName = PluginJniHelper::jstring2string(className);
  31. PluginProtocol* pPlugin = PluginUtils::getPluginPtr(strClassName);
  32. PluginUtils::outputLog("ProtocolSocial", "nativeOnSocialResult(), Get plugin ptr : %p", pPlugin);
  33. if (pPlugin != NULL)
  34. {
  35. PluginUtils::outputLog("ProtocolSocial", "nativeOnSocialResult(), Get plugin name : %s", pPlugin->getPluginName());
  36. ProtocolSocial* pSocial = dynamic_cast<ProtocolSocial*>(pPlugin);
  37. if (pSocial != NULL)
  38. {
  39. SocialListener* pListener = pSocial->getListener();
  40. if (NULL != pListener)
  41. {
  42. pListener->onSocialResult((SocialRetCode) ret, strMsg.c_str());
  43. }
  44. else
  45. {
  46. ProtocolSocial::ProtocolSocialCallback callback = pSocial->getCallback();
  47. if (callback)
  48. {
  49. callback(ret, strMsg);
  50. }
  51. }
  52. }
  53. }
  54. }
  55. }
  56. ProtocolSocial::ProtocolSocial()
  57. : _listener(NULL)
  58. {
  59. }
  60. ProtocolSocial::~ProtocolSocial()
  61. {
  62. }
  63. void ProtocolSocial::configDeveloperInfo(TSocialDeveloperInfo devInfo)
  64. {
  65. if (devInfo.empty())
  66. {
  67. PluginUtils::outputLog("ProtocolSocial", "The developer info is empty!");
  68. return;
  69. }
  70. else
  71. {
  72. PluginJavaData* pData = PluginUtils::getPluginJavaData(this);
  73. PluginJniMethodInfo t;
  74. if (PluginJniHelper::getMethodInfo(t
  75. , pData->jclassName.c_str()
  76. , "configDeveloperInfo"
  77. , "(Ljava/util/Hashtable;)V"))
  78. {
  79. // generate the hashtable from map
  80. jobject obj_Map = PluginUtils::createJavaMapObject(&devInfo);
  81. // invoke java method
  82. t.env->CallVoidMethod(pData->jobj, t.methodID, obj_Map);
  83. t.env->DeleteLocalRef(obj_Map);
  84. t.env->DeleteLocalRef(t.classID);
  85. }
  86. }
  87. }
  88. void ProtocolSocial::submitScore(const char* leadboardID, long score)
  89. {
  90. PluginJavaData* pData = PluginUtils::getPluginJavaData(this);
  91. PluginJniMethodInfo t;
  92. if (PluginJniHelper::getMethodInfo(t
  93. , pData->jclassName.c_str()
  94. , "submitScore"
  95. , "(Ljava/lang/String;J)V"))
  96. {
  97. jstring strID = PluginUtils::getEnv()->NewStringUTF(leadboardID);
  98. // invoke java method
  99. t.env->CallVoidMethod(pData->jobj, t.methodID, strID, score);
  100. t.env->DeleteLocalRef(strID);
  101. t.env->DeleteLocalRef(t.classID);
  102. }
  103. }
  104. void ProtocolSocial::submitScore(const char* leadboardID, long score, ProtocolSocialCallback cb)
  105. {
  106. _callback = cb;
  107. submitScore(leadboardID, score);
  108. }
  109. void ProtocolSocial::showLeaderboard(const char* leaderboardID)
  110. {
  111. PluginJavaData* pData = PluginUtils::getPluginJavaData(this);
  112. PluginJniMethodInfo t;
  113. if (PluginJniHelper::getMethodInfo(t
  114. , pData->jclassName.c_str()
  115. , "showLeaderboard"
  116. , "(Ljava/lang/String;)V"))
  117. {
  118. jstring strID = PluginUtils::getEnv()->NewStringUTF(leaderboardID);
  119. // invoke java method
  120. t.env->CallVoidMethod(pData->jobj, t.methodID, strID);
  121. t.env->DeleteLocalRef(strID);
  122. t.env->DeleteLocalRef(t.classID);
  123. }
  124. }
  125. void ProtocolSocial::unlockAchievement(TAchievementInfo achInfo)
  126. {
  127. if (achInfo.empty())
  128. {
  129. PluginUtils::outputLog("ProtocolSocial", "The achievement info is empty!");
  130. return;
  131. }
  132. else
  133. {
  134. PluginJavaData* pData = PluginUtils::getPluginJavaData(this);
  135. PluginJniMethodInfo t;
  136. if (PluginJniHelper::getMethodInfo(t
  137. , pData->jclassName.c_str()
  138. , "unlockAchievement"
  139. , "(Ljava/util/Hashtable;)V"))
  140. {
  141. // generate the hashtable from map
  142. jobject obj_Map = PluginUtils::createJavaMapObject(&achInfo);
  143. // invoke java method
  144. t.env->CallVoidMethod(pData->jobj, t.methodID, obj_Map);
  145. t.env->DeleteLocalRef(obj_Map);
  146. t.env->DeleteLocalRef(t.classID);
  147. }
  148. }
  149. }
  150. void ProtocolSocial::unlockAchievement(TAchievementInfo achInfo, ProtocolSocialCallback cb)
  151. {
  152. _callback = cb;
  153. unlockAchievement(achInfo);
  154. }
  155. void ProtocolSocial::showAchievements()
  156. {
  157. PluginUtils::callJavaFunctionWithName(this, "showAchievements");
  158. }
  159. }} // namespace cocos2d { namespace plugin {