Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Dec 15, 2023
1 parent 245eb3e commit b434d34
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/io/legado/app/constant/PreferKey.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.legado.app.constant

@Suppress("ConstPropertyName")
object PreferKey {
const val language = "language"
const val fontScale = "fontScale"
Expand Down
30 changes: 26 additions & 4 deletions app/src/main/java/io/legado/app/service/AudioPlayService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.IntentFilter
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.AudioManager
import android.net.wifi.WifiManager.WIFI_MODE_FULL_HIGH_PERF
import android.os.Build
import android.os.Bundle
import android.os.PowerManager
Expand Down Expand Up @@ -42,6 +43,7 @@ import kotlinx.coroutines.Dispatchers.Main
import splitties.init.appCtx
import splitties.systemservices.audioManager
import splitties.systemservices.powerManager
import splitties.systemservices.wifiManager

@UnstableApi
/**
Expand Down Expand Up @@ -82,6 +84,12 @@ class AudioPlayService : BaseService(),
this.setReferenceCounted(false)
}
}
private val wifiLock by lazy {
@Suppress("DEPRECATION")
wifiManager?.createWifiLock(WIFI_MODE_FULL_HIGH_PERF, "legado:AudioPlayService")?.apply {
setReferenceCounted(false)
}
}
private val mFocusRequest: AudioFocusRequestCompat by lazy {
MediaHelp.buildAudioFocusRequestCompat(this)
}
Expand Down Expand Up @@ -147,7 +155,10 @@ class AudioPlayService : BaseService(),

override fun onDestroy() {
super.onDestroy()
if (useWakeLock) wakeLock.release()
if (useWakeLock) {
wakeLock.release()
wifiLock?.release()
}
isRun = false
abandonFocus()
exoPlayer.release()
Expand All @@ -161,8 +172,12 @@ class AudioPlayService : BaseService(),
/**
* 播放音频
*/
@SuppressLint("WakelockTimeout")
private fun play() {
if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
if (useWakeLock) {
wakeLock.acquire()
wifiLock?.acquire()
}
upNotification()
if (!requestFocus()) {
return
Expand Down Expand Up @@ -197,7 +212,10 @@ class AudioPlayService : BaseService(),
* 暂停播放
*/
private fun pause(abandonFocus: Boolean = true) {
if (useWakeLock) wakeLock.release()
if (useWakeLock) {
wakeLock.release()
wifiLock?.release()
}
try {
pause = true
if (abandonFocus) {
Expand All @@ -218,8 +236,12 @@ class AudioPlayService : BaseService(),
/**
* 恢复播放
*/
@SuppressLint("WakelockTimeout")
private fun resume() {
if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
if (useWakeLock) {
wakeLock.acquire()
wifiLock?.acquire()
}
try {
pause = false
if (url.isEmpty()) {
Expand Down
24 changes: 21 additions & 3 deletions app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.content.IntentFilter
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.AudioManager
import android.net.wifi.WifiManager
import android.os.Bundle
import android.os.PowerManager
import android.support.v4.media.session.MediaSessionCompat
Expand Down Expand Up @@ -37,6 +38,7 @@ import splitties.init.appCtx
import splitties.systemservices.audioManager
import splitties.systemservices.notificationManager
import splitties.systemservices.powerManager
import splitties.systemservices.wifiManager

/**
* 朗读服务
Expand Down Expand Up @@ -70,6 +72,12 @@ abstract class BaseReadAloudService : BaseService(),
this.setReferenceCounted(false)
}
}
private val wifiLock by lazy {
@Suppress("DEPRECATION")
wifiManager?.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "legado:AudioPlayService")?.apply {
setReferenceCounted(false)
}
}
private val mFocusRequest: AudioFocusRequestCompat by lazy {
MediaHelp.buildAudioFocusRequestCompat(this)
}
Expand Down Expand Up @@ -135,7 +143,10 @@ abstract class BaseReadAloudService : BaseService(),

override fun onDestroy() {
super.onDestroy()
if (useWakeLock) wakeLock.release()
if (useWakeLock) {
wakeLock.release()
wifiLock?.release()
}
isRun = false
pause = true
abandonFocus()
Expand Down Expand Up @@ -210,8 +221,12 @@ abstract class BaseReadAloudService : BaseService(),
}
}

@SuppressLint("WakelockTimeout")
open fun play() {
if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
if (useWakeLock) {
wakeLock.acquire()
wifiLock?.acquire()
}
isRun = true
pause = false
needResumeOnAudioFocusGain = false
Expand All @@ -223,7 +238,10 @@ abstract class BaseReadAloudService : BaseService(),

@CallSuper
open fun pauseReadAloud(abandonFocus: Boolean = true) {
if (useWakeLock) wakeLock.release()
if (useWakeLock) {
wakeLock.release()
wifiLock?.release()
}
pause = true
if (abandonFocus) {
abandonFocus()
Expand Down
27 changes: 24 additions & 3 deletions app/src/main/java/io/legado/app/service/WebService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.legado.app.service
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.wifi.WifiManager
import android.os.Build
import android.os.PowerManager
import androidx.core.app.NotificationCompat
Expand All @@ -19,6 +20,7 @@ import io.legado.app.web.HttpServer
import io.legado.app.web.WebSocketServer
import splitties.init.appCtx
import splitties.systemservices.powerManager
import splitties.systemservices.wifiManager
import java.io.IOException

class WebService : BaseService() {
Expand Down Expand Up @@ -49,16 +51,27 @@ class WebService : BaseService() {
setReferenceCounted(false)
}
}
private val wifiLock by lazy {
@Suppress("DEPRECATION")
wifiManager?.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "legado:AudioPlayService")
?.apply {
setReferenceCounted(false)
}
}
private var httpServer: HttpServer? = null
private var webSocketServer: WebSocketServer? = null
private var notificationContent = appCtx.getString(R.string.service_starting)
private val networkChangedListener by lazy {
NetworkChangedListener(this)
}

@SuppressLint("WakelockTimeout")
override fun onCreate() {
super.onCreate()
if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
if (useWakeLock) {
wakeLock.acquire()
wifiLock?.acquire()
}
isRun = true
upTile(true)
networkChangedListener.register()
Expand All @@ -77,19 +90,27 @@ class WebService : BaseService() {
}
}

@SuppressLint("WakelockTimeout")
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
IntentAction.stop -> stopSelf()
"copyHostAddress" -> sendToClip(hostAddress)
"serve" -> if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
"serve" -> if (useWakeLock) {
wakeLock.acquire()
wifiLock?.acquire()
}

else -> upWebServer()
}
return super.onStartCommand(intent, flags, startId)
}

override fun onDestroy() {
super.onDestroy()
if (useWakeLock) wakeLock.release()
if (useWakeLock) {
wakeLock.release()
wifiLock?.release()
}
networkChangedListener.unRegister()
isRun = false
if (httpServer?.isAlive == true) {
Expand Down

0 comments on commit b434d34

Please sign in to comment.