NewPipe/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt

114 lines
4.0 KiB
Kotlin
Raw Normal View History

package org.schabi.newpipe.settings
import android.content.SharedPreferences
import android.util.Log
2020-06-13 17:29:57 +02:00
import org.schabi.newpipe.streams.io.SharpOutputStream
2020-06-15 16:26:52 +02:00
import org.schabi.newpipe.streams.io.StoredFileHelper
import org.schabi.newpipe.util.ZipHelper
import java.io.BufferedOutputStream
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.util.zip.ZipOutputStream
class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
companion object {
const val TAG = "ContentSetManager"
}
/**
* Exports given [SharedPreferences] to the file in given outputPath.
* It also creates the file.
*/
@Throws(Exception::class)
2020-06-13 17:29:57 +02:00
fun exportDatabase(preferences: SharedPreferences, file: StoredFileHelper) {
file.create()
ZipOutputStream(BufferedOutputStream(SharpOutputStream(file.stream)))
2020-12-19 16:54:42 +01:00
.use { outZip ->
ZipHelper.addFileToZip(outZip, fileLocator.db.path, "newpipe.db")
2020-12-19 16:54:42 +01:00
try {
ObjectOutputStream(FileOutputStream(fileLocator.settings)).use { output ->
output.writeObject(preferences.all)
output.flush()
}
2020-12-19 16:54:42 +01:00
} catch (e: IOException) {
Log.e(TAG, "Unable to exportDatabase", e)
2020-12-19 16:54:42 +01:00
}
2020-12-19 16:54:42 +01:00
ZipHelper.addFileToZip(outZip, fileLocator.settings.path, "newpipe.settings")
}
}
2020-12-19 16:35:30 +01:00
fun deleteSettingsFile() {
fileLocator.settings.delete()
}
/**
* Tries to create database directory if it does not exist.
*
* @return Whether the directory exists afterwards.
*/
fun ensureDbDirectoryExists(): Boolean {
2020-12-19 16:53:11 +01:00
return fileLocator.dbDir.exists() || fileLocator.dbDir.mkdir()
}
2020-06-13 17:29:57 +02:00
fun extractDb(file: StoredFileHelper): Boolean {
val success = ZipHelper.extractFileFromZip(file, fileLocator.db.path, "newpipe.db")
if (success) {
fileLocator.dbJournal.delete()
fileLocator.dbWal.delete()
fileLocator.dbShm.delete()
}
return success
}
2020-06-13 17:29:57 +02:00
fun extractSettings(file: StoredFileHelper): Boolean {
return ZipHelper.extractFileFromZip(file, fileLocator.settings.path, "newpipe.settings")
}
2020-12-19 15:16:22 +01:00
fun loadSharedPreferences(preferences: SharedPreferences) {
try {
val preferenceEditor = preferences.edit()
ObjectInputStream(FileInputStream(fileLocator.settings)).use { input ->
preferenceEditor.clear()
2020-12-19 16:54:42 +01:00
@Suppress("UNCHECKED_CAST")
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 22:15:33 +02:00
is Set<*> -> {
// There are currently only Sets with type String possible
2021-09-25 12:07:07 +02:00
@Suppress("UNCHECKED_CAST")
2021-09-26 22:15:33 +02:00
preferenceEditor.putStringSet(key, value as Set<String>?)
2021-09-25 12:07:07 +02:00
}
}
}
preferenceEditor.commit()
}
} catch (e: IOException) {
Log.e(TAG, "Unable to loadSharedPreferences", e)
} catch (e: ClassNotFoundException) {
Log.e(TAG, "Unable to loadSharedPreferences", e)
}
}
}