Typed settings export/import

pull/162/head
M66B 5 years ago
parent d96d9fa4f7
commit b6bbe9fdc9

@ -577,8 +577,22 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
JSONArray jsettings = new JSONArray(); JSONArray jsettings = new JSONArray();
for (String key : prefs.getAll().keySet()) { for (String key : prefs.getAll().keySet()) {
JSONObject jsetting = new JSONObject(); JSONObject jsetting = new JSONObject();
Object value = prefs.getAll().get(key);
jsetting.put("key", key); jsetting.put("key", key);
jsetting.put("value", prefs.getAll().get(key)); jsetting.put("value", value);
if (value instanceof Boolean)
jsetting.put("type", "bool");
else if (value instanceof Integer)
jsetting.put("type", "int");
else if (value instanceof Long)
jsetting.put("type", "long");
else if (value instanceof String)
jsetting.put("type", "string");
else if (value != null) {
String type = value.getClass().getName();
Log.w("Unknown type=" + type);
jsetting.put("type", type);
}
jsettings.put(jsetting); jsettings.put(jsetting);
} }
@ -894,16 +908,40 @@ public class ActivitySetup extends ActivityBase implements FragmentManager.OnBac
continue; continue;
Object value = jsetting.get("value"); Object value = jsetting.get("value");
if (value instanceof Boolean) String type = jsetting.optString("type");
editor.putBoolean(key, (Boolean) value); Log.i("Setting name=" + key + " value=" + value + " type=" + type);
else if (value instanceof Integer) switch (type) {
editor.putInt(key, (Integer) value); case "bool":
else if (value instanceof Long) editor.putBoolean(key, (Boolean) value);
editor.putLong(key, (Long) value); break;
else if (value instanceof String) case "int":
editor.putString(key, (String) value); editor.putInt(key, (Integer) value);
else break;
throw new IllegalArgumentException("Unknown settings type key=" + key); case "long":
if (value instanceof Integer)
editor.putLong(key, Long.valueOf((Integer) value));
else
editor.putLong(key, (Long) value);
break;
case "string":
editor.putString(key, (String) value);
break;
default:
Log.w("Inferring type of value=" + value);
if (value instanceof Boolean)
editor.putBoolean(key, (Boolean) value);
else if (value instanceof Integer)
if (key.endsWith(".account"))
editor.putLong(key, (Long) value);
else
editor.putInt(key, (Integer) value);
else if (value instanceof Long)
editor.putLong(key, (Long) value);
else if (value instanceof String)
editor.putString(key, (String) value);
else
throw new IllegalArgumentException("Unknown settings type key=" + key);
}
Log.i("Imported setting=" + key); Log.i("Imported setting=" + key);
} }

Loading…
Cancel
Save