kopia lustrzana https://github.com/TeamNewPipe/NewPipe
Extract import database logic in ContentSettingsManager
rodzic
68175c1cf0
commit
f78a7fa630
|
@ -224,33 +224,24 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
|||
|
||||
private void importDatabase(final String filePath) {
|
||||
// check if file is supported
|
||||
try (ZipFile zipFile = new ZipFile(filePath)) {
|
||||
} catch (final IOException ioe) {
|
||||
if (!manager.isValidZipFile(filePath)) {
|
||||
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
|
||||
.show();
|
||||
.show();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if (!databasesDir.exists() && !databasesDir.mkdir()) {
|
||||
if (!manager.ensureDbDirectoryExists()) {
|
||||
throw new Exception("Could not create databases dir");
|
||||
}
|
||||
|
||||
final boolean isDbFileExtracted = ZipHelper.extractFileFromZip(filePath,
|
||||
newpipeDb.getPath(), "newpipe.db");
|
||||
|
||||
if (isDbFileExtracted) {
|
||||
newpipeDbJournal.delete();
|
||||
newpipeDbWal.delete();
|
||||
newpipeDbShm.delete();
|
||||
} else {
|
||||
if (!manager.extractDb(filePath)) {
|
||||
Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG)
|
||||
.show();
|
||||
.show();
|
||||
}
|
||||
|
||||
//If settings file exist, ask if it should be imported.
|
||||
if (ZipHelper.extractFileFromZip(filePath, newpipeSettings.getPath(),
|
||||
"newpipe.settings")) {
|
||||
if (manager.containSettings(filePath)) {
|
||||
final AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
|
||||
alert.setTitle(R.string.import_settings);
|
||||
|
||||
|
@ -261,7 +252,8 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
|||
});
|
||||
alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> {
|
||||
dialog.dismiss();
|
||||
loadSharedPreferences(newpipeSettings);
|
||||
manager.loadSharedPreferences(PreferenceManager
|
||||
.getDefaultSharedPreferences(requireContext()));
|
||||
// restart app to properly load db
|
||||
System.exit(0);
|
||||
});
|
||||
|
@ -275,34 +267,6 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
|||
}
|
||||
}
|
||||
|
||||
private void loadSharedPreferences(final File src) {
|
||||
try (ObjectInputStream input = new ObjectInputStream(new FileInputStream(src))) {
|
||||
final SharedPreferences.Editor prefEdit = PreferenceManager
|
||||
.getDefaultSharedPreferences(requireContext()).edit();
|
||||
prefEdit.clear();
|
||||
final Map<String, ?> entries = (Map<String, ?>) input.readObject();
|
||||
for (final Map.Entry<String, ?> entry : entries.entrySet()) {
|
||||
final Object v = entry.getValue();
|
||||
final String key = entry.getKey();
|
||||
|
||||
if (v instanceof Boolean) {
|
||||
prefEdit.putBoolean(key, (Boolean) v);
|
||||
} else if (v instanceof Float) {
|
||||
prefEdit.putFloat(key, (Float) v);
|
||||
} else if (v instanceof Integer) {
|
||||
prefEdit.putInt(key, (Integer) v);
|
||||
} else if (v instanceof Long) {
|
||||
prefEdit.putLong(key, (Long) v);
|
||||
} else if (v instanceof String) {
|
||||
prefEdit.putString(key, (String) v);
|
||||
}
|
||||
}
|
||||
prefEdit.commit();
|
||||
} catch (final IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////////////////
|
||||
// Error
|
||||
//////////////////////////////////////////////////////////////////////////*/
|
||||
|
|
|
@ -1,23 +1,34 @@
|
|||
package org.schabi.newpipe.settings
|
||||
|
||||
import android.content.SharedPreferences
|
||||
import androidx.preference.PreferenceManager
|
||||
import org.schabi.newpipe.util.ZipHelper
|
||||
import java.io.BufferedOutputStream
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.ObjectInputStream
|
||||
import java.io.ObjectOutputStream
|
||||
import java.lang.Exception
|
||||
import java.util.zip.ZipFile
|
||||
import java.util.zip.ZipOutputStream
|
||||
|
||||
class ContentSettingsManager(
|
||||
private val newpipeDb: File,
|
||||
private val newpipeSettings: File
|
||||
private val databasesDir: File,
|
||||
private val newpipeDb: File,
|
||||
private val newpipeDbJournal: File,
|
||||
private var newpipeDbShm: File,
|
||||
private val newpipeDbWal: File,
|
||||
private val newpipeSettings: File,
|
||||
) {
|
||||
|
||||
constructor(homeDir: File) : this(
|
||||
File(homeDir, "databases/newpipe.db"),
|
||||
File(homeDir, "databases/newpipe.settings")
|
||||
File(homeDir, "/databases"),
|
||||
File(homeDir, "/databases/newpipe.db"),
|
||||
File(homeDir, "/databases/newpipe.db-journal"),
|
||||
File(homeDir, "/databases/newpipe.db-shm"),
|
||||
File(homeDir, "/databases/newpipe.db-wal"),
|
||||
File(homeDir, "/databases/newpipe.settings")
|
||||
)
|
||||
|
||||
/**
|
||||
|
@ -42,4 +53,75 @@ class ContentSettingsManager(
|
|||
ZipHelper.addFileToZip(outZip, newpipeSettings.path, "newpipe.settings")
|
||||
}
|
||||
}
|
||||
|
||||
fun isValidZipFile(filePath: String): Boolean {
|
||||
try {
|
||||
ZipFile(filePath).use {
|
||||
return@isValidZipFile true
|
||||
}
|
||||
} catch (ioe: IOException) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to create database directory if it does not exist.
|
||||
*
|
||||
* @return Whether the directory exists afterwards.
|
||||
*/
|
||||
fun ensureDbDirectoryExists(): Boolean {
|
||||
return !databasesDir.exists() && !databasesDir.mkdir()
|
||||
}
|
||||
|
||||
fun extractDb(filePath: String): Boolean {
|
||||
val success = ZipHelper.extractFileFromZip(filePath, newpipeDb.path, "newpipe.db")
|
||||
if (success) {
|
||||
newpipeDbJournal.delete()
|
||||
newpipeDbWal.delete()
|
||||
newpipeDbShm.delete()
|
||||
}
|
||||
|
||||
return success
|
||||
}
|
||||
|
||||
fun containSettings(filePath: String): Boolean {
|
||||
return ZipHelper
|
||||
.extractFileFromZip(filePath, newpipeSettings.path, "newpipe.settings")
|
||||
}
|
||||
|
||||
fun loadSharedPreferences(preferences: SharedPreferences) {
|
||||
try {
|
||||
val preferenceEditor = preferences.edit()
|
||||
|
||||
ObjectInputStream(FileInputStream(newpipeSettings)).use { input ->
|
||||
preferenceEditor.clear()
|
||||
val entries = input.readObject() as Map<String, *>
|
||||
for ((key, value) in entries) {
|
||||
when (value) {
|
||||
is Boolean -> {
|
||||
preferenceEditor.putBoolean(key, value)
|
||||
}
|
||||
is Float -> {
|
||||
preferenceEditor.putFloat(key, value)
|
||||
}
|
||||
is Int -> {
|
||||
preferenceEditor.putInt(key, value)
|
||||
}
|
||||
is Long -> {
|
||||
preferenceEditor.putLong(key, value)
|
||||
}
|
||||
is String -> {
|
||||
preferenceEditor.putString(key, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
preferenceEditor.commit()
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
} catch (e: ClassNotFoundException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue