Skip to content

Commit

Permalink
Fix data exporting
Browse files Browse the repository at this point in the history
  • Loading branch information
seven332 committed Aug 1, 2019
1 parent 28eade9 commit d72f686
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 36 deletions.
75 changes: 55 additions & 20 deletions app/src/main/java/com/hippo/ehviewer/EhDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
import com.hippo.ehviewer.download.DownloadManager;
import com.hippo.util.ExceptionUtils;
import com.hippo.util.SqlUtils;
import com.hippo.yorozuya.FileUtils;
import com.hippo.yorozuya.IOUtils;
import com.hippo.yorozuya.ObjectUtils;
import com.hippo.yorozuya.collect.SparseJLArray;
import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.query.CloseableListIterator;
import de.greenrobot.dao.query.LazyList;
import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -645,30 +646,64 @@ public static synchronized void triggerFilter(Filter filter) {
sDaoSession.getFilterDao().update(filter);
}

public static synchronized boolean exportDB(Context context, File file) {
File dbFile = context.getDatabasePath("eh.db");
if (null == dbFile || !dbFile.isFile()) {
return false;
}
if (null == file || !FileUtils.ensureFile(file)) {
private static <T> boolean copyDao(AbstractDao<T, ?> from, AbstractDao<T, ?> to) {
try (CloseableListIterator<T> iterator = from.queryBuilder().listIterator()) {
while (iterator.hasNext()) {
to.insert(iterator.next());
}
} catch (IOException e) {
return false;
}
InputStream is = null;
OutputStream os = null;
return true;
}

public static synchronized boolean exportDB(Context context, File file) {
final String ehExportName = "eh.export.db";

// Delete old export db
context.deleteDatabase(ehExportName);

DBOpenHelper helper = new DBOpenHelper(context.getApplicationContext(), ehExportName, null);

try {
is = new FileInputStream(dbFile);
os = new FileOutputStream(file);
IOUtils.copy(is, os);
return true;
} catch (IOException e) {
e.printStackTrace();
// Copy data to a export db
try (SQLiteDatabase db = helper.getWritableDatabase()) {
DaoMaster daoMaster = new DaoMaster(db);
DaoSession exportSession = daoMaster.newSession();
if (!copyDao(sDaoSession.getDownloadsDao(), exportSession.getDownloadsDao())) return false;
if (!copyDao(sDaoSession.getDownloadLabelDao(), exportSession.getDownloadLabelDao())) return false;
if (!copyDao(sDaoSession.getDownloadDirnameDao(), exportSession.getDownloadDirnameDao())) return false;
if (!copyDao(sDaoSession.getHistoryDao(), exportSession.getHistoryDao())) return false;
if (!copyDao(sDaoSession.getQuickSearchDao(), exportSession.getQuickSearchDao())) return false;
if (!copyDao(sDaoSession.getLocalFavoritesDao(), exportSession.getLocalFavoritesDao())) return false;
if (!copyDao(sDaoSession.getBookmarksBao(), exportSession.getBookmarksBao())) return false;
if (!copyDao(sDaoSession.getFilterDao(), exportSession.getFilterDao())) return false;
}

// Copy export db to data dir
File dbFile = context.getDatabasePath(ehExportName);
if (dbFile == null || !dbFile.isFile()) {
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;
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
context.deleteDatabase(ehExportName);
}
// Delete failed file
file.delete();
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2019 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.hippo.ehviewer.preference;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.hippo.ehviewer.AppConfig;
import com.hippo.ehviewer.EhDB;
import com.hippo.ehviewer.GetText;
import com.hippo.ehviewer.R;
import com.hippo.util.ReadableTime;
import java.io.File;

public class ExportDataPreference extends TaskPreference {

public ExportDataPreference(Context context) {
super(context);
}

public ExportDataPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}

public ExportDataPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@NonNull
@Override
protected Task onCreateTask() {
return new ExportDataTask(getContext());
}

private static class ExportDataTask extends Task {

public ExportDataTask(@NonNull Context context) {
super(context);
}

@Override
protected Object doInBackground(Void... voids) {
File dir = AppConfig.getExternalDataDir();
if (dir != null) {
File file = new File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".db");
if (EhDB.exportDB(getApplication(), file)) {
return file;
}
}
return null;
}

@Override
protected void onPostExecute(Object o) {
Toast.makeText(getApplication(),
(o instanceof File)
? GetText.getString(R.string.settings_advanced_export_data_to, ((File) o).getPath())
: GetText.getString(R.string.settings_advanced_export_data_failed),
Toast.LENGTH_SHORT).show();
super.onPostExecute(o);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ public Task(@NonNull Context context) {
mApplication = (EhApplication) context.getApplicationContext();
}

public EhApplication getApplication() {
return mApplication;
}

@Nullable
public TaskPreference getPreference() {
return mPreference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public class AdvancedFragment extends PreferenceFragment
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_APP_LANGUAGE = "app_language";
private static final String KEY_EXPORT_DATA = "export_data";
private static final String KEY_IMPORT_DATA = "import_data";

@Override
Expand All @@ -51,12 +50,10 @@ public void onCreate(Bundle savedInstanceState) {
Preference dumpLogcat = findPreference(KEY_DUMP_LOGCAT);
Preference clearMemoryCache = findPreference(KEY_CLEAR_MEMORY_CACHE);
Preference appLanguage = findPreference(KEY_APP_LANGUAGE);
Preference exportData = findPreference(KEY_EXPORT_DATA);
Preference importData = findPreference(KEY_IMPORT_DATA);

dumpLogcat.setOnPreferenceClickListener(this);
clearMemoryCache.setOnPreferenceClickListener(this);
exportData.setOnPreferenceClickListener(this);
importData.setOnPreferenceClickListener(this);

appLanguage.setOnPreferenceChangeListener(this);
Expand Down Expand Up @@ -88,18 +85,6 @@ public boolean onPreferenceClick(Preference preference) {
} 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.exportDB(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 true;
} else if (KEY_IMPORT_DATA.equals(key)) {
importData(getActivity());
getActivity().setResult(Activity.RESULT_OK);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/advanced_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
android:summary="@string/settings_advanced_custom_hosts_summary"
app:activity="com.hippo.ehviewer.ui.HostsActivity"/>

<Preference
<com.hippo.ehviewer.preference.ExportDataPreference
android:key="export_data"
android:title="@string/settings_advanced_export_data"
android:summary="@string/settings_advanced_export_data_summary"/>
Expand Down

0 comments on commit d72f686

Please sign in to comment.