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

Commit

Permalink
use SAF import data in Kikat or later
Browse files Browse the repository at this point in the history
  • Loading branch information
onlymash committed Mar 24, 2018
1 parent 7bc82bc commit 615985b
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 21 deletions.
100 changes: 100 additions & 0 deletions app/src/main/java/com/hippo/ehviewer/EhDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -770,4 +770,104 @@ public static synchronized String importDB(Context context, File file) {
return context.getString(R.string.cant_read_the_file);
}
}

/**
* @param uri The db uri
* @return error string, null for no error
*/
public static synchronized String importDB(Context context, Uri uri) {
try {
File file = new File(context.getCacheDir().getPath()+"/import.db");

InputStream is = null;
OutputStream os = null;
ContentResolver resolver = context.getContentResolver();
try {
is = resolver.openInputStream(uri);
os = new FileOutputStream(file);
IOUtils.copy(is, os);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}

SQLiteDatabase db = SQLiteDatabase.openDatabase(
file.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
int newVersion = DaoMaster.SCHEMA_VERSION;
int oldVersion = db.getVersion();
if (oldVersion < newVersion) {
upgradeDB(db, oldVersion);
db.setVersion(newVersion);
} else if (oldVersion > newVersion) {
return context.getString(R.string.cant_read_the_file);
}

DaoMaster daoMaster = new DaoMaster(db);
DaoSession session = daoMaster.newSession();

// Downloads
DownloadManager manager = EhApplication.getDownloadManager(context);
List<DownloadInfo> downloadInfoList = session.getDownloadsDao().queryBuilder().list();
manager.addDownload(downloadInfoList);

// Download label
List<DownloadLabel> downloadLabelList = session.getDownloadLabelDao().queryBuilder().list();
manager.addDownloadLabel(downloadLabelList);

// Download dirname
List<DownloadDirname> downloadDirnameList = session.getDownloadDirnameDao().queryBuilder().list();
for (DownloadDirname dirname: downloadDirnameList) {
putDownloadDirname(dirname.getGid(), dirname.getDirname());
}

// History
List<HistoryInfo> historyInfoList = session.getHistoryDao().queryBuilder().list();
putHistoryInfo(historyInfoList);

// QuickSearch
List<QuickSearch> quickSearchList = session.getQuickSearchDao().queryBuilder().list();
List<QuickSearch> currentQuickSearchList = sDaoSession.getQuickSearchDao().queryBuilder().list();
for (QuickSearch quickSearch: quickSearchList) {
String name = quickSearch.name;
for (QuickSearch q: currentQuickSearchList) {
if (ObjectUtils.equal(q.name, name)) {
// The same name
name = null;
break;
}
}
if (null == name) {
continue;
}
insertQuickSearch(quickSearch);
}

// LocalFavorites
List<LocalFavoriteInfo> localFavoriteInfoList = session.getLocalFavoritesDao().queryBuilder().list();
for (LocalFavoriteInfo info: localFavoriteInfoList) {
putLocalFavorites(info);
}

// Bookmarks
// TODO

// Filter
List<Filter> filterList = session.getFilterDao().queryBuilder().list();
List<Filter> currentFilterList = sDaoSession.getFilterDao().queryBuilder().list();
for (Filter filter: filterList) {
if (!currentFilterList.contains(filter)) {
addFilter(filter);
}
}

file.delete();

return null;
} catch (Exception e) {
// Ignore
return context.getString(R.string.cant_read_the_file);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ 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 READ_REQUEST_CODE = 42;
private static final int WRITE_REQUEST_CODE = 43;

@Override
Expand Down Expand Up @@ -139,30 +140,51 @@ public void onActivityResult(int requestCode, int resultCode, Intent resultData)
}
}
}
}

private static void importData(final Context context) {
final File dir = AppConfig.getExternalDataDir();
if (null == dir) {
Toast.makeText(context, R.string.cant_get_data_dir, Toast.LENGTH_SHORT).show();
return;
}
final String[] files = dir.list();
if (null == files || files.length <= 0) {
Toast.makeText(context, R.string.cant_find_any_data, Toast.LENGTH_SHORT).show();
return;
}
Arrays.sort(files);
new AlertDialog.Builder(context).setItems(files, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File file = new File(dir, files[which]);
String error = EhDB.importDB(context, file);
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
String error = EhDB.importDB(getActivity(), uri);
if (null == error) {
error = context.getString(R.string.settings_advanced_import_data_successfully);
error = getString(R.string.settings_advanced_import_data_successfully);
}
Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
Toast.makeText(getActivity(), error, Toast.LENGTH_SHORT).show();
}
}
}

private void importData(final Context context) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, READ_REQUEST_CODE);

}else {
final File dir = AppConfig.getExternalDataDir();
if (null == dir) {
Toast.makeText(context, R.string.cant_get_data_dir, Toast.LENGTH_SHORT).show();
return;
}
}).show();
final String[] files = dir.list();
if (null == files || files.length <= 0) {
Toast.makeText(context, R.string.cant_find_any_data, Toast.LENGTH_SHORT).show();
return;
}
Arrays.sort(files);
new AlertDialog.Builder(context).setItems(files, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File file = new File(dir, files[which]);
String error = EhDB.importDB(context, file);
if (null == error) {
error = context.getString(R.string.settings_advanced_import_data_successfully);
}
Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
}
}).show();
}
}
}

0 comments on commit 615985b

Please sign in to comment.