From cef791ba1baa639476ebc00a8060d5f4ac1c07a1 Mon Sep 17 00:00:00 2001 From: XiangRongLin <41164160+XiangRongLin@users.noreply.github.com> Date: Sat, 19 Dec 2020 14:02:34 +0100 Subject: [PATCH] Introduce NewPipeFileLocator class It handles locating specific NewPipe files based on the home directory of the app. --- .../settings/ContentSettingsFragment.java | 2 +- .../settings/ContentSettingsManager.kt | 68 ++++++++----------- .../newpipe/settings/NewPipeFileLocator.kt | 22 ++++++ 3 files changed, 51 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/settings/NewPipeFileLocator.kt diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java index 1d9b8e102..f783df85e 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsFragment.java @@ -129,7 +129,7 @@ public class ContentSettingsFragment extends BasePreferenceFragment { newpipeSettings = new File(homeDir, "/databases/newpipe.settings"); newpipeSettings.delete(); - manager = new ContentSettingsManager(homeDir); + manager = new ContentSettingsManager(new NewPipeFileLocator(homeDir)); addPreferencesFromResource(R.xml.content_settings); diff --git a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt index 2a2c4df20..5e92f59bf 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt +++ b/app/src/main/java/org/schabi/newpipe/settings/ContentSettingsManager.kt @@ -1,10 +1,8 @@ package org.schabi.newpipe.settings import android.content.SharedPreferences -import androidx.preference.PreferenceManager import org.schabi.newpipe.util.ZipHelper import java.io.BufferedOutputStream -import java.io.File import java.io.FileInputStream import java.io.FileOutputStream import java.io.IOException @@ -13,23 +11,7 @@ import java.io.ObjectOutputStream import java.util.zip.ZipFile import java.util.zip.ZipOutputStream -class ContentSettingsManager( - 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") - ) +class ContentSettingsManager(private val fileLocator: NewPipeFileLocator) { /** * Exports given [SharedPreferences] to the file in given outputPath. @@ -38,19 +20,19 @@ class ContentSettingsManager( @Throws(Exception::class) fun exportDatabase(preferences: SharedPreferences, outputPath: String) { ZipOutputStream(BufferedOutputStream(FileOutputStream(outputPath))) - .use { outZip -> - ZipHelper.addFileToZip(outZip, newpipeDb.path, "newpipe.db") + .use { outZip -> + ZipHelper.addFileToZip(outZip, fileLocator.dbDir.path, "newpipe.db") - try { - ObjectOutputStream(FileOutputStream(newpipeSettings)).use { output -> - output.writeObject(preferences.all) - output.flush() + try { + ObjectOutputStream(FileOutputStream(fileLocator.settings)).use { output -> + output.writeObject(preferences.all) + output.flush() + } + } catch (e: IOException) { + e.printStackTrace() } - } catch (e: IOException) { - 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. */ 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) { - newpipeDbJournal.delete() - newpipeDbWal.delete() - newpipeDbShm.delete() + fileLocator.dbJournal.delete() + fileLocator.dbWal.delete() + fileLocator.dbShm.delete() } return success } - fun containSettings(filePath: String): Boolean { + fun containSettings( + filePath: String, + ): Boolean { return ZipHelper - .extractFileFromZip(filePath, newpipeSettings.path, "newpipe.settings") + .extractFileFromZip(filePath, fileLocator.settings.path, "newpipe.settings") } - fun loadSharedPreferences(preferences: SharedPreferences) { + fun loadSharedPreferences( + preferences: SharedPreferences, + ) { try { val preferenceEditor = preferences.edit() - ObjectInputStream(FileInputStream(newpipeSettings)).use { input -> + ObjectInputStream(FileInputStream(fileLocator.settings)).use { input -> preferenceEditor.clear() val entries = input.readObject() as Map for ((key, value) in entries) { @@ -123,5 +112,4 @@ class ContentSettingsManager( e.printStackTrace() } } - } diff --git a/app/src/main/java/org/schabi/newpipe/settings/NewPipeFileLocator.kt b/app/src/main/java/org/schabi/newpipe/settings/NewPipeFileLocator.kt new file mode 100644 index 000000000..1fe2fffa0 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/settings/NewPipeFileLocator.kt @@ -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") } + +}