|
|
|
|
@ -5,6 +5,14 @@ import android.content.SharedPreferences; |
|
|
|
|
import androidx.preference.PreferenceManager; |
|
|
|
|
|
|
|
|
|
import com.github.catvod.Init; |
|
|
|
|
import com.google.gson.Gson; |
|
|
|
|
import com.google.gson.GsonBuilder; |
|
|
|
|
import com.google.gson.ToNumberPolicy; |
|
|
|
|
import com.google.gson.internal.LazilyParsedNumber; |
|
|
|
|
import com.google.gson.reflect.TypeToken; |
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
public class Prefers { |
|
|
|
|
|
|
|
|
|
@ -12,6 +20,17 @@ 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, ""); |
|
|
|
|
} |
|
|
|
|
@ -48,6 +67,8 @@ public class Prefers { |
|
|
|
|
getPrefers().edit().putInt(key, (Integer) obj).apply(); |
|
|
|
|
} else if (obj instanceof Long) { |
|
|
|
|
getPrefers().edit().putLong(key, (Long) obj).apply(); |
|
|
|
|
} else if (obj instanceof LazilyParsedNumber) { |
|
|
|
|
getPrefers().edit().putInt(key, ((LazilyParsedNumber) obj).intValue()).apply(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|