优化
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package io.legado.app.constant
|
package io.legado.app.constant
|
||||||
|
|
||||||
|
@Suppress("ConstPropertyName")
|
||||||
object PreferKey {
|
object PreferKey {
|
||||||
const val language = "language"
|
const val language = "language"
|
||||||
const val fontScale = "fontScale"
|
const val fontScale = "fontScale"
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import android.content.IntentFilter
|
|||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
import android.media.AudioManager
|
import android.media.AudioManager
|
||||||
|
import android.net.wifi.WifiManager.WIFI_MODE_FULL_HIGH_PERF
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
@@ -42,6 +43,7 @@ import kotlinx.coroutines.Dispatchers.Main
|
|||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
import splitties.systemservices.audioManager
|
import splitties.systemservices.audioManager
|
||||||
import splitties.systemservices.powerManager
|
import splitties.systemservices.powerManager
|
||||||
|
import splitties.systemservices.wifiManager
|
||||||
|
|
||||||
@UnstableApi
|
@UnstableApi
|
||||||
/**
|
/**
|
||||||
@@ -82,6 +84,12 @@ class AudioPlayService : BaseService(),
|
|||||||
this.setReferenceCounted(false)
|
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 {
|
private val mFocusRequest: AudioFocusRequestCompat by lazy {
|
||||||
MediaHelp.buildAudioFocusRequestCompat(this)
|
MediaHelp.buildAudioFocusRequestCompat(this)
|
||||||
}
|
}
|
||||||
@@ -147,7 +155,10 @@ class AudioPlayService : BaseService(),
|
|||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
if (useWakeLock) wakeLock.release()
|
if (useWakeLock) {
|
||||||
|
wakeLock.release()
|
||||||
|
wifiLock?.release()
|
||||||
|
}
|
||||||
isRun = false
|
isRun = false
|
||||||
abandonFocus()
|
abandonFocus()
|
||||||
exoPlayer.release()
|
exoPlayer.release()
|
||||||
@@ -161,8 +172,12 @@ class AudioPlayService : BaseService(),
|
|||||||
/**
|
/**
|
||||||
* 播放音频
|
* 播放音频
|
||||||
*/
|
*/
|
||||||
|
@SuppressLint("WakelockTimeout")
|
||||||
private fun play() {
|
private fun play() {
|
||||||
if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
|
if (useWakeLock) {
|
||||||
|
wakeLock.acquire()
|
||||||
|
wifiLock?.acquire()
|
||||||
|
}
|
||||||
upNotification()
|
upNotification()
|
||||||
if (!requestFocus()) {
|
if (!requestFocus()) {
|
||||||
return
|
return
|
||||||
@@ -197,7 +212,10 @@ class AudioPlayService : BaseService(),
|
|||||||
* 暂停播放
|
* 暂停播放
|
||||||
*/
|
*/
|
||||||
private fun pause(abandonFocus: Boolean = true) {
|
private fun pause(abandonFocus: Boolean = true) {
|
||||||
if (useWakeLock) wakeLock.release()
|
if (useWakeLock) {
|
||||||
|
wakeLock.release()
|
||||||
|
wifiLock?.release()
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
pause = true
|
pause = true
|
||||||
if (abandonFocus) {
|
if (abandonFocus) {
|
||||||
@@ -218,8 +236,12 @@ class AudioPlayService : BaseService(),
|
|||||||
/**
|
/**
|
||||||
* 恢复播放
|
* 恢复播放
|
||||||
*/
|
*/
|
||||||
|
@SuppressLint("WakelockTimeout")
|
||||||
private fun resume() {
|
private fun resume() {
|
||||||
if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
|
if (useWakeLock) {
|
||||||
|
wakeLock.acquire()
|
||||||
|
wifiLock?.acquire()
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
pause = false
|
pause = false
|
||||||
if (url.isEmpty()) {
|
if (url.isEmpty()) {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import android.content.IntentFilter
|
|||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import android.graphics.BitmapFactory
|
import android.graphics.BitmapFactory
|
||||||
import android.media.AudioManager
|
import android.media.AudioManager
|
||||||
|
import android.net.wifi.WifiManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
import android.support.v4.media.session.MediaSessionCompat
|
import android.support.v4.media.session.MediaSessionCompat
|
||||||
@@ -37,6 +38,7 @@ import splitties.init.appCtx
|
|||||||
import splitties.systemservices.audioManager
|
import splitties.systemservices.audioManager
|
||||||
import splitties.systemservices.notificationManager
|
import splitties.systemservices.notificationManager
|
||||||
import splitties.systemservices.powerManager
|
import splitties.systemservices.powerManager
|
||||||
|
import splitties.systemservices.wifiManager
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 朗读服务
|
* 朗读服务
|
||||||
@@ -70,6 +72,12 @@ abstract class BaseReadAloudService : BaseService(),
|
|||||||
this.setReferenceCounted(false)
|
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 {
|
private val mFocusRequest: AudioFocusRequestCompat by lazy {
|
||||||
MediaHelp.buildAudioFocusRequestCompat(this)
|
MediaHelp.buildAudioFocusRequestCompat(this)
|
||||||
}
|
}
|
||||||
@@ -135,7 +143,10 @@ abstract class BaseReadAloudService : BaseService(),
|
|||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
if (useWakeLock) wakeLock.release()
|
if (useWakeLock) {
|
||||||
|
wakeLock.release()
|
||||||
|
wifiLock?.release()
|
||||||
|
}
|
||||||
isRun = false
|
isRun = false
|
||||||
pause = true
|
pause = true
|
||||||
abandonFocus()
|
abandonFocus()
|
||||||
@@ -210,8 +221,12 @@ abstract class BaseReadAloudService : BaseService(),
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("WakelockTimeout")
|
||||||
open fun play() {
|
open fun play() {
|
||||||
if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
|
if (useWakeLock) {
|
||||||
|
wakeLock.acquire()
|
||||||
|
wifiLock?.acquire()
|
||||||
|
}
|
||||||
isRun = true
|
isRun = true
|
||||||
pause = false
|
pause = false
|
||||||
needResumeOnAudioFocusGain = false
|
needResumeOnAudioFocusGain = false
|
||||||
@@ -223,7 +238,10 @@ abstract class BaseReadAloudService : BaseService(),
|
|||||||
|
|
||||||
@CallSuper
|
@CallSuper
|
||||||
open fun pauseReadAloud(abandonFocus: Boolean = true) {
|
open fun pauseReadAloud(abandonFocus: Boolean = true) {
|
||||||
if (useWakeLock) wakeLock.release()
|
if (useWakeLock) {
|
||||||
|
wakeLock.release()
|
||||||
|
wifiLock?.release()
|
||||||
|
}
|
||||||
pause = true
|
pause = true
|
||||||
if (abandonFocus) {
|
if (abandonFocus) {
|
||||||
abandonFocus()
|
abandonFocus()
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package io.legado.app.service
|
|||||||
import android.annotation.SuppressLint
|
import android.annotation.SuppressLint
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.net.wifi.WifiManager
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.PowerManager
|
import android.os.PowerManager
|
||||||
import androidx.core.app.NotificationCompat
|
import androidx.core.app.NotificationCompat
|
||||||
@@ -19,6 +20,7 @@ import io.legado.app.web.HttpServer
|
|||||||
import io.legado.app.web.WebSocketServer
|
import io.legado.app.web.WebSocketServer
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
import splitties.systemservices.powerManager
|
import splitties.systemservices.powerManager
|
||||||
|
import splitties.systemservices.wifiManager
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
class WebService : BaseService() {
|
class WebService : BaseService() {
|
||||||
@@ -49,6 +51,13 @@ class WebService : BaseService() {
|
|||||||
setReferenceCounted(false)
|
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 httpServer: HttpServer? = null
|
||||||
private var webSocketServer: WebSocketServer? = null
|
private var webSocketServer: WebSocketServer? = null
|
||||||
private var notificationContent = appCtx.getString(R.string.service_starting)
|
private var notificationContent = appCtx.getString(R.string.service_starting)
|
||||||
@@ -56,9 +65,13 @@ class WebService : BaseService() {
|
|||||||
NetworkChangedListener(this)
|
NetworkChangedListener(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("WakelockTimeout")
|
||||||
override fun onCreate() {
|
override fun onCreate() {
|
||||||
super.onCreate()
|
super.onCreate()
|
||||||
if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
|
if (useWakeLock) {
|
||||||
|
wakeLock.acquire()
|
||||||
|
wifiLock?.acquire()
|
||||||
|
}
|
||||||
isRun = true
|
isRun = true
|
||||||
upTile(true)
|
upTile(true)
|
||||||
networkChangedListener.register()
|
networkChangedListener.register()
|
||||||
@@ -77,11 +90,16 @@ class WebService : BaseService() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("WakelockTimeout")
|
||||||
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.stop -> stopSelf()
|
IntentAction.stop -> stopSelf()
|
||||||
"copyHostAddress" -> sendToClip(hostAddress)
|
"copyHostAddress" -> sendToClip(hostAddress)
|
||||||
"serve" -> if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/)
|
"serve" -> if (useWakeLock) {
|
||||||
|
wakeLock.acquire()
|
||||||
|
wifiLock?.acquire()
|
||||||
|
}
|
||||||
|
|
||||||
else -> upWebServer()
|
else -> upWebServer()
|
||||||
}
|
}
|
||||||
return super.onStartCommand(intent, flags, startId)
|
return super.onStartCommand(intent, flags, startId)
|
||||||
@@ -89,7 +107,10 @@ class WebService : BaseService() {
|
|||||||
|
|
||||||
override fun onDestroy() {
|
override fun onDestroy() {
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
if (useWakeLock) wakeLock.release()
|
if (useWakeLock) {
|
||||||
|
wakeLock.release()
|
||||||
|
wifiLock?.release()
|
||||||
|
}
|
||||||
networkChangedListener.unRegister()
|
networkChangedListener.unRegister()
|
||||||
isRun = false
|
isRun = false
|
||||||
if (httpServer?.isAlive == true) {
|
if (httpServer?.isAlive == true) {
|
||||||
|
|||||||
Reference in New Issue
Block a user