This commit is contained in:
kunfei
2023-02-15 23:33:30 +08:00
parent afe7af182c
commit 46a72d3265
7 changed files with 11 additions and 22 deletions
@@ -25,6 +25,7 @@ abstract class BaseService : LifecycleService(), CoroutineScope by MainScope() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
LifecycleHelp.onServiceCreate(this) LifecycleHelp.onServiceCreate(this)
upNotification()
checkNotificationPermission() checkNotificationPermission()
} }
@@ -93,7 +93,6 @@ class AudioPlayService : BaseService(),
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
isRun = true isRun = true
upNotification()
exoPlayer.addListener(this) exoPlayer.addListener(this)
initMediaSession() initMediaSession()
initBroadcastReceiver() initBroadcastReceiver()
@@ -94,7 +94,6 @@ abstract class BaseReadAloudService : BaseService(),
observeLiveBus() observeLiveBus()
initMediaSession() initMediaSession()
initBroadcastReceiver() initBroadcastReceiver()
upNotification()
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING) upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
setTimer(AppConfig.ttsTimer) setTimer(AppConfig.ttsTimer)
if (AppConfig.ttsTimer > 0) { if (AppConfig.ttsTimer > 0) {
@@ -33,7 +33,7 @@ class CacheBookService : BaseService() {
private var cachePool = private var cachePool =
Executors.newFixedThreadPool(min(threadCount, AppConst.MAX_THREAD)).asCoroutineDispatcher() Executors.newFixedThreadPool(min(threadCount, AppConst.MAX_THREAD)).asCoroutineDispatcher()
private var downloadJob: Job? = null private var downloadJob: Job? = null
private var notificationContent = appCtx.getString(R.string.service_starting)
private val notificationBuilder by lazy { private val notificationBuilder by lazy {
val builder = NotificationCompat.Builder(this, AppConst.channelIdDownload) val builder = NotificationCompat.Builder(this, AppConst.channelIdDownload)
.setSmallIcon(R.drawable.ic_download) .setSmallIcon(R.drawable.ic_download)
@@ -51,11 +51,11 @@ class CacheBookService : BaseService() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
isRun = true isRun = true
upNotification(getString(R.string.starting_download))
launch { launch {
while (isActive) { while (isActive) {
delay(1000) delay(1000)
upNotification(CacheBook.downloadSummary) notificationContent = CacheBook.downloadSummary
upNotification()
postEvent(EventBus.UP_DOWNLOAD, "") postEvent(EventBus.UP_DOWNLOAD, "")
} }
} }
@@ -105,7 +105,8 @@ class CacheBookService : BaseService() {
end end
} }
cacheBook.addDownload(start, end2) cacheBook.addDownload(start, end2)
upNotification(CacheBook.downloadSummary) notificationContent = CacheBook.downloadSummary
upNotification()
if (downloadJob == null) { if (downloadJob == null) {
download() download()
} }
@@ -149,7 +150,7 @@ class CacheBookService : BaseService() {
/** /**
* 更新通知 * 更新通知
*/ */
private fun upNotification(notificationContent: String) { override fun upNotification() {
notificationBuilder.setContentText(notificationContent) notificationBuilder.setContentText(notificationContent)
val notification = notificationBuilder.build() val notification = notificationBuilder.build()
startForeground(AppConst.notificationIdCache, notification) startForeground(AppConst.notificationIdCache, notification)
@@ -31,6 +31,7 @@ import kotlinx.coroutines.TimeoutCancellationException
import kotlinx.coroutines.asCoroutineDispatcher import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.mozilla.javascript.WrappedException import org.mozilla.javascript.WrappedException
import splitties.init.appCtx
import java.util.concurrent.Executors import java.util.concurrent.Executors
import kotlin.math.min import kotlin.math.min
@@ -44,7 +45,7 @@ class CheckSourceService : BaseService() {
private val allIds = ArrayList<String>() private val allIds = ArrayList<String>()
private val checkedIds = ArrayList<String>() private val checkedIds = ArrayList<String>()
private var processIndex = 0 private var processIndex = 0
private var notificationMsg = "" private var notificationMsg = appCtx.getString(R.string.service_starting)
private val notificationBuilder by lazy { private val notificationBuilder by lazy {
NotificationCompat.Builder(this, AppConst.channelIdReadAloud) NotificationCompat.Builder(this, AppConst.channelIdReadAloud)
@@ -62,12 +63,6 @@ class CheckSourceService : BaseService() {
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
} }
override fun onCreate() {
super.onCreate()
notificationMsg = getString(R.string.start)
upNotification()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) { when (intent?.action) {
IntentAction.start -> intent.getStringArrayListExtra("selectIds")?.let { IntentAction.start -> intent.getStringArrayListExtra("selectIds")?.let {
@@ -41,7 +41,6 @@ class DownloadService : BaseService() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
upSummaryNotification()
registerReceiver(downloadReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) registerReceiver(downloadReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))
} }
@@ -198,10 +197,7 @@ class DownloadService : BaseService() {
} }
} }
/** override fun upNotification() {
* 更新下载进度条
*/
private fun upSummaryNotification() {
val notification = NotificationCompat.Builder(this, AppConst.channelIdDownload) val notification = NotificationCompat.Builder(this, AppConst.channelIdDownload)
.setSmallIcon(R.drawable.ic_download) .setSmallIcon(R.drawable.ic_download)
.setSubText(getString(R.string.action_download)) .setSubText(getString(R.string.action_download))
@@ -49,7 +49,7 @@ class WebService : BaseService() {
} }
private var httpServer: HttpServer? = null private var httpServer: HttpServer? = null
private var webSocketServer: WebSocketServer? = null private var webSocketServer: WebSocketServer? = null
private var notificationContent = "" private var notificationContent = appCtx.getString(R.string.service_starting)
private val networkChangedListener by lazy { private val networkChangedListener by lazy {
NetworkChangedListener(this) NetworkChangedListener(this)
} }
@@ -58,8 +58,6 @@ class WebService : BaseService() {
super.onCreate() super.onCreate()
if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/) if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
isRun = true isRun = true
notificationContent = getString(R.string.service_starting)
upNotification()
upTile(true) upTile(true)
networkChangedListener.register() networkChangedListener.register()
networkChangedListener.onNetworkChanged = { networkChangedListener.onNetworkChanged = {