From 69f452d7c4e2a13a8ec793959438d7a18b8c1784 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Thu, 22 Sep 2022 18:18:10 +0200 Subject: [PATCH] move the preference backup to the unified backup dialog --- .../com/github/libretube/obj/BackupFile.kt | 3 +- .../github/libretube/obj/PreferenceItem.kt | 6 ++ .../libretube/ui/dialogs/BackupDialog.kt | 15 +++- .../ui/preferences/AdvancedSettings.kt | 31 ------- .../com/github/libretube/util/BackupHelper.kt | 84 ++++++------------- .../github/libretube/util/PreferenceHelper.kt | 5 +- app/src/main/res/xml/advanced_settings.xml | 14 ---- 7 files changed, 49 insertions(+), 109 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/obj/PreferenceItem.kt diff --git a/app/src/main/java/com/github/libretube/obj/BackupFile.kt b/app/src/main/java/com/github/libretube/obj/BackupFile.kt index 3ab5320ce..5d6770956 100644 --- a/app/src/main/java/com/github/libretube/obj/BackupFile.kt +++ b/app/src/main/java/com/github/libretube/obj/BackupFile.kt @@ -11,5 +11,6 @@ data class BackupFile( var watchPositions: List? = null, var searchHistory: List? = null, var localSubscriptions: List? = null, - var customInstances: List? = null + var customInstances: List? = null, + var preferences: List? = null ) diff --git a/app/src/main/java/com/github/libretube/obj/PreferenceItem.kt b/app/src/main/java/com/github/libretube/obj/PreferenceItem.kt new file mode 100644 index 000000000..7c547fea2 --- /dev/null +++ b/app/src/main/java/com/github/libretube/obj/PreferenceItem.kt @@ -0,0 +1,6 @@ +package com.github.libretube.obj + +data class PreferenceItem( + val key: String? = null, + val value: Any? = null +) diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt index 8474ea0a3..494eb63c7 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt @@ -8,7 +8,9 @@ import com.github.libretube.R import com.github.libretube.databinding.DialogBackupBinding import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.obj.BackupFile +import com.github.libretube.obj.PreferenceItem import com.github.libretube.ui.adapters.BackupOptionsAdapter +import com.github.libretube.util.PreferenceHelper import com.google.android.material.dialog.MaterialAlertDialogBuilder class BackupDialog( @@ -24,10 +26,11 @@ class BackupDialog( R.string.watch_positions, R.string.search_history, R.string.local_subscriptions, - R.string.backup_customInstances + R.string.backup_customInstances, + R.string.preferences ) - val selected = mutableListOf(false, false, false, false, false) + val selected = MutableList(backupOptions.size) { false } binding = DialogBackupBinding.inflate(layoutInflater) binding.backupOptionsRecycler.layoutManager = LinearLayoutManager(context) @@ -62,6 +65,14 @@ class BackupDialog( backupFile.customInstances = Database.customInstanceDao().getAll() } + if (selected[5]) { + backupFile.preferences = PreferenceHelper.settings.all.map { + PreferenceItem( + it.key, + it.value + ) + } + } } thread.start() thread.join() diff --git a/app/src/main/java/com/github/libretube/ui/preferences/AdvancedSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/AdvancedSettings.kt index bbf458ee9..55f34d94f 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/AdvancedSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/AdvancedSettings.kt @@ -20,28 +20,12 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder class AdvancedSettings : MaterialPreferenceFragment() { - // backup and restore prefs - private lateinit var getPrefFile: ActivityResultLauncher - private lateinit var createPrefFile: ActivityResultLauncher - // backup and restore database private lateinit var getBackupFile: ActivityResultLauncher private lateinit var createBackupFile: ActivityResultLauncher private var backupFile = BackupFile() override fun onCreate(savedInstanceState: Bundle?) { - getPrefFile = - registerForActivityResult( - ActivityResultContracts.GetContent() - ) { uri: Uri? -> - BackupHelper(requireContext()).restoreSharedPreferences(uri) - } - createPrefFile = registerForActivityResult( - CreateDocument("application/json") - ) { uri: Uri? -> - BackupHelper(requireContext()).backupSharedPreferences(uri) - } - getBackupFile = registerForActivityResult( ActivityResultContracts.GetContent() @@ -76,21 +60,6 @@ class AdvancedSettings : MaterialPreferenceFragment() { true } - val backupSettings = findPreference(PreferenceKeys.BACKUP_SETTINGS) - backupSettings?.setOnPreferenceClickListener { - createPrefFile.launch("preferences.xml") - true - } - - val restoreSettings = findPreference(PreferenceKeys.RESTORE_SETTINGS) - restoreSettings?.setOnPreferenceClickListener { - getPrefFile.launch("*/*") - // reset the token - PreferenceHelper.setToken("") - activity?.recreate() - true - } - val advancesBackup = findPreference("backup") advancesBackup?.setOnPreferenceClickListener { BackupDialog { diff --git a/app/src/main/java/com/github/libretube/util/BackupHelper.kt b/app/src/main/java/com/github/libretube/util/BackupHelper.kt index 899072cf2..3c18c59df 100644 --- a/app/src/main/java/com/github/libretube/util/BackupHelper.kt +++ b/app/src/main/java/com/github/libretube/util/BackupHelper.kt @@ -8,69 +8,15 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.extensions.query import com.github.libretube.obj.BackupFile -import java.io.FileInputStream +import com.github.libretube.obj.PreferenceItem import java.io.FileOutputStream -import java.io.ObjectInputStream -import java.io.ObjectOutputStream /** * Backup and restore the preferences */ class BackupHelper(private val context: Context) { /** - * Backup the default shared preferences to a file - */ - fun backupSharedPreferences(uri: Uri?) { - if (uri == null) return - try { - context.contentResolver.openFileDescriptor(uri, "w")?.use { - ObjectOutputStream(FileOutputStream(it.fileDescriptor)).use { output -> - val pref = PreferenceManager.getDefaultSharedPreferences(context) - // write all preference objects to the output file - output.writeObject(pref.all) - } - } - } catch (e: Exception) { - e.printStackTrace() - } - } - - /** - * restore the default shared preferences from a file - */ - @Suppress("UNCHECKED_CAST") - fun restoreSharedPreferences(uri: Uri?) { - if (uri == null) return - try { - context.contentResolver.openFileDescriptor(uri, "r")?.use { - ObjectInputStream(FileInputStream(it.fileDescriptor)).use { input -> - // map all the preference keys and their values - val entries = input.readObject() as Map - PreferenceManager.getDefaultSharedPreferences(context).edit(commit = true) { - // clear the previous settings - clear() - - // decide for each preference which type it is and save it to the - // preferences - for ((key, value) in entries) { - when (value) { - is Boolean -> putBoolean(key, value) - is Float -> putFloat(key, value) - is Int -> putInt(key, value) - is Long -> putLong(key, value) - is String -> putString(key, value) - } - } - } - } - } - } catch (e: Exception) { - e.printStackTrace() - } - } - - /** - * Backup the database + * Write a [BackupFile] containing the database content as well as the preferences */ fun advancedBackup(uri: Uri?, backupFile: BackupFile) { if (uri == null) return @@ -88,7 +34,7 @@ class BackupHelper(private val context: Context) { } /** - * Restore a database backup + * Restore data from a [BackupFile] */ fun restoreAdvancedBackup(uri: Uri?) { if (uri == null) return @@ -116,6 +62,30 @@ class BackupHelper(private val context: Context) { Database.customInstanceDao().insertAll( *backupFile.customInstances?.toTypedArray().orEmpty() ) + + restorePreferences(backupFile.preferences) + } + } + + /** + * Restore the shared preferences from a backup file + */ + private fun restorePreferences(preferences: List?) { + if (preferences == null) return + PreferenceManager.getDefaultSharedPreferences(context).edit(commit = true) { + // clear the previous settings + clear() + + // decide for each preference which type it is and save it to the preferences + preferences.forEach { + when (it.value) { + is Boolean -> putBoolean(it.key, it.value) + is Float -> putFloat(it.key, it.value) + is Int -> putInt(it.key, it.value) + is Long -> putLong(it.key, it.value) + is String -> putString(it.key, it.value) + } + } } } } diff --git a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt index 8665b1a49..0861bc555 100644 --- a/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PreferenceHelper.kt @@ -3,14 +3,13 @@ package com.github.libretube.util import android.content.Context import android.content.SharedPreferences import androidx.preference.PreferenceManager -import com.fasterxml.jackson.databind.ObjectMapper import com.github.libretube.constants.PreferenceKeys object PreferenceHelper { /** * for normal preferences */ - private lateinit var settings: SharedPreferences + lateinit var settings: SharedPreferences private lateinit var editor: SharedPreferences.Editor /** @@ -19,8 +18,6 @@ object PreferenceHelper { private lateinit var authSettings: SharedPreferences private lateinit var authEditor: SharedPreferences.Editor - private val mapper = ObjectMapper() - /** * set the context that is being used to access the shared preferences */ diff --git a/app/src/main/res/xml/advanced_settings.xml b/app/src/main/res/xml/advanced_settings.xml index b48fd81bb..02f1f0a27 100644 --- a/app/src/main/res/xml/advanced_settings.xml +++ b/app/src/main/res/xml/advanced_settings.xml @@ -38,20 +38,6 @@ - - - - - - - -