diff --git a/app/src/main/java/io/legado/app/service/CacheBookService.kt b/app/src/main/java/io/legado/app/service/CacheBookService.kt index 4f6c4fb67..181985df3 100644 --- a/app/src/main/java/io/legado/app/service/CacheBookService.kt +++ b/app/src/main/java/io/legado/app/service/CacheBookService.kt @@ -60,7 +60,7 @@ class CacheBookService : BaseService() { override fun onCreate() { super.onCreate() - updateNotification(notificationContent) + upNotification() handler.postDelayed(runnable, 1000) } @@ -126,8 +126,9 @@ class CacheBookService : BaseService() { private fun addDownloadData(bookUrl: String?, start: Int, end: Int) { bookUrl ?: return if (downloadMap.containsKey(bookUrl)) { - updateNotification(getString(R.string.already_in_download)) - toast(R.string.already_in_download) + notificationContent = getString(R.string.already_in_download) + upNotification() + toast(notificationContent) return } downloadCount[bookUrl] = DownloadCount() @@ -193,15 +194,15 @@ class CacheBookService : BaseService() { synchronized(this) { downloadingList.remove(bookChapter.url) } - CacheBook.addLog("getContentError${it.localizedMessage}") - updateNotification("getContentError${it.localizedMessage}") + notificationContent = "getContentError${it.localizedMessage}" + upNotification() } .onSuccess { synchronized(this@CacheBookService) { downloadCount[book.bookUrl]?.increaseSuccess() downloadCount[book.bookUrl]?.increaseFinished() downloadCount[book.bookUrl]?.let { - updateNotification( + upNotification( it, downloadMap[book.bookUrl]?.size, bookChapter.title @@ -230,8 +231,9 @@ class CacheBookService : BaseService() { } } }.onError { - CacheBook.addLog("ERROR:${it.localizedMessage}") - updateNotification("ERROR:${it.localizedMessage}") + notificationContent = "ERROR:${it.localizedMessage}" + CacheBook.addLog(notificationContent) + upNotification() } tasks.add(task) } @@ -253,13 +255,13 @@ class CacheBookService : BaseService() { } private fun upDownload() { - updateNotification(notificationContent) + upNotification() postEvent(EventBus.UP_DOWNLOAD, downloadMap) handler.removeCallbacks(runnable) handler.postDelayed(runnable, 1000) } - private fun updateNotification( + private fun upNotification( downloadCount: DownloadCount, totalCount: Int?, content: String @@ -271,8 +273,8 @@ class CacheBookService : BaseService() { /** * 更新通知 */ - private fun updateNotification(content: String) { - notificationBuilder.setContentText(content) + private fun upNotification() { + notificationBuilder.setContentText(notificationContent) val notification = notificationBuilder.build() startForeground(AppConst.notificationIdDownload, notification) } 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 e6871af3c..aa3bd6799 100644 --- a/app/src/main/java/io/legado/app/service/CheckSourceService.kt +++ b/app/src/main/java/io/legado/app/service/CheckSourceService.kt @@ -26,6 +26,7 @@ class CheckSourceService : BaseService() { private val allIds = ArrayList() private val checkedIds = ArrayList() private var processIndex = 0 + private var notificationMsg = "" private val notificationBuilder by lazy { NotificationCompat.Builder(this, AppConst.channelIdReadAloud) .setSmallIcon(R.drawable.ic_network_check) @@ -44,7 +45,8 @@ class CheckSourceService : BaseService() { override fun onCreate() { super.onCreate() - updateNotification(0, getString(R.string.start)) + notificationMsg = getString(R.string.start) + upNotification() } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -75,7 +77,8 @@ class CheckSourceService : BaseService() { allIds.addAll(ids) processIndex = 0 threadCount = min(allIds.size, threadCount) - updateNotification(0, getString(R.string.progress_show, "", 0, allIds.size)) + notificationMsg = getString(R.string.progress_show, "", 0, allIds.size) + upNotification() for (i in 0 until threadCount) { check() } @@ -109,10 +112,9 @@ class CheckSourceService : BaseService() { synchronized(this) { check() checkedIds.add(sourceUrl) - updateNotification( - checkedIds.size, + notificationMsg = getString(R.string.progress_show, sourceName, checkedIds.size, allIds.size) - ) + upNotification() if (processIndex >= allIds.size + threadCount - 1) { stopSelf() } @@ -122,10 +124,10 @@ class CheckSourceService : BaseService() { /** * 更新通知 */ - private fun updateNotification(state: Int, msg: String) { - notificationBuilder.setContentText(msg) - notificationBuilder.setProgress(allIds.size, state, false) - postEvent(EventBus.CHECK_SOURCE, msg) + private fun upNotification() { + notificationBuilder.setContentText(notificationMsg) + notificationBuilder.setProgress(allIds.size, checkedIds.size, false) + postEvent(EventBus.CHECK_SOURCE, notificationMsg) startForeground(112202, notificationBuilder.build()) } diff --git a/app/src/main/java/io/legado/app/service/WebService.kt b/app/src/main/java/io/legado/app/service/WebService.kt index 83168593c..03bddff2c 100644 --- a/app/src/main/java/io/legado/app/service/WebService.kt +++ b/app/src/main/java/io/legado/app/service/WebService.kt @@ -42,11 +42,13 @@ class WebService : BaseService() { private var httpServer: HttpServer? = null private var webSocketServer: WebSocketServer? = null + private var notificationContent = "" override fun onCreate() { super.onCreate() isRun = true - updateNotification(getString(R.string.service_starting)) + notificationContent = getString(R.string.service_starting) + upNotification() } override fun onDestroy() { @@ -87,7 +89,8 @@ class WebService : BaseService() { hostAddress = getString(R.string.http_ip, address.hostAddress, port) isRun = true postEvent(EventBus.WEB_SERVICE, hostAddress) - updateNotification(hostAddress) + notificationContent = hostAddress + upNotification() } catch (e: IOException) { launch { toast(e.localizedMessage ?: "") @@ -110,12 +113,12 @@ class WebService : BaseService() { /** * 更新通知 */ - private fun updateNotification(content: String) { + private fun upNotification() { val builder = NotificationCompat.Builder(this, AppConst.channelIdWeb) .setSmallIcon(R.drawable.ic_web_service_noti) .setOngoing(true) .setContentTitle(getString(R.string.web_service)) - .setContentText(content) + .setContentText(notificationContent) builder.addAction( R.drawable.ic_stop_black_24dp, getString(R.string.cancel),