diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index a1018b991..344463246 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -3,9 +3,10 @@ * 关注合作公众号 **[小说拾遗]** 获取好看的小说。 * 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 -**2021/03/26** +**2021/03/31** * 优化epubLib by ag2s20150909 * 升级库,修改弃用方法 +* tts引擎添加导入导出功能 **2021/03/23** * 修复繁简转换“勐”“十”问题。使用了剥离HanLP简繁代码的民间库。APK减少6M左右 diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt index de629e355..d53b987e1 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineDialog.kt @@ -41,11 +41,16 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener private val viewModel: SpeakEngineViewModel by viewModels() private var httpTTSData: LiveData>? = null private var engineId = appCtx.getPrefLong(PreferKey.speakEngine) - private val importDoc = registerForActivityResult(FilePicker()) { + private val importDocResult = registerForActivityResult(FilePicker()) { it?.let { viewModel.importLocal(it) } } + private val exportDirResult = registerForActivityResult(FilePicker()) { + it?.let { + viewModel.export(it) + } + } override fun onStart() { super.onStart() @@ -109,13 +114,14 @@ class SpeakEngineDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener when (item?.itemId) { R.id.menu_add -> editHttpTTS() R.id.menu_default -> viewModel.importDefault() - R.id.menu_import_local -> importDoc.launch( + R.id.menu_import_local -> importDocResult.launch( FilePickerParam( mode = FilePicker.FILE, allowExtensions = arrayOf("txt", "json") ) ) R.id.menu_import_onLine -> importAlert() + R.id.menu_export -> exportDirResult.launch(null) } return true } diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt index 613b77eaa..c7eed9534 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/SpeakEngineViewModel.kt @@ -60,4 +60,10 @@ class SpeakEngineViewModel(application: Application) : BaseViewModel(application } } + fun export(uri: Uri) { + execute { + val httpTTS = appDb.httpTTSDao.all + uri.writeBytes(context, "httpTts.json", GSON.toJson(httpTTS).toByteArray()) + } + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/utils/UriExtensions.kt b/app/src/main/java/io/legado/app/utils/UriExtensions.kt index 509271fb2..3e99bd0a2 100644 --- a/app/src/main/java/io/legado/app/utils/UriExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/UriExtensions.kt @@ -2,13 +2,14 @@ package io.legado.app.utils import android.content.Context import android.net.Uri +import androidx.documentfile.provider.DocumentFile import java.io.File fun Uri.isContentScheme() = this.scheme == "content" @Throws(Exception::class) fun Uri.readBytes(context: Context): ByteArray? { - if (this.toString().isContentScheme()) { + if (this.isContentScheme()) { return DocumentUtils.readBytes(context, this) } else { val path = RealPathUtil.getPath(context, this) @@ -32,7 +33,7 @@ fun Uri.writeBytes( context: Context, byteArray: ByteArray ): Boolean { - if (this.toString().isContentScheme()) { + if (this.isContentScheme()) { return DocumentUtils.writeBytes(context, byteArray, this) } else { val path = RealPathUtil.getPath(context, this) @@ -48,3 +49,22 @@ fun Uri.writeBytes( fun Uri.writeText(context: Context, text: String): Boolean { return writeBytes(context, text.toByteArray()) } + +fun Uri.writeBytes( + context: Context, + fileName: String, + byteArray: ByteArray +): Boolean { + if (this.isContentScheme()) { + DocumentFile.fromTreeUri(context, this)?.let { pDoc -> + DocumentUtils.createFileIfNotExist(pDoc, fileName)?.let { + return it.uri.writeBytes(context, byteArray) + } + } + } else { + FileUtils.createFileWithReplace(path + File.separatorChar + fileName) + .writeBytes(byteArray) + return true + } + return false +} \ No newline at end of file diff --git a/app/src/main/res/menu/speak_engine.xml b/app/src/main/res/menu/speak_engine.xml index 80ba12ff1..6956a1ebc 100644 --- a/app/src/main/res/menu/speak_engine.xml +++ b/app/src/main/res/menu/speak_engine.xml @@ -25,4 +25,9 @@ android:title="@string/import_on_line" app:showAsAction="never" /> + + \ No newline at end of file