LocalStorage-android.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /***************************************************************************
  2. Copyright (c) 2012 Zynga Inc.
  3. Copyright (c) 2013 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologic Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ***************************************************************************/
  22. /*
  23. Local Storage support for the JS Bindings for iOS.
  24. Works on cocos2d-iphone and cocos2d-x.
  25. */
  26. #include "storage/local-storage/LocalStorage.h"
  27. #include "platform/CCPlatformMacros.h"
  28. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <assert.h>
  32. #include "jni.h"
  33. #include "platform/android/jni/JniHelper.h"
  34. USING_NS_CC;
  35. static int _initialized = 0;
  36. static std::string className = "org/cocos2dx/lib/Cocos2dxLocalStorage";
  37. static void splitFilename (std::string& str)
  38. {
  39. size_t found = 0;
  40. found = str.find_last_of("/\\");
  41. if (found != std::string::npos)
  42. {
  43. str = str.substr(found + 1);
  44. }
  45. }
  46. void localStorageInit( const std::string& fullpath)
  47. {
  48. if (fullpath.empty())
  49. return;
  50. if (!_initialized)
  51. {
  52. std::string strDBFilename = fullpath;
  53. splitFilename(strDBFilename);
  54. if (JniHelper::callStaticBooleanMethod(className, "init", strDBFilename, "data")) {
  55. _initialized = 1;
  56. }
  57. }
  58. }
  59. void localStorageFree()
  60. {
  61. if (_initialized) {
  62. JniHelper::callStaticVoidMethod(className, "destroy");
  63. _initialized = 0;
  64. }
  65. }
  66. /** sets an item in the LS */
  67. void localStorageSetItem( const std::string& key, const std::string& value)
  68. {
  69. assert( _initialized );
  70. JniHelper::callStaticVoidMethod(className, "setItem", key, value);
  71. }
  72. /** gets an item from the LS */
  73. bool localStorageGetItem( const std::string& key, std::string *outItem )
  74. {
  75. assert( _initialized );
  76. JniMethodInfo t;
  77. if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "getItem", "(Ljava/lang/String;)Ljava/lang/String;"))
  78. {
  79. jstring jkey = t.env->NewStringUTF(key.c_str());
  80. jstring jret = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, jkey);
  81. if (jret == nullptr)
  82. {
  83. t.env->DeleteLocalRef(jret);
  84. t.env->DeleteLocalRef(jkey);
  85. t.env->DeleteLocalRef(t.classID);
  86. return false;
  87. }
  88. else
  89. {
  90. outItem->assign(JniHelper::jstring2string(jret));
  91. t.env->DeleteLocalRef(jret);
  92. t.env->DeleteLocalRef(jkey);
  93. t.env->DeleteLocalRef(t.classID);
  94. return true;
  95. }
  96. }
  97. else
  98. {
  99. return false;
  100. }
  101. }
  102. /** removes an item from the LS */
  103. void localStorageRemoveItem( const std::string& key )
  104. {
  105. assert( _initialized );
  106. JniHelper::callStaticVoidMethod(className, "removeItem", key);
  107. }
  108. /** removes all items from the LS */
  109. void localStorageClear()
  110. {
  111. assert( _initialized );
  112. JniHelper::callStaticVoidMethod(className, "clear");
  113. }
  114. #endif // #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)