axmol/cocos/base/CCUserDefault-android.cpp

540 lines
13 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "base/CCUserDefault.h"
#include "platform/CCPlatformConfig.h"
2014-07-14 20:45:24 +08:00
#include "base/ccUtils.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCCommon.h"
2014-04-30 08:37:36 +08:00
#include "base/base64.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
// root name of xml
#define USERDEFAULT_ROOT_NAME "userDefaultRoot"
#define KEEP_COMPATABILITY
#define XML_FILE_NAME "UserDefault.xml"
#ifdef KEEP_COMPATABILITY
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "platform/CCFileUtils.h"
#include "tinyxml2.h"
#endif
static const std::string helperClassName = "org/cocos2dx/lib/Cocos2dxHelper";
using namespace std;
NS_CC_BEGIN
/**
* implements of UserDefault
*/
UserDefault* UserDefault::_userDefault = nullptr;
string UserDefault::_filePath = string("");
bool UserDefault::_isFilePathInitialized = false;
#ifdef KEEP_COMPATABILITY
static tinyxml2::XMLElement* getXMLNodeForKey(const char* pKey, tinyxml2::XMLDocument **doc)
{
tinyxml2::XMLElement* curNode = nullptr;
tinyxml2::XMLElement* rootNode = nullptr;
2015-07-06 17:01:17 +08:00
if (! UserDefault::isXMLFileExist())
{
return nullptr;
}
2015-07-06 17:01:17 +08:00
// check the key value
if (! pKey)
{
return nullptr;
}
2015-07-06 17:01:17 +08:00
do
{
tinyxml2::XMLDocument* xmlDoc = new (std::nothrow) tinyxml2::XMLDocument();
*doc = xmlDoc;
2013-12-05 17:19:01 +08:00
ssize_t size;
2015-07-06 17:01:17 +08:00
std::string xmlBuffer = FileUtils::getInstance()->getStringFromFile(UserDefault::getInstance()->getXMLFilePath());
if (xmlBuffer.empty())
{
CCLOG("can not read xml file");
break;
}
xmlDoc->Parse(xmlBuffer.c_str());
// get root node
rootNode = xmlDoc->RootElement();
if (nullptr == rootNode)
{
CCLOG("read root node error");
break;
}
// find the node
curNode = rootNode->FirstChildElement();
if (!curNode)
{
// There is not xml node, delete xml file.
remove(UserDefault::getInstance()->getXMLFilePath().c_str());
2015-07-06 17:01:17 +08:00
return nullptr;
}
2015-07-06 17:01:17 +08:00
while (nullptr != curNode)
{
const char* nodeName = curNode->Value();
if (!strcmp(nodeName, pKey))
{
// delete the node
break;
}
2015-07-06 17:01:17 +08:00
curNode = curNode->NextSiblingElement();
}
} while (0);
2015-07-06 17:01:17 +08:00
return curNode;
}
static void deleteNode(tinyxml2::XMLDocument* doc, tinyxml2::XMLElement* node)
{
if (node)
{
doc->DeleteNode(node);
doc->SaveFile(UserDefault::getInstance()->getXMLFilePath().c_str());
delete doc;
}
}
static void deleteNodeByKey(const char *pKey)
{
tinyxml2::XMLDocument* doc = nullptr;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
deleteNode(doc, node);
}
#endif
UserDefault::~UserDefault()
{
}
UserDefault::UserDefault()
{
}
// FIXME:: deprecated
void UserDefault::purgeSharedUserDefault()
{
UserDefault::destroyInstance();
}
void UserDefault::destroyInstance()
{
CC_SAFE_DELETE(_userDefault);
}
bool UserDefault::getBoolForKey(const char* pKey)
{
return getBoolForKey(pKey, false);
}
bool UserDefault::getBoolForKey(const char* pKey, bool defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = nullptr;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
const char* value = (const char*)node->FirstChild()->Value();
bool ret = (! strcmp(value, "true"));
2015-07-06 17:01:17 +08:00
// set value in NSUserDefaults
setBoolForKey(pKey, ret);
flush();
2015-07-06 17:01:17 +08:00
// delete xmle node
deleteNode(doc, node);
2015-07-06 17:01:17 +08:00
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
return JniHelper::callStaticBooleanMethod(helperClassName, "getBoolForKey", pKey, defaultValue);
}
int UserDefault::getIntegerForKey(const char* pKey)
{
return getIntegerForKey(pKey, 0);
}
int UserDefault::getIntegerForKey(const char* pKey, int defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = nullptr;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
int ret = atoi((const char*)node->FirstChild()->Value());
2015-07-06 17:01:17 +08:00
// set value in NSUserDefaults
setIntegerForKey(pKey, ret);
flush();
2015-07-06 17:01:17 +08:00
// delete xmle node
deleteNode(doc, node);
2015-07-06 17:01:17 +08:00
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
2015-07-06 17:01:17 +08:00
return JniHelper::callStaticIntMethod(helperClassName, "getIntegerForKey", pKey, defaultValue);
}
float UserDefault::getFloatForKey(const char* pKey)
{
return getFloatForKey(pKey, 0.0f);
}
float UserDefault::getFloatForKey(const char* pKey, float defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = nullptr;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
2014-07-14 20:45:24 +08:00
float ret = utils::atof((const char*)node->FirstChild()->Value());
2015-07-06 17:01:17 +08:00
// set value in NSUserDefaults
setFloatForKey(pKey, ret);
flush();
2015-07-06 17:01:17 +08:00
// delete xmle node
deleteNode(doc, node);
2015-07-06 17:01:17 +08:00
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
return JniHelper::callStaticFloatMethod(helperClassName, "getFloatForKey", pKey, defaultValue);
}
double UserDefault::getDoubleForKey(const char* pKey)
{
return getDoubleForKey(pKey, 0.0);
}
double UserDefault::getDoubleForKey(const char* pKey, double defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = nullptr;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
2014-07-14 20:45:24 +08:00
double ret = utils::atof((const char*)node->FirstChild()->Value());
2015-07-06 17:01:17 +08:00
// set value in NSUserDefaults
setDoubleForKey(pKey, ret);
flush();
2015-07-06 17:01:17 +08:00
// delete xmle node
deleteNode(doc, node);
2015-07-06 17:01:17 +08:00
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
return JniHelper::callStaticDoubleMethod(helperClassName, "getDoubleForKey", pKey, defaultValue);
}
std::string UserDefault::getStringForKey(const char* pKey)
{
return getStringForKey(pKey, "");
}
string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = nullptr;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
string ret = (const char*)node->FirstChild()->Value();
2015-07-06 17:01:17 +08:00
// set value in NSUserDefaults
setStringForKey(pKey, ret);
flush();
2015-07-06 17:01:17 +08:00
// delete xmle node
deleteNode(doc, node);
2015-07-06 17:01:17 +08:00
return ret;
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
return JniHelper::callStaticStringMethod(helperClassName, "getStringForKey", pKey, defaultValue);
}
Data UserDefault::getDataForKey(const char* pKey)
{
return getDataForKey(pKey, Data::Null);
}
Data UserDefault::getDataForKey(const char* pKey, const Data& defaultValue)
{
#ifdef KEEP_COMPATABILITY
tinyxml2::XMLDocument* doc = nullptr;
tinyxml2::XMLElement* node = getXMLNodeForKey(pKey, &doc);
if (node)
{
if (node->FirstChild())
{
const char * encodedData = node->FirstChild()->Value();
2015-07-06 17:01:17 +08:00
unsigned char * decodedData;
int decodedDataLen = base64Decode((unsigned char*)encodedData, (unsigned int)strlen(encodedData), &decodedData);
2015-07-06 17:01:17 +08:00
if (decodedData) {
Data ret;
ret.fastSet(decodedData, decodedDataLen);
2015-07-06 17:01:17 +08:00
// set value in NSUserDefaults
setDataForKey(pKey, ret);
2015-07-06 17:01:17 +08:00
flush();
2015-07-06 17:01:17 +08:00
// delete xmle node
deleteNode(doc, node);
2015-07-06 17:01:17 +08:00
return ret;
}
}
else
{
// delete xmle node
deleteNode(doc, node);
}
}
#endif
2015-07-06 17:01:17 +08:00
char * encodedDefaultData = NULL;
2013-12-20 21:51:53 +08:00
unsigned int encodedDefaultDataLen = !defaultValue.isNull() ? base64Encode(defaultValue.getBytes(), defaultValue.getSize(), &encodedDefaultData) : 0;
2015-07-06 17:01:17 +08:00
string encodedStr = JniHelper::callStaticStringMethod(helperClassName, "getStringForKey", pKey, (const char*)encodedDefaultData);
if (encodedDefaultData)
free(encodedDefaultData);
CCLOG("ENCODED STRING: --%s--%d", encodedStr.c_str(), (int)encodedStr.length());
2015-07-06 17:01:17 +08:00
unsigned char * decodedData = NULL;
int decodedDataLen = base64Decode((unsigned char*)encodedStr.c_str(), (unsigned int)encodedStr.length(), &decodedData);
CCLOG("DECODED DATA: %s %d", decodedData, decodedDataLen);
2015-07-06 17:01:17 +08:00
if (decodedData && decodedDataLen) {
Data ret;
ret.fastSet(decodedData, decodedDataLen);
return ret;
}
return defaultValue;
}
void UserDefault::setBoolForKey(const char* pKey, bool value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
JniHelper::callStaticVoidMethod(helperClassName, "setBoolForKey", pKey, value);
}
void UserDefault::setIntegerForKey(const char* pKey, int value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
JniHelper::callStaticVoidMethod(helperClassName, "setIntegerForKey", pKey, value);
}
void UserDefault::setFloatForKey(const char* pKey, float value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
JniHelper::callStaticVoidMethod(helperClassName, "setFloatForKey", pKey, value);
}
void UserDefault::setDoubleForKey(const char* pKey, double value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
JniHelper::callStaticVoidMethod(helperClassName, "setDoubleForKey", pKey, value);
}
void UserDefault::setStringForKey(const char* pKey, const std::string& value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
JniHelper::callStaticVoidMethod(helperClassName, "setStringForKey", pKey, value);
}
void UserDefault::setDataForKey(const char* pKey, const Data& value)
{
#ifdef KEEP_COMPATABILITY
deleteNodeByKey(pKey);
#endif
2015-07-06 17:01:17 +08:00
2014-07-11 17:39:56 +08:00
CCLOG("SET DATA FOR KEY: --%s--%d", value.getBytes(), (int)(value.getSize()));
char * encodedData = nullptr;
unsigned int encodedDataLen = base64Encode(value.getBytes(), value.getSize(), &encodedData);
CCLOG("SET DATA ENCODED: --%s", encodedData);
2015-07-06 17:01:17 +08:00
JniHelper::callStaticVoidMethod(helperClassName, "setStringForKey", pKey, (const char*)encodedData);
2015-07-06 17:01:17 +08:00
if (encodedData)
free(encodedData);
}
// FIXME:: deprecated
UserDefault* UserDefault::sharedUserDefault()
{
return UserDefault::getInstance();
}
UserDefault* UserDefault::getInstance()
2015-07-06 17:01:17 +08:00
{
if (! _userDefault)
{
#ifdef KEEP_COMPATABILITY
initXMLFilePath();
#endif
_userDefault = new (std::nothrow) UserDefault();
}
return _userDefault;
}
bool UserDefault::isXMLFileExist()
{
return FileUtils::getInstance()->isFileExist(_filePath);
}
void UserDefault::initXMLFilePath()
{
#ifdef KEEP_COMPATABILITY
if (! _isFilePathInitialized)
{
// UserDefault.xml is stored in /data/data/<package-path>/ before v2.1.2
std::string packageName = JniHelper::callStaticStringMethod(helperClassName, "getCocos2dxPackageName");
_filePath += "/data/data/" + packageName + "/" + XML_FILE_NAME;
_isFilePathInitialized = true;
}
#endif
}
// create new xml file
bool UserDefault::createXMLFile()
{
return false;
}
const string& UserDefault::getXMLFilePath()
{
return _filePath;
}
void UserDefault::flush()
{
}
2015-07-06 17:01:17 +08:00
void UserDefault::deleteValueForKey(const char* key)
{
// check the params
if (!key)
{
CCLOG("the key is invalid");
}
JniHelper::callStaticVoidMethod(helperClassName, "deleteValueForKey", key);
2015-07-06 17:01:17 +08:00
flush();
}
NS_CC_END
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)