2022-08-13 18:42:09 +05:30
|
|
|
package com.github.libretube.util
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.net.Uri
|
2022-09-17 14:59:07 +05:30
|
|
|
import androidx.core.content.edit
|
2022-08-13 18:42:09 +05:30
|
|
|
import androidx.preference.PreferenceManager
|
2022-09-18 16:11:35 +05:30
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper
|
2022-09-18 22:54:31 +05:30
|
|
|
import com.github.libretube.db.DatabaseHolder.Companion.Database
|
2022-09-18 16:11:35 +05:30
|
|
|
import com.github.libretube.extensions.query
|
|
|
|
import com.github.libretube.obj.BackupFile
|
2022-09-22 21:48:10 +05:30
|
|
|
import com.github.libretube.obj.PreferenceItem
|
2022-08-13 18:42:09 +05:30
|
|
|
import java.io.FileOutputStream
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backup and restore the preferences
|
|
|
|
*/
|
2022-09-17 14:59:07 +05:30
|
|
|
class BackupHelper(private val context: Context) {
|
2022-08-13 18:42:09 +05:30
|
|
|
/**
|
2022-09-22 21:48:10 +05:30
|
|
|
* Write a [BackupFile] containing the database content as well as the preferences
|
2022-09-18 16:11:35 +05:30
|
|
|
*/
|
|
|
|
fun advancedBackup(uri: Uri?, backupFile: BackupFile) {
|
|
|
|
if (uri == null) return
|
|
|
|
try {
|
|
|
|
context.contentResolver.openFileDescriptor(uri, "w")?.use {
|
|
|
|
FileOutputStream(it.fileDescriptor).use { fileOutputStream ->
|
|
|
|
fileOutputStream.write(
|
|
|
|
ObjectMapper().writeValueAsBytes(backupFile)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e: Exception) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-22 21:48:10 +05:30
|
|
|
* Restore data from a [BackupFile]
|
2022-09-18 16:11:35 +05:30
|
|
|
*/
|
|
|
|
fun restoreAdvancedBackup(uri: Uri?) {
|
|
|
|
if (uri == null) return
|
|
|
|
|
|
|
|
val mapper = ObjectMapper()
|
|
|
|
val json = context.contentResolver.openInputStream(uri)?.use {
|
|
|
|
it.bufferedReader().use { reader -> reader.readText() }
|
|
|
|
}.orEmpty()
|
|
|
|
|
|
|
|
val backupFile = mapper.readValue(json, BackupFile::class.java)
|
|
|
|
|
|
|
|
query {
|
2022-09-18 22:54:31 +05:30
|
|
|
Database.watchHistoryDao().insertAll(
|
2022-09-18 16:11:35 +05:30
|
|
|
*backupFile.watchHistory?.toTypedArray().orEmpty()
|
|
|
|
)
|
2022-09-18 22:54:31 +05:30
|
|
|
Database.searchHistoryDao().insertAll(
|
2022-09-18 16:11:35 +05:30
|
|
|
*backupFile.searchHistory?.toTypedArray().orEmpty()
|
|
|
|
)
|
2022-09-18 22:54:31 +05:30
|
|
|
Database.watchPositionDao().insertAll(
|
2022-09-18 16:11:35 +05:30
|
|
|
*backupFile.watchPositions?.toTypedArray().orEmpty()
|
|
|
|
)
|
2022-09-18 22:54:31 +05:30
|
|
|
Database.localSubscriptionDao().insertAll(
|
2022-09-18 16:11:35 +05:30
|
|
|
*backupFile.localSubscriptions?.toTypedArray().orEmpty()
|
|
|
|
)
|
2022-09-18 22:54:31 +05:30
|
|
|
Database.customInstanceDao().insertAll(
|
2022-09-18 16:11:35 +05:30
|
|
|
*backupFile.customInstances?.toTypedArray().orEmpty()
|
|
|
|
)
|
2022-09-22 21:48:10 +05:30
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-09-18 16:11:35 +05:30
|
|
|
}
|
|
|
|
}
|
2022-08-13 18:42:09 +05:30
|
|
|
}
|