2020-12-01 19:12:42 +00:00
|
|
|
package org.schabi.newpipe.settings
|
|
|
|
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
import org.schabi.newpipe.util.ZipHelper
|
|
|
|
import java.io.BufferedOutputStream
|
2020-12-19 10:22:25 +00:00
|
|
|
import java.io.FileInputStream
|
2020-12-01 19:12:42 +00:00
|
|
|
import java.io.FileOutputStream
|
|
|
|
import java.io.IOException
|
2020-12-19 10:22:25 +00:00
|
|
|
import java.io.ObjectInputStream
|
2020-12-01 19:12:42 +00:00
|
|
|
import java.io.ObjectOutputStream
|
2020-12-19 10:22:25 +00:00
|
|
|
import java.util.zip.ZipFile
|
2020-12-01 19:12:42 +00:00
|
|
|
import java.util.zip.ZipOutputStream
|
|
|
|
|
2020-12-19 13:02:34 +00:00
|
|
|
class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
|
2020-12-01 19:12:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports given [SharedPreferences] to the file in given outputPath.
|
|
|
|
* It also creates the file.
|
|
|
|
*/
|
|
|
|
@Throws(Exception::class)
|
|
|
|
fun exportDatabase(preferences: SharedPreferences, outputPath: String) {
|
|
|
|
ZipOutputStream(BufferedOutputStream(FileOutputStream(outputPath)))
|
2020-12-19 13:02:34 +00:00
|
|
|
.use { outZip ->
|
2020-12-19 13:10:10 +00:00
|
|
|
ZipHelper.addFileToZip(outZip, fileLocator.db.path, "newpipe.db")
|
2020-12-01 19:12:42 +00:00
|
|
|
|
2020-12-19 13:02:34 +00:00
|
|
|
try {
|
|
|
|
ObjectOutputStream(FileOutputStream(fileLocator.settings)).use { output ->
|
|
|
|
output.writeObject(preferences.all)
|
|
|
|
output.flush()
|
|
|
|
}
|
|
|
|
} catch (e: IOException) {
|
|
|
|
e.printStackTrace()
|
2020-12-01 19:12:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 13:02:34 +00:00
|
|
|
ZipHelper.addFileToZip(outZip, fileLocator.settings.path, "newpipe.settings")
|
2020-12-01 19:12:42 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-19 10:22:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tries to create database directory if it does not exist.
|
|
|
|
*
|
|
|
|
* @return Whether the directory exists afterwards.
|
|
|
|
*/
|
|
|
|
fun ensureDbDirectoryExists(): Boolean {
|
2020-12-19 13:02:34 +00:00
|
|
|
return !fileLocator.dbDir.exists() && !fileLocator.dbDir.mkdir()
|
2020-12-19 10:22:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 13:02:34 +00:00
|
|
|
|
|
|
|
fun extractDb(
|
|
|
|
filePath: String,
|
|
|
|
): Boolean {
|
|
|
|
val success = ZipHelper.extractFileFromZip(filePath, fileLocator.db.path, "newpipe.db")
|
2020-12-19 10:22:25 +00:00
|
|
|
if (success) {
|
2020-12-19 13:02:34 +00:00
|
|
|
fileLocator.dbJournal.delete()
|
|
|
|
fileLocator.dbWal.delete()
|
|
|
|
fileLocator.dbShm.delete()
|
2020-12-19 10:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return success
|
|
|
|
}
|
|
|
|
|
2020-12-19 13:02:34 +00:00
|
|
|
fun containSettings(
|
|
|
|
filePath: String,
|
|
|
|
): Boolean {
|
2020-12-19 10:22:25 +00:00
|
|
|
return ZipHelper
|
2020-12-19 13:02:34 +00:00
|
|
|
.extractFileFromZip(filePath, fileLocator.settings.path, "newpipe.settings")
|
2020-12-19 10:22:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 13:02:34 +00:00
|
|
|
fun loadSharedPreferences(
|
|
|
|
preferences: SharedPreferences,
|
|
|
|
) {
|
2020-12-19 10:22:25 +00:00
|
|
|
try {
|
|
|
|
val preferenceEditor = preferences.edit()
|
|
|
|
|
2020-12-19 13:02:34 +00:00
|
|
|
ObjectInputStream(FileInputStream(fileLocator.settings)).use { input ->
|
2020-12-19 10:22:25 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
2020-12-01 19:12:42 +00:00
|
|
|
}
|