diff --git a/app/src/main/java/com/hippo/ehviewer/AppConfig.java b/app/src/main/java/com/hippo/ehviewer/AppConfig.java index 8e116fc55..e2efaf13c 100644 --- a/app/src/main/java/com/hippo/ehviewer/AppConfig.java +++ b/app/src/main/java/com/hippo/ehviewer/AppConfig.java @@ -34,6 +34,7 @@ public class AppConfig { private static final String CRASH = "crash"; private static final String PARSE_ERROR = "parse_error"; private static final String LOGCAT = "logcat"; + private static final String DATA = "data"; private static Context sContext; @@ -103,6 +104,11 @@ public class AppConfig { return getDirInExternalAppDir(LOGCAT); } + @Nullable + public static File getExternalDataDir() { + return getDirInExternalAppDir(DATA); + } + @Nullable public static File createExternalTempFile() { return FileUtils.createTempFile(getExternalTempDir(), null); diff --git a/app/src/main/java/com/hippo/ehviewer/EhDB.java b/app/src/main/java/com/hippo/ehviewer/EhDB.java index 49a76990f..89a9d25a9 100644 --- a/app/src/main/java/com/hippo/ehviewer/EhDB.java +++ b/app/src/main/java/com/hippo/ehviewer/EhDB.java @@ -43,8 +43,16 @@ import com.hippo.ehviewer.dao.LocalFavoritesDao; import com.hippo.ehviewer.dao.QuickSearch; import com.hippo.ehviewer.dao.QuickSearchDao; import com.hippo.util.SqlUtils; +import com.hippo.yorozuya.FileUtils; +import com.hippo.yorozuya.IOUtils; import com.hippo.yorozuya.sparse.SparseJLArray; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.ArrayList; import java.util.List; @@ -564,4 +572,172 @@ public class EhDB { public static synchronized void deleteFilter(Filter filter) { sDaoSession.getFilterDao().delete(filter); } + + public static synchronized boolean export(Context context, File file) { + File dbFile = context.getDatabasePath("eh.db"); + if (null == dbFile || !dbFile.isFile()) { + return false; + } + if (null == file || !FileUtils.ensureFile(file)) { + return false; + } + InputStream is = null; + OutputStream os = null; + try { + is = new FileInputStream(dbFile); + os = new FileOutputStream(file); + IOUtils.copy(is, os); + return true; + } catch (IOException e) { + e.printStackTrace(); + } finally { + IOUtils.closeQuietly(is); + IOUtils.closeQuietly(os); + } + // Delete failed file + file.delete(); + return false; + } + + + + /* + public static synchronized void write(OutputStream os) throws JSONException { + JSONObject jo = new JSONObject(); + jo.put("version", DaoMaster.SCHEMA_VERSION); + jo.put("data", getDBJson()); + + + + } + + private static JSONObject getDBJson() throws JSONException { + JSONObject jo = new JSONObject(); + jo.put(DownloadsDao.TABLENAME, getDownloadsDaoJson()); + jo.put(DownloadLabelDao.TABLENAME, getDownloadLabelDaoJson()); + jo.put(DownloadDirnameDao.TABLENAME, getDownloadDirnameDaoJson()); + + + + return jo; + } + + private static JSONArray getDownloadsDaoJson() { + DownloadsDao dao = sDaoSession.getDownloadsDao(); + JSONArray ja = new JSONArray(); + LazyList list = dao.queryBuilder().listLazy(); + for (DownloadInfo info: list) { + JSONObject jo = getDownloadInfoJson(info); + if (null != jo) { + ja.put(jo); + } + } + list.close(); + return ja; + } + + private static JSONObject getDownloadInfoJson(DownloadInfo info) { + try { + JSONObject jo = new JSONObject(); + jo.put("gid", info.gid); + jo.put("token", info.token); + jo.put("title", info.title); + jo.put("titleJpn", info.titleJpn); + jo.put("thumb", info.thumb); + jo.put("category", info.category); + jo.put("posted", info.posted); + jo.put("uploader", info.uploader); + jo.put("rating", info.rating); + jo.put("simpleLanguage", info.simpleLanguage); + jo.put("state", info.state); + jo.put("legacy", info.legacy); + jo.put("time", info.time); + jo.put("label", info.label); + return jo; + } catch (JSONException e) { + e.printStackTrace(); + return null; + } + } + + private static JSONArray getDownloadLabelDaoJson() { + DownloadLabelDao dao = sDaoSession.getDownloadLabelDao(); + JSONArray ja = new JSONArray(); + LazyList list = dao.queryBuilder().listLazy(); + for (DownloadLabel label: list) { + JSONObject jo = getDownloadLabelJson(label); + if (null != jo) { + ja.put(jo); + } + } + list.close(); + return ja; + } + + private static JSONObject getDownloadLabelJson(DownloadLabel label) { + try { + JSONObject jo = new JSONObject(); + jo.put("label", label.getLabel()); + jo.put("time", label.getTime()); + return jo; + } catch (JSONException e) { + e.printStackTrace(); + return null; + } + } + + private static JSONArray getDownloadDirnameDaoJson() { + DownloadDirnameDao dao = sDaoSession.getDownloadDirnameDao(); + JSONArray ja = new JSONArray(); + LazyList list = dao.queryBuilder().listLazy(); + for (DownloadDirname dirname: list) { + JSONObject jo = getDownloadDirnameJson(dirname); + if (null != jo) { + ja.put(jo); + } + } + list.close(); + return ja; + } + + private static JSONObject getDownloadDirnameJson(DownloadDirname dirname) { + try { + JSONObject jo = new JSONObject(); + jo.put("gid", dirname.getGid()); + jo.put("dirname", dirname.getDirname()); + return jo; + } catch (JSONException e) { + e.printStackTrace(); + return null; + } + } + + private static JSONArray getHistoryDaoJson() { + HistoryDao dao = sDaoSession.getHistoryDao(); + JSONArray ja = new JSONArray(); + LazyList list = dao.queryBuilder().listLazy(); + for (HistoryInfo info: list) { + JSONObject jo = getDownloadDirnameJson(info); + if (null != jo) { + ja.put(jo); + } + } + list.close(); + return ja; + } + + private static JSONObject getHistoryInfoJson(HistoryInfo info) { + try { + JSONObject jo = new JSONObject(); + + + + + return jo; + } catch (JSONException e) { + e.printStackTrace(); + return null; + } + } + */ } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.java b/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.java index 6d7865c1a..af8bd3427 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/fragment/AdvancedFragment.java @@ -16,6 +16,7 @@ package com.hippo.ehviewer.ui.fragment; +import android.content.Context; import android.content.res.Resources; import android.os.Bundle; import android.preference.Preference; @@ -25,6 +26,7 @@ import android.widget.Toast; import com.hippo.ehviewer.AppConfig; import com.hippo.ehviewer.EhApplication; +import com.hippo.ehviewer.EhDB; import com.hippo.ehviewer.R; import com.hippo.ehviewer.Settings; import com.hippo.util.LogCat; @@ -37,6 +39,7 @@ public class AdvancedFragment extends PreferenceFragment implements Preference.O private static final String KEY_DUMP_LOGCAT = "dump_logcat"; private static final String KEY_CLEAR_MEMORY_CACHE = "clear_memory_cache"; private static final String KEY_PATTERN_PROTECTION = "pattern_protection"; + private static final String KEY_EXPORT_DATA = "export_data"; @Override public void onCreate(Bundle savedInstanceState) { @@ -45,9 +48,11 @@ public class AdvancedFragment extends PreferenceFragment implements Preference.O Preference dumpLogcat = findPreference(KEY_DUMP_LOGCAT); Preference clearMemoryCache = findPreference(KEY_CLEAR_MEMORY_CACHE); + Preference exportData = findPreference(KEY_EXPORT_DATA); dumpLogcat.setOnPreferenceClickListener(this); clearMemoryCache.setOnPreferenceClickListener(this); + exportData.setOnPreferenceClickListener(this); } @Override @@ -81,7 +86,38 @@ public class AdvancedFragment extends PreferenceFragment implements Preference.O } else if (KEY_CLEAR_MEMORY_CACHE.equals(key)) { ((EhApplication) getActivity().getApplication()).clearMemoryCache(); Runtime.getRuntime().gc(); + } else if (KEY_EXPORT_DATA.equals(key)) { + File dir = AppConfig.getExternalDataDir(); + if (dir != null) { + File file = new File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".db"); + if (EhDB.export(getActivity(), file)) { + Toast.makeText(getActivity(), + getString(R.string.settings_advanced_export_data_to, file.getPath()), Toast.LENGTH_SHORT).show(); + return true; + } + } + Toast.makeText(getActivity(),R.string.settings_advanced_export_data_failed, Toast.LENGTH_SHORT).show(); + return false; } return false; } + + + private static boolean exportData(Context context) { + File dir = AppConfig.getExternalAppDir(); + if (null == dir) { + return false; + } + File file = new File(ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".json"); + + + + + + + + + return true; + } + } diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 18f19ac9d..98f9f5bd7 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -402,6 +402,10 @@ 图案保护 未设置图案保护 已设置图案保护 + 导出数据 + 保存数据至外置存储器,例如下载列表,快速搜索列表 + 已导出数据至 %s + 导出数据失败 关于 作者 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 3344c612d..c2f3da514 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -402,6 +402,10 @@ 圖案保護 未設置圖案保護 已設置圖案保護 + 導出數據 + 保存數據至外置存儲器,例如下載列表,快速搜索列表 + 已導出數據至 %s + 導出數據失敗 關於 作者 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 612683c39..af19dc78a 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -402,6 +402,10 @@ 圖案保護 未設定圖案保護 已設定圖案保護 + 匯出資料 + 儲存資料至外接儲存器,例如下載列表,快速搜尋列表 + 已匯出資料至 %s + 匯出資料失敗 關於 作者 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e24e8e594..d02b6c07e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -444,6 +444,10 @@ Pattern protection Pattern protection has not been set Pattern protection has been set + Export data + Save data to external storage, such as download list, quick search list + Exported data to %s + Export data failed About Author diff --git a/app/src/main/res/xml/advanced_settings.xml b/app/src/main/res/xml/advanced_settings.xml index 85464d365..f417bf2d3 100644 --- a/app/src/main/res/xml/advanced_settings.xml +++ b/app/src/main/res/xml/advanced_settings.xml @@ -45,4 +45,9 @@ android:title="@string/settings_advanced_pattern_protection_title" app:activity="com.hippo.ehviewer.ui.SetSecurityActivity"/> + +