mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 06:10:31 +05:30
commit
0ac796c2d6
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
10
app/src/main/res/drawable/ic_backup.xml
Normal file
10
app/src/main/res/drawable/ic_backup.xml
Normal file
@ -0,0 +1,10 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:tint="?attr/colorControlNormal"
|
||||
android:viewportWidth="48"
|
||||
android:viewportHeight="48">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M12.55,40q-4.4,0 -7.475,-3.075Q2,33.85 2,29.45q0,-3.9 2.5,-6.85 2.5,-2.95 6.35,-3.55 0.9,-4.5 4.15,-7.5 3.25,-3 7.55,-3.4V25.6l-4.15,-4.15 -2.15,2.15 7.8,7.8 7.8,-7.8 -2.15,-2.15 -4.15,4.15V8.15q5.05,0.55 8.45,4.5 3.4,3.95 3.4,9.25v1.2q3.6,-0.1 6.1,2.325Q46,27.85 46,31.55q0,3.45 -2.5,5.95T37.55,40Z" />
|
||||
</vector>
|
10
app/src/main/res/drawable/ic_restore.xml
Normal file
10
app/src/main/res/drawable/ic_restore.xml
Normal file
@ -0,0 +1,10 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:tint="?attr/colorControlNormal"
|
||||
android:viewportWidth="48"
|
||||
android:viewportHeight="48">
|
||||
<path
|
||||
android:fillColor="#FF000000"
|
||||
android:pathData="M22.5,40h-9.95q-4.4,0 -7.475,-3.075Q2,33.85 2,29.45q0,-3.9 2.5,-6.85 2.5,-2.95 6.35,-3.55 1,-4.85 4.7,-7.925T24.1,8.05q5.6,0 9.45,4.075Q37.4,16.2 37.4,21.9v1.2q3.6,-0.1 6.1,2.325Q46,27.85 46,31.55q0,3.45 -2.5,5.95T37.55,40H25.5V24.1l4.15,4.15 2.15,-2.15 -7.8,-7.8 -7.8,7.8 2.15,2.15 4.15,-4.15Z" />
|
||||
</vector>
|
@ -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,18 @@
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
<PreferenceCategory app:title="@string/backup_restore">
|
||||
|
||||
<Preference
|
||||
android:icon="@drawable/ic_backup"
|
||||
app:title="@string/backup"
|
||||
app:key="backup_settings" />
|
||||
|
||||
<Preference
|
||||
android:icon="@drawable/ic_restore"
|
||||
app:title="@string/restore"
|
||||
app:key="restore_settings" />
|
||||
|
||||
</PreferenceCategory>
|
||||
|
||||
</PreferenceScreen>
|
Loading…
Reference in New Issue
Block a user