Fix automatic backups (#1074)

* Fix automatic backups

* Small fixes

* small fixes
This commit is contained in:
Bram van de Kerkhof 2017-11-28 22:55:50 +01:00 committed by GitHub
parent 80fd49d60b
commit d1d1d60c30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 19 deletions

View file

@ -46,7 +46,7 @@ class BackupCreateService : IntentService(NAME) {
* Make a backup from library * Make a backup from library
* *
* @param context context of application * @param context context of application
* @param path path of Uri * @param uri path of Uri
* @param flags determines what to backup * @param flags determines what to backup
* @param isJob backup called from job * @param isJob backup called from job
*/ */
@ -80,7 +80,7 @@ class BackupCreateService : IntentService(NAME) {
* @param uri path of Uri * @param uri path of Uri
* @param isJob backup called from job * @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 // Create root object
val root = JsonObject() val root = JsonObject()
@ -113,8 +113,9 @@ class BackupCreateService : IntentService(NAME) {
try { try {
// When BackupCreatorJob // When BackupCreatorJob
if (isJob) { if (isJob) {
// Get dir of file // Get dir of file and create
val dir = UniFile.fromUri(this, uri) var dir = UniFile.fromUri(this, uri)
dir = dir.createDirectory("automatic")
// Delete older backups // Delete older backups
val numberOfBackups = backupManager.numberOfBackups() val numberOfBackups = backupManager.numberOfBackups()

View file

@ -8,13 +8,12 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.preference.getOrDefault
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.io.File
class BackupCreatorJob : Job() { class BackupCreatorJob : Job() {
override fun onRunJob(params: Params): Result { override fun onRunJob(params: Params): Result {
val preferences = Injekt.get<PreferencesHelper>() val preferences = Injekt.get<PreferencesHelper>()
val uri = Uri.fromFile(File(preferences.backupsDirectory().getOrDefault())) val uri = Uri.parse(preferences.backupsDirectory().getOrDefault())
val flags = BackupCreateService.BACKUP_ALL val flags = BackupCreateService.BACKUP_ALL
BackupCreateService.makeBackup(context, uri, flags, true) BackupCreateService.makeBackup(context, uri, flags, true)
return Result.SUCCESS return Result.SUCCESS

View file

@ -1,6 +1,5 @@
package eu.kanade.tachiyomi.ui.setting package eu.kanade.tachiyomi.ui.setting
import android.Manifest.permission.READ_EXTERNAL_STORAGE
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
import android.app.Activity import android.app.Activity
import android.app.Dialog import android.app.Dialog
@ -135,7 +134,7 @@ class SettingsBackupController : SettingsController() {
preferences.backupsDirectory().asObservable() preferences.backupsDirectory().asObservable()
.subscribeUntilDestroy { path -> .subscribeUntilDestroy { path ->
val dir = UniFile.fromUri(context, Uri.parse(path)) val dir = UniFile.fromUri(context, Uri.parse(path))
summary = dir.filePath ?: path summary = dir.filePath + "/automatic"
} }
} }
val backupNumber = intListPreference { val backupNumber = intListPreference {
@ -160,19 +159,19 @@ class SettingsBackupController : SettingsController() {
when (requestCode) { when (requestCode) {
CODE_BACKUP_DIR -> if (data != null && resultCode == Activity.RESULT_OK) { CODE_BACKUP_DIR -> if (data != null && resultCode == Activity.RESULT_OK) {
val activity = activity ?: return val activity = activity ?: return
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // Get uri of backup folder.
val uri = Uri.fromFile(File(data.data.path))
preferences.backupsDirectory().set(uri.toString())
} else {
val uri = data.data 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 val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION Intent.FLAG_GRANT_WRITE_URI_PERMISSION
activity.contentResolver.takePersistableUriPermission(uri, flags) 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) { CODE_BACKUP_CREATE -> if (data != null && resultCode == Activity.RESULT_OK) {
val activity = activity ?: return val activity = activity ?: return
@ -240,7 +239,7 @@ class SettingsBackupController : SettingsController() {
.itemsDisabledIndices(0) .itemsDisabledIndices(0)
.itemsCallbackMultiChoice(arrayOf(0, 1, 2, 3, 4), { _, positions, _ -> .itemsCallbackMultiChoice(arrayOf(0, 1, 2, 3, 4), { _, positions, _ ->
var flags = 0 var flags = 0
for (i in 1..positions.size - 1) { for (i in 1 until positions.size) {
when (positions[i]) { when (positions[i]) {
1 -> flags = flags or BackupCreateService.BACKUP_CATEGORY 1 -> flags = flags or BackupCreateService.BACKUP_CATEGORY
2 -> flags = flags or BackupCreateService.BACKUP_CHAPTER 2 -> flags = flags or BackupCreateService.BACKUP_CHAPTER
@ -281,7 +280,7 @@ class SettingsBackupController : SettingsController() {
override fun onCreateDialog(savedViewState: Bundle?): Dialog { override fun onCreateDialog(savedViewState: Bundle?): Dialog {
val activity = activity!! val activity = activity!!
val unifile = UniFile.fromUri(activity, args.getParcelable<Uri>(KEY_URI)) val unifile = UniFile.fromUri(activity, args.getParcelable(KEY_URI))
return MaterialDialog.Builder(activity) return MaterialDialog.Builder(activity)
.title(R.string.backup_created) .title(R.string.backup_created)
.content(activity.getString(R.string.file_saved, unifile.filePath)) .content(activity.getString(R.string.file_saved, unifile.filePath))
@ -315,7 +314,7 @@ class SettingsBackupController : SettingsController() {
val context = applicationContext val context = applicationContext
if (context != null) { if (context != null) {
RestoringBackupDialog().showDialog(router, TAG_RESTORING_BACKUP_DIALOG) RestoringBackupDialog().showDialog(router, TAG_RESTORING_BACKUP_DIALOG)
BackupRestoreService.start(context, args.getParcelable<Uri>(KEY_URI)) BackupRestoreService.start(context, args.getParcelable(KEY_URI))
} }
} }
.build() .build()