2020-12-01 19:12:42 +00:00
|
|
|
package org.schabi.newpipe.settings
|
|
|
|
|
|
|
|
import android.content.SharedPreferences
|
2021-11-05 17:04:49 +00:00
|
|
|
import android.util.Log
|
2020-06-13 15:29:57 +00:00
|
|
|
import org.schabi.newpipe.streams.io.SharpOutputStream
|
2020-06-15 14:26:52 +00:00
|
|
|
import org.schabi.newpipe.streams.io.StoredFileHelper
|
2020-12-01 19:12:42 +00:00
|
|
|
import org.schabi.newpipe.util.ZipHelper
|
|
|
|
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
|
|
|
|
import java.util.zip.ZipOutputStream
|
|
|
|
|
2020-12-19 13:02:34 +00:00
|
|
|
class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
|
2021-11-05 17:04:49 +00:00
|
|
|
companion object {
|
|
|
|
const val TAG = "ContentSetManager"
|
|
|
|
}
|
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)
|
2020-06-13 15:29:57 +00:00
|
|
|
fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) {
|
|
|
|
file.create()
|
2022-07-18 00:51:51 +00:00
|
|
|
ZipOutputStream(SharpOutputStream(file.stream).buffered())
|
2020-12-19 15:54:42 +00:00
|
|
|
.use { outZip ->
|
|
|
|
ZipHelper.addFileToZip(outZip, fileLocator.db.path, "newpipe.db")
|
2020-12-01 19:12:42 +00:00
|
|
|
|
2020-12-19 15:54:42 +00:00
|
|
|
try {
|
2022-07-18 00:51:51 +00:00
|
|
|
ObjectOutputStream(fileLocator.settings.outputStream()).use { output ->
|
2020-12-19 15:54:42 +00:00
|
|
|
output.writeObject(preferences.all)
|
|
|
|
output.flush()
|
2020-12-01 19:12:42 +00:00
|
|
|
}
|
2020-12-19 15:54:42 +00:00
|
|
|
} catch (e: IOException) {
|
2021-11-05 17:04:49 +00:00
|
|
|
Log.e(TAG, "Unable to exportDatabase", e)
|
2020-12-19 15:54:42 +00:00
|
|
|
}
|
2020-12-01 19:12:42 +00:00
|
|
|
|
2020-12-19 15:54:42 +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
|
|
|
|
2020-12-19 15:35:30 +00:00
|
|
|
fun deleteSettingsFile() {
|
|
|
|
fileLocator.settings.delete()
|
|
|
|
}
|
|
|
|
|
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 15:53:11 +00:00
|
|
|
return fileLocator.dbDir.exists() || fileLocator.dbDir.mkdir()
|
2020-12-19 10:22:25 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 15:29:57 +00:00
|
|
|
fun extractDb(file: StoredFileHelper): Boolean {
|
|
|
|
val success = ZipHelper.extractFileFromZip(file, 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-06-13 15:29:57 +00:00
|
|
|
fun extractSettings(file: StoredFileHelper): Boolean {
|
|
|
|
return ZipHelper.extractFileFromZip(file, fileLocator.settings.path, "newpipe.settings")
|
2020-12-19 10:22:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-19 14:16:22 +00:00
|
|
|
fun loadSharedPreferences(preferences: SharedPreferences) {
|
2020-12-19 10:22:25 +00:00
|
|
|
try {
|
|
|
|
val preferenceEditor = preferences.edit()
|
|
|
|
|
2022-07-18 00:51:51 +00:00
|
|
|
ObjectInputStream(fileLocator.settings.inputStream()).use { input ->
|
2020-12-19 10:22:25 +00:00
|
|
|
preferenceEditor.clear()
|
2020-12-19 15:54:42 +00:00
|
|
|
@Suppress("UNCHECKED_CAST")
|
2020-12-19 10:22:25 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-09-26 20:15:33 +00:00
|
|
|
is Set<*> -> {
|
|
|
|
// There are currently only Sets with type String possible
|
2021-09-25 10:07:07 +00:00
|
|
|
@Suppress("UNCHECKED_CAST")
|
2021-09-26 20:15:33 +00:00
|
|
|
preferenceEditor.putStringSet(key, value as Set<String>?)
|
2021-09-25 10:07:07 +00:00
|
|
|
}
|
2020-12-19 10:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
preferenceEditor.commit()
|
|
|
|
}
|
|
|
|
} catch (e: IOException) {
|
2021-11-05 17:04:49 +00:00
|
|
|
Log.e(TAG, "Unable to loadSharedPreferences", e)
|
2020-12-19 10:22:25 +00:00
|
|
|
} catch (e: ClassNotFoundException) {
|
2021-11-05 17:04:49 +00:00
|
|
|
Log.e(TAG, "Unable to loadSharedPreferences", e)
|
2020-12-19 10:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-01 19:12:42 +00:00
|
|
|
}
|