mirror of
https://github.com/gedoor/legado.git
synced 2024-07-06 23:47:49 +08:00
优化
This commit is contained in:
parent
245eb3e8bd
commit
b434d3499b
@ -1,5 +1,6 @@
|
||||
package io.legado.app.constant
|
||||
|
||||
@Suppress("ConstPropertyName")
|
||||
object PreferKey {
|
||||
const val language = "language"
|
||||
const val fontScale = "fontScale"
|
||||
|
@ -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
|
||||
@ -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
|
||||
/**
|
||||
@ -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)
|
||||
}
|
||||
@ -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()
|
||||
@ -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
|
||||
@ -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) {
|
||||
@ -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()) {
|
||||
|
@ -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
|
||||
@ -37,6 +38,7 @@ import splitties.init.appCtx
|
||||
import splitties.systemservices.audioManager
|
||||
import splitties.systemservices.notificationManager
|
||||
import splitties.systemservices.powerManager
|
||||
import splitties.systemservices.wifiManager
|
||||
|
||||
/**
|
||||
* 朗读服务
|
||||
@ -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)
|
||||
}
|
||||
@ -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()
|
||||
@ -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
|
||||
@ -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()
|
||||
|
@ -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
|
||||
@ -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() {
|
||||
@ -49,6 +51,13 @@ 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)
|
||||
@ -56,9 +65,13 @@ class WebService : BaseService() {
|
||||
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()
|
||||
@ -77,11 +90,16 @@ 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)
|
||||
@ -89,7 +107,10 @@ class WebService : BaseService() {
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
if (useWakeLock) wakeLock.release()
|
||||
if (useWakeLock) {
|
||||
wakeLock.release()
|
||||
wifiLock?.release()
|
||||
}
|
||||
networkChangedListener.unRegister()
|
||||
isRun = false
|
||||
if (httpServer?.isAlive == true) {
|
||||
|
Loading…
Reference in New Issue
Block a user