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;
}
}