MCPcopy Create free account
hub / github.com/FongMi/TV / AppDatabase

Class AppDatabase

app/src/main/java/com/fongmi/android/tv/db/AppDatabase.java:35–116  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33import java.util.List;
34
35@Database(entities = {Keep.class, Site.class, Live.class, Track.class, Config.class, Device.class, History.class}, version = AppDatabase.VERSION)
36public abstract class AppDatabase extends RoomDatabase {
37
38 public static final int VERSION = 35;
39 public static final String NAME = "tv";
40 public static final String SYMBOL = "@@@";
41
42 private static volatile AppDatabase instance;
43
44 public static synchronized AppDatabase get() {
45 if (instance == null) instance = create(App.get());
46 return instance;
47 }
48
49 public static void backup() {
50 backup(new com.fongmi.android.tv.impl.Callback());
51 }
52
53 public static void backup(com.fongmi.android.tv.impl.Callback callback) {
54 Task.execute(() -> {
55 File file = new File(Path.tv(), "tv-" + LocalDate.now().format(Formatters.DATE) + ".bk");
56 Backup backup = Backup.create();
57 if (backup.getConfig().isEmpty()) {
58 App.post(callback::error);
59 } else {
60 Path.write(file, backup.toString().getBytes());
61 FileUtil.gzipCompress(file);
62 App.post(callback::success);
63 cleanOld();
64 }
65 });
66 }
67
68 public static void restore(File file, com.fongmi.android.tv.impl.Callback callback) {
69 Task.execute(() -> {
70 File restore = Path.cache("restore");
71 FileUtil.gzipDecompress(file, restore);
72 Backup backup = Backup.objectFrom(Path.read(restore));
73 if (backup.getConfig().isEmpty()) {
74 App.post(callback::error);
75 } else {
76 backup.restore();
77 Path.clear(restore);
78 App.post(callback::success);
79 }
80 });
81 }
82
83 private static void cleanOld() {
84 List<File> items = new ArrayList<>();
85 File[] files = Path.tv().listFiles();
86 if (files == null) files = new File[0];
87 for (File file : files) if (file.getName().startsWith("tv") && file.getName().endsWith(".bk.gz")) items.add(file);
88 if (!items.isEmpty()) items.sort((f1, f2) -> Long.compare(f2.lastModified(), f1.lastModified()));
89 if (items.size() > 7) for (int i = 7; i < items.size(); i++) Path.clear(items.get(i));
90 }
91
92 private static AppDatabase create(Context context) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected