添加朗读快捷方式

pull/883/head
gedoor 4 years ago
parent 19a903030d
commit 2c16a24b17
  1. 14
      app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt
  2. 14
      app/src/main/java/io/legado/app/receiver/SharedReceiverActivity.kt
  3. 21
      app/src/main/res/xml/shortcuts.xml

@ -31,6 +31,12 @@ import kotlinx.coroutines.withContext
class MediaButtonReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (handleIntent(context, intent) && isOrderedBroadcast) {
abortBroadcast()
}
}
companion object {
fun handleIntent(context: Context, intent: Intent): Boolean {
@ -55,7 +61,7 @@ class MediaButtonReceiver : BroadcastReceiver() {
return true
}
private fun readAloud(context: Context) {
fun readAloud(context: Context) {
when {
BaseReadAloudService.isRun -> if (BaseReadAloudService.isPlay()) {
ReadAloud.pause(context)
@ -97,10 +103,4 @@ class MediaButtonReceiver : BroadcastReceiver() {
}
}
override fun onReceive(context: Context, intent: Intent) {
if (handleIntent(context, intent) && isOrderedBroadcast) {
abortBroadcast()
}
}
}

@ -7,6 +7,7 @@ import androidx.appcompat.app.AppCompatActivity
import io.legado.app.ui.book.search.SearchActivity
import io.legado.app.ui.main.MainActivity
import io.legado.app.utils.startActivity
import splitties.init.appCtx
class SharedReceiverActivity : AppCompatActivity() {
@ -19,7 +20,8 @@ class SharedReceiverActivity : AppCompatActivity() {
}
private fun initIntent() {
if (Intent.ACTION_SEND == intent.action && intent.type == receivingType) {
when {
Intent.ACTION_SEND == intent.action && intent.type == receivingType -> {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
if (openUrl(it)) {
startActivity<SearchActivity> {
@ -27,10 +29,10 @@ class SharedReceiverActivity : AppCompatActivity() {
}
}
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
&& Intent.ACTION_PROCESS_TEXT == intent.action
&& intent.type == receivingType
) {
&& intent.type == receivingType -> {
intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT)?.let {
if (openUrl(it)) {
startActivity<SearchActivity> {
@ -39,6 +41,10 @@ class SharedReceiverActivity : AppCompatActivity() {
}
}
}
intent.getStringExtra("action") == "mediaButton" -> {
MediaButtonReceiver.readAloud(appCtx)
}
}
}
private fun openUrl(text: String): Boolean {

@ -21,6 +21,26 @@
<categories android:name="android.shortcut.conversation" />
</shortcut>
<shortcut
android:shortcutId="readAloud"
android:enabled="true"
android:icon="@drawable/icon_read_book"
android:shortcutShortLabel="@string/read_aloud"
android:shortcutLongLabel="@string/read_aloud"
android:shortcutDisabledMessage="@string/read_aloud"
tools:ignore="UnusedAttribute">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="${applicationId}"
android:targetClass="io.legado.app.receiver.SharedReceiverActivity">
<extra
android:name="action"
android:value="mediaButton" />
</intent>
<categories android:name="android.shortcut.conversation" />
</shortcut>
<shortcut
android:shortcutId="bookshelf"
android:enabled="true"
@ -36,4 +56,5 @@
android:targetClass="io.legado.app.ui.main.MainActivity" />
<categories android:name="android.shortcut.conversation" />
</shortcut>
</shortcuts>

Loading…
Cancel
Save