LibreTube/app/src/main/java/com/github/libretube/util/BackupHelper.kt

119 lines
4.2 KiB
Kotlin
Raw Normal View History

2022-08-13 18:42:09 +05:30
package com.github.libretube.util
import android.content.Context
import android.net.Uri
2023-01-19 14:08:21 +05:30
import android.util.Log
import androidx.core.content.edit
2022-08-13 18:42:09 +05:30
import androidx.preference.PreferenceManager
2023-01-19 14:08:21 +05:30
import com.github.libretube.api.JsonHelper
import com.github.libretube.constants.PreferenceKeys
2022-09-18 22:54:31 +05:30
import com.github.libretube.db.DatabaseHolder.Companion.Database
2023-01-19 14:08:21 +05:30
import com.github.libretube.extensions.TAG
2022-09-18 16:11:35 +05:30
import com.github.libretube.extensions.query
import com.github.libretube.obj.BackupFile
import com.github.libretube.obj.PreferenceItem
2023-01-19 14:08:21 +05:30
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.decodeFromStream
import kotlinx.serialization.json.encodeToStream
import kotlinx.serialization.json.floatOrNull
import kotlinx.serialization.json.intOrNull
import kotlinx.serialization.json.longOrNull
2022-08-13 18:42:09 +05:30
/**
* Backup and restore the preferences
*/
class BackupHelper(private val context: Context) {
2022-08-13 18:42:09 +05:30
/**
* Write a [BackupFile] containing the database content as well as the preferences
2022-09-18 16:11:35 +05:30
*/
2023-01-19 14:08:21 +05:30
fun createAdvancedBackup(uri: Uri?, backupFile: BackupFile) {
uri?.let {
try {
context.contentResolver.openOutputStream(it)?.use { outputStream ->
JsonHelper.json.encodeToStream(backupFile, outputStream)
2022-09-18 16:11:35 +05:30
}
2023-01-19 14:08:21 +05:30
} catch (e: Exception) {
Log.e(TAG(), "Error while writing backup: $e")
2022-09-18 16:11:35 +05:30
}
}
}
/**
* Restore data from a [BackupFile]
2022-09-18 16:11:35 +05:30
*/
fun restoreAdvancedBackup(uri: Uri?) {
2023-01-19 14:08:21 +05:30
val backupFile = uri?.let {
context.contentResolver.openInputStream(it)?.use { inputStream ->
JsonHelper.json.decodeFromStream<BackupFile>(inputStream)
}
} ?: return
2022-09-18 16:11:35 +05:30
query {
2022-09-18 22:54:31 +05:30
Database.watchHistoryDao().insertAll(
2023-01-19 14:08:21 +05:30
*backupFile.watchHistory.toTypedArray()
2022-09-18 16:11:35 +05:30
)
2022-09-18 22:54:31 +05:30
Database.searchHistoryDao().insertAll(
2023-01-19 14:08:21 +05:30
*backupFile.searchHistory.toTypedArray()
2022-09-18 16:11:35 +05:30
)
2022-09-18 22:54:31 +05:30
Database.watchPositionDao().insertAll(
2023-01-19 14:08:21 +05:30
*backupFile.watchPositions.toTypedArray()
2022-09-18 16:11:35 +05:30
)
2022-09-18 22:54:31 +05:30
Database.localSubscriptionDao().insertAll(
2023-01-19 14:08:21 +05:30
*backupFile.localSubscriptions.toTypedArray()
2022-09-18 16:11:35 +05:30
)
2022-09-18 22:54:31 +05:30
Database.customInstanceDao().insertAll(
2023-01-19 14:08:21 +05:30
*backupFile.customInstances.toTypedArray()
)
Database.playlistBookmarkDao().insertAll(
2023-01-19 14:08:21 +05:30
*backupFile.playlistBookmarks.toTypedArray()
2022-09-18 16:11:35 +05:30
)
2023-01-19 14:08:21 +05:30
backupFile.localPlaylists.forEach {
Database.localPlaylistsDao().createPlaylist(it.playlist)
val playlistId = Database.localPlaylistsDao().getAll().last().playlist.id
it.videos.forEach {
it.playlistId = playlistId
Database.localPlaylistsDao().addPlaylistVideo(it)
}
}
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 { (key, jsonValue) ->
val value = if (jsonValue.isString) {
jsonValue.content
} else {
jsonValue.booleanOrNull
?: jsonValue.intOrNull
?: jsonValue.longOrNull
?: jsonValue.floatOrNull
}
2023-01-19 14:08:21 +05:30
when (value) {
is Boolean -> putBoolean(key, value)
is Float -> putFloat(key, value)
is Long -> putLong(key, value)
is Int -> {
when (key) {
PreferenceKeys.START_FRAGMENT -> putInt(key, value)
else -> putLong(key, value.toLong())
}
}
is String -> putString(key, value)
}
}
2022-09-18 16:11:35 +05:30
}
}
2022-08-13 18:42:09 +05:30
}