Merge pull request #12123 from WenhaiLin/v3-UserDefault-robustness

Improve UserDefault's robustness on Android.
This commit is contained in:
子龙山人 2015-06-02 10:11:19 +08:00
commit 2c611693c6
1 changed files with 83 additions and 6 deletions

View File

@ -27,6 +27,7 @@ package org.cocos2dx.lib;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.util.Locale; import java.util.Locale;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.lang.Runnable; import java.lang.Runnable;
@ -379,29 +380,105 @@ public class Cocos2dxHelper {
public static boolean getBoolForKey(String key, boolean defaultValue) { public static boolean getBoolForKey(String key, boolean defaultValue) {
SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
try {
return settings.getBoolean(key, defaultValue); return settings.getBoolean(key, defaultValue);
} }
catch (Exception ex) {
ex.printStackTrace();
Map allValues = settings.getAll();
Object value = allValues.get(key);
if ( value instanceof String)
{
return Boolean.parseBoolean(value.toString());
}
else if (value instanceof Integer)
{
int intValue = ((Integer) value).intValue();
return (intValue != 0) ;
}
else if (value instanceof Float)
{
float floatValue = ((Float) value).floatValue();
return (floatValue != 0.0f);
}
}
return false;
}
public static int getIntegerForKey(String key, int defaultValue) { public static int getIntegerForKey(String key, int defaultValue) {
SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
try {
return settings.getInt(key, defaultValue); return settings.getInt(key, defaultValue);
} }
catch (Exception ex) {
ex.printStackTrace();
Map allValues = settings.getAll();
Object value = allValues.get(key);
if ( value instanceof String) {
return Integer.parseInt(value.toString());
}
else if (value instanceof Float)
{
return ((Float) value).intValue();
}
else if (value instanceof Boolean)
{
boolean booleanValue = ((Boolean) value).booleanValue();
if (booleanValue)
return 1;
}
}
return 0;
}
public static float getFloatForKey(String key, float defaultValue) { public static float getFloatForKey(String key, float defaultValue) {
SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
try {
return settings.getFloat(key, defaultValue); return settings.getFloat(key, defaultValue);
} }
catch (Exception ex) {
ex.printStackTrace();;
Map allValues = settings.getAll();
Object value = allValues.get(key);
if ( value instanceof String) {
return Float.parseFloat(value.toString());
}
else if (value instanceof Integer)
{
return ((Integer) value).floatValue();
}
else if (value instanceof Boolean)
{
boolean booleanValue = ((Boolean) value).booleanValue();
if (booleanValue)
return 1.0f;
}
}
return 0.0f;
}
public static double getDoubleForKey(String key, double defaultValue) { public static double getDoubleForKey(String key, double defaultValue) {
// SharedPreferences doesn't support saving double value // SharedPreferences doesn't support saving double value
SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); return getFloatForKey(key, (float) defaultValue);
return settings.getFloat(key, (float)defaultValue);
} }
public static String getStringForKey(String key, String defaultValue) { public static String getStringForKey(String key, String defaultValue) {
SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);
try {
return settings.getString(key, defaultValue); return settings.getString(key, defaultValue);
} }
catch (Exception ex) {
ex.printStackTrace();
return settings.getAll().get(key).toString();
}
}
public static void setBoolForKey(String key, boolean value) { public static void setBoolForKey(String key, boolean value) {
SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0); SharedPreferences settings = sActivity.getSharedPreferences(Cocos2dxHelper.PREFS_NAME, 0);