Fix backup

This commit is contained in:
Horis 2022-12-25 23:42:55 +08:00
parent 2ba78eb37e
commit 9f9c774985
2 changed files with 59 additions and 54 deletions

View file

@ -4,6 +4,7 @@ import android.content.ComponentName
import android.content.Intent import android.content.Intent
import android.content.res.ColorStateList import android.content.res.ColorStateList
import android.content.res.Configuration import android.content.res.Configuration
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.KeyEvent import android.view.KeyEvent
@ -786,7 +787,7 @@ class MainActivity : AppCompatActivity(), ColorPickerDialogListener {
nav_rail?.itemActiveIndicatorColor = rippleColor nav_rail?.itemActiveIndicatorColor = rippleColor
nav_view?.itemActiveIndicatorColor = rippleColor nav_view?.itemActiveIndicatorColor = rippleColor
if (!checkWrite()) { if (!checkWrite() && Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
requestRW() requestRW()
if (checkWrite()) return if (checkWrite()) return
} }

View file

@ -1,5 +1,6 @@
package com.lagradost.cloudstream3.utils package com.lagradost.cloudstream3.utils
import android.annotation.SuppressLint
import android.content.ContentValues import android.content.ContentValues
import android.content.Context import android.content.Context
import android.net.Uri import android.net.Uri
@ -74,7 +75,7 @@ object BackupUtils {
return !nonTransferableKeys.contains(this) return !nonTransferableKeys.contains(this)
} }
var restoreFileSelector: ActivityResultLauncher<Array<String>>? = null private var restoreFileSelector: ActivityResultLauncher<Array<String>>? = null
// Kinda hack, but I couldn't think of a better way // Kinda hack, but I couldn't think of a better way
data class BackupVars( data class BackupVars(
@ -91,6 +92,7 @@ object BackupUtils {
@JsonProperty("settings") val settings: BackupVars @JsonProperty("settings") val settings: BackupVars
) )
@Suppress("UNCHECKED_CAST")
fun Context.getBackup(): BackupFile { fun Context.getBackup(): BackupFile {
val allData = getSharedPrefs().all.filter { it.key.isTransferable() } val allData = getSharedPrefs().all.filter { it.key.isTransferable() }
val allSettings = getDefaultSharedPrefs().all.filter { it.key.isTransferable() } val allSettings = getDefaultSharedPrefs().all.filter { it.key.isTransferable() }
@ -143,17 +145,24 @@ object BackupUtils {
} }
} }
@SuppressLint("SimpleDateFormat")
fun FragmentActivity.backup() { fun FragmentActivity.backup() {
try { try {
if (checkWrite()) { if (!checkWrite() && Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
showToast(this, getString(R.string.backup_failed), Toast.LENGTH_LONG)
requestRW()
return
}
val subDir = getBasePath().first val subDir = getBasePath().first
val date = SimpleDateFormat("yyyy_MM_dd_HH_mm").format(Date(currentTimeMillis())) val date = SimpleDateFormat("yyyy_MM_dd_HH_mm").format(Date(currentTimeMillis()))
val ext = "json" val ext = "json"
val displayName = "CS3_Backup_${date}" val displayName = "CS3_Backup_${date}"
val backupFile = getBackup() val backupFile = getBackup()
val steam = val steam = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && subDir?.isDownloadDir() == true) { && subDir?.isDownloadDir() == true
) {
val cr = this.contentResolver val cr = this.contentResolver
val contentUri = val contentUri =
MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) // USE INSTEAD OF MediaStore.Downloads.EXTERNAL_CONTENT_URI MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) // USE INSTEAD OF MediaStore.Downloads.EXTERNAL_CONTENT_URI
@ -196,11 +205,6 @@ object BackupUtils {
R.string.backup_success, R.string.backup_success,
Toast.LENGTH_LONG Toast.LENGTH_LONG
) )
} else {
showToast(this, getString(R.string.backup_failed), Toast.LENGTH_LONG)
requestRW()
return
}
} catch (e: Exception) { } catch (e: Exception) {
logError(e) logError(e)
try { try {