pull/1174/head
gedoor 3 years ago
parent 9906247ba7
commit bf661560f0
  1. 13
      app/src/main/java/io/legado/app/help/IntentHelp.kt
  2. 25
      app/src/main/java/io/legado/app/service/DownloadService.kt
  3. 4
      app/src/main/java/io/legado/app/service/WebService.kt

@ -3,7 +3,6 @@ package io.legado.app.help
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle
import io.legado.app.R import io.legado.app.R
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
@ -37,26 +36,22 @@ object IntentHelp {
inline fun <reified T> servicePendingIntent( inline fun <reified T> servicePendingIntent(
context: Context, context: Context,
action: String, action: String,
bundle: Bundle? = null configIntent: Intent.() -> Unit = {}
): PendingIntent? { ): PendingIntent? {
val intent = Intent(context, T::class.java) val intent = Intent(context, T::class.java)
intent.action = action intent.action = action
bundle?.let { configIntent.invoke(intent)
intent.putExtras(bundle)
}
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
} }
inline fun <reified T> activityPendingIntent( inline fun <reified T> activityPendingIntent(
context: Context, context: Context,
action: String, action: String,
bundle: Bundle? = null configIntent: Intent.() -> Unit = {}
): PendingIntent? { ): PendingIntent? {
val intent = Intent(context, T::class.java) val intent = Intent(context, T::class.java)
intent.action = action intent.action = action
bundle?.let { configIntent.invoke(intent)
intent.putExtras(bundle)
}
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
} }
} }

@ -11,7 +11,6 @@ import android.os.Handler
import android.os.Looper import android.os.Looper
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import androidx.core.os.bundleOf
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseService import io.legado.app.base.BaseService
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
@ -152,27 +151,21 @@ class DownloadService : BaseService() {
.setOngoing(true) .setOngoing(true)
.setContentTitle(getString(R.string.action_download)) .setContentTitle(getString(R.string.action_download))
notificationBuilder.setContentIntent( notificationBuilder.setContentIntent(
IntentHelp.servicePendingIntent<DownloadService>( IntentHelp.servicePendingIntent<DownloadService>(this, IntentAction.play) {
this, putExtra("downloadId", downloadId)
IntentAction.play, }
bundleOf("downloadId" to downloadId)
)
) )
notificationBuilder.addAction( notificationBuilder.addAction(
R.drawable.ic_stop_black_24dp, R.drawable.ic_stop_black_24dp,
getString(R.string.cancel), getString(R.string.cancel),
IntentHelp.servicePendingIntent<DownloadService>( IntentHelp.servicePendingIntent<DownloadService>(this, IntentAction.stop) {
this, putExtra("downloadId", downloadId)
IntentAction.stop, }
bundleOf("downloadId" to downloadId)
)
) )
notificationBuilder.setDeleteIntent( notificationBuilder.setDeleteIntent(
IntentHelp.servicePendingIntent<DownloadService>( IntentHelp.servicePendingIntent<DownloadService>(this, IntentAction.stop) {
this, putExtra("downloadId", downloadId)
IntentAction.stop, }
bundleOf("downloadId" to downloadId)
)
) )
notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) notificationBuilder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
notificationBuilder.setContentText(content) notificationBuilder.setContentText(content)

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

Loading…
Cancel
Save