From 98003f6e0871cc2c8a0e4991d958ca2a8374e0ea Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 18 Sep 2019 17:52:11 +0800 Subject: [PATCH] up --- .../java/io/legado/app/help/IntentHelp.kt | 12 +++++ .../app/service/BaseReadAloudService.kt | 11 ++--- .../legado/app/service/CheckSourceService.kt | 46 +++++++++++++++++++ 3 files changed, 62 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/IntentHelp.kt b/app/src/main/java/io/legado/app/help/IntentHelp.kt index 55ee61ed0..13a812e8e 100644 --- a/app/src/main/java/io/legado/app/help/IntentHelp.kt +++ b/app/src/main/java/io/legado/app/help/IntentHelp.kt @@ -1,5 +1,6 @@ package io.legado.app.help +import android.app.PendingIntent import android.content.Context import android.content.Intent import io.legado.app.R @@ -19,4 +20,15 @@ object IntentHelp { context.toast(R.string.tip_cannot_jump_setting_page) } } + + inline fun servicePendingIntent(context: Context, action: String): PendingIntent? { + return PendingIntent.getService( + context, + 0, + Intent(context, T::class.java).apply { + this.action = action + }, + PendingIntent.FLAG_UPDATE_CURRENT + ) + } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index 4a74683f5..2e9f34ef8 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -18,6 +18,7 @@ import io.legado.app.constant.AppConst import io.legado.app.constant.Bus import io.legado.app.constant.Status import io.legado.app.help.IntentDataHelp +import io.legado.app.help.IntentHelp import io.legado.app.help.MediaHelp import io.legado.app.receiver.MediaButtonReceiver import io.legado.app.ui.book.read.ReadBookActivity @@ -289,7 +290,9 @@ abstract class BaseReadAloudService : BaseService(), .setOngoing(true) .setContentTitle(nTitle) .setContentText(nSubtitle) - .setContentIntent(readBookActivityPendingIntent(this)) + .setContentIntent( + IntentHelp.servicePendingIntent(this, "activity") + ) if (pause) { builder.addAction( R.drawable.ic_play_24dp, @@ -323,12 +326,6 @@ abstract class BaseReadAloudService : BaseService(), startForeground(112201, notification) } - private fun readBookActivityPendingIntent(context: Context): PendingIntent { - val intent = Intent(context, ReadBookActivity::class.java) - intent.action = "readBookActivity" - return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) - } - abstract fun aloudServicePendingIntent(context: Context, actionStr: String): PendingIntent } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/CheckSourceService.kt b/app/src/main/java/io/legado/app/service/CheckSourceService.kt index 247a137a9..75bb49ee0 100644 --- a/app/src/main/java/io/legado/app/service/CheckSourceService.kt +++ b/app/src/main/java/io/legado/app/service/CheckSourceService.kt @@ -1,8 +1,54 @@ package io.legado.app.service +import android.content.Intent +import androidx.core.app.NotificationCompat +import io.legado.app.R import io.legado.app.base.BaseService +import io.legado.app.constant.Action +import io.legado.app.constant.AppConst +import io.legado.app.data.entities.BookSource +import io.legado.app.help.IntentHelp +import io.legado.app.ui.book.source.manage.BookSourceActivity class CheckSourceService : BaseService() { + private var sourceList: List? = null + + override fun onCreate() { + super.onCreate() + } + + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + return super.onStartCommand(intent, flags, startId) + } + + override fun onDestroy() { + super.onDestroy() + } + + /** + * 更新通知 + */ + private fun updateNotification(state: Int, msg: String) { + val builder = NotificationCompat.Builder(this, AppConst.channelIdReadAloud) + .setSmallIcon(R.drawable.ic_network_check) + .setOngoing(true) + .setContentTitle(getString(R.string.check_book_source)) + .setContentText(msg) + .setContentIntent( + IntentHelp.servicePendingIntent(this, "activity") + ) + .addAction( + R.drawable.ic_stop_black_24dp, + getString(R.string.cancel), + IntentHelp.servicePendingIntent(this, Action.stop) + ) + sourceList?.let { + builder.setProgress(it.size, state, false) + } + builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) + val notification = builder.build() + startForeground(112202, notification) + } } \ No newline at end of file