From a6275b5af91b073cd7ccaaef52734c33c591f95c Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 7 Jan 2020 08:53:39 +0800 Subject: [PATCH] up --- .../io/legado/app/ui/main/MainActivity.kt | 117 ---------------- .../io/legado/app/ui/main/my/MyFragment.kt | 126 +++++++++++++++++- 2 files changed, 119 insertions(+), 124 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 30e72a0dd..39a5ad05f 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -1,12 +1,8 @@ package io.legado.app.ui.main -import android.app.Activity -import android.content.Intent -import android.net.Uri import android.os.Bundle import android.view.KeyEvent import android.view.MenuItem -import androidx.documentfile.provider.DocumentFile import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentStatePagerAdapter @@ -17,12 +13,6 @@ import io.legado.app.BuildConfig import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.Bus -import io.legado.app.constant.PreferKey -import io.legado.app.help.permission.Permissions -import io.legado.app.help.permission.PermissionsCompat -import io.legado.app.help.storage.Backup -import io.legado.app.help.storage.Restore -import io.legado.app.help.storage.WebDavHelp import io.legado.app.lib.theme.ATH import io.legado.app.service.BaseReadAloudService import io.legado.app.service.help.ReadAloud @@ -34,7 +24,6 @@ import io.legado.app.ui.main.rss.RssFragment import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.launch -import org.jetbrains.anko.toast class MainActivity : VMBaseActivity(R.layout.activity_main), BottomNavigationView.OnNavigationItemSelectedListener, @@ -126,7 +115,6 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), override fun finish() { if (!BuildConfig.DEBUG) { launch { - backup() super.finish() } } else { @@ -153,111 +141,6 @@ class MainActivity : VMBaseActivity(R.layout.activity_main), } } - fun backup() { - val backupPath = getPrefString(PreferKey.backupPath) - if (backupPath?.isNotEmpty() == true) { - val uri = Uri.parse(backupPath) - val doc = DocumentFile.fromTreeUri(this, uri) - if (doc?.canWrite() == true) { - launch { - Backup.backup(this@MainActivity, uri) - } - } else { - selectBackupFolder() - } - } else { - selectBackupFolder() - } - } - - fun restore() { - launch { - if (!WebDavHelp.showRestoreDialog(this@MainActivity)) { - val backupPath = getPrefString(PreferKey.backupPath) - if (backupPath?.isNotEmpty() == true) { - val uri = Uri.parse(backupPath) - val doc = DocumentFile.fromTreeUri(this@MainActivity, uri) - if (doc?.canWrite() == true) { - Restore.restore(this@MainActivity, uri) - toast(R.string.restore_success) - } else { - selectBackupFolder() - } - } else { - selectRestoreFolder() - } - } - } - } - - private fun selectBackupFolder() { - try { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - startActivityForResult(intent, backupSelectRequestCode) - } catch (e: Exception) { - PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - launch { - Backup.backup(this@MainActivity, null) - } - } - .request() - } - } - - private fun selectRestoreFolder() { - try { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) - intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - startActivityForResult(intent, restoreSelectRequestCode) - } catch (e: java.lang.Exception) { - PermissionsCompat.Builder(this) - .addPermissions(*Permissions.Group.STORAGE) - .rationale(R.string.tip_perm_request_storage) - .onGranted { - launch { - Restore.restore(Backup.legadoPath) - toast(R.string.restore_success) - } - } - .request() - } - } - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - when (requestCode) { - backupSelectRequestCode -> if (resultCode == Activity.RESULT_OK) { - data?.data?.let { uri -> - contentResolver.takePersistableUriPermission( - uri, - Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION - ) - putPrefString(PreferKey.backupPath, uri.toString()) - launch { - Backup.backup(this@MainActivity, uri) - } - } - } - restoreSelectRequestCode -> if (resultCode == Activity.RESULT_OK) { - data?.data?.let { uri -> - contentResolver.takePersistableUriPermission( - uri, - Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION - ) - putPrefString(PreferKey.backupPath, uri.toString()) - launch { - Restore.restore(this@MainActivity, uri) - toast(R.string.restore_success) - } - } - } - } - } - private inner class TabFragmentPageAdapter internal constructor(fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) { diff --git a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt index 5b2a24114..6d79f55d4 100644 --- a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt @@ -1,10 +1,14 @@ package io.legado.app.ui.main.my +import android.app.Activity +import android.content.Intent import android.content.SharedPreferences +import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View +import androidx.documentfile.provider.DocumentFile import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.preference.SwitchPreference @@ -12,7 +16,13 @@ import io.legado.app.App import io.legado.app.R import io.legado.app.base.BaseFragment import io.legado.app.constant.Bus +import io.legado.app.constant.PreferKey import io.legado.app.help.BookHelp +import io.legado.app.help.permission.Permissions +import io.legado.app.help.permission.PermissionsCompat +import io.legado.app.help.storage.Backup +import io.legado.app.help.storage.Restore +import io.legado.app.help.storage.WebDavHelp import io.legado.app.lib.theme.ATH import io.legado.app.service.WebService import io.legado.app.ui.about.AboutActivity @@ -20,13 +30,15 @@ import io.legado.app.ui.about.DonateActivity import io.legado.app.ui.book.source.manage.BookSourceActivity import io.legado.app.ui.config.ConfigActivity import io.legado.app.ui.config.ConfigViewModel -import io.legado.app.ui.main.MainActivity import io.legado.app.ui.replacerule.ReplaceRuleActivity import io.legado.app.utils.* import kotlinx.android.synthetic.main.view_title_bar.* +import kotlinx.coroutines.launch import org.jetbrains.anko.startActivity class MyFragment : BaseFragment(R.layout.fragment_my_config) { + private val backupSelectRequestCode = 22 + private val restoreSelectRequestCode = 33 override fun onViewCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) @@ -43,13 +55,113 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config) { override fun onCompatOptionsItemSelected(item: MenuItem) { when (item.itemId) { R.id.menu_help -> startActivity() - R.id.menu_backup -> { - val activity = activity as? MainActivity - activity?.backup() + R.id.menu_backup -> backup() + R.id.menu_restore -> restore() + } + } + + + private fun backup() { + val backupPath = getPrefString(PreferKey.backupPath) + if (backupPath?.isNotEmpty() == true) { + val uri = Uri.parse(backupPath) + val doc = DocumentFile.fromTreeUri(requireContext(), uri) + if (doc?.canWrite() == true) { + launch { + Backup.backup(requireContext(), uri) + } + } else { + selectBackupFolder() + } + } else { + selectBackupFolder() + } + } + + private fun selectBackupFolder() { + try { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + startActivityForResult(intent, backupSelectRequestCode) + } catch (e: java.lang.Exception) { + PermissionsCompat.Builder(this) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + launch { + Backup.backup(requireContext(), null) + } + } + .request() + } + } + + fun restore() { + launch { + if (!WebDavHelp.showRestoreDialog(requireContext())) { + val backupPath = getPrefString(PreferKey.backupPath) + if (backupPath?.isNotEmpty() == true) { + val uri = Uri.parse(backupPath) + val doc = DocumentFile.fromTreeUri(requireContext(), uri) + if (doc?.canWrite() == true) { + Restore.restore(requireContext(), uri) + toast(R.string.restore_success) + } else { + selectBackupFolder() + } + } else { + selectRestoreFolder() + } } - R.id.menu_restore -> { - val activity = activity as? MainActivity - activity?.restore() + } + } + + private fun selectRestoreFolder() { + try { + val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + startActivityForResult(intent, restoreSelectRequestCode) + } catch (e: java.lang.Exception) { + PermissionsCompat.Builder(this) + .addPermissions(*Permissions.Group.STORAGE) + .rationale(R.string.tip_perm_request_storage) + .onGranted { + launch { + Restore.restore(Backup.legadoPath) + toast(R.string.restore_success) + } + } + .request() + } + } + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + when (requestCode) { + backupSelectRequestCode -> if (resultCode == Activity.RESULT_OK) { + data?.data?.let { uri -> + requireContext().contentResolver.takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + ) + putPrefString(PreferKey.backupPath, uri.toString()) + launch { + Backup.backup(requireContext(), uri) + } + } + } + restoreSelectRequestCode -> if (resultCode == Activity.RESULT_OK) { + data?.data?.let { uri -> + requireContext().contentResolver.takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + ) + putPrefString(PreferKey.backupPath, uri.toString()) + launch { + Restore.restore(requireContext(), uri) + toast(R.string.restore_success) + } + } } } }