PluginUtils.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /****************************************************************************
  2. Copyright (c) 2012-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 "PluginUtils.h"
  21. #include <android/log.h>
  22. #include <map>
  23. #define MAX_LOG_LEN 256
  24. namespace cocos2d { namespace plugin {
  25. #define JAVAVM cocos2d::PluginJniHelper::getJavaVM()
  26. void PluginUtils::initPluginWrapper(android_app* app)
  27. {
  28. PluginJniMethodInfo t;
  29. if (! PluginJniHelper::getStaticMethodInfo(t
  30. , "org/cocos2dx/plugin/PluginWrapper"
  31. , "initFromNativeActivity"
  32. , "(Landroid/app/Activity;)V"))
  33. {
  34. outputLog("PluginUtils", "Failed to init context of plugin");
  35. return;
  36. }
  37. t.env->CallStaticVoidMethod(t.classID, t.methodID, app->activity->clazz);
  38. t.env->DeleteLocalRef(t.classID);
  39. }
  40. jobject PluginUtils::createJavaMapObject(std::map<std::string, std::string>* paramMap)
  41. {
  42. JNIEnv* env = getEnv();
  43. jclass class_Hashtable = env->FindClass("java/util/Hashtable");
  44. jmethodID construct_method = env->GetMethodID( class_Hashtable, "<init>","()V");
  45. jobject obj_Map = env->NewObject( class_Hashtable, construct_method, "");
  46. if (paramMap != NULL)
  47. {
  48. jmethodID add_method= env->GetMethodID( class_Hashtable,"put","(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
  49. for (std::map<std::string, std::string>::const_iterator it = paramMap->begin(); it != paramMap->end(); ++it)
  50. {
  51. jstring first = env->NewStringUTF(it->first.c_str());
  52. jstring second = env->NewStringUTF(it->second.c_str());
  53. env->CallObjectMethod(obj_Map, add_method, first, second);
  54. env->DeleteLocalRef(first);
  55. env->DeleteLocalRef(second);
  56. }
  57. }
  58. env->DeleteLocalRef(class_Hashtable);
  59. return obj_Map;
  60. }
  61. void PluginUtils::initJavaPlugin(PluginProtocol* pPlugin, jobject jObj, const char* className)
  62. {
  63. cocos2d::plugin::PluginJavaData* pUserData = new cocos2d::plugin::PluginJavaData();
  64. pUserData->jobj = PluginUtils::getEnv()->NewGlobalRef(jObj);
  65. pUserData->jclassName = className;
  66. cocos2d::plugin::PluginUtils::setPluginJavaData(pPlugin, pUserData);
  67. }
  68. JNIEnv* PluginUtils::getEnv()
  69. {
  70. bool bRet = false;
  71. JNIEnv* env = NULL;
  72. do
  73. {
  74. if (JAVAVM->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK)
  75. {
  76. outputLog("PluginUtils", "Failed to get the environment using GetEnv()");
  77. break;
  78. }
  79. if (JAVAVM->AttachCurrentThread(&env, 0) < 0)
  80. {
  81. outputLog("PluginUtils", "Failed to get the environment using AttachCurrentThread()");
  82. break;
  83. }
  84. bRet = true;
  85. } while (0);
  86. if (!bRet) {
  87. env = NULL;
  88. }
  89. return env;
  90. }
  91. std::map<PluginProtocol*, PluginJavaData*> s_PluginObjMap;
  92. std::map<std::string, PluginProtocol*> s_JObjPluginMap;
  93. typedef std::map<PluginProtocol*, PluginJavaData*>::iterator ObjMapIter;
  94. typedef std::map<std::string, PluginProtocol*>::iterator JObjPluginMapIter;
  95. PluginJavaData* PluginUtils::getPluginJavaData(PluginProtocol* pKeyObj)
  96. {
  97. PluginJavaData* ret = NULL;
  98. ObjMapIter it = s_PluginObjMap.find(pKeyObj);
  99. if (it != s_PluginObjMap.end()) {
  100. ret = it->second;
  101. }
  102. return ret;
  103. }
  104. PluginProtocol* PluginUtils::getPluginPtr(std::string className)
  105. {
  106. PluginProtocol* ret = NULL;
  107. JObjPluginMapIter it = s_JObjPluginMap.find(className);
  108. if (it != s_JObjPluginMap.end()) {
  109. ret = it->second;
  110. }
  111. return ret;
  112. }
  113. void PluginUtils::setPluginJavaData(PluginProtocol* pKeyObj, PluginJavaData* pData)
  114. {
  115. erasePluginJavaData(pKeyObj);
  116. s_PluginObjMap.insert(std::pair<PluginProtocol*, PluginJavaData*>(pKeyObj, pData));
  117. s_JObjPluginMap.insert(std::pair<std::string, PluginProtocol*>(pData->jclassName, pKeyObj));
  118. }
  119. void PluginUtils::erasePluginJavaData(PluginProtocol* pKeyObj)
  120. {
  121. ObjMapIter it = s_PluginObjMap.find(pKeyObj);
  122. if (it != s_PluginObjMap.end()) {
  123. PluginJavaData* pData = it->second;
  124. if (pData != NULL)
  125. {
  126. jobject jobj = pData->jobj;
  127. JObjPluginMapIter pluginIt = s_JObjPluginMap.find(pData->jclassName);
  128. if (pluginIt != s_JObjPluginMap.end())
  129. {
  130. s_JObjPluginMap.erase(pluginIt);
  131. }
  132. JNIEnv* pEnv = getEnv();
  133. outputLog("PluginUtils", "Delete global reference.");
  134. pEnv->DeleteGlobalRef(jobj);
  135. delete pData;
  136. }
  137. s_PluginObjMap.erase(it);
  138. }
  139. }
  140. void PluginUtils::outputLog(const char* logTag, const char* pFormat, ...)
  141. {
  142. char buf[MAX_LOG_LEN + 1];
  143. va_list args;
  144. va_start(args, pFormat);
  145. vsnprintf(buf, MAX_LOG_LEN, pFormat, args);
  146. va_end(args);
  147. __android_log_print(ANDROID_LOG_DEBUG, logTag, "%s", buf);
  148. }
  149. jobject PluginUtils::getJObjFromParam(PluginParam* param)
  150. {
  151. if (NULL == param)
  152. {
  153. return NULL;
  154. }
  155. jobject obj = NULL;
  156. PluginJniMethodInfo t;
  157. JNIEnv* env = PluginUtils::getEnv();
  158. switch(param->getCurrentType())
  159. {
  160. case PluginParam::kParamTypeInt:
  161. if (PluginJniHelper::getStaticMethodInfo(t, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;"))
  162. {
  163. obj = t.env->CallStaticObjectMethod(t.classID, t.methodID, param->getIntValue());
  164. t.env->DeleteLocalRef(t.classID);
  165. }
  166. break;
  167. case PluginParam::kParamTypeFloat:
  168. if (PluginJniHelper::getStaticMethodInfo(t, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;"))
  169. {
  170. obj = t.env->CallStaticObjectMethod(t.classID, t.methodID, param->getFloatValue());
  171. t.env->DeleteLocalRef(t.classID);
  172. }
  173. break;
  174. case PluginParam::kParamTypeBool:
  175. if (PluginJniHelper::getStaticMethodInfo(t, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;"))
  176. {
  177. obj = t.env->CallStaticObjectMethod(t.classID, t.methodID, param->getBoolValue());
  178. t.env->DeleteLocalRef(t.classID);
  179. }
  180. break;
  181. case PluginParam::kParamTypeString:
  182. obj = env->NewStringUTF(param->getStringValue());
  183. break;
  184. case PluginParam::kParamTypeStringMap:
  185. {
  186. jclass cls = env->FindClass("org/json/JSONObject");
  187. jmethodID mid = env->GetMethodID(cls,"<init>","()V");
  188. obj = env->NewObject(cls,mid);
  189. env->DeleteLocalRef(cls);
  190. std::map<std::string, std::string>::iterator it;
  191. std::map<std::string, std::string> mapParam = param->getStrMapValue();
  192. for (it = mapParam.begin(); it != mapParam.end(); it++)
  193. {
  194. PluginJniMethodInfo tInfo;
  195. if (PluginJniHelper::getMethodInfo(tInfo, "org/json/JSONObject", "put", "(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;"))
  196. {
  197. jstring strKey = tInfo.env->NewStringUTF(it->first.c_str());
  198. jstring strValue = tInfo.env->NewStringUTF(it->second.c_str());
  199. tInfo.env->CallObjectMethod(obj, tInfo.methodID, strKey, strValue);
  200. tInfo.env->DeleteLocalRef(tInfo.classID);
  201. tInfo.env->DeleteLocalRef(strKey);
  202. tInfo.env->DeleteLocalRef(strValue);
  203. }
  204. }
  205. }
  206. break;
  207. case PluginParam::kParamTypeMap:
  208. {
  209. jclass cls = env->FindClass("org/json/JSONObject");
  210. jmethodID mid = env->GetMethodID(cls,"<init>","()V");
  211. obj = env->NewObject(cls,mid);
  212. env->DeleteLocalRef(cls);
  213. std::map<std::string, PluginParam*>::iterator it;
  214. std::map<std::string, PluginParam*> mapParam = param->getMapValue();
  215. for (it = mapParam.begin(); it != mapParam.end(); it++)
  216. {
  217. PluginJniMethodInfo tInfo;
  218. if (PluginJniHelper::getMethodInfo(tInfo, "org/json/JSONObject", "put", "(Ljava/lang/String;Ljava/lang/Object;)Lorg/json/JSONObject;"))
  219. {
  220. jstring strKey = tInfo.env->NewStringUTF(it->first.c_str());
  221. jobject objValue = PluginUtils::getJObjFromParam(it->second);
  222. tInfo.env->CallObjectMethod(obj, tInfo.methodID, strKey, objValue);
  223. tInfo.env->DeleteLocalRef(tInfo.classID);
  224. tInfo.env->DeleteLocalRef(strKey);
  225. PluginUtils::getEnv()->DeleteLocalRef(objValue);
  226. }
  227. }
  228. }
  229. break;
  230. default:
  231. break;
  232. }
  233. return obj;
  234. }
  235. }}// namespace cocos2d { namespace plugin {