Merge pull request #1344 from Bnyro/master

move the preference backup to the unified backup dialog
This commit is contained in:
Bnyro 2022-09-22 18:18:30 +02:00 committed by GitHub
commit 6f3cede7fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 109 deletions

View File

@ -11,5 +11,6 @@ data class BackupFile(
var watchPositions: List<WatchPosition>? = null, var watchPositions: List<WatchPosition>? = null,
var searchHistory: List<SearchHistoryItem>? = null, var searchHistory: List<SearchHistoryItem>? = null,
var localSubscriptions: List<LocalSubscription>? = null, var localSubscriptions: List<LocalSubscription>? = null,
var customInstances: List<CustomInstance>? = null var customInstances: List<CustomInstance>? = null,
var preferences: List<PreferenceItem>? = null
) )

View File

@ -0,0 +1,6 @@
package com.github.libretube.obj
data class PreferenceItem(
val key: String? = null,
val value: Any? = null
)

View File

@ -8,7 +8,9 @@ import com.github.libretube.R
import com.github.libretube.databinding.DialogBackupBinding import com.github.libretube.databinding.DialogBackupBinding
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.obj.BackupFile import com.github.libretube.obj.BackupFile
import com.github.libretube.obj.PreferenceItem
import com.github.libretube.ui.adapters.BackupOptionsAdapter import com.github.libretube.ui.adapters.BackupOptionsAdapter
import com.github.libretube.util.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
class BackupDialog( class BackupDialog(
@ -24,10 +26,11 @@ class BackupDialog(
R.string.watch_positions, R.string.watch_positions,
R.string.search_history, R.string.search_history,
R.string.local_subscriptions, 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 = DialogBackupBinding.inflate(layoutInflater)
binding.backupOptionsRecycler.layoutManager = LinearLayoutManager(context) binding.backupOptionsRecycler.layoutManager = LinearLayoutManager(context)
@ -62,6 +65,14 @@ class BackupDialog(
backupFile.customInstances = backupFile.customInstances =
Database.customInstanceDao().getAll() Database.customInstanceDao().getAll()
} }
if (selected[5]) {
backupFile.preferences = PreferenceHelper.settings.all.map {
PreferenceItem(
it.key,
it.value
)
}
}
} }
thread.start() thread.start()
thread.join() thread.join()

View File

@ -20,28 +20,12 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
class AdvancedSettings : MaterialPreferenceFragment() { class AdvancedSettings : MaterialPreferenceFragment() {
// backup and restore prefs
private lateinit var getPrefFile: ActivityResultLauncher<String>
private lateinit var createPrefFile: ActivityResultLauncher<String>
// backup and restore database // backup and restore database
private lateinit var getBackupFile: ActivityResultLauncher<String> private lateinit var getBackupFile: ActivityResultLauncher<String>
private lateinit var createBackupFile: ActivityResultLauncher<String> private lateinit var createBackupFile: ActivityResultLauncher<String>
private var backupFile = BackupFile() private var backupFile = BackupFile()
override fun onCreate(savedInstanceState: Bundle?) { 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 = getBackupFile =
registerForActivityResult( registerForActivityResult(
ActivityResultContracts.GetContent() ActivityResultContracts.GetContent()
@ -76,21 +60,6 @@ class AdvancedSettings : MaterialPreferenceFragment() {
true true
} }
val backupSettings = findPreference<Preference>(PreferenceKeys.BACKUP_SETTINGS)
backupSettings?.setOnPreferenceClickListener {
createPrefFile.launch("preferences.xml")
true
}
val restoreSettings = findPreference<Preference>(PreferenceKeys.RESTORE_SETTINGS)
restoreSettings?.setOnPreferenceClickListener {
getPrefFile.launch("*/*")
// reset the token
PreferenceHelper.setToken("")
activity?.recreate()
true
}
val advancesBackup = findPreference<Preference>("backup") val advancesBackup = findPreference<Preference>("backup")
advancesBackup?.setOnPreferenceClickListener { advancesBackup?.setOnPreferenceClickListener {
BackupDialog { BackupDialog {

View File

@ -8,69 +8,15 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.extensions.query import com.github.libretube.extensions.query
import com.github.libretube.obj.BackupFile import com.github.libretube.obj.BackupFile
import java.io.FileInputStream import com.github.libretube.obj.PreferenceItem
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
/** /**
* Backup and restore the preferences * Backup and restore the preferences
*/ */
class BackupHelper(private val context: Context) { class BackupHelper(private val context: Context) {
/** /**
* Backup the default shared preferences to a file * Write a [BackupFile] containing the database content as well as the preferences
*/
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<String, *>
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
*/ */
fun advancedBackup(uri: Uri?, backupFile: BackupFile) { fun advancedBackup(uri: Uri?, backupFile: BackupFile) {
if (uri == null) return 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?) { fun restoreAdvancedBackup(uri: Uri?) {
if (uri == null) return if (uri == null) return
@ -116,6 +62,30 @@ class BackupHelper(private val context: Context) {
Database.customInstanceDao().insertAll( Database.customInstanceDao().insertAll(
*backupFile.customInstances?.toTypedArray().orEmpty() *backupFile.customInstances?.toTypedArray().orEmpty()
) )
restorePreferences(backupFile.preferences)
}
}
/**
* Restore the shared preferences from a backup file
*/
private fun restorePreferences(preferences: List<PreferenceItem>?) {
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)
}
}
} }
} }
} }

View File

@ -3,14 +3,13 @@ package com.github.libretube.util
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
object PreferenceHelper { object PreferenceHelper {
/** /**
* for normal preferences * for normal preferences
*/ */
private lateinit var settings: SharedPreferences lateinit var settings: SharedPreferences
private lateinit var editor: SharedPreferences.Editor private lateinit var editor: SharedPreferences.Editor
/** /**
@ -19,8 +18,6 @@ object PreferenceHelper {
private lateinit var authSettings: SharedPreferences private lateinit var authSettings: SharedPreferences
private lateinit var authEditor: SharedPreferences.Editor private lateinit var authEditor: SharedPreferences.Editor
private val mapper = ObjectMapper()
/** /**
* set the context that is being used to access the shared preferences * set the context that is being used to access the shared preferences
*/ */

View File

@ -38,20 +38,6 @@
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory app:title="@string/preferences">
<Preference
android:icon="@drawable/ic_backup"
app:key="backup_settings"
app:title="@string/backup" />
<Preference
android:icon="@drawable/ic_restore"
app:key="restore_settings"
app:title="@string/restore" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/backup_restore"> <PreferenceCategory app:title="@string/backup_restore">
<Preference <Preference