From 8df3080e0d0c11b06d2999019a415b0790ca0a5a Mon Sep 17 00:00:00 2001 From: len Date: Tue, 25 Apr 2017 16:29:39 +0200 Subject: [PATCH] Pass backup uri as parcelable to restore service --- .../data/backup/BackupRestoreService.kt | 13 ++++----- .../ui/setting/SettingsBackupFragment.kt | 29 +++++-------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt index f4c0223161..02df6b303d 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/backup/BackupRestoreService.kt @@ -10,7 +10,6 @@ import com.github.salomonbrys.kotson.fromJson import com.google.gson.JsonArray import com.google.gson.JsonParser import com.google.gson.stream.JsonReader -import com.hippo.unifile.UniFile import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.backup.models.Backup.CATEGORIES import eu.kanade.tachiyomi.data.backup.models.Backup.CHAPTERS @@ -67,7 +66,7 @@ class BackupRestoreService : Service() { * @param context context of application * @param uri path of Uri */ - fun start(context: Context, uri: String) { + fun start(context: Context, uri: Uri) { if (!isRunning(context)) { val intent = Intent(context, BackupRestoreService::class.java).apply { putExtra(EXTRA_URI, uri) @@ -165,14 +164,14 @@ class BackupRestoreService : Service() { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { if (intent == null) return Service.START_NOT_STICKY - val file = UniFile.fromUri(this, Uri.parse(intent.getStringExtra(EXTRA_URI))) + val uri = intent.getParcelableExtra(EXTRA_URI) // Unsubscribe from any previous subscription if needed. subscription?.unsubscribe() subscription = Observable.using( { db.lowLevel().beginTransaction() }, - { getRestoreObservable(file).doOnNext{ db.lowLevel().setTransactionSuccessful() } }, + { getRestoreObservable(uri).doOnNext{ db.lowLevel().setTransactionSuccessful() } }, { executor.execute { db.lowLevel().endTransaction() } }) .doAfterTerminate { stopSelf(startId) } .subscribeOn(Schedulers.from(executor)) @@ -184,13 +183,13 @@ class BackupRestoreService : Service() { /** * Returns an [Observable] containing restore process. * - * @param file restore file + * @param uri restore file * @return [Observable] */ - private fun getRestoreObservable(file: UniFile): Observable> { + private fun getRestoreObservable(uri: Uri): Observable> { val startTime = System.currentTimeMillis() - val reader = JsonReader(file.openInputStream().bufferedReader()) + val reader = JsonReader(contentResolver.openInputStream(uri).bufferedReader()) val json = JsonParser().parse(reader).asJsonObject // Get parser version diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupFragment.kt index f05274c8ec..7ef9a70cd8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/setting/SettingsBackupFragment.kt @@ -284,17 +284,11 @@ class SettingsBackupFragment : SettingsFragment() { } restoreBackup.setOnPreferenceClickListener { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { - val intent = Intent() - intent.type = "application/*" - intent.action = Intent.ACTION_GET_CONTENT - startActivityForResult(Intent.createChooser(intent, getString(R.string.file_select_backup)), BACKUP_RESTORE) - } else { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT) - intent.addCategory(Intent.CATEGORY_OPENABLE) - intent.type = "application/*" - startActivityForResult(intent, BACKUP_RESTORE) - } + val intent = Intent(Intent.ACTION_GET_CONTENT) + intent.addCategory(Intent.CATEGORY_OPENABLE) + intent.type = "application/*" + val chooser = Intent.createChooser(intent, getString(R.string.file_select_backup)) + startActivityForResult(chooser, BACKUP_RESTORE) true } @@ -385,16 +379,7 @@ class SettingsBackupFragment : SettingsFragment() { } } BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) { - val uri = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { - Uri.fromFile(File(data.data.path)) - } else { - val uri = data.data - val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or - Intent.FLAG_GRANT_WRITE_URI_PERMISSION - - context.contentResolver.takePersistableUriPermission(uri, flags) - UniFile.fromUri(context, uri).uri - } + val uri = data.data MaterialDialog.Builder(context) .title(getString(R.string.pref_restore_backup)) @@ -402,7 +387,7 @@ class SettingsBackupFragment : SettingsFragment() { .positiveText(getString(R.string.action_restore)) .onPositive { _, _ -> restoreDialog.safeShow() - BackupRestoreService.start(context, uri.toString()) + BackupRestoreService.start(context, uri) } .safeShow() }