From d1d1d60c30ef02012bb7d189c2e080a88328f013 Mon Sep 17 00:00:00 2001 From: Bram van de Kerkhof Date: Tue, 28 Nov 2017 22:55:50 +0100 Subject: [PATCH] Fix automatic backups (#1074) * Fix automatic backups * Small fixes * small fixes --- .../data/backup/BackupCreateService.kt | 9 ++++--- .../tachiyomi/data/backup/BackupCreatorJob.kt | 3 +-- .../ui/setting/SettingsBackupController.kt | 25 +++++++++---------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateService.kt index 9f3d577e72..cf3d2535ca 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreateService.kt @@ -46,7 +46,7 @@ class BackupCreateService : IntentService(NAME) { * Make a backup from library * * @param context context of application - * @param path path of Uri + * @param uri path of Uri * @param flags determines what to backup * @param isJob backup called from job */ @@ -80,7 +80,7 @@ class BackupCreateService : IntentService(NAME) { * @param uri path of Uri * @param isJob backup called from job */ - fun createBackupFromApp(uri: Uri, flags: Int, isJob: Boolean) { + private fun createBackupFromApp(uri: Uri, flags: Int, isJob: Boolean) { // Create root object val root = JsonObject() @@ -113,8 +113,9 @@ class BackupCreateService : IntentService(NAME) { try { // When BackupCreatorJob if (isJob) { - // Get dir of file - val dir = UniFile.fromUri(this, uri) + // Get dir of file and create + var dir = UniFile.fromUri(this, uri) + dir = dir.createDirectory("automatic") // Delete older backups val numberOfBackups = backupManager.numberOfBackups() diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt index 2e33f642c7..49c9aaecfb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupCreatorJob.kt @@ -8,13 +8,12 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get -import java.io.File class BackupCreatorJob : Job() { override fun onRunJob(params: Params): Result { val preferences = Injekt.get() - val uri = Uri.fromFile(File(preferences.backupsDirectory().getOrDefault())) + val uri = Uri.parse(preferences.backupsDirectory().getOrDefault()) val flags = BackupCreateService.BACKUP_ALL BackupCreateService.makeBackup(context, uri, flags, true) return Result.SUCCESS diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt index 6c19948352..f8c1c930ba 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupController.kt @@ -1,6 +1,5 @@ package eu.kanade.tachiyomi.ui.setting -import android.Manifest.permission.READ_EXTERNAL_STORAGE import android.Manifest.permission.WRITE_EXTERNAL_STORAGE import android.app.Activity import android.app.Dialog @@ -135,7 +134,7 @@ class SettingsBackupController : SettingsController() { preferences.backupsDirectory().asObservable() .subscribeUntilDestroy { path -> val dir = UniFile.fromUri(context, Uri.parse(path)) - summary = dir.filePath ?: path + summary = dir.filePath + "/automatic" } } val backupNumber = intListPreference { @@ -160,19 +159,19 @@ class SettingsBackupController : SettingsController() { when (requestCode) { CODE_BACKUP_DIR -> if (data != null && resultCode == Activity.RESULT_OK) { val activity = activity ?: return - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - val uri = Uri.fromFile(File(data.data.path)) - preferences.backupsDirectory().set(uri.toString()) - } else { - val uri = data.data + // Get uri of backup folder. + val uri = data.data + + // Get UriPermission so it's possible to write files post kitkat. + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION activity.contentResolver.takePersistableUriPermission(uri, flags) - - val file = UniFile.fromUri(activity, uri) - preferences.backupsDirectory().set(file.uri.toString()) } + + // Set backup Uri. + preferences.backupsDirectory().set(uri.toString()) } CODE_BACKUP_CREATE -> if (data != null && resultCode == Activity.RESULT_OK) { val activity = activity ?: return @@ -240,7 +239,7 @@ class SettingsBackupController : SettingsController() { .itemsDisabledIndices(0) .itemsCallbackMultiChoice(arrayOf(0, 1, 2, 3, 4), { _, positions, _ -> var flags = 0 - for (i in 1..positions.size - 1) { + for (i in 1 until positions.size) { when (positions[i]) { 1 -> flags = flags or BackupCreateService.BACKUP_CATEGORY 2 -> flags = flags or BackupCreateService.BACKUP_CHAPTER @@ -281,7 +280,7 @@ class SettingsBackupController : SettingsController() { override fun onCreateDialog(savedViewState: Bundle?): Dialog { val activity = activity!! - val unifile = UniFile.fromUri(activity, args.getParcelable(KEY_URI)) + val unifile = UniFile.fromUri(activity, args.getParcelable(KEY_URI)) return MaterialDialog.Builder(activity) .title(R.string.backup_created) .content(activity.getString(R.string.file_saved, unifile.filePath)) @@ -315,7 +314,7 @@ class SettingsBackupController : SettingsController() { val context = applicationContext if (context != null) { RestoringBackupDialog().showDialog(router, TAG_RESTORING_BACKUP_DIALOG) - BackupRestoreService.start(context, args.getParcelable(KEY_URI)) + BackupRestoreService.start(context, args.getParcelable(KEY_URI)) } } .build()