From eb662f12348d44cbd0c4f26353334c62667186d8 Mon Sep 17 00:00:00 2001 From: len Date: Thu, 14 Apr 2016 17:24:34 +0200 Subject: [PATCH] Fix some crashes when restoring backups --- app/build.gradle | 2 +- .../tachiyomi/ui/backup/BackupFragment.kt | 31 +++++++++++++++++-- .../ui/base/activity/BaseActivity.kt | 21 +++++++++++++ .../tachiyomi/ui/manga/MangaActivity.kt | 18 ----------- app/src/main/res/values/strings.xml | 1 + 5 files changed, 51 insertions(+), 22 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 744af7baf1..7333b8ee87 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -125,7 +125,7 @@ dependencies { compile "com.squareup.retrofit2:adapter-rxjava:$RETROFIT_VERSION" // IO - compile 'com.squareup.okio:okio:1.6.0' + compile 'com.squareup.okio:okio:1.7.0' // JSON compile 'com.google.code.gson:gson:2.6.2' diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/backup/BackupFragment.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/backup/BackupFragment.kt index f840944e05..153aed5390 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/backup/BackupFragment.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/backup/BackupFragment.kt @@ -14,6 +14,11 @@ import eu.kanade.tachiyomi.ui.base.fragment.BaseRxFragment import eu.kanade.tachiyomi.util.toast import kotlinx.android.synthetic.main.fragment_backup.* import nucleus.factory.RequiresPresenter +import rx.Observable +import rx.android.schedulers.AndroidSchedulers +import rx.internal.util.SubscriptionList +import rx.schedulers.Schedulers +import timber.log.Timber import java.io.File import java.text.SimpleDateFormat import java.util.* @@ -28,11 +33,16 @@ class BackupFragment : BaseRxFragment() { private var backupDialog: Dialog? = null private var restoreDialog: Dialog? = null + private lateinit var subscriptions: SubscriptionList + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?): View { return inflater.inflate(R.layout.fragment_backup, container, false) } override fun onViewCreated(view: View, savedState: Bundle?) { + baseActivity.requestPermissionsOnMarshmallow() + subscriptions = SubscriptionList() + backup_button.setOnClickListener { val today = SimpleDateFormat("yyyy-MM-dd").format(Date()) val file = File(activity.externalCacheDir, "tachiyomi-$today.json") @@ -48,11 +58,16 @@ class BackupFragment : BaseRxFragment() { val intent = Intent(Intent.ACTION_GET_CONTENT) intent.addCategory(Intent.CATEGORY_OPENABLE) intent.type = "application/octet-stream" - val chooser = Intent.createChooser(intent, getString(R.string.file_select_cover)) + val chooser = Intent.createChooser(intent, getString(R.string.file_select_backup)) startActivityForResult(chooser, REQUEST_BACKUP_OPEN) } } + override fun onDestroyView() { + subscriptions.unsubscribe() + super.onDestroyView() + } + /** * Called from the presenter when the backup is completed. * @@ -99,8 +114,18 @@ class BackupFragment : BaseRxFragment() { .progress(true, 0) .show() - val stream = context.contentResolver.openInputStream(data.data) - presenter.restoreBackup(stream) + // When using cloud services, we have to open the input stream in a background thread. + Observable.fromCallable { context.contentResolver.openInputStream(data.data) } + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + presenter.restoreBackup(it) + }, { + context.toast(it.message) + Timber.e(it, it.message) + }) + .apply { subscriptions.add(this) } + } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt index 9e8d1a23e4..06bf3a7146 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/activity/BaseActivity.kt @@ -1,7 +1,12 @@ package eu.kanade.tachiyomi.ui.base.activity +import android.Manifest +import android.content.pm.PackageManager import android.graphics.Color +import android.os.Build import android.support.design.widget.Snackbar +import android.support.v4.app.ActivityCompat +import android.support.v4.content.ContextCompat import android.support.v7.app.AppCompatActivity import android.support.v7.widget.Toolbar import android.view.View @@ -42,6 +47,22 @@ open class BaseActivity : AppCompatActivity() { supportActionBar?.subtitle = getString(titleResource) } + /** + * Requests read and write permissions on Android M and higher. + */ + fun requestPermissionsOnMarshmallow() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (ContextCompat.checkSelfPermission(this, + Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + + ActivityCompat.requestPermissions(this, + arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE), + 1) + + } + } + } + protected val app: App get() = App.get(this) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaActivity.kt index 0e963db7e3..00e74d7f74 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaActivity.kt @@ -1,16 +1,11 @@ package eu.kanade.tachiyomi.ui.manga -import android.Manifest import android.content.Context import android.content.Intent -import android.content.pm.PackageManager -import android.os.Build import android.os.Bundle -import android.support.v4.app.ActivityCompat import android.support.v4.app.Fragment import android.support.v4.app.FragmentManager import android.support.v4.app.FragmentPagerAdapter -import android.support.v4.content.ContextCompat import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.event.MangaEvent @@ -69,19 +64,6 @@ class MangaActivity : BaseRxActivity() { setToolbarTitle(manga.title) } - private fun requestPermissionsOnMarshmallow() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - if (ContextCompat.checkSelfPermission(this, - Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { - - ActivityCompat.requestPermissions(this, - arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE), - 1) - - } - } - } - internal class MangaDetailAdapter(fm: FragmentManager, activity: MangaActivity) : FragmentPagerAdapter(fm) { private var pageCount: Int = 0 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fa73030f7e..c9ebb5c75f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -265,6 +265,7 @@ Select cover image + Select backup file New update available!