Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Jan 27, 2024
1 parent c9d07d0 commit 3882904
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 151 deletions.
13 changes: 9 additions & 4 deletions app/src/main/java/io/legado/app/base/BaseService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ abstract class BaseService : LifecycleService() {
super.onCreate()
LifecycleHelp.onServiceCreate(this)
checkNotificationPermission()
upNotification()
}

@CallSuper
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
startForegroundNotification()
return super.onStartCommand(intent, flags, startId)
}

@CallSuper
Expand All @@ -52,9 +57,9 @@ abstract class BaseService : LifecycleService() {
}

/**
* 更新通知
* 开启前台服务并发送通知
*/
open fun upNotification() {
open fun startForegroundNotification() {

}

Expand All @@ -67,7 +72,7 @@ abstract class BaseService : LifecycleService() {
.rationale(R.string.notification_permission_rationale)
.onGranted {
if (lifecycleScope.isActive) {
upNotification()
startForegroundNotification()
}
}
.request()
Expand Down
10 changes: 8 additions & 2 deletions app/src/main/java/io/legado/app/data/entities/BookChapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ import io.legado.app.help.RuleBigDataHelp
import io.legado.app.help.config.AppConfig
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.RuleDataInterface
import io.legado.app.utils.*
import io.legado.app.utils.ChineseUtils
import io.legado.app.utils.GSON
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.replace
import io.legado.app.utils.toastOnUi
import kotlinx.coroutines.CancellationException
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
Expand Down Expand Up @@ -110,7 +116,7 @@ data class BookChapter(
try {
val mDisplayTitle = if (item.isRegex) {
displayTitle.replace(
item.pattern.toRegex(),
item.regex,
item.replacement,
item.getValidTimeoutMillisecond()
)
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/io/legado/app/data/entities/ReplaceRule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import android.os.Parcelable
import android.text.TextUtils
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.Index
import androidx.room.PrimaryKey
import io.legado.app.R
import io.legado.app.constant.AppLog
import io.legado.app.exception.NoStackTraceException
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import splitties.init.appCtx
import java.util.regex.Pattern
Expand Down Expand Up @@ -68,6 +70,12 @@ data class ReplaceRule(
return id.hashCode()
}

@delegate:Ignore
@IgnoredOnParcel
val regex: Regex by lazy {
pattern.toRegex()
}

fun getDisplayNameGroup(): String {
return if (group.isNullOrBlank()) {
name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class ContentProcessor private constructor(
try {
val tmp = if (item.isRegex) {
mContent.replace(
item.pattern.toRegex(),
item.regex,
item.replacement,
item.getValidTimeoutMillisecond()
)
Expand Down
48 changes: 0 additions & 48 deletions app/src/main/java/io/legado/app/help/coroutine/OrderCoroutine.kt

This file was deleted.

11 changes: 11 additions & 0 deletions app/src/main/java/io/legado/app/model/CacheBook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ object CacheBook {
}
}

fun clear() {
successDownloadSet.clear()
errorDownloadMap.clear()
}

fun close() {
cacheBookMap.forEach { it.value.stop() }
cacheBookMap.clear()
clear()
}

val downloadSummary: String
get() {
return "正在下载:${onDownloadCount}|等待中:${waitCount}|失败:${errorDownloadMap.count()}|成功:${successDownloadSet.size}"
Expand Down
138 changes: 75 additions & 63 deletions app/src/main/java/io/legado/app/service/AudioPlayService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import splitties.init.appCtx
import splitties.systemservices.audioManager
import splitties.systemservices.notificationManager
import splitties.systemservices.powerManager
import splitties.systemservices.wifiManager

Expand Down Expand Up @@ -135,7 +136,7 @@ class AudioPlayService : BaseService(),
.get()
}.onSuccess {
cover = it
upNotification()
upAudioPlayNotification()
}
}

Expand Down Expand Up @@ -191,7 +192,7 @@ class AudioPlayService : BaseService(),
wakeLock.acquire()
wifiLock?.acquire()
}
upNotification()
upAudioPlayNotification()
if (!requestFocus()) {
return
}
Expand Down Expand Up @@ -235,7 +236,7 @@ class AudioPlayService : BaseService(),
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED)
AudioPlay.status = Status.PAUSE
postEvent(EventBus.AUDIO_STATE, Status.PAUSE)
upNotification()
upAudioPlayNotification()
} catch (e: Exception) {
e.printOnDebug()
}
Expand Down Expand Up @@ -263,7 +264,7 @@ class AudioPlayService : BaseService(),
upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING)
AudioPlay.status = Status.PLAY
postEvent(EventBus.AUDIO_STATE, Status.PLAY)
upNotification()
upAudioPlayNotification()
} catch (e: Exception) {
e.printOnDebug()
stopSelf()
Expand Down Expand Up @@ -335,7 +336,7 @@ class AudioPlayService : BaseService(),
AudioPlay.next(this)
}
}
upNotification()
upAudioPlayNotification()
}

/**
Expand Down Expand Up @@ -370,7 +371,7 @@ class AudioPlayService : BaseService(),
*/
private fun doDs() {
postEvent(EventBus.AUDIO_DS, timeMinute)
upNotification()
upAudioPlayNotification()
dsJob?.cancel()
dsJob = lifecycleScope.launch {
while (isActive) {
Expand All @@ -384,7 +385,7 @@ class AudioPlayService : BaseService(),
}
}
postEvent(EventBus.AUDIO_DS, timeMinute)
upNotification()
upAudioPlayNotification()
}
}
}
Expand Down Expand Up @@ -583,67 +584,78 @@ class AudioPlayService : BaseService(),
}
}

private fun createNotification(): NotificationCompat.Builder {
var nTitle: String = when {
pause -> getString(R.string.audio_pause)
timeMinute in 1..60 -> getString(
R.string.playing_timer,
timeMinute
)

/**
* 更新通知
*/
override fun upNotification() {
execute {
var nTitle: String = when {
pause -> getString(R.string.audio_pause)
timeMinute in 1..60 -> getString(
R.string.playing_timer,
timeMinute
)

else -> getString(R.string.audio_play_t)
}
nTitle += ": ${AudioPlay.book?.name}"
var nSubtitle = AudioPlay.durChapter?.title
if (nSubtitle.isNullOrEmpty()) {
nSubtitle = getString(R.string.audio_play_s)
}
val builder = NotificationCompat
.Builder(this@AudioPlayService, AppConst.channelIdReadAloud)
.setSmallIcon(R.drawable.ic_volume_up)
.setSubText(getString(R.string.audio))
.setOngoing(true)
.setContentTitle(nTitle)
.setContentText(nSubtitle)
.setContentIntent(
activityPendingIntent<AudioPlayActivity>("activity")
)
builder.setLargeIcon(cover)
if (pause) {
builder.addAction(
R.drawable.ic_play_24dp,
getString(R.string.resume),
servicePendingIntent<AudioPlayService>(IntentAction.resume)
)
} else {
builder.addAction(
R.drawable.ic_pause_24dp,
getString(R.string.pause),
servicePendingIntent<AudioPlayService>(IntentAction.pause)
)
}
builder.addAction(
R.drawable.ic_stop_black_24dp,
getString(R.string.stop),
servicePendingIntent<AudioPlayService>(IntentAction.stop)
else -> getString(R.string.audio_play_t)
}
nTitle += ": ${AudioPlay.book?.name}"
var nSubtitle = AudioPlay.durChapter?.title
if (nSubtitle.isNullOrEmpty()) {
nSubtitle = getString(R.string.audio_play_s)
}
val builder = NotificationCompat
.Builder(this@AudioPlayService, AppConst.channelIdReadAloud)
.setSmallIcon(R.drawable.ic_volume_up)
.setSubText(getString(R.string.audio))
.setOngoing(true)
.setContentTitle(nTitle)
.setContentText(nSubtitle)
.setContentIntent(
activityPendingIntent<AudioPlayActivity>("activity")
)
builder.setLargeIcon(cover)
if (pause) {
builder.addAction(
R.drawable.ic_time_add_24dp,
getString(R.string.set_timer),
servicePendingIntent<AudioPlayService>(IntentAction.addTimer)
R.drawable.ic_play_24dp,
getString(R.string.resume),
servicePendingIntent<AudioPlayService>(IntentAction.resume)
)
builder.setStyle(
androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mediaSessionCompat?.sessionToken)
} else {
builder.addAction(
R.drawable.ic_pause_24dp,
getString(R.string.pause),
servicePendingIntent<AudioPlayService>(IntentAction.pause)
)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
builder
}
builder.addAction(
R.drawable.ic_stop_black_24dp,
getString(R.string.stop),
servicePendingIntent<AudioPlayService>(IntentAction.stop)
)
builder.addAction(
R.drawable.ic_time_add_24dp,
getString(R.string.set_timer),
servicePendingIntent<AudioPlayService>(IntentAction.addTimer)
)
builder.setStyle(
androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mediaSessionCompat?.sessionToken)
)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
return builder
}

private fun upAudioPlayNotification() {
execute {
createNotification()
}.onSuccess {
notificationManager.notify(NotificationId.AudioPlayService, it.build())
}
}

/**
* 更新通知
*/
override fun startForegroundNotification() {
execute {
createNotification()
}.onSuccess {
startForeground(NotificationId.AudioPlayService, it.build())
}
Expand Down
Loading

0 comments on commit 3882904

Please sign in to comment.