2023-01-31 21:13:39 +05:30
|
|
|
package com.github.libretube.helpers
|
2022-08-13 18:42:09 +05:30
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.net.Uri
|
2023-01-19 14:08:21 +05:30
|
|
|
import android.util.Log
|
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
|
2023-01-19 14:08:21 +05:30
|
|
|
import com.github.libretube.api.JsonHelper
|
2022-12-15 23:46:12 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2023-02-11 07:26:07 +05:30
|
|
|
import com.github.libretube.db.DatabaseHolder.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.obj.BackupFile
|
2022-09-22 21:48:10 +05:30
|
|
|
import com.github.libretube.obj.PreferenceItem
|
2023-01-31 20:25:02 +05:30
|
|
|
import kotlinx.serialization.ExperimentalSerializationApi
|
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
|
|
|
|
*/
|
2023-02-03 18:55:12 +05:30
|
|
|
object BackupHelper {
|
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
|
|
|
*/
|
2023-01-31 20:25:02 +05:30
|
|
|
@OptIn(ExperimentalSerializationApi::class)
|
2023-02-03 18:55:12 +05:30
|
|
|
fun createAdvancedBackup(context: Context, uri: Uri, backupFile: BackupFile) {
|
|
|
|
try {
|
|
|
|
context.contentResolver.openOutputStream(uri)?.use { outputStream ->
|
|
|
|
JsonHelper.json.encodeToStream(backupFile, outputStream)
|
2022-09-18 16:11:35 +05:30
|
|
|
}
|
2023-02-03 18:55:12 +05:30
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.e(TAG(), "Error while writing backup: $e")
|
2022-09-18 16:11:35 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-09-22 21:48:10 +05:30
|
|
|
* Restore data from a [BackupFile]
|
2022-09-18 16:11:35 +05:30
|
|
|
*/
|
2023-01-31 20:25:02 +05:30
|
|
|
@OptIn(ExperimentalSerializationApi::class)
|
2023-02-03 20:24:00 +05:30
|
|
|
suspend fun restoreAdvancedBackup(context: Context, uri: Uri) {
|
|
|
|
val backupFile = context.contentResolver.openInputStream(uri)?.use {
|
|
|
|
JsonHelper.json.decodeFromStream<BackupFile>(it)
|
2023-01-19 14:08:21 +05:30
|
|
|
} ?: return
|
2022-09-18 16:11:35 +05:30
|
|
|
|
2023-02-09 05:48:28 +05:30
|
|
|
Database.watchHistoryDao().insertAll(backupFile.watchHistory.orEmpty())
|
2023-02-19 18:49:05 +05:30
|
|
|
Database.searchHistoryDao().insertAll(backupFile.searchHistory.orEmpty())
|
2023-02-22 04:07:36 +05:30
|
|
|
Database.watchPositionDao().insertAll(backupFile.watchPositions.orEmpty())
|
2023-02-03 20:24:00 +05:30
|
|
|
Database.localSubscriptionDao().insertAll(backupFile.localSubscriptions.orEmpty())
|
2023-02-10 07:14:20 +05:30
|
|
|
Database.customInstanceDao().insertAll(backupFile.customInstances.orEmpty())
|
2023-02-22 03:58:04 +05:30
|
|
|
Database.playlistBookmarkDao().insertAll(backupFile.playlistBookmarks.orEmpty())
|
2023-03-28 21:15:21 +05:30
|
|
|
Database.subscriptionGroupsDao().insertAll(backupFile.channelGroups.orEmpty())
|
2022-09-22 21:48:10 +05:30
|
|
|
|
2023-02-22 03:58:04 +05:30
|
|
|
backupFile.localPlaylists?.forEach {
|
2023-02-03 20:24:00 +05:30
|
|
|
Database.localPlaylistsDao().createPlaylist(it.playlist)
|
|
|
|
val playlistId = Database.localPlaylistsDao().getAll().last().playlist.id
|
2023-03-28 21:15:21 +05:30
|
|
|
it.videos.forEach { playlistItem ->
|
|
|
|
playlistItem.playlistId = playlistId
|
|
|
|
Database.localPlaylistsDao().addPlaylistVideo(playlistItem)
|
2022-11-23 22:50:43 +05:30
|
|
|
}
|
2022-09-22 21:48:10 +05:30
|
|
|
}
|
2023-02-03 20:24:00 +05:30
|
|
|
|
|
|
|
restorePreferences(context, backupFile.preferences)
|
2022-09-22 21:48:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restore the shared preferences from a backup file
|
|
|
|
*/
|
2023-02-03 18:55:12 +05:30
|
|
|
private fun restorePreferences(context: Context, preferences: List<PreferenceItem>?) {
|
2022-09-22 21:48:10 +05:30
|
|
|
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
|
2023-01-20 04:45:55 +05:30
|
|
|
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) {
|
2023-01-20 04:45:55 +05:30
|
|
|
is Boolean -> putBoolean(key, value)
|
|
|
|
is Float -> putFloat(key, value)
|
|
|
|
is Long -> putLong(key, value)
|
2022-12-15 23:46:12 +05:30
|
|
|
is Int -> {
|
2023-07-11 15:50:25 +05:30
|
|
|
when {
|
|
|
|
// we only use integers for SponsorBlock colors and the start fragment
|
|
|
|
key == PreferenceKeys.START_FRAGMENT || key.orEmpty().contains("_color") -> putInt(key, value)
|
2023-01-20 04:45:55 +05:30
|
|
|
else -> putLong(key, value.toLong())
|
2022-12-15 23:46:12 +05:30
|
|
|
}
|
|
|
|
}
|
2023-06-24 23:27:00 +05:30
|
|
|
|
2023-01-20 04:45:55 +05:30
|
|
|
is String -> putString(key, value)
|
2022-09-22 21:48:10 +05:30
|
|
|
}
|
|
|
|
}
|
2022-09-18 16:11:35 +05:30
|
|
|
}
|
|
|
|
}
|
2022-08-13 18:42:09 +05:30
|
|
|
}
|