diff --git a/Aretha/src/com/aretha/content/config/PersistentConfig.java b/Aretha/src/com/aretha/content/config/PersistentConfig.java
index 3b276e6..1575ebc 100644
--- a/Aretha/src/com/aretha/content/config/PersistentConfig.java
+++ b/Aretha/src/com/aretha/content/config/PersistentConfig.java
@@ -23,9 +23,9 @@
/**
* A simple wrapper for the {@link SharedPreferences}, all the subclass's field
- * with {@link PersistentConfigEntry} can be saved into the xml file, but remember only
- * {@link String}, {@link Integer}, {@link Long}, {@link Boolean}, {@link Float}
- * can be accept.
+ * with {@link PersistentConfigEntry} can be saved into the xml file, but
+ * remember only {@link String}, {@link Integer}, {@link Long}, {@link Boolean},
+ * {@link Float} can be accept.
* usage:
*
* class YourClass extends PersistentConfig{
@@ -64,14 +64,40 @@ private void read() {
for (Field field : fields) {
field.setAccessible(true);
- PersistentConfigEntry annotation = field.getAnnotation(PersistentConfigEntry.class);
+ PersistentConfigEntry annotation = field
+ .getAnnotation(PersistentConfigEntry.class);
String key = field.getName();
+
+ // ignore the field without PersistentConfigEntry
if (null == annotation) {
- String tempKey = annotation.key();
- key = tempKey.length() == 0 ? key : tempKey;
+ continue;
}
+
+ // use the PersistentConfigEntry key first
+ String tempKey = annotation.key();
+ key = tempKey.length() == 0 ? key : tempKey;
+
+ String defaultValue = annotation.defaultValue();
+ Object value = preferences.get(key);
+
+ // not saved in preference and has default value in PersistentConfigEntry
+ if (null == value && defaultValue.length() != 0) {
+ Class> type = field.getType();
+ if (type == String.class) {
+ value = defaultValue;
+ } else if (type == Integer.class || type == int.class) {
+ value = Integer.parseInt(defaultValue);
+ } else if (type == Float.class || type == float.class) {
+ value = Float.parseFloat(defaultValue);
+ } else if (type == Boolean.class || type == boolean.class) {
+ value = Boolean.parseBoolean(defaultValue);
+ } else if (type == Long.class || type == long.class) {
+ value = Long.parseLong(defaultValue);
+ }
+ }
+
try {
- field.set(this, preferences.get(key));
+ field.set(this, value);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
}
@@ -86,7 +112,8 @@ public void save() {
TAG, Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
for (Field field : fields) {
- PersistentConfigEntry annotation = field.getAnnotation(PersistentConfigEntry.class);
+ PersistentConfigEntry annotation = field
+ .getAnnotation(PersistentConfigEntry.class);
if (null == annotation) {
continue;
}
diff --git a/Aretha/src/com/aretha/content/config/PersistentConfigEntry.java b/Aretha/src/com/aretha/content/config/PersistentConfigEntry.java
index f3dc029..d0053d6 100644
--- a/Aretha/src/com/aretha/content/config/PersistentConfigEntry.java
+++ b/Aretha/src/com/aretha/content/config/PersistentConfigEntry.java
@@ -29,4 +29,5 @@
* @return key name
*/
String key() default "";
+ String defaultValue() default "";
}