Add exporting data

This commit is contained in:
seven332 committed 2016-03-24 01:43:27 +08:00
1 parent 492fcd56a0
commit 9c11ffde3c
8 files changed
+239

No files matched your search

@@ -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);
@@ -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<DownloadInfo> 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<DownloadLabel> 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<DownloadDirname> 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<HistoryInfo> 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;
}
}
*/
}
@@ -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;
}
}
@@ -402,6 +402,10 @@
<string name="settings_advanced_pattern_protection_title">图案保护</string>
<string name="settings_advanced_pattern_protection_not_set">未设置图案保护</string>
<string name="settings_advanced_pattern_protection_set">已设置图案保护</string>
<string name="settings_advanced_export_data">导出数据</string>
<string name="settings_advanced_export_data_summary">保存数据至外置存储器,例如下载列表,快速搜索列表</string>
<string name="settings_advanced_export_data_to">已导出数据至 %s</string>
<string name="settings_advanced_export_data_failed">导出数据失败</string>
<string name="settings_about">关于</string>
<string name="settings_about_author">作者</string>
@@ -402,6 +402,10 @@
<string name="settings_advanced_pattern_protection_title">圖案保護</string>
<string name="settings_advanced_pattern_protection_not_set">未設置圖案保護</string>
<string name="settings_advanced_pattern_protection_set">已設置圖案保護</string>
<string name="settings_advanced_export_data">導出數據</string>
<string name="settings_advanced_export_data_summary">保存數據至外置存儲器,例如下載列表,快速搜索列表</string>
<string name="settings_advanced_export_data_to">已導出數據至 %s</string>
<string name="settings_advanced_export_data_failed">導出數據失敗</string>
<string name="settings_about">關於</string>
<string name="settings_about_author">作者</string>
@@ -402,6 +402,10 @@
<string name="settings_advanced_pattern_protection_title">圖案保護</string>
<string name="settings_advanced_pattern_protection_not_set">未設定圖案保護</string>
<string name="settings_advanced_pattern_protection_set">已設定圖案保護</string>
<string name="settings_advanced_export_data">匯出資料</string>
<string name="settings_advanced_export_data_summary">儲存資料至外接儲存器,例如下載列表,快速搜尋列表</string>
<string name="settings_advanced_export_data_to">已匯出資料至 %s</string>
<string name="settings_advanced_export_data_failed">匯出資料失敗</string>
<string name="settings_about">關於</string>
<string name="settings_about_author">作者</string>
+4
View File
@@ -444,6 +444,10 @@
<string name="settings_advanced_pattern_protection_title">Pattern protection</string>
<string name="settings_advanced_pattern_protection_not_set">Pattern protection has not been set</string>
<string name="settings_advanced_pattern_protection_set">Pattern protection has been set</string>
<string name="settings_advanced_export_data">Export data</string>
<string name="settings_advanced_export_data_summary">Save data to external storage, such as download list, quick search list</string>
<string name="settings_advanced_export_data_to">Exported data to %s</string>
<string name="settings_advanced_export_data_failed">Export data failed</string>
<string name="settings_about">About</string>
<string name="settings_about_author">Author</string>
@@ -45,4 +45,9 @@
android:title="@string/settings_advanced_pattern_protection_title"
app:activity="com.hippo.ehviewer.ui.SetSecurityActivity"/>
<Preference
android:key="export_data"
android:title="@string/settings_advanced_export_data"
android:summary="@string/settings_advanced_export_data_summary"/>
</PreferenceScreen>