pull/1296/head
gedoor 3 years ago
parent 42f4cd698d
commit bc4a80457d
  1. 11
      app/src/main/java/io/legado/app/help/IntentHelp.kt
  2. 13
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  3. 4
      app/src/main/java/io/legado/app/service/CacheBookService.kt
  4. 3
      app/src/main/java/io/legado/app/service/CheckSourceService.kt
  5. 8
      app/src/main/java/io/legado/app/service/DownloadService.kt
  6. 3
      app/src/main/java/io/legado/app/service/HttpReadAloudService.kt
  7. 4
      app/src/main/java/io/legado/app/service/TTSReadAloudService.kt
  8. 2
      app/src/main/java/io/legado/app/service/WebService.kt
  9. 11
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt

@ -33,17 +33,6 @@ object IntentHelp {
}
}
inline fun <reified T> servicePendingIntent(
context: Context,
action: String,
configIntent: Intent.() -> Unit = {}
): PendingIntent? {
val intent = Intent(context, T::class.java)
intent.action = action
configIntent.invoke(intent)
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
inline fun <reified T> activityPendingIntent(
context: Context,
action: String,

@ -1,7 +1,6 @@
package io.legado.app.service
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
@ -36,6 +35,7 @@ import io.legado.app.model.webBook.WebBook
import io.legado.app.receiver.MediaButtonReceiver
import io.legado.app.ui.book.audio.AudioPlayActivity
import io.legado.app.utils.postEvent
import io.legado.app.utils.servicePendingIntent
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.Main
@ -476,24 +476,24 @@ class AudioPlayService : BaseService(),
builder.addAction(
R.drawable.ic_play_24dp,
getString(R.string.resume),
thisPendingIntent(IntentAction.resume)
servicePendingIntent<AudioPlayService>(IntentAction.resume)
)
} else {
builder.addAction(
R.drawable.ic_pause_24dp,
getString(R.string.pause),
thisPendingIntent(IntentAction.pause)
servicePendingIntent<AudioPlayService>(IntentAction.pause)
)
}
builder.addAction(
R.drawable.ic_stop_black_24dp,
getString(R.string.stop),
thisPendingIntent(IntentAction.stop)
servicePendingIntent<AudioPlayService>(IntentAction.stop)
)
builder.addAction(
R.drawable.ic_time_add_24dp,
getString(R.string.set_timer),
thisPendingIntent(IntentAction.addTimer)
servicePendingIntent<AudioPlayService>(IntentAction.addTimer)
)
builder.setStyle(
androidx.media.app.NotificationCompat.MediaStyle()
@ -511,7 +511,4 @@ class AudioPlayService : BaseService(),
return MediaHelp.requestFocus(audioManager, mFocusRequest)
}
private fun thisPendingIntent(action: String): PendingIntent? {
return IntentHelp.servicePendingIntent<AudioPlayService>(this, action)
}
}

@ -13,12 +13,12 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookSource
import io.legado.app.help.AppConfig
import io.legado.app.help.BookHelp
import io.legado.app.help.IntentHelp
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.CacheBook
import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.postEvent
import io.legado.app.utils.servicePendingIntent
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.delay
@ -54,7 +54,7 @@ class CacheBookService : BaseService() {
builder.addAction(
R.drawable.ic_stop_black_24dp,
getString(R.string.cancel),
IntentHelp.servicePendingIntent<CacheBookService>(this, IntentAction.stop)
servicePendingIntent<CacheBookService>(IntentAction.stop)
)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
}

@ -17,6 +17,7 @@ import io.legado.app.model.Debug
import io.legado.app.model.webBook.WebBook
import io.legado.app.ui.book.source.manage.BookSourceActivity
import io.legado.app.utils.postEvent
import io.legado.app.utils.servicePendingIntent
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.asCoroutineDispatcher
import java.util.concurrent.Executors
@ -42,7 +43,7 @@ class CheckSourceService : BaseService() {
.addAction(
R.drawable.ic_stop_black_24dp,
getString(R.string.cancel),
IntentHelp.servicePendingIntent<CheckSourceService>(this, IntentAction.stop)
servicePendingIntent<CheckSourceService>(IntentAction.stop)
)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
}

@ -13,9 +13,9 @@ import io.legado.app.R
import io.legado.app.base.BaseService
import io.legado.app.constant.AppConst
import io.legado.app.constant.IntentAction
import io.legado.app.help.IntentHelp
import io.legado.app.utils.RealPathUtil
import io.legado.app.utils.msg
import io.legado.app.utils.servicePendingIntent
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
@ -153,19 +153,19 @@ class DownloadService : BaseService() {
.setOngoing(true)
.setContentTitle(getString(R.string.action_download))
notificationBuilder.setContentIntent(
IntentHelp.servicePendingIntent<DownloadService>(this, IntentAction.play) {
servicePendingIntent<DownloadService>(IntentAction.play) {
putExtra("downloadId", downloadId)
}
)
notificationBuilder.addAction(
R.drawable.ic_stop_black_24dp,
getString(R.string.cancel),
IntentHelp.servicePendingIntent<DownloadService>(this, IntentAction.stop) {
servicePendingIntent<DownloadService>(IntentAction.stop) {
putExtra("downloadId", downloadId)
}
)
notificationBuilder.setDeleteIntent(
IntentHelp.servicePendingIntent<DownloadService>(this, IntentAction.stop) {
servicePendingIntent<DownloadService>(IntentAction.stop) {
putExtra("downloadId", downloadId)
}
)

@ -4,7 +4,6 @@ import android.app.PendingIntent
import android.media.MediaPlayer
import io.legado.app.constant.EventBus
import io.legado.app.help.AppConfig
import io.legado.app.help.IntentHelp
import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.ReadAloud
import io.legado.app.model.ReadBook
@ -264,6 +263,6 @@ class HttpReadAloudService : BaseReadAloudService(),
}
override fun aloudServicePendingIntent(actionStr: String): PendingIntent? {
return IntentHelp.servicePendingIntent<HttpReadAloudService>(this, actionStr)
return servicePendingIntent<HttpReadAloudService>(actionStr)
}
}

@ -7,11 +7,11 @@ import io.legado.app.R
import io.legado.app.constant.AppConst
import io.legado.app.constant.EventBus
import io.legado.app.help.AppConfig
import io.legado.app.help.IntentHelp
import io.legado.app.help.MediaHelp
import io.legado.app.model.ReadBook
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.postEvent
import io.legado.app.utils.servicePendingIntent
import io.legado.app.utils.toastOnUi
import java.util.*
@ -151,7 +151,7 @@ class TTSReadAloudService : BaseReadAloudService(), TextToSpeech.OnInitListener
}
override fun aloudServicePendingIntent(actionStr: String): PendingIntent? {
return IntentHelp.servicePendingIntent<TTSReadAloudService>(this, actionStr)
return servicePendingIntent<TTSReadAloudService>(actionStr)
}
}

@ -118,7 +118,7 @@ class WebService : BaseService() {
builder.addAction(
R.drawable.ic_stop_black_24dp,
getString(R.string.cancel),
IntentHelp.servicePendingIntent<WebService>(this, IntentAction.stop)
servicePendingIntent<WebService>(IntentAction.stop)
)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
val notification = builder.build()

@ -4,6 +4,7 @@ package io.legado.app.utils
import android.annotation.SuppressLint
import android.app.Activity
import android.app.PendingIntent
import android.app.Service
import android.content.*
import android.content.pm.PackageManager
@ -42,6 +43,16 @@ inline fun <reified T : Service> Context.stopService() {
stopService(Intent(this, T::class.java))
}
inline fun <reified T : Service> Context.servicePendingIntent(
action: String,
configIntent: Intent.() -> Unit = {}
): PendingIntent? {
val intent = Intent(this, T::class.java)
intent.action = action
configIntent.invoke(intent)
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
fun Context.toastOnUi(message: Int) {
runOnUI {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

Loading…
Cancel
Save