1
0

CCUserDefault-android.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "base/CCUserDefault.h"
  22. #include "platform/CCPlatformConfig.h"
  23. #include "base/ccUtils.h"
  24. #include "platform/CCCommon.h"
  25. #include "base/base64.h"
  26. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  27. #include "platform/android/jni/JniHelper.h"
  28. // root name of xml
  29. #define USERDEFAULT_ROOT_NAME "userDefaultRoot"
  30. #define KEEP_COMPATABILITY
  31. #define XML_FILE_NAME "UserDefault.xml"
  32. #ifdef KEEP_COMPATABILITY
  33. #include "platform/CCFileUtils.h"
  34. #include "tinyxml2.h"
  35. #endif
  36. static const std::string helperClassName = "org/cocos2dx/lib/Cocos2dxHelper";
  37. using namespace std;
  38. NS_CC_BEGIN
  39. /**
  40. * implements of UserDefault
  41. */
  42. UserDefault* UserDefault::_userDefault = nullptr;
  43. string UserDefault::_filePath = string("");
  44. bool UserDefault::_isFilePathInitialized = false;
  45. #ifdef KEEP_COMPATABILITY
  46. static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDocument **doc)
  47. {
  48. tinyxml2::XMLElement* curNode = nullptr;
  49. tinyxml2::XMLElement* rootNode = nullptr;
  50. if (! UserDefault::isXMLFileExist())
  51. {
  52. return nullptr;
  53. }
  54. // check the key value
  55. if (! pKey)
  56. {
  57. return nullptr;
  58. }
  59. do
  60. {
  61. tinyxml2::XMLDocument* xmlDoc = new (std::nothrow) tinyxml2::XMLDocument();
  62. *doc = xmlDoc;
  63. ssize_t size;
  64. std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
  65. if (xmlBuffer.empty())
  66. {
  67. CCLOG("can not read xml file");
  68. break;
  69. }
  70. xmlDoc->Parse(xmlBuffer.c_str());
  71. // get root node
  72. rootNode = xmlDoc->RootElement();
  73. if (nullptr == rootNode)
  74. {
  75. CCLOG("read root node error");
  76. break;
  77. }
  78. // find the node
  79. curNode = rootNode->FirstChildElement();
  80. if (!curNode)
  81. {
  82. // There is not xml node, delete xml file.
  83. remove(UserDefault::getInstance()->getXMLFilePath().c_str());
  84. return nullptr;
  85. }
  86. while (nullptr != curNode)
  87. {
  88. const char* nodeName = curNode->Value();
  89. if (!strcmp(nodeName, pKey))
  90. {
  91. // delete the node
  92. break;
  93. }
  94. curNode = curNode->NextSiblingElement();
  95. }
  96. } while (0);
  97. return curNode;
  98. }
  99. static void deleteNode(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* node)
  100. {
  101. if (node)
  102. {
  103. doc->DeleteNode(node);
  104. doc->SaveFile(UserDefault::getInstance()->getXMLFilePath().c_str());
  105. delete doc;
  106. }
  107. }
  108. static void deleteNodeByKey(const char *pKey)
  109. {
  110. tinyxml2::XMLDocument* doc = nullptr;
  111. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  112. deleteNode(doc, node);
  113. }
  114. #endif
  115. UserDefault::~UserDefault()
  116. {
  117. }
  118. UserDefault::UserDefault()
  119. {
  120. }
  121. // FIXME:: deprecated
  122. void UserDefault::purgeSharedUserDefault()
  123. {
  124. UserDefault::destroyInstance();
  125. }
  126. void UserDefault::destroyInstance()
  127. {
  128. CC_SAFE_DELETE(_userDefault);
  129. }
  130. bool UserDefault::getBoolForKey(const char* pKey)
  131. {
  132. return getBoolForKey(pKey, false);
  133. }
  134. bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
  135. {
  136. #ifdef KEEP_COMPATABILITY
  137. tinyxml2::XMLDocument* doc = nullptr;
  138. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  139. if (node)
  140. {
  141. if (node->FirstChild())
  142. {
  143. const char* value = (const char*)node->FirstChild()->Value();
  144. bool ret = (! strcmp(value, "true"));
  145. // set value in NSUserDefaults
  146. setBoolForKey(pKey, ret);
  147. flush();
  148. // delete xmle node
  149. deleteNode(doc, node);
  150. return ret;
  151. }
  152. else
  153. {
  154. // delete xmle node
  155. deleteNode(doc, node);
  156. }
  157. }
  158. #endif
  159. return JniHelper::callStaticBooleanMethod(helperClassName, "getBoolForKey", pKey, defaultValue);
  160. }
  161. int UserDefault::getIntegerForKey(const char* pKey)
  162. {
  163. return getIntegerForKey(pKey, 0);
  164. }
  165. int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
  166. {
  167. #ifdef KEEP_COMPATABILITY
  168. tinyxml2::XMLDocument* doc = nullptr;
  169. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  170. if (node)
  171. {
  172. if (node->FirstChild())
  173. {
  174. int ret = atoi((const char*)node->FirstChild()->Value());
  175. // set value in NSUserDefaults
  176. setIntegerForKey(pKey, ret);
  177. flush();
  178. // delete xmle node
  179. deleteNode(doc, node);
  180. return ret;
  181. }
  182. else
  183. {
  184. // delete xmle node
  185. deleteNode(doc, node);
  186. }
  187. }
  188. #endif
  189. return JniHelper::callStaticIntMethod(helperClassName, "getIntegerForKey", pKey, defaultValue);
  190. }
  191. float UserDefault::getFloatForKey(const char* pKey)
  192. {
  193. return getFloatForKey(pKey, 0.0f);
  194. }
  195. float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
  196. {
  197. #ifdef KEEP_COMPATABILITY
  198. tinyxml2::XMLDocument* doc = nullptr;
  199. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  200. if (node)
  201. {
  202. if (node->FirstChild())
  203. {
  204. float ret = utils::atof((const char*)node->FirstChild()->Value());
  205. // set value in NSUserDefaults
  206. setFloatForKey(pKey, ret);
  207. flush();
  208. // delete xmle node
  209. deleteNode(doc, node);
  210. return ret;
  211. }
  212. else
  213. {
  214. // delete xmle node
  215. deleteNode(doc, node);
  216. }
  217. }
  218. #endif
  219. return JniHelper::callStaticFloatMethod(helperClassName, "getFloatForKey", pKey, defaultValue);
  220. }
  221. double UserDefault::getDoubleForKey(const char* pKey)
  222. {
  223. return getDoubleForKey(pKey, 0.0);
  224. }
  225. double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
  226. {
  227. #ifdef KEEP_COMPATABILITY
  228. tinyxml2::XMLDocument* doc = nullptr;
  229. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  230. if (node)
  231. {
  232. if (node->FirstChild())
  233. {
  234. double ret = utils::atof((const char*)node->FirstChild()->Value());
  235. // set value in NSUserDefaults
  236. setDoubleForKey(pKey, ret);
  237. flush();
  238. // delete xmle node
  239. deleteNode(doc, node);
  240. return ret;
  241. }
  242. else
  243. {
  244. // delete xmle node
  245. deleteNode(doc, node);
  246. }
  247. }
  248. #endif
  249. return JniHelper::callStaticDoubleMethod(helperClassName, "getDoubleForKey", pKey, defaultValue);
  250. }
  251. std::string UserDefault::getStringForKey(const char* pKey)
  252. {
  253. return getStringForKey(pKey, "");
  254. }
  255. string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
  256. {
  257. #ifdef KEEP_COMPATABILITY
  258. tinyxml2::XMLDocument* doc = nullptr;
  259. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  260. if (node)
  261. {
  262. if (node->FirstChild())
  263. {
  264. string ret = (const char*)node->FirstChild()->Value();
  265. // set value in NSUserDefaults
  266. setStringForKey(pKey, ret);
  267. flush();
  268. // delete xmle node
  269. deleteNode(doc, node);
  270. return ret;
  271. }
  272. else
  273. {
  274. // delete xmle node
  275. deleteNode(doc, node);
  276. }
  277. }
  278. #endif
  279. return JniHelper::callStaticStringMethod(helperClassName, "getStringForKey", pKey, defaultValue);
  280. }
  281. Data UserDefault::getDataForKey(const char* pKey)
  282. {
  283. return getDataForKey(pKey, Data::Null);
  284. }
  285. Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
  286. {
  287. #ifdef KEEP_COMPATABILITY
  288. tinyxml2::XMLDocument* doc = nullptr;
  289. tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
  290. if (node)
  291. {
  292. if (node->FirstChild())
  293. {
  294. const char * encodedData = node->FirstChild()->Value();
  295. unsigned char * decodedData;
  296. int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
  297. if (decodedData) {
  298. Data ret;
  299. ret.fastSet(decodedData, decodedDataLen);
  300. // set value in NSUserDefaults
  301. setDataForKey(pKey, ret);
  302. flush();
  303. // delete xmle node
  304. deleteNode(doc, node);
  305. return ret;
  306. }
  307. }
  308. else
  309. {
  310. // delete xmle node
  311. deleteNode(doc, node);
  312. }
  313. }
  314. #endif
  315. char * encodedDefaultData = NULL;
  316. unsigned int encodedDefaultDataLen = !defaultValue.isNull() ? base64Encode(defaultValue.getBytes(), defaultValue.getSize(), &encodedDefaultData) : 0;
  317. string encodedStr = JniHelper::callStaticStringMethod(helperClassName, "getStringForKey", pKey, (const char*)encodedDefaultData);
  318. if (encodedDefaultData)
  319. free(encodedDefaultData);
  320. CCLOG("ENCODED STRING: --%s--%d", encodedStr.c_str(), encodedStr.length());
  321. unsigned char * decodedData = NULL;
  322. int decodedDataLen = base64Decode((unsigned char*)encodedStr.c_str(), (unsigned int)encodedStr.length(), &decodedData);
  323. CCLOG("DECODED DATA: %s %d", decodedData, decodedDataLen);
  324. if (decodedData && decodedDataLen) {
  325. Data ret;
  326. ret.fastSet(decodedData, decodedDataLen);
  327. return ret;
  328. }
  329. return defaultValue;
  330. }
  331. void UserDefault::setBoolForKey(const char* pKey, bool value)
  332. {
  333. #ifdef KEEP_COMPATABILITY
  334. deleteNodeByKey(pKey);
  335. #endif
  336. JniHelper::callStaticVoidMethod(helperClassName, "setBoolForKey", pKey, value);
  337. }
  338. void UserDefault::setIntegerForKey(const char* pKey, int value)
  339. {
  340. #ifdef KEEP_COMPATABILITY
  341. deleteNodeByKey(pKey);
  342. #endif
  343. JniHelper::callStaticVoidMethod(helperClassName, "setIntegerForKey", pKey, value);
  344. }
  345. void UserDefault::setFloatForKey(const char* pKey, float value)
  346. {
  347. #ifdef KEEP_COMPATABILITY
  348. deleteNodeByKey(pKey);
  349. #endif
  350. JniHelper::callStaticVoidMethod(helperClassName, "setFloatForKey", pKey, value);
  351. }
  352. void UserDefault::setDoubleForKey(const char* pKey, double value)
  353. {
  354. #ifdef KEEP_COMPATABILITY
  355. deleteNodeByKey(pKey);
  356. #endif
  357. JniHelper::callStaticVoidMethod(helperClassName, "setDoubleForKey", pKey, value);
  358. }
  359. void UserDefault::setStringForKey(const char* pKey, const std::string& value)
  360. {
  361. #ifdef KEEP_COMPATABILITY
  362. deleteNodeByKey(pKey);
  363. #endif
  364. JniHelper::callStaticVoidMethod(helperClassName, "setStringForKey", pKey, value);
  365. }
  366. void UserDefault::setDataForKey(const char* pKey, const Data& value)
  367. {
  368. #ifdef KEEP_COMPATABILITY
  369. deleteNodeByKey(pKey);
  370. #endif
  371. CCLOG("SET DATA FOR KEY: --%s--%d", value.getBytes(), (int)(value.getSize()));
  372. char * encodedData = nullptr;
  373. unsigned int encodedDataLen = base64Encode(value.getBytes(), value.getSize(), &encodedData);
  374. CCLOG("SET DATA ENCODED: --%s", encodedData);
  375. JniHelper::callStaticVoidMethod(helperClassName, "setStringForKey", pKey, (const char*)encodedData);
  376. if (encodedData)
  377. free(encodedData);
  378. }
  379. // FIXME:: deprecated
  380. UserDefault* UserDefault::sharedUserDefault()
  381. {
  382. return UserDefault::getInstance();
  383. }
  384. UserDefault* UserDefault::getInstance()
  385. {
  386. if (! _userDefault)
  387. {
  388. #ifdef KEEP_COMPATABILITY
  389. initXMLFilePath();
  390. #endif
  391. _userDefault = new (std::nothrow) UserDefault();
  392. }
  393. return _userDefault;
  394. }
  395. bool UserDefault::isXMLFileExist()
  396. {
  397. return FileUtils::getInstance()->isFileExist(_filePath);
  398. }
  399. void UserDefault::initXMLFilePath()
  400. {
  401. #ifdef KEEP_COMPATABILITY
  402. if (! _isFilePathInitialized)
  403. {
  404. // UserDefault.xml is stored in /data/data/<package-path>/ before v2.1.2
  405. std::string packageName = JniHelper::callStaticStringMethod(helperClassName, "getCocos2dxPackageName");
  406. _filePath += "/data/data/" + packageName + "/" + XML_FILE_NAME;
  407. _isFilePathInitialized = true;
  408. }
  409. #endif
  410. }
  411. // create new xml file
  412. bool UserDefault::createXMLFile()
  413. {
  414. return false;
  415. }
  416. const string& UserDefault::getXMLFilePath()
  417. {
  418. return _filePath;
  419. }
  420. void UserDefault::flush()
  421. {
  422. }
  423. void UserDefault::deleteValueForKey(const char* key)
  424. {
  425. // check the params
  426. if (!key)
  427. {
  428. CCLOG("the key is invalid");
  429. }
  430. JniHelper::callStaticVoidMethod(helperClassName, "deleteValueForKey", key);
  431. flush();
  432. }
  433. NS_CC_END
  434. #endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)