|
|
|
|
@ -20,23 +20,16 @@ public class Prefers { |
|
|
|
|
return PreferenceManager.getDefaultSharedPreferences(Init.context()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void backup(File file) { |
|
|
|
|
Path.write(file, new Gson().toJson(getPrefers().getAll()).getBytes()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void restore(File file) { |
|
|
|
|
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LAZILY_PARSED_NUMBER).create(); |
|
|
|
|
Map<String, Object> map = gson.fromJson(Path.read(file), new TypeToken<Map<String, Object>>() {}.getType()); |
|
|
|
|
if (map != null) for (Map.Entry<String, ?> entry : map.entrySet()) Prefers.put(entry.getKey(), entry.getValue()); |
|
|
|
|
Path.clear(file); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getString(String key) { |
|
|
|
|
return getString(key, ""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static String getString(String key, String defaultValue) { |
|
|
|
|
return getPrefers().getString(key, defaultValue); |
|
|
|
|
try { |
|
|
|
|
return getPrefers().getString(key, defaultValue); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return defaultValue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static int getInt(String key) { |
|
|
|
|
@ -44,15 +37,23 @@ public class Prefers { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static int getInt(String key, int defaultValue) { |
|
|
|
|
return getPrefers().getInt(key, defaultValue); |
|
|
|
|
try { |
|
|
|
|
return getPrefers().getInt(key, defaultValue); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return defaultValue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static float getFloat(String key) { |
|
|
|
|
return getFloat(key, 0); |
|
|
|
|
return getFloat(key, 0f); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static float getFloat(String key, float defaultValue) { |
|
|
|
|
return getPrefers().getFloat(key, defaultValue); |
|
|
|
|
try { |
|
|
|
|
return getPrefers().getFloat(key, defaultValue); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return defaultValue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean getBoolean(String key) { |
|
|
|
|
@ -60,7 +61,11 @@ public class Prefers { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static boolean getBoolean(String key, boolean defaultValue) { |
|
|
|
|
return getPrefers().getBoolean(key, defaultValue); |
|
|
|
|
try { |
|
|
|
|
return getPrefers().getBoolean(key, defaultValue); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
return defaultValue; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void put(String key, Object obj) { |
|
|
|
|
@ -83,4 +88,28 @@ public class Prefers { |
|
|
|
|
public static void remove(String key) { |
|
|
|
|
getPrefers().edit().remove(key).apply(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void backup(File file) { |
|
|
|
|
Path.write(file, new Gson().toJson(getPrefers().getAll()).getBytes()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void restore(File file) { |
|
|
|
|
try { |
|
|
|
|
Gson gson = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LAZILY_PARSED_NUMBER).create(); |
|
|
|
|
Map<String, Object> map = gson.fromJson(Path.read(file), new TypeToken<Map<String, Object>>() {}.getType()); |
|
|
|
|
for (Map.Entry<String, ?> entry : map.entrySet()) Prefers.put(entry.getKey(), convert(entry)); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} finally { |
|
|
|
|
Path.clear(file); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static Object convert(Map.Entry<String, ?> entry) { |
|
|
|
|
if (entry.getKey().equals("danmu_size")) { |
|
|
|
|
return Float.parseFloat(entry.getValue().toString()); |
|
|
|
|
} else { |
|
|
|
|
return entry.getValue(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|