issue #1529:use NSUserDefaults on iOS and SharedPreferences on Android to implement CCUserDefault

This commit is contained in:
minggo 2013-03-06 16:36:44 +08:00
parent a9fa61af34
commit 648e9a7bf1
9 changed files with 415 additions and 8 deletions

View File

@ -111,7 +111,7 @@ support/CCNotificationCenter.cpp \
support/CCProfiling.cpp \
support/CCPointExtension.cpp \
support/TransformUtils.cpp \
support/CCUserDefault.cpp \
support/user_default/CCUserDefault.cpp \
support/base64.cpp \
support/ccUtils.cpp \
support/CCVertex.cpp \

View File

@ -50,7 +50,7 @@ THE SOFTWARE.
#include "CCAccelerometer.h"
#include "sprite_nodes/CCAnimationCache.h"
#include "touch_dispatcher/CCTouch.h"
#include "support/CCUserDefault/CCUserDefault.h"
#include "support/user_default/CCUserDefault.h"
#include "shaders/ccGLStateCache.h"
#include "shaders/CCShaderCache.h"
#include "kazmath/kazmath.h"

View File

@ -221,7 +221,7 @@ THE SOFTWARE.
#include "support/CCNotificationCenter.h"
#include "support/CCPointExtension.h"
#include "support/CCProfiling.h"
#include "support/CCUserDefault/CCUserDefault.h"
#include "support/user_default/CCUserDefault.h"
#include "support/CCVertex.h"
#include "support/tinyxml2/tinyxml2.h"

View File

@ -1,5 +1,5 @@
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2010-2013 cocos2d-x.org
http://www.cocos2d-x.org
@ -23,16 +23,15 @@ THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.lib;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
@ -41,6 +40,7 @@ public class Cocos2dxHelper {
// ===========================================================
// Constants
// ===========================================================
private static final String PREFS_NAME = "Cocos2dxPrefsFile";
// ===========================================================
// Fields
@ -271,6 +271,67 @@ public class Cocos2dxHelper {
return -1;
}
// ===========================================================
// Functions for CCUserDefault
// ===========================================================
public static boolean getBoolForKey(String key) {
SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
return settings.getBoolean(key, false);
}
public static int getIntegerForKey(String key) {
SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
return settings.getInt(key, 0);
}
public static float getFloatForKey(String key) {
SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
return settings.getFloat(key, 0);
}
public static double getDoubleForKey(String key) {
// SharedPreferences doesn't support saving float value
SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
return settings.getFloat(key, 0);
}
public static String getStringForKey(String key) {
SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
return settings.getString(key, "");
}
public static void setBoolForKey(String key, boolean value) {
SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(key, value);
}
public static void setIntegerForKey(String key, int value) {
SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt(key, value);
}
public static void setFloatForKey(String key, float value) {
SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putFloat(key, value);
}
public static void setDoubleForKey(String key, double value) {
// SharedPreferences doesn't support recording double value
SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putFloat(key, (float)value);
}
public static void setStringForKey(String key, String value) {
SharedPreferences settings = ((Activity)sContext).getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================

View File

@ -183,4 +183,160 @@ extern "C" {
t.env->DeleteLocalRef(t.classID);
}
}
// functions for CCUserDefault
bool getBoolForKeyJNI(const char* pKey)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getBoolForKey", "(Ljava/lang/String;)Z")) {
jstring stringArg = t.env->NewStringUTF(pKey);
jboolean ret = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
return ret;
}
return false;
}
int getIntegerForKeyJNI(const char* pKey)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getIntegerForKey", "(Ljava/lang/String;)I")) {
jstring stringArg = t.env->NewStringUTF(pKey);
jint ret = t.env->CallStaticIntMethod(t.classID, t.methodID, stringArg);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
return ret;
}
return 0;
}
float getFloatForKeyJNI(const char* pKey)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getFloatForKey", "(Ljava/lang/String;)F")) {
jstring stringArg = t.env->NewStringUTF(pKey);
jfloat ret = t.env->CallStaticFloatMethod(t.classID, t.methodID, stringArg);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
return ret;
}
return 0;
}
double getDoubleForKeyJNI(const char* pKey)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getDoubleForKey", "(Ljava/lang/String;)D")) {
jstring stringArg = t.env->NewStringUTF(pKey);
jdouble ret = t.env->CallStaticDoubleMethod(t.classID, t.methodID, stringArg);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
return ret;
}
return 0;
}
const char* getStringForKeyJNI(const char* pKey)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "getStringForKey", "(Ljava/lang/String;)Ljava/lang/String;")) {
jstring stringArg = t.env->NewStringUTF(pKey);
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, stringArg);
CCString *ret = new CCString(JniHelper::jstring2string(str).c_str());
ret->autorelease();
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
t.env->DeleteLocalRef(str);
return ret->getCString();
}
return 0;
}
void setBoolForKeyJNI(const char* pKey, bool value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setBoolForKey", "(Ljava/lang/String;Z)V")) {
jstring stringArg = t.env->NewStringUTF(pKey);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
}
}
void setIntegerForKeyJNI(const char* pKey, int value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setIntegerForKey", "(Ljava/lang/String;I)V")) {
jstring stringArg = t.env->NewStringUTF(pKey);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
}
}
void setFloatForKeyJNI(const char* pKey, float value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setFloatForKey", "(Ljava/lang/String;F)V")) {
jstring stringArg = t.env->NewStringUTF(pKey);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
}
}
void setDoubleForKeyJNI(const char* pKey, double value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setDoubleForKey", "(Ljava/lang/String;D)V")) {
jstring stringArg = t.env->NewStringUTF(pKey);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg, value);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg);
}
}
void setStringForKeyJNI(const char* pKey, const char* value)
{
JniMethodInfo t;
if (JniHelper::getStaticMethodInfo(t, CLASS_NAME, "setStringForKeyKey", "(Ljava/lang/String;Ljava/lang/String;)V")) {
jstring stringArg1 = t.env->NewStringUTF(pKey);
jstring stringArg2 = t.env->NewStringUTF(value);
t.env->CallStaticVoidMethod(t.classID, t.methodID, stringArg1, stringArg2);
t.env->DeleteLocalRef(t.classID);
t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(stringArg2);
}
}
}

View File

@ -39,6 +39,17 @@ extern "C" {
extern void enableAccelerometerJNI();
extern void disableAccelerometerJNI();
extern void setAccelerometerIntervalJNI(float interval);
// functions for CCUserDefault
extern bool getBoolForKeyJNI(const char* pKey);
extern int getIntegerForKeyJNI(const char* pKey);
extern float getFloatForKeyJNI(const char* pKey);
extern double getDoubleForKeyJNI(const char* pKey);
extern const char* getStringForKeyJNI(const char* pKey);
extern void setBoolForKeyJNI(const char* pKey, bool value);
extern void setIntegerForKeyJNI(const char* pKey, int value);
extern void setFloatForKeyJNI(const char* pKey, float value);
extern void setDoubleForKeyJNI(const char* pKey, double value);
extern void setStringForKeyJNI(const char* pKey, const char* value);
}
#endif

View File

@ -1 +1 @@
6cfbb92a36ed9c81afdc3a72359e730ee588ba32
c7e210f8b33a816358feec83a71bfe927820a395

View File

@ -0,0 +1,179 @@
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
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.
****************************************************************************/
#include "CCUserDefault.h"
#include "platform/CCPlatformConfig.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
// root name of xml
#define USERDEFAULT_ROOT_NAME "userDefaultRoot"
#define XML_FILE_NAME "UserDefault.xml"
using namespace std;
NS_CC_BEGIN
/**
* implements of CCUserDefault
*/
CCUserDefault* CCUserDefault::m_spUserDefault = 0;
string CCUserDefault::m_sFilePath = string("");
bool CCUserDefault::m_sbIsFilePathInitialized = false;
/**
* If the user invoke delete CCUserDefault::sharedUserDefault(), should set m_spUserDefault
* to null to avoid error when he invoke CCUserDefault::sharedUserDefault() later.
*/
CCUserDefault::~CCUserDefault()
{
CC_SAFE_DELETE(m_spUserDefault);
m_spUserDefault = NULL;
}
CCUserDefault::CCUserDefault()
{
m_spUserDefault = NULL;
}
void CCUserDefault::purgeSharedUserDefault()
{
m_spUserDefault = NULL;
}
bool CCUserDefault::getBoolForKey(const char* pKey)
{
return getBoolForKey(pKey, false);
}
bool CCUserDefault::getBoolForKey(const char* pKey, bool defaultValue)
{
}
int CCUserDefault::getIntegerForKey(const char* pKey)
{
return getIntegerForKey(pKey, 0);
}
int CCUserDefault::getIntegerForKey(const char* pKey, int defaultValue)
{
}
float CCUserDefault::getFloatForKey(const char* pKey)
{
return getFloatForKey(pKey, 0.0f);
}
float CCUserDefault::getFloatForKey(const char* pKey, float defaultValue)
{
float ret = (float)getDoubleForKey(pKey, (double)defaultValue);
return ret;
}
double CCUserDefault::getDoubleForKey(const char* pKey)
{
return getDoubleForKey(pKey, 0.0);
}
double CCUserDefault::getDoubleForKey(const char* pKey, double defaultValue)
{
}
std::string CCUserDefault::getStringForKey(const char* pKey)
{
return getStringForKey(pKey, "");
}
string CCUserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
{
}
void CCUserDefault::setBoolForKey(const char* pKey, bool value)
{
}
void CCUserDefault::setIntegerForKey(const char* pKey, int value)
{
}
void CCUserDefault::setFloatForKey(const char* pKey, float value)
{
}
void CCUserDefault::setDoubleForKey(const char* pKey, double value)
{
}
void CCUserDefault::setStringForKey(const char* pKey, const std::string & value)
{
}
CCUserDefault* CCUserDefault::sharedUserDefault()
{
if (! m_spUserDefault)
{
m_spUserDefault = new CCUserDefault();
}
return m_spUserDefault;
}
bool CCUserDefault::isXMLFileExist()
{
return false;
}
void CCUserDefault::initXMLFilePath()
{
}
// create new xml file
bool CCUserDefault::createXMLFile()
{
return false;
}
const string& CCUserDefault::getXMLFilePath()
{
return m_sFilePath;
}
void CCUserDefault::flush()
{
}
NS_CC_END
#endif // (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

View File

@ -1,6 +1,6 @@
#include "CCConfiguration.h"
#include "RenderTextureTest.h"
#include "testBasic.h"
#include "../testBasic.h"
// Test #1 by Jason Booth (slipster216)
// Test #3 by David Deaco (ddeaco)