mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
backup and restore settings
This commit is contained in:
parent
b6fb971142
commit
f97ad0d1f7
@ -1,15 +1,40 @@
|
||||
package com.github.libretube.preferences
|
||||
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.preference.Preference
|
||||
import com.github.libretube.R
|
||||
import com.github.libretube.activities.SettingsActivity
|
||||
import com.github.libretube.util.BackupHelper
|
||||
import com.github.libretube.views.MaterialPreferenceFragment
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
|
||||
class AdvancedSettings : MaterialPreferenceFragment() {
|
||||
val TAG = "AdvancedSettings"
|
||||
|
||||
/**
|
||||
* result listeners for importing and exporting subscriptions
|
||||
*/
|
||||
private lateinit var getContent: ActivityResultLauncher<String>
|
||||
private lateinit var createFile: ActivityResultLauncher<String>
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
getContent =
|
||||
registerForActivityResult(
|
||||
ActivityResultContracts.GetContent()
|
||||
) { uri: Uri? ->
|
||||
BackupHelper(requireContext()).restoreSharedPreferences(uri)
|
||||
}
|
||||
createFile = registerForActivityResult(
|
||||
ActivityResultContracts.CreateDocument()
|
||||
) { uri: Uri? ->
|
||||
BackupHelper(requireContext()).backupSharedPreferences(uri)
|
||||
}
|
||||
super.onCreate(savedInstanceState)
|
||||
}
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
setPreferencesFromResource(R.xml.advanced_settings, rootKey)
|
||||
|
||||
@ -21,6 +46,20 @@ class AdvancedSettings : MaterialPreferenceFragment() {
|
||||
showResetDialog()
|
||||
true
|
||||
}
|
||||
|
||||
val backupSettings = findPreference<Preference>(PreferenceKeys.BACKUP_SETTINGS)
|
||||
backupSettings?.setOnPreferenceClickListener {
|
||||
createFile.launch("preferences.xml")
|
||||
activity?.recreate()
|
||||
true
|
||||
}
|
||||
|
||||
val restoreSettings = findPreference<Preference>(PreferenceKeys.RESTORE_SETTINGS)
|
||||
restoreSettings?.setOnPreferenceClickListener {
|
||||
getContent.launch("*/*")
|
||||
activity?.recreate()
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private fun showResetDialog() {
|
||||
|
@ -127,8 +127,7 @@ class InstanceSettings : MaterialPreferenceFragment() {
|
||||
|
||||
val deleteAccount = findPreference<Preference>(PreferenceKeys.DELETE_ACCOUNT)
|
||||
deleteAccount?.setOnPreferenceClickListener {
|
||||
val token = PreferenceHelper.getToken()
|
||||
if (token != "") {
|
||||
if (PreferenceHelper.getToken() != "") {
|
||||
val newFragment = DeleteAccountDialog()
|
||||
newFragment.show(childFragmentManager, DeleteAccountDialog::class.java.name)
|
||||
} else {
|
||||
|
@ -88,6 +88,8 @@ object PreferenceKeys {
|
||||
const val CLEAR_WATCH_HISTORY = "clear_watch_history"
|
||||
const val CLEAR_WATCH_POSITIONS = "clear_watch_positions"
|
||||
const val SHARE_WITH_TIME_CODE = "share_with_time_code"
|
||||
const val BACKUP_SETTINGS = "backup_settings"
|
||||
const val RESTORE_SETTINGS = "restore_settings"
|
||||
|
||||
/**
|
||||
* History
|
||||
|
87
app/src/main/java/com/github/libretube/util/BackupHelper.kt
Normal file
87
app/src/main/java/com/github/libretube/util/BackupHelper.kt
Normal file
@ -0,0 +1,87 @@
|
||||
package com.github.libretube.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.net.Uri
|
||||
import androidx.preference.PreferenceManager
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.io.ObjectInputStream
|
||||
import java.io.ObjectOutputStream
|
||||
|
||||
/**
|
||||
* Backup and restore the preferences
|
||||
*/
|
||||
class BackupHelper(
|
||||
private val context: Context
|
||||
) {
|
||||
private val TAG = this::class.java.name
|
||||
|
||||
/**
|
||||
* Backup the default shared preferences to a file
|
||||
*/
|
||||
fun backupSharedPreferences(uri: Uri?) {
|
||||
if (uri == null) return
|
||||
var output: ObjectOutputStream? = null
|
||||
try {
|
||||
val fileDescriptor = context.contentResolver.openFileDescriptor(uri, "w")?.fileDescriptor
|
||||
output = ObjectOutputStream(FileOutputStream(fileDescriptor))
|
||||
val pref: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
||||
// write all preference objects to the output file
|
||||
output.writeObject(pref.all)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
try {
|
||||
// close the outputStream
|
||||
if (output != null) {
|
||||
output.flush()
|
||||
output.close()
|
||||
}
|
||||
} catch (ex: IOException) {
|
||||
ex.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* restore the default shared preferences from a file
|
||||
*/
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
fun restoreSharedPreferences(uri: Uri?) {
|
||||
if (uri == null) return
|
||||
var input: ObjectInputStream? = null
|
||||
try {
|
||||
val fileDescriptor = context.contentResolver.openFileDescriptor(uri, "r")?.fileDescriptor
|
||||
input = ObjectInputStream(FileInputStream(fileDescriptor))
|
||||
val editor = PreferenceManager.getDefaultSharedPreferences(context).edit()
|
||||
|
||||
// clear the previous settings
|
||||
editor.clear()
|
||||
|
||||
// map all the preference keys and their values
|
||||
val entries = input.readObject() as Map<String, *>
|
||||
|
||||
// decide for each preference which type it is and save it to the preferences
|
||||
for ((key, value) in entries) {
|
||||
if (value is Boolean) editor.putBoolean(key, value)
|
||||
else if (value is Float) editor.putFloat(key, value)
|
||||
else if (value is Int) editor.putInt(key, value)
|
||||
else if (value is Long) editor.putLong(key, value)
|
||||
else if (value is String) editor.putString(key, value)
|
||||
}
|
||||
editor.commit()
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
} finally {
|
||||
try {
|
||||
if (input != null) {
|
||||
input.close()
|
||||
}
|
||||
} catch (ex: IOException) {
|
||||
ex.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -310,4 +310,6 @@
|
||||
<string name="resize_mode_zoom">Zoom</string>
|
||||
<string name="repeat_mode_none">None</string>
|
||||
<string name="repeat_mode_current">Current</string>
|
||||
<string name="backup_restore">Backup & restore</string>
|
||||
<string name="backup">Backup</string>
|
||||
</resources>
|
||||
|
@ -47,4 +47,16 @@
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory app:title="@string/backup_restore">
|
||||
|
||||
<Preference
|
||||
app:title="@string/backup"
|
||||
app:key="backup_settings" />
|
||||
|
||||
<Preference
|
||||
app:title="@string/restore"
|
||||
app:key="restore_settings" />
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
Loading…
x
Reference in New Issue
Block a user