Skip to content
This repository has been archived by the owner on Aug 26, 2019. It is now read-only.

Commit

Permalink
use SAF eport data in Kikat or later
Browse files Browse the repository at this point in the history
  • Loading branch information
onlymash committed Mar 26, 2018
1 parent 1c62fe9 commit c539fab
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 9 deletions.
39 changes: 39 additions & 0 deletions app/src/main/java/com/hippo/ehviewer/EhDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@

package com.hippo.ehviewer;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
Expand Down Expand Up @@ -51,6 +55,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -650,6 +655,40 @@ public static synchronized boolean exportDB(Context context, File file) {
return false;
}

public static synchronized boolean exportDB(Context context, Uri uri) {
File dbFile = context.getDatabasePath("eh.db");
if (null == dbFile || !dbFile.isFile()) {
return false;
}
if (null == uri) {
return false;
}
InputStream is = null;
OutputStream os = null;
ContentResolver resolver = context.getContentResolver();
try {
is = new FileInputStream(dbFile);
os = resolver.openOutputStream(uri);
IOUtils.copy(is, os);
return true;
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}
// Delete failed file
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
try {
DocumentsContract.deleteDocument(resolver, uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

return false;
}

/**
* @param file The db file
* @return error string, null for no error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.text.TextUtils;
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;
import com.hippo.util.ReadableTime;

Expand All @@ -45,6 +46,8 @@ public class AdvancedFragment extends PreferenceFragment implements Preference.O
private static final String KEY_EXPORT_DATA = "export_data";
private static final String KEY_IMPORT_DATA = "import_data";

private static final int WRITE_REQUEST_CODE = 43;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -88,13 +91,20 @@ public boolean onPreferenceClick(Preference preference) {
((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;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
String filename = ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".db";
exportData("*/*", filename);
return true;

}else {
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();
Expand All @@ -107,6 +117,30 @@ public boolean onPreferenceClick(Preference preference) {
return false;
}

private void exportData(String mimeType, String fileName){
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(mimeType);
intent.putExtra(Intent.EXTRA_TITLE, fileName);
startActivityForResult(intent, WRITE_REQUEST_CODE);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {

if (requestCode == WRITE_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
if (EhDB.exportDB(getActivity(), uri)) {
Toast.makeText(getActivity(),
getString(R.string.settings_advanced_export_data_to, uri.toString()), Toast.LENGTH_SHORT).show();
}
}
}
}

private static void importData(final Context context) {
final File dir = AppConfig.getExternalDataDir();
if (null == dir) {
Expand Down

0 comments on commit c539fab

Please sign in to comment.