pull/1296/head
gedoor 3 years ago
parent bc4a80457d
commit c4162fca01
  1. 22
      app/src/main/java/io/legado/app/help/IntentHelp.kt
  2. 12
      app/src/main/java/io/legado/app/service/AudioPlayService.kt
  3. 12
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  4. 4
      app/src/main/java/io/legado/app/service/CheckSourceService.kt
  5. 3
      app/src/main/java/io/legado/app/service/WebService.kt
  6. 26
      app/src/main/java/io/legado/app/utils/ContextExtensions.kt

@ -1,6 +1,5 @@
package io.legado.app.help
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import io.legado.app.R
@ -33,25 +32,4 @@ object IntentHelp {
}
}
inline fun <reified T> activityPendingIntent(
context: Context,
action: String,
configIntent: Intent.() -> Unit = {}
): PendingIntent? {
val intent = Intent(context, T::class.java)
intent.action = action
configIntent.invoke(intent)
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
inline fun <reified T> broadcastPendingIntent(
context: Context,
action: String,
configIntent: Intent.() -> Unit = {}
): PendingIntent? {
val intent = Intent(context, T::class.java)
intent.action = action
configIntent.invoke(intent)
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
}
}

@ -26,7 +26,6 @@ import io.legado.app.data.appDb
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter
import io.legado.app.help.ExoPlayerHelper
import io.legado.app.help.IntentHelp
import io.legado.app.help.MediaHelp
import io.legado.app.model.AudioPlay
import io.legado.app.model.ReadAloud
@ -34,9 +33,7 @@ import io.legado.app.model.analyzeRule.AnalyzeUrl
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 io.legado.app.utils.*
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers.Main
@ -401,10 +398,7 @@ class AudioPlayService : BaseService(),
}
})
mediaSessionCompat?.setMediaButtonReceiver(
IntentHelp.broadcastPendingIntent<MediaButtonReceiver>(
this,
Intent.ACTION_MEDIA_BUTTON
)
broadcastPendingIntent<MediaButtonReceiver>(Intent.ACTION_MEDIA_BUTTON)
)
mediaSessionCompat?.isActive = true
}
@ -470,7 +464,7 @@ class AudioPlayService : BaseService(),
.setContentTitle(nTitle)
.setContentText(nSubtitle)
.setContentIntent(
IntentHelp.activityPendingIntent<AudioPlayActivity>(this, "activity")
activityPendingIntent<AudioPlayActivity>("activity")
)
if (pause) {
builder.addAction(

@ -17,16 +17,13 @@ import io.legado.app.R
import io.legado.app.base.BaseService
import io.legado.app.constant.*
import io.legado.app.help.IntentDataHelp
import io.legado.app.help.IntentHelp
import io.legado.app.help.MediaHelp
import io.legado.app.model.ReadAloud
import io.legado.app.model.ReadBook
import io.legado.app.receiver.MediaButtonReceiver
import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.utils.getPrefBoolean
import io.legado.app.utils.postEvent
import io.legado.app.utils.toastOnUi
import io.legado.app.utils.*
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
@ -256,10 +253,7 @@ abstract class BaseReadAloudService : BaseService(),
}
})
mediaSessionCompat.setMediaButtonReceiver(
IntentHelp.broadcastPendingIntent<MediaButtonReceiver>(
this,
Intent.ACTION_MEDIA_BUTTON
)
broadcastPendingIntent<MediaButtonReceiver>(Intent.ACTION_MEDIA_BUTTON)
)
mediaSessionCompat.isActive = true
}
@ -324,7 +318,7 @@ abstract class BaseReadAloudService : BaseService(),
.setContentTitle(nTitle)
.setContentText(nSubtitle)
.setContentIntent(
IntentHelp.activityPendingIntent<ReadBookActivity>(this, "activity")
activityPendingIntent<ReadBookActivity>("activity")
)
if (pause) {
builder.addAction(

@ -10,12 +10,12 @@ import io.legado.app.constant.IntentAction
import io.legado.app.data.appDb
import io.legado.app.data.entities.BookSource
import io.legado.app.help.AppConfig
import io.legado.app.help.IntentHelp
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.model.CheckSource
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.activityPendingIntent
import io.legado.app.utils.postEvent
import io.legado.app.utils.servicePendingIntent
import io.legado.app.utils.toastOnUi
@ -38,7 +38,7 @@ class CheckSourceService : BaseService() {
.setOngoing(true)
.setContentTitle(getString(R.string.check_book_source))
.setContentIntent(
IntentHelp.activityPendingIntent<BookSourceActivity>(this, "activity")
activityPendingIntent<BookSourceActivity>("activity")
)
.addAction(
R.drawable.ic_stop_black_24dp,

@ -9,7 +9,6 @@ import io.legado.app.constant.AppConst
import io.legado.app.constant.EventBus
import io.legado.app.constant.IntentAction
import io.legado.app.constant.PreferKey
import io.legado.app.help.IntentHelp
import io.legado.app.ui.main.MainActivity
import io.legado.app.utils.*
import io.legado.app.web.HttpServer
@ -113,7 +112,7 @@ class WebService : BaseService() {
.setContentTitle(getString(R.string.web_service))
.setContentText(notificationContent)
.setContentIntent(
IntentHelp.activityPendingIntent<MainActivity>(this, "webService")
activityPendingIntent<MainActivity>("webService")
)
builder.addAction(
R.drawable.ic_stop_black_24dp,

@ -5,6 +5,7 @@ package io.legado.app.utils
import android.annotation.SuppressLint
import android.app.Activity
import android.app.PendingIntent
import android.app.PendingIntent.*
import android.app.Service
import android.content.*
import android.content.pm.PackageManager
@ -43,6 +44,7 @@ inline fun <reified T : Service> Context.stopService() {
stopService(Intent(this, T::class.java))
}
@SuppressLint("UnspecifiedImmutableFlag")
inline fun <reified T : Service> Context.servicePendingIntent(
action: String,
configIntent: Intent.() -> Unit = {}
@ -50,7 +52,29 @@ inline fun <reified T : Service> Context.servicePendingIntent(
val intent = Intent(this, T::class.java)
intent.action = action
configIntent.invoke(intent)
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
return getService(this, 0, intent, FLAG_UPDATE_CURRENT)
}
@SuppressLint("UnspecifiedImmutableFlag")
inline fun <reified T : Activity> Context.activityPendingIntent(
action: String,
configIntent: Intent.() -> Unit = {}
): PendingIntent? {
val intent = Intent(this, T::class.java)
intent.action = action
configIntent.invoke(intent)
return getActivity(this, 0, intent, FLAG_UPDATE_CURRENT)
}
@SuppressLint("UnspecifiedImmutableFlag")
inline fun <reified T : BroadcastReceiver> Context.broadcastPendingIntent(
action: String,
configIntent: Intent.() -> Unit = {}
): PendingIntent? {
val intent = Intent(this, T::class.java)
intent.action = action
configIntent.invoke(intent)
return getBroadcast(this, 0, intent, FLAG_CANCEL_CURRENT)
}
fun Context.toastOnUi(message: Int) {

Loading…
Cancel
Save