mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-10-26 12:57:39 +00:00
Extract import database logic in ContentSettingsManager
This commit is contained in:
@@ -224,33 +224,24 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
|||||||
|
|
||||||
private void importDatabase(final String filePath) {
|
private void importDatabase(final String filePath) {
|
||||||
// check if file is supported
|
// check if file is supported
|
||||||
try (ZipFile zipFile = new ZipFile(filePath)) {
|
if (!manager.isValidZipFile(filePath)) {
|
||||||
} catch (final IOException ioe) {
|
|
||||||
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
|
Toast.makeText(getContext(), R.string.no_valid_zip_file, Toast.LENGTH_SHORT)
|
||||||
.show();
|
.show();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (!databasesDir.exists() && !databasesDir.mkdir()) {
|
if (!manager.ensureDbDirectoryExists()) {
|
||||||
throw new Exception("Could not create databases dir");
|
throw new Exception("Could not create databases dir");
|
||||||
}
|
}
|
||||||
|
|
||||||
final boolean isDbFileExtracted = ZipHelper.extractFileFromZip(filePath,
|
if (!manager.extractDb(filePath)) {
|
||||||
newpipeDb.getPath(), "newpipe.db");
|
|
||||||
|
|
||||||
if (isDbFileExtracted) {
|
|
||||||
newpipeDbJournal.delete();
|
|
||||||
newpipeDbWal.delete();
|
|
||||||
newpipeDbShm.delete();
|
|
||||||
} else {
|
|
||||||
Toast.makeText(getContext(), R.string.could_not_import_all_files, Toast.LENGTH_LONG)
|
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 settings file exist, ask if it should be imported.
|
||||||
if (ZipHelper.extractFileFromZip(filePath, newpipeSettings.getPath(),
|
if (manager.containSettings(filePath)) {
|
||||||
"newpipe.settings")) {
|
|
||||||
final AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
|
final AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
|
||||||
alert.setTitle(R.string.import_settings);
|
alert.setTitle(R.string.import_settings);
|
||||||
|
|
||||||
@@ -261,7 +252,8 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
|
|||||||
});
|
});
|
||||||
alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> {
|
alert.setPositiveButton(getString(R.string.finish), (dialog, which) -> {
|
||||||
dialog.dismiss();
|
dialog.dismiss();
|
||||||
loadSharedPreferences(newpipeSettings);
|
manager.loadSharedPreferences(PreferenceManager
|
||||||
|
.getDefaultSharedPreferences(requireContext()));
|
||||||
// restart app to properly load db
|
// restart app to properly load db
|
||||||
System.exit(0);
|
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
|
// Error
|
||||||
//////////////////////////////////////////////////////////////////////////*/
|
//////////////////////////////////////////////////////////////////////////*/
|
||||||
|
|||||||
@@ -1,23 +1,34 @@
|
|||||||
package org.schabi.newpipe.settings
|
package org.schabi.newpipe.settings
|
||||||
|
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
import org.schabi.newpipe.util.ZipHelper
|
import org.schabi.newpipe.util.ZipHelper
|
||||||
import java.io.BufferedOutputStream
|
import java.io.BufferedOutputStream
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.FileInputStream
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
import java.io.ObjectInputStream
|
||||||
import java.io.ObjectOutputStream
|
import java.io.ObjectOutputStream
|
||||||
import java.lang.Exception
|
import java.util.zip.ZipFile
|
||||||
import java.util.zip.ZipOutputStream
|
import java.util.zip.ZipOutputStream
|
||||||
|
|
||||||
class ContentSettingsManager(
|
class ContentSettingsManager(
|
||||||
|
private val databasesDir: File,
|
||||||
private val newpipeDb: File,
|
private val newpipeDb: File,
|
||||||
private val newpipeSettings: File
|
private val newpipeDbJournal: File,
|
||||||
|
private var newpipeDbShm: File,
|
||||||
|
private val newpipeDbWal: File,
|
||||||
|
private val newpipeSettings: File,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
constructor(homeDir: File) : this(
|
constructor(homeDir: File) : this(
|
||||||
File(homeDir, "databases/newpipe.db"),
|
File(homeDir, "/databases"),
|
||||||
File(homeDir, "databases/newpipe.settings")
|
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")
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user