pull/457/head
gedoor 4 years ago
parent c08215c196
commit 0e4e5c7eda
  1. 70
      app/src/main/java/io/legado/app/help/storage/ImportOldData.kt
  2. 16
      app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt
  3. 54
      app/src/main/java/io/legado/app/ui/book/local/ImportBookActivity.kt
  4. 7
      app/src/main/java/io/legado/app/ui/book/read/config/BgTextConfigDialog.kt
  5. 12
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  6. 4
      app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt
  7. 52
      app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt
  8. 20
      app/src/main/java/io/legado/app/ui/filepicker/FilePickerDialog.kt
  9. 4
      app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt
  10. 12
      app/src/main/java/io/legado/app/ui/replace/ReplaceRuleActivity.kt
  11. 12
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt
  12. 12
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  13. 19
      app/src/main/java/io/legado/app/ui/widget/font/FontSelectDialog.kt
  14. 5
      app/src/main/res/menu/import_book.xml
  15. 1
      app/src/main/res/values-zh-rHK/strings.xml
  16. 1
      app/src/main/res/values-zh-rTW/strings.xml
  17. 1
      app/src/main/res/values-zh/strings.xml
  18. 1
      app/src/main/res/values/strings.xml

@ -7,12 +7,48 @@ import io.legado.app.App
import io.legado.app.data.entities.BookSource
import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.FileUtils
import io.legado.app.utils.isContentPath
import org.jetbrains.anko.toast
import java.io.File
object ImportOldData {
fun import(context: Context, file: File) {
fun importUri(context: Context, uri: Uri) {
if (uri.isContentPath()) {
DocumentFile.fromTreeUri(context, uri)?.listFiles()?.forEach {
when (it.name) {
"myBookShelf.json" ->
try {
DocumentUtils.readText(context, it.uri)?.let { json ->
val importCount = importOldBookshelf(json)
context.toast("成功导入书籍${importCount}")
}
} catch (e: java.lang.Exception) {
context.toast("导入书籍失败\n${e.localizedMessage}")
}
"myBookSource.json" ->
try {
DocumentUtils.readText(context, it.uri)?.let { json ->
val importCount = importOldSource(json)
context.toast("成功导入书源${importCount}")
}
} catch (e: Exception) {
context.toast("导入源失败\n${e.localizedMessage}")
}
"myBookReplaceRule.json" ->
try {
DocumentUtils.readText(context, it.uri)?.let { json ->
val importCount = importOldReplaceRule(json)
context.toast("成功导入替换规则${importCount}")
}
} catch (e: Exception) {
context.toast("导入替换规则失败\n${e.localizedMessage}")
}
}
}
} else {
uri.path?.let {
val file = File(it)
try {// 导入书架
val shelfFile =
FileUtils.createFileIfNotExist(file, "myBookShelf.json")
@ -46,38 +82,6 @@ object ImportOldData {
context.toast("导入替换规则失败\n${e.localizedMessage}")
}
}
fun importUri(uri: Uri) {
DocumentFile.fromTreeUri(App.INSTANCE, uri)?.listFiles()?.forEach {
when (it.name) {
"myBookShelf.json" ->
try {
DocumentUtils.readText(App.INSTANCE, it.uri)?.let { json ->
val importCount = importOldBookshelf(json)
App.INSTANCE.toast("成功导入书籍${importCount}")
}
} catch (e: java.lang.Exception) {
App.INSTANCE.toast("导入书籍失败\n${e.localizedMessage}")
}
"myBookSource.json" ->
try {
DocumentUtils.readText(App.INSTANCE, it.uri)?.let { json ->
val importCount = importOldSource(json)
App.INSTANCE.toast("成功导入书源${importCount}")
}
} catch (e: Exception) {
App.INSTANCE.toast("导入源失败\n${e.localizedMessage}")
}
"myBookReplaceRule.json" ->
try {
DocumentUtils.readText(App.INSTANCE, it.uri)?.let { json ->
val importCount = importOldReplaceRule(json)
App.INSTANCE.toast("成功导入替换规则${importCount}")
}
} catch (e: Exception) {
App.INSTANCE.toast("导入替换规则失败\n${e.localizedMessage}")
}
}
}
}

@ -202,26 +202,24 @@ class CacheActivity : VMBaseActivity<CacheViewModel>(R.layout.activity_download)
}
}
override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) {
exportRequestCode -> {
ACache.get(this@CacheActivity).put(exportBookPathKey, currentPath)
startExport(currentPath)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
exportRequestCode -> if (resultCode == Activity.RESULT_OK) {
data?.data?.let { uri ->
if (uri.isContentPath()) {
contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
ACache.get(this@CacheActivity).put(exportBookPathKey, uri.toString())
startExport(uri.toString())
} else {
uri.path?.let { path ->
ACache.get(this@CacheActivity).put(exportBookPathKey, path)
startExport(path)
}
}
}
}

@ -62,6 +62,24 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
return super.onCompatCreateOptionsMenu(menu)
}
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_select_folder -> FilePicker.selectFolder(this, requestCodeSelectFolder)
R.id.menu_scan_folder -> scanFolder()
}
return super.onCompatOptionsItemSelected(item)
}
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_del_selection ->
viewModel.deleteDoc(adapter.selectedUris) {
upPath()
}
}
return false
}
private fun initView() {
lay_top.setBackgroundColor(backgroundColor)
recycler_view.layoutManager = LinearLayoutManager(this)
@ -94,23 +112,6 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
}
}
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_select_folder -> FilePicker.selectFolder(this, requestCodeSelectFolder)
}
return super.onCompatOptionsItemSelected(item)
}
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_del_selection ->
viewModel.deleteDoc(adapter.selectedUris) {
upPath()
}
}
return false
}
private fun initData() {
localUriLiveData?.removeObservers(this)
localUriLiveData = App.db.bookDao().observeLocalUri()
@ -229,12 +230,12 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
adapter.setData(docList)
}
override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) {
requestCodeSelectFolder -> {
AppConfig.importBookPath = currentPath
initRootDoc()
}
/**
* 扫描当前文件夹
*/
private fun scanFolder() {
rootDoc?.let {
}
}
@ -243,12 +244,19 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
when (requestCode) {
requestCodeSelectFolder -> if (resultCode == Activity.RESULT_OK) {
data?.data?.let { uri ->
if (uri.isContentPath()) {
contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
AppConfig.importBookPath = uri.toString()
initRootDoc()
} else {
uri.path?.let { path ->
AppConfig.importBookPath = path
initRootDoc()
}
}
}
}
}

@ -369,13 +369,6 @@ class BgTextConfigDialog : BaseDialogFragment(), FilePickerDialog.CallBack {
}
}
override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) {
requestCodeImport -> importConfig(Uri.fromFile(File(currentPath)))
requestCodeExport -> exportConfig(Uri.fromFile(File(currentPath)))
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {

@ -457,18 +457,6 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
viewModel.bottomSource(bookSource)
}
override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) {
exportRequestCode -> viewModel.exportSelection(
adapter.getSelection(),
File(currentPath)
)
importRequestCode -> {
startActivity<ImportBookSourceActivity>(Pair("filePath", currentPath))
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {

@ -133,10 +133,6 @@ class BackupConfigFragment : BasePreferenceFragment(),
}
}
override fun onFilePicked(requestCode: Int, currentPath: String) {
BackupRestoreUi.onFilePicked(requestCode, currentPath)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
BackupRestoreUi.onActivityResult(requestCode, resultCode, data)

@ -23,7 +23,6 @@ import io.legado.app.utils.longToast
import io.legado.app.utils.toast
import kotlinx.coroutines.Dispatchers.Main
import org.jetbrains.anko.toast
import java.io.File
object BackupRestoreUi {
private const val selectFolderRequestCode = 21
@ -122,35 +121,11 @@ object BackupRestoreUi {
FilePicker.selectFolder(fragment, oldDataRequestCode)
}
fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) {
backupSelectRequestCode -> {
AppConfig.backupPath = currentPath
Coroutine.async {
Backup.backup(App.INSTANCE, currentPath)
}.onSuccess {
App.INSTANCE.toast(R.string.backup_success)
}
}
restoreSelectRequestCode -> {
AppConfig.backupPath = currentPath
Coroutine.async {
Restore.restore(App.INSTANCE, currentPath)
}
}
selectFolderRequestCode -> {
AppConfig.backupPath = currentPath
}
oldDataRequestCode -> {
ImportOldData.import(App.INSTANCE, File(currentPath))
}
}
}
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
when (requestCode) {
backupSelectRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.isContentPath()) {
App.INSTANCE.contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
@ -162,10 +137,21 @@ object BackupRestoreUi {
}.onSuccess {
App.INSTANCE.toast(R.string.backup_success)
}
} else {
uri.path?.let { path ->
AppConfig.backupPath = path
Coroutine.async {
Backup.backup(App.INSTANCE, path)
}.onSuccess {
App.INSTANCE.toast(R.string.backup_success)
}
}
}
}
}
restoreSelectRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.isContentPath()) {
App.INSTANCE.contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
@ -175,21 +161,33 @@ object BackupRestoreUi {
Coroutine.async {
Restore.restore(App.INSTANCE, uri.toString())
}
} else {
uri.path?.let { path ->
AppConfig.backupPath = path
Coroutine.async {
Restore.restore(App.INSTANCE, path)
}
}
}
}
}
selectFolderRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.isContentPath()) {
App.INSTANCE.contentResolver.takePersistableUriPermission(
uri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
AppConfig.backupPath = uri.toString()
} else {
AppConfig.backupPath = uri.path
}
}
}
oldDataRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
ImportOldData.importUri(uri)
ImportOldData.importUri(App.INSTANCE, uri)
}
}
}

@ -1,5 +1,8 @@
package io.legado.app.ui.filepicker
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.MenuItem
@ -17,6 +20,7 @@ import io.legado.app.ui.filepicker.adapter.PathAdapter
import io.legado.app.ui.widget.recycler.VerticalDivider
import io.legado.app.utils.*
import kotlinx.android.synthetic.main.dialog_file_chooser.*
import java.io.File
class FilePickerDialog : DialogFragment(),
@ -145,8 +149,11 @@ class FilePickerDialog : DialogFragment(),
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_ok -> fileAdapter.currentPath?.let {
(parentFragment as? CallBack)?.onFilePicked(requestCode, it)
(activity as? CallBack)?.onFilePicked(requestCode, it)
val data = Intent().setData(Uri.fromFile(File(it)))
(parentFragment as? CallBack)
?.onActivityResult(requestCode, Activity.RESULT_OK, data)
(activity as? CallBack)
?.onActivityResult(requestCode, Activity.RESULT_OK, data)
dismiss()
}
else -> item?.title?.let {
@ -169,8 +176,11 @@ class FilePickerDialog : DialogFragment(),
} else if (allowExtensions == null ||
allowExtensions?.contains(FileUtils.getExtension(path)) == true
) {
(parentFragment as? CallBack)?.onFilePicked(requestCode, path)
(activity as? CallBack)?.onFilePicked(requestCode, path)
val data = Intent().setData(Uri.fromFile(File(path)))
(parentFragment as? CallBack)
?.onActivityResult(requestCode, Activity.RESULT_OK, data)
(activity as? CallBack)
?.onActivityResult(requestCode, Activity.RESULT_OK, data)
dismiss()
} else {
toast("不能打开此文件")
@ -206,7 +216,7 @@ class FilePickerDialog : DialogFragment(),
}
interface CallBack {
fun onFilePicked(requestCode: Int, currentPath: String)
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
fun onMenuClick(menu: String) {}
}
}

@ -56,10 +56,6 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config), FilePickerDialog.C
}
}
override fun onFilePicked(requestCode: Int, currentPath: String) {
BackupRestoreUi.onFilePicked(requestCode, currentPath)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
BackupRestoreUi.onActivityResult(requestCode, resultCode, data)

@ -244,18 +244,6 @@ class ReplaceRuleActivity :
return false
}
override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) {
importRequestCode -> {
startActivity<ImportReplaceRuleActivity>("filePath" to currentPath)
}
exportRequestCode -> viewModel.exportSelection(
adapter.getSelection(),
File(currentPath)
)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {

@ -294,20 +294,12 @@ class ReadRssActivity : VMBaseActivity<ReadRssViewModel>(R.layout.activity_rss_r
}
}
override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) {
savePathRequestCode -> {
ACache.get(this).put(imagePathKey, currentPath)
viewModel.saveImage(webPic, currentPath)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
savePathRequestCode -> data?.data?.let {
onFilePicked(requestCode, it.toString())
ACache.get(this).put(imagePathKey, it.toString())
viewModel.saveImage(webPic, it.toString())
}
}
}

@ -245,18 +245,6 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
}.show().applyTint()
}
override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) {
importRequestCode -> {
startActivity<ImportRssSourceActivity>("filePath" to currentPath)
}
exportRequestCode -> viewModel.exportSelection(
adapter.getSelection(),
File(currentPath)
)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {

@ -204,23 +204,12 @@ class FontSelectDialog : BaseDialogFragment(),
}
}
/**
* 字体文件夹
*/
override fun onFilePicked(requestCode: Int, currentPath: String) {
when (requestCode) {
fontFolderRequestCode -> {
putPrefString(PreferKey.fontFolder, currentPath)
loadFontFilesByPermission(currentPath)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
fontFolderRequestCode -> if (resultCode == RESULT_OK) {
data?.data?.let { uri ->
if (uri.toString().isContentPath()) {
putPrefString(PreferKey.fontFolder, uri.toString())
val doc = DocumentFile.fromTreeUri(requireContext(), uri)
if (doc != null) {
@ -234,6 +223,12 @@ class FontSelectDialog : BaseDialogFragment(),
loadFontFilesByPermission(it)
}
}
} else {
uri.path?.let { path ->
putPrefString(PreferKey.fontFolder, path)
loadFontFilesByPermission(path)
}
}
}
}
}

@ -8,4 +8,9 @@
android:icon="@drawable/ic_folder_open"
app:showAsAction="ifRoom" />
<item
android:id="@+id/menu_scan_folder"
android:title="@string/scan_folder"
app:showAsAction="never" />
</menu>

@ -777,5 +777,6 @@
<string name="re_segment">重新分段</string>
<string name="style_name">样式名称:</string>
<string name="empty_msg_import_book">点击右上角文件夹图标,选择文件夹</string>
<string name="scan_folder">智能扫描</string>
</resources>

@ -777,5 +777,6 @@
<string name="re_segment">重新分段</string>
<string name="style_name">样式名称:</string>
<string name="empty_msg_import_book">点击右上角文件夹图标,选择文件夹</string>
<string name="scan_folder">智能扫描</string>
</resources>

@ -780,5 +780,6 @@
<string name="re_segment">重新分段</string>
<string name="style_name">样式名称:</string>
<string name="empty_msg_import_book">点击右上角文件夹图标,选择文件夹</string>
<string name="scan_folder">智能扫描</string>
</resources>

@ -783,5 +783,6 @@
<string name="re_segment">重新分段</string>
<string name="style_name">样式名称:</string>
<string name="empty_msg_import_book">点击右上角文件夹图标,选择文件夹</string>
<string name="scan_folder">智能扫描</string>
</resources>

Loading…
Cancel
Save