From 684c2dd4d486b1c76e86f5f858144c12e3d20a45 Mon Sep 17 00:00:00 2001 From: yangyxd Date: Tue, 17 Mar 2020 12:40:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A1=86=E6=94=B9?= =?UTF-8?q?=E6=88=90=E5=9C=86=E8=A7=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/res/drawable/bg_searchview.xml | 9 +++++++++ app/src/main/res/layout/view_search.xml | 9 +++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/drawable/bg_searchview.xml diff --git a/app/src/main/res/drawable/bg_searchview.xml b/app/src/main/res/drawable/bg_searchview.xml new file mode 100644 index 000000000..89382ba0c --- /dev/null +++ b/app/src/main/res/drawable/bg_searchview.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/view_search.xml b/app/src/main/res/layout/view_search.xml index 5fd67be63..e7269040c 100644 --- a/app/src/main/res/layout/view_search.xml +++ b/app/src/main/res/layout/view_search.xml @@ -3,8 +3,13 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/search_view" - android:layout_width="match_parent" android:theme="?attr/actionBarStyle" - android:layout_height="wrap_content" + android:background="@drawable/bg_searchview" + android:layout_width="match_parent" + android:layout_height="34dp" + android:layout_marginTop="8dp" + android:layout_marginBottom="8dp" android:imeOptions="actionSearch" + app:queryBackground="@drawable/bg_searchview" + app:submitBackground="@color/transparent" app:defaultQueryHint="搜索"/> \ No newline at end of file From 474be5830f1c62e616bcfe4b9b0f115ae0c5774d Mon Sep 17 00:00:00 2001 From: yangyxd Date: Tue, 17 Mar 2020 14:34:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=98=85=E8=AF=BB?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E9=95=BF=E6=8C=89=E9=80=89=E6=8B=A9=E6=96=87?= =?UTF-8?q?=E5=AD=97=E6=9C=97=E8=AF=BB=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/book/read/TextActionMenu.kt | 38 ++++++++++++++++++- .../main/res/menu/content_select_action.xml | 4 ++ app/src/main/res/values/strings.xml | 1 + 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt index 3fe423f8e..357f2d437 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt @@ -7,6 +7,7 @@ import android.content.Intent import android.content.pm.ResolveInfo import android.net.Uri import android.os.Build +import android.speech.tts.TextToSpeech import android.view.LayoutInflater import android.view.Menu import android.view.ViewGroup @@ -19,6 +20,7 @@ import androidx.core.view.isVisible import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter +import io.legado.app.service.BaseReadAloudService import io.legado.app.utils.gone import io.legado.app.utils.isAbsUrl import io.legado.app.utils.sendToClip @@ -28,10 +30,12 @@ import kotlinx.android.synthetic.main.popup_action_menu.view.* import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.share import org.jetbrains.anko.toast +import java.util.* @SuppressLint("RestrictedApi") class TextActionMenu(private val context: Context, private val callBack: CallBack) : - PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) { + PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT), + TextToSpeech.OnInitListener { private val adapter = Adapter(context) private val menu = MenuBuilder(context) @@ -111,6 +115,13 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac when (item.itemId) { R.id.menu_copy -> context.sendToClip(callBack.selectedText) R.id.menu_share_str -> context.share(callBack.selectedText) + R.id.menu_aloud -> { + if (BaseReadAloudService.isRun) { + context.toast(R.string.alouding_disable) + return + } + readAloud(callBack.selectedText) + } R.id.menu_browser -> { try { val intent = if (callBack.selectedText.isAbsUrl()) { @@ -137,6 +148,31 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac } } + private var textToSpeech: TextToSpeech? = null + private var ttsInitFinish = false + private var lastText: String = "" + + @SuppressLint("SetJavaScriptEnabled") + private fun readAloud(text: String) { + if (textToSpeech == null && !ttsInitFinish) { + lastText = text + textToSpeech = TextToSpeech(context, this) + return + } + if (text == "") return + if (textToSpeech?.isSpeaking == true) + textToSpeech?.stop() + textToSpeech?.speak(text, TextToSpeech.QUEUE_ADD, null, "select_text") + lastText = "" + } + + @Synchronized + override fun onInit(status: Int) { + textToSpeech?.language = Locale.CHINA + ttsInitFinish = true + readAloud(lastText) + } + @RequiresApi(Build.VERSION_CODES.M) private fun createProcessTextIntent(): Intent { return Intent() diff --git a/app/src/main/res/menu/content_select_action.xml b/app/src/main/res/menu/content_select_action.xml index d11ee6154..ff83efd96 100644 --- a/app/src/main/res/menu/content_select_action.xml +++ b/app/src/main/res/menu/content_select_action.xml @@ -9,6 +9,10 @@ android:id="@+id/menu_copy" android:title="@android:string/copy" /> + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 41c3005f7..558e90cbe 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -648,4 +648,5 @@ 主菜单 点击授予权限 阅读需要访问存储卡权限,请点击下方的"授予权限"按钮,或前往“设置”—“应用权限”—打开所需权限。如果授予权限后仍然不正常,请点击右上角的“选择文件夹”,使用系统文件夹选择器。 + 全文朗读中不能朗读选中文字