Pass backup uri as parcelable to restore service

This commit is contained in:
len 2017-04-25 16:29:39 +02:00
parent f88794c752
commit 8df3080e0d
2 changed files with 13 additions and 29 deletions

View File

@ -10,7 +10,6 @@ import com.github.salomonbrys.kotson.fromJson
import com.google.gson.JsonArray import com.google.gson.JsonArray
import com.google.gson.JsonParser import com.google.gson.JsonParser
import com.google.gson.stream.JsonReader import com.google.gson.stream.JsonReader
import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.backup.models.Backup.CATEGORIES import eu.kanade.tachiyomi.data.backup.models.Backup.CATEGORIES
import eu.kanade.tachiyomi.data.backup.models.Backup.CHAPTERS import eu.kanade.tachiyomi.data.backup.models.Backup.CHAPTERS
@ -67,7 +66,7 @@ class BackupRestoreService : Service() {
* @param context context of application * @param context context of application
* @param uri path of Uri * @param uri path of Uri
*/ */
fun start(context: Context, uri: String) { fun start(context: Context, uri: Uri) {
if (!isRunning(context)) { if (!isRunning(context)) {
val intent = Intent(context, BackupRestoreService::class.java).apply { val intent = Intent(context, BackupRestoreService::class.java).apply {
putExtra(EXTRA_URI, uri) putExtra(EXTRA_URI, uri)
@ -165,14 +164,14 @@ class BackupRestoreService : Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent == null) return Service.START_NOT_STICKY if (intent == null) return Service.START_NOT_STICKY
val file = UniFile.fromUri(this, Uri.parse(intent.getStringExtra(EXTRA_URI))) val uri = intent.getParcelableExtra<Uri>(EXTRA_URI)
// Unsubscribe from any previous subscription if needed. // Unsubscribe from any previous subscription if needed.
subscription?.unsubscribe() subscription?.unsubscribe()
subscription = Observable.using( subscription = Observable.using(
{ db.lowLevel().beginTransaction() }, { db.lowLevel().beginTransaction() },
{ getRestoreObservable(file).doOnNext{ db.lowLevel().setTransactionSuccessful() } }, { getRestoreObservable(uri).doOnNext{ db.lowLevel().setTransactionSuccessful() } },
{ executor.execute { db.lowLevel().endTransaction() } }) { executor.execute { db.lowLevel().endTransaction() } })
.doAfterTerminate { stopSelf(startId) } .doAfterTerminate { stopSelf(startId) }
.subscribeOn(Schedulers.from(executor)) .subscribeOn(Schedulers.from(executor))
@ -184,13 +183,13 @@ class BackupRestoreService : Service() {
/** /**
* Returns an [Observable] containing restore process. * Returns an [Observable] containing restore process.
* *
* @param file restore file * @param uri restore file
* @return [Observable<Manga>] * @return [Observable<Manga>]
*/ */
private fun getRestoreObservable(file: UniFile): Observable<List<Manga>> { private fun getRestoreObservable(uri: Uri): Observable<List<Manga>> {
val startTime = System.currentTimeMillis() val startTime = System.currentTimeMillis()
val reader = JsonReader(file.openInputStream().bufferedReader()) val reader = JsonReader(contentResolver.openInputStream(uri).bufferedReader())
val json = JsonParser().parse(reader).asJsonObject val json = JsonParser().parse(reader).asJsonObject
// Get parser version // Get parser version

View File

@ -284,17 +284,11 @@ class SettingsBackupFragment : SettingsFragment() {
} }
restoreBackup.setOnPreferenceClickListener { restoreBackup.setOnPreferenceClickListener {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { val intent = Intent(Intent.ACTION_GET_CONTENT)
val intent = Intent() intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "application/*" intent.type = "application/*"
intent.action = Intent.ACTION_GET_CONTENT val chooser = Intent.createChooser(intent, getString(R.string.file_select_backup))
startActivityForResult(Intent.createChooser(intent, getString(R.string.file_select_backup)), BACKUP_RESTORE) startActivityForResult(chooser, BACKUP_RESTORE)
} else {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type = "application/*"
startActivityForResult(intent, BACKUP_RESTORE)
}
true true
} }
@ -385,16 +379,7 @@ class SettingsBackupFragment : SettingsFragment() {
} }
} }
BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) { BACKUP_RESTORE -> if (data != null && resultCode == Activity.RESULT_OK) {
val uri = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { val uri = data.data
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
}
MaterialDialog.Builder(context) MaterialDialog.Builder(context)
.title(getString(R.string.pref_restore_backup)) .title(getString(R.string.pref_restore_backup))
@ -402,7 +387,7 @@ class SettingsBackupFragment : SettingsFragment() {
.positiveText(getString(R.string.action_restore)) .positiveText(getString(R.string.action_restore))
.onPositive { _, _ -> .onPositive { _, _ ->
restoreDialog.safeShow() restoreDialog.safeShow()
BackupRestoreService.start(context, uri.toString()) BackupRestoreService.start(context, uri)
} }
.safeShow() .safeShow()
} }