1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-07-01 01:23:19 +00:00

Introduce NewPipeFileLocator class

It handles locating specific NewPipe files based on the home directory of the app.
This commit is contained in:
XiangRongLin 2020-12-19 14:02:34 +01:00
parent f78a7fa630
commit cef791ba1b
3 changed files with 51 additions and 41 deletions

View File

@ -129,7 +129,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment {
newpipeSettings = new File(homeDir, "/databases/newpipe.settings"); newpipeSettings = new File(homeDir, "/databases/newpipe.settings");
newpipeSettings.delete(); newpipeSettings.delete();
manager = new ContentSettingsManager(homeDir); manager = new ContentSettingsManager(new NewPipeFileLocator(homeDir));
addPreferencesFromResource(R.xml.content_settings); addPreferencesFromResource(R.xml.content_settings);

View File

@ -1,10 +1,8 @@
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.FileInputStream import java.io.FileInputStream
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.IOException import java.io.IOException
@ -13,23 +11,7 @@ import java.io.ObjectOutputStream
import java.util.zip.ZipFile import java.util.zip.ZipFile
import java.util.zip.ZipOutputStream import java.util.zip.ZipOutputStream
class ContentSettingsManager( class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) {
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"),
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")
)
/** /**
* Exports given [SharedPreferences] to the file in given outputPath. * Exports given [SharedPreferences] to the file in given outputPath.
@ -39,10 +21,10 @@ class ContentSettingsManager(
fun exportDatabase(preferences: SharedPreferences, outputPath: String) { fun exportDatabase(preferences: SharedPreferences, outputPath: String) {
ZipOutputStream(BufferedOutputStream(FileOutputStream(outputPath))) ZipOutputStream(BufferedOutputStream(FileOutputStream(outputPath)))
.use { outZip -> .use { outZip ->
ZipHelper.addFileToZip(outZip, newpipeDb.path, "newpipe.db") ZipHelper.addFileToZip(outZip, fileLocator.dbDir.path, "newpipe.db")
try { try {
ObjectOutputStream(FileOutputStream(newpipeSettings)).use { output -> ObjectOutputStream(FileOutputStream(fileLocator.settings)).use { output ->
output.writeObject(preferences.all) output.writeObject(preferences.all)
output.flush() output.flush()
} }
@ -50,7 +32,7 @@ class ContentSettingsManager(
e.printStackTrace() e.printStackTrace()
} }
ZipHelper.addFileToZip(outZip, newpipeSettings.path, "newpipe.settings") ZipHelper.addFileToZip(outZip, fileLocator.settings.path, "newpipe.settings")
} }
} }
@ -70,30 +52,37 @@ class ContentSettingsManager(
* @return Whether the directory exists afterwards. * @return Whether the directory exists afterwards.
*/ */
fun ensureDbDirectoryExists(): Boolean { fun ensureDbDirectoryExists(): Boolean {
return !databasesDir.exists() && !databasesDir.mkdir() return !fileLocator.dbDir.exists() && !fileLocator.dbDir.mkdir()
} }
fun extractDb(filePath: String): Boolean {
val success = ZipHelper.extractFileFromZip(filePath, newpipeDb.path, "newpipe.db") fun extractDb(
filePath: String,
): Boolean {
val success = ZipHelper.extractFileFromZip(filePath, fileLocator.db.path, "newpipe.db")
if (success) { if (success) {
newpipeDbJournal.delete() fileLocator.dbJournal.delete()
newpipeDbWal.delete() fileLocator.dbWal.delete()
newpipeDbShm.delete() fileLocator.dbShm.delete()
} }
return success return success
} }
fun containSettings(filePath: String): Boolean { fun containSettings(
filePath: String,
): Boolean {
return ZipHelper return ZipHelper
.extractFileFromZip(filePath, newpipeSettings.path, "newpipe.settings") .extractFileFromZip(filePath, fileLocator.settings.path, "newpipe.settings")
} }
fun loadSharedPreferences(preferences: SharedPreferences) { fun loadSharedPreferences(
preferences: SharedPreferences,
) {
try { try {
val preferenceEditor = preferences.edit() val preferenceEditor = preferences.edit()
ObjectInputStream(FileInputStream(newpipeSettings)).use { input -> ObjectInputStream(FileInputStream(fileLocator.settings)).use { input ->
preferenceEditor.clear() preferenceEditor.clear()
val entries = input.readObject() as Map<String, *> val entries = input.readObject() as Map<String, *>
for ((key, value) in entries) { for ((key, value) in entries) {
@ -123,5 +112,4 @@ class ContentSettingsManager(
e.printStackTrace() e.printStackTrace()
} }
} }
} }

View File

@ -0,0 +1,22 @@
package org.schabi.newpipe.settings
import java.io.File
/**
* Locates specific files of NewPipe based on the home directory of the app.
*/
class NewPipeFileLocator(private val homeDir: File) {
val dbDir by lazy { File(homeDir, "/databases") }
val db by lazy { File(homeDir, "/databases/newpipe.db") }
val dbJournal by lazy { File(homeDir, "/databases/newpipe.db-journal") }
val dbShm by lazy { File(homeDir, "/databases/newpipe.db-shm") }
val dbWal by lazy { File(homeDir, "/databases/newpipe.db-wal") }
val settings by lazy { File(homeDir, "/databases/newpipe.settings") }
}