Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
gedoor committed Oct 15, 2019
1 parent a79c41a commit 386965b
Showing 1 changed file with 50 additions and 43 deletions.
93 changes: 50 additions & 43 deletions app/src/main/java/com/kunfei/bookshelf/help/DataRestore.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.kunfei.bookshelf.DbHelper;
import com.kunfei.bookshelf.MApplication;
import com.kunfei.bookshelf.R;
import com.kunfei.bookshelf.bean.BookShelfBean;
import com.kunfei.bookshelf.bean.BookSourceBean;
import com.kunfei.bookshelf.bean.ReplaceRuleBean;
Expand Down Expand Up @@ -34,57 +35,15 @@ public static DataRestore getInstance() {

public Boolean run() {
String dirPath = FileUtils.getSdCardPath() + File.separator + "YueDu";
restoreConfig(dirPath);
restoreBookSource(dirPath);
restoreBookShelf(dirPath);
restoreSearchHistory(dirPath);
restoreReplaceRule(dirPath);
restoreTxtChapterRule(dirPath);
restoreConfig(dirPath);
return true;
}

private void restoreConfig(String dirPath) {
Map<String, ?> entries = null;
try (FileInputStream ins = new FileInputStream(dirPath + File.separator + "config.xml")) {
entries = XmlUtils.readMapXml(ins);
} catch (Exception ignored) {
}
if (entries == null || entries.isEmpty()) return;
long donateHb = MApplication.getConfigPreferences().getLong("DonateHb", 0);
donateHb = donateHb > System.currentTimeMillis() ? 0 : donateHb;
SharedPreferences.Editor editor = MApplication.getConfigPreferences().edit();
editor.clear();
for (Map.Entry<String, ?> entry : entries.entrySet()) {
Object v = entry.getValue();
String key = entry.getKey();
String type = v.getClass().getSimpleName();

switch (type) {
case "Integer":
editor.putInt(key, (Integer) v);
break;
case "Boolean":
editor.putBoolean(key, (Boolean) v);
break;
case "String":
editor.putString(key, (String) v);
break;
case "Float":
editor.putFloat(key, (Float) v);
break;
case "Long":
editor.putLong(key, (Long) v);
break;
}
}
editor.putLong("DonateHb", donateHb);
editor.putInt("versionCode", MApplication.getVersionCode());
editor.apply();
ReadBookControl.getInstance().updateReaderSettings();
MApplication.getInstance().upThemeStore();
MApplication.getInstance().initNightTheme();
}

private void restoreBookShelf(String file) {
try {
String json = DocumentHelper.readString("myBookShelf.json", file);
Expand Down Expand Up @@ -151,4 +110,52 @@ private void restoreTxtChapterRule(String file) {
e.printStackTrace();
}
}

private void restoreConfig(String dirPath) {
try {
Map<String, ?> entries = null;
try (FileInputStream ins = new FileInputStream(dirPath + File.separator + "config.xml")) {
entries = XmlUtils.readMapXml(ins);
} catch (Exception ignored) {
}
if (entries == null || entries.isEmpty()) return;
long donateHb = MApplication.getConfigPreferences().getLong("DonateHb", 0);
donateHb = donateHb > System.currentTimeMillis() ? 0 : donateHb;
SharedPreferences.Editor editor = MApplication.getConfigPreferences().edit();
editor.clear();
for (Map.Entry<String, ?> entry : entries.entrySet()) {
Object v = entry.getValue();
String key = entry.getKey();
String type = v.getClass().getSimpleName();

switch (type) {
case "Integer":
editor.putInt(key, (Integer) v);
break;
case "Boolean":
editor.putBoolean(key, (Boolean) v);
break;
case "String":
editor.putString(key, (String) v);
break;
case "Float":
editor.putFloat(key, (Float) v);
break;
case "Long":
editor.putLong(key, (Long) v);
break;
}
}
editor.putLong("DonateHb", donateHb);
editor.putInt("versionCode", MApplication.getVersionCode());
editor.apply();
LauncherIcon.ChangeIcon(MApplication.getConfigPreferences().getString("launcher_icon", MApplication.getInstance().getString(R.string.icon_main)));
ReadBookControl.getInstance().updateReaderSettings();
MApplication.getInstance().upThemeStore();
MApplication.getInstance().initNightTheme();
} catch (Exception e) {
e.printStackTrace();
}
}

}

0 comments on commit 386965b

Please sign in to comment.