diff --git a/app/src/main/java/io/legado/app/constant/PreferKey.kt b/app/src/main/java/io/legado/app/constant/PreferKey.kt index 71ed456f3..b1a4809b7 100644 --- a/app/src/main/java/io/legado/app/constant/PreferKey.kt +++ b/app/src/main/java/io/legado/app/constant/PreferKey.kt @@ -119,7 +119,9 @@ object PreferKey { const val ttsTimer = "ttsTimer" const val noAnimScrollPage = "noAnimScrollPage" const val webDavDeviceName = "webDavDeviceName" - const val wakeLock = "wakeLock" + const val webServiceWakeLock = "webServiceWakeLock" + const val audioPlayWakeLock = "audioPlayWakeLock" + const val readAloudWakeLock = "readAloudWakeLock" const val showLastUpdateTime = "showLastUpdateTime" const val cPrimary = "colorPrimary" diff --git a/app/src/main/java/io/legado/app/help/config/AppConfig.kt b/app/src/main/java/io/legado/app/help/config/AppConfig.kt index 1519907d7..501b8adf1 100644 --- a/app/src/main/java/io/legado/app/help/config/AppConfig.kt +++ b/app/src/main/java/io/legado/app/help/config/AppConfig.kt @@ -423,6 +423,12 @@ object AppConfig : SharedPreferences.OnSharedPreferenceChangeListener { appCtx.putPrefInt(PreferKey.sourceEditMaxLine, value) } + var audioPlayUseWakeLock: Boolean + get() = appCtx.getPrefBoolean(PreferKey.audioPlayWakeLock) + set(value) { + appCtx.putPrefBoolean(PreferKey.audioPlayWakeLock, value) + } + fun detectClickArea() { if (clickActionTL * clickActionTC * clickActionTR * clickActionML * clickActionMC * clickActionMR diff --git a/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt b/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt index 4b0de9718..e23c35824 100644 --- a/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt +++ b/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt @@ -32,7 +32,7 @@ object BackupConfig { PreferKey.webDavPassword, PreferKey.launcherIcon, PreferKey.bitmapCacheSize, - PreferKey.wakeLock + PreferKey.webServiceWakeLock ) //配置忽略标题 diff --git a/app/src/main/java/io/legado/app/service/AudioPlayService.kt b/app/src/main/java/io/legado/app/service/AudioPlayService.kt index a5354cabf..f81ab1a2b 100644 --- a/app/src/main/java/io/legado/app/service/AudioPlayService.kt +++ b/app/src/main/java/io/legado/app/service/AudioPlayService.kt @@ -63,8 +63,12 @@ class AudioPlayService : BaseService(), private set } + private val useWakeLock = AppConfig.audioPlayUseWakeLock private val wakeLock by lazy { - powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "legado:webService") + powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "legado:AudioPlayService") + .apply { + this.setReferenceCounted(false) + } } private val mFocusRequest: AudioFocusRequestCompat by lazy { MediaHelp.buildAudioFocusRequestCompat(this) @@ -87,10 +91,8 @@ class AudioPlayService : BaseService(), private var upPlayProgressJob: Job? = null private var playSpeed: Float = 1f - @SuppressLint("WakelockTimeout") override fun onCreate() { super.onCreate() - wakeLock.acquire() isRun = true upNotification() exoPlayer.addListener(this) @@ -127,7 +129,7 @@ class AudioPlayService : BaseService(), override fun onDestroy() { super.onDestroy() - wakeLock.release() + if (useWakeLock) wakeLock.release() isRun = false abandonFocus() exoPlayer.release() @@ -142,6 +144,7 @@ class AudioPlayService : BaseService(), * 播放音频 */ private fun play() { + if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/) upNotification() if (requestFocus()) { execute(context = Main) { @@ -176,6 +179,7 @@ class AudioPlayService : BaseService(), * 暂停播放 */ private fun pause(abandonFocus: Boolean = true) { + if (useWakeLock) wakeLock.release() try { pause = true if (abandonFocus) { @@ -197,6 +201,7 @@ class AudioPlayService : BaseService(), * 恢复播放 */ private fun resume() { + if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/) try { pause = false if (url.isEmpty()) { diff --git a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt index 7cba4dd70..b05b1f709 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -9,6 +9,7 @@ import android.content.IntentFilter import android.graphics.BitmapFactory import android.media.AudioManager import android.os.Bundle +import android.os.PowerManager import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.PlaybackStateCompat import androidx.annotation.CallSuper @@ -27,7 +28,9 @@ import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.utils.* import kotlinx.coroutines.* +import splitties.init.appCtx import splitties.systemservices.audioManager +import splitties.systemservices.powerManager /** * 朗读服务 @@ -51,8 +54,16 @@ abstract class BaseReadAloudService : BaseService(), fun isPlay(): Boolean { return isRun && !pause } + } + private val useWakeLock = appCtx.getPrefBoolean(PreferKey.readAloudWakeLock, false) + private val wakeLock by lazy { + powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "legado:ReadAloudService") + .apply { + this.setReferenceCounted(false) + } + } private val mFocusRequest: AudioFocusRequestCompat by lazy { MediaHelp.buildAudioFocusRequestCompat(this) } @@ -102,6 +113,7 @@ abstract class BaseReadAloudService : BaseService(), override fun onDestroy() { super.onDestroy() + if (useWakeLock) wakeLock.release() isRun = false pause = true abandonFocus() @@ -150,6 +162,7 @@ abstract class BaseReadAloudService : BaseService(), } open fun play() { + if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/) isRun = true pause = false needResumeOnAudioFocusGain = false @@ -161,6 +174,7 @@ abstract class BaseReadAloudService : BaseService(), @CallSuper open fun pauseReadAloud(abandonFocus: Boolean = true) { + if (useWakeLock) wakeLock.release() pause = true if (abandonFocus) { abandonFocus() diff --git a/app/src/main/java/io/legado/app/service/WebService.kt b/app/src/main/java/io/legado/app/service/WebService.kt index 7871341b0..8d54f76a1 100644 --- a/app/src/main/java/io/legado/app/service/WebService.kt +++ b/app/src/main/java/io/legado/app/service/WebService.kt @@ -35,11 +35,19 @@ class WebService : BaseService() { context.stopService() } + fun serve() { + appCtx.startService { + action = "serve" + } + } } - private val useWakeLock = appCtx.getPrefBoolean(PreferKey.wakeLock, false) - private val wakeLock by lazy { + private val useWakeLock = appCtx.getPrefBoolean(PreferKey.webServiceWakeLock, false) + private val wakeLock: PowerManager.WakeLock by lazy { powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "legado:webService") + .apply { + setReferenceCounted(false) + } } private var httpServer: HttpServer? = null private var webSocketServer: WebSocketServer? = null @@ -78,6 +86,7 @@ class WebService : BaseService() { when (intent?.action) { IntentAction.stop -> stopSelf() "copyHostAddress" -> sendToClip(hostAddress) + "serve" -> if (useWakeLock) wakeLock.acquire(10 * 60 * 1000L /*10 minutes*/) else -> upWebServer() } return super.onStartCommand(intent, flags, startId) diff --git a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt index d80f1a0bc..6acaffd80 100644 --- a/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/audio/AudioPlayActivity.kt @@ -51,7 +51,6 @@ class AudioPlayActivity : override val binding by viewBinding(ActivityAudioPlayBinding::inflate) override val viewModel by viewModels() - private var menu: Menu? = null private var adjustProgress = false private val timerViewState = mutableStateOf(false) private val progressTimeFormat by lazy { @@ -94,10 +93,10 @@ class AudioPlayActivity : return super.onCompatCreateOptionsMenu(menu) } - override fun onPrepareOptionsMenu(menu: Menu): Boolean { - this.menu = menu - upMenu() - return super.onPrepareOptionsMenu(menu) + override fun onMenuOpened(featureId: Int, menu: Menu): Boolean { + menu.findItem(R.id.menu_login)?.isVisible = !AudioPlay.bookSource?.loginUrl.isNullOrBlank() + menu.findItem(R.id.menu_wake_lock)?.isChecked = AppConfig.audioPlayUseWakeLock + return super.onMenuOpened(featureId, menu) } override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { @@ -111,6 +110,7 @@ class AudioPlayActivity : putExtra("key", it.bookSourceUrl) } } + R.id.menu_wake_lock -> AppConfig.audioPlayUseWakeLock = !AppConfig.audioPlayUseWakeLock R.id.menu_copy_audio_url -> sendToClip(AudioPlayService.url) R.id.menu_edit_source -> AudioPlay.bookSource?.let { sourceEditResult.launch { @@ -181,13 +181,6 @@ class AudioPlayActivity : } } - private fun upMenu() { - menu?.let { menu -> - menu.findItem(R.id.menu_login)?.isVisible = - !AudioPlay.bookSource?.loginUrl.isNullOrBlank() - } - } - private fun upCover(path: String?) { BookCover.load(this, path, sourceOrigin = AudioPlay.bookSource?.bookSourceUrl) .into(binding.ivCover) diff --git a/app/src/main/java/io/legado/app/web/HttpServer.kt b/app/src/main/java/io/legado/app/web/HttpServer.kt index 593260b4c..71b915410 100644 --- a/app/src/main/java/io/legado/app/web/HttpServer.kt +++ b/app/src/main/java/io/legado/app/web/HttpServer.kt @@ -8,6 +8,7 @@ import io.legado.app.api.controller.BookController import io.legado.app.api.controller.BookSourceController import io.legado.app.api.controller.ReplaceRuleController import io.legado.app.api.controller.RssSourceController +import io.legado.app.service.WebService import io.legado.app.utils.FileUtils import io.legado.app.utils.externalFiles import io.legado.app.web.utils.AssetsWeb @@ -18,8 +19,8 @@ import java.io.* class HttpServer(port: Int) : NanoHTTPD(port) { private val assetsWeb = AssetsWeb("web") - override fun serve(session: IHTTPSession): Response { + WebService.serve() var returnData: ReturnData? = null val ct = ContentType(session.headers["content-type"]).tryUTF8() session.headers["content-type"] = ct.contentTypeHeader diff --git a/app/src/main/java/io/legado/app/web/WebSocketServer.kt b/app/src/main/java/io/legado/app/web/WebSocketServer.kt index 45fd04bd4..3e95c2101 100644 --- a/app/src/main/java/io/legado/app/web/WebSocketServer.kt +++ b/app/src/main/java/io/legado/app/web/WebSocketServer.kt @@ -1,12 +1,14 @@ package io.legado.app.web import fi.iki.elonen.NanoWSD +import io.legado.app.service.WebService import io.legado.app.web.socket.BookSourceDebugWebSocket import io.legado.app.web.socket.RssSourceDebugWebSocket class WebSocketServer(port: Int) : NanoWSD(port) { override fun openWebSocket(handshake: IHTTPSession): WebSocket? { + WebService.serve() return when (handshake.uri) { "/bookSourceDebug" -> { BookSourceDebugWebSocket(handshake) diff --git a/app/src/main/res/menu/audio_play.xml b/app/src/main/res/menu/audio_play.xml index f21032067..8d9362f28 100644 --- a/app/src/main/res/menu/audio_play.xml +++ b/app/src/main/res/menu/audio_play.xml @@ -25,6 +25,13 @@ android:title="@string/edit_book_source" app:showAsAction="never" /> + + + 是否确认清除所有搜索历史记录 禁用滚动点击动画 设备名称 - 唤醒锁 - 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + WebService唤醒锁 + 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 朗读服务唤醒锁 + 开启朗读的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 音频服务唤醒锁 + 播放音频的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 拷贝规则 粘贴规则 diff --git a/app/src/main/res/values-ja-rJP/strings.xml b/app/src/main/res/values-ja-rJP/strings.xml index 0272c3849..62e063a9e 100644 --- a/app/src/main/res/values-ja-rJP/strings.xml +++ b/app/src/main/res/values-ja-rJP/strings.xml @@ -1048,8 +1048,12 @@ 是否确认清除所有搜索历史记录 禁用滚动点击动画 设备名称 - 唤醒锁 - 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + WebService唤醒锁 + 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 朗读服务唤醒锁 + 开启朗读的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 音频服务唤醒锁 + 播放音频的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 拷贝规则 粘贴规则 diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 9dd2f96de..a97c47864 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -1048,8 +1048,12 @@ 是否确认清除所有搜索历史记录 禁用滚动点击动画 设备名称 - 唤醒锁 - 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + WebService唤醒锁 + 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 朗读服务唤醒锁 + 开启朗读的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 音频服务唤醒锁 + 播放音频的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 拷贝规则 粘贴规则 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 4bdc973cb..96bfdd412 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -1045,8 +1045,12 @@ 是否确认清除所有搜索历史记录 禁用滚动点击动画 设备名称 - 唤醒锁 - 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + WebService唤醒锁 + 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 朗读服务唤醒锁 + 开启朗读的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 音频服务唤醒锁 + 播放音频的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 拷贝规则 粘贴规则 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index d6ea4f09a..a9716f94f 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -1047,8 +1047,12 @@ 是否确认清除所有搜索历史记录 禁用滚动点击动画 设备名称 - 唤醒锁 - 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + WebService唤醒锁 + 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 朗读服务唤醒锁 + 开启朗读的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 音频服务唤醒锁 + 播放音频的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 拷贝规则 粘贴规则 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 5884fea6a..033f0e42d 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -1047,8 +1047,12 @@ 是否确认清除所有搜索历史记录 禁用滚动点击动画 设备名称 - 唤醒锁 - 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + WebService唤醒锁 + 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 朗读服务唤醒锁 + 开启朗读的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 音频服务唤醒锁 + 播放音频的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 拷贝规则 粘贴规则 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 920f6d8ba..4cc0b2cb8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1048,8 +1048,12 @@ 是否确认清除所有搜索历史记录 禁用滚动点击动画 设备名称 - 唤醒锁 - 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + WebService唤醒锁 + 开启web服务的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 朗读服务唤醒锁 + 开启朗读的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 + 音频服务唤醒锁 + 播放音频的时候启用唤醒锁,有些手机开启唤醒锁会被杀后台 切换搜索范围 拷贝规则 粘贴规则 diff --git a/app/src/main/res/xml/pref_config_aloud.xml b/app/src/main/res/xml/pref_config_aloud.xml index 333e0aa40..48e505776 100644 --- a/app/src/main/res/xml/pref_config_aloud.xml +++ b/app/src/main/res/xml/pref_config_aloud.xml @@ -8,6 +8,13 @@ app:allowDividerBelow="false" app:iconSpaceReserved="false"> + + + android:key="webServiceWakeLock" + android:title="@string/web_service_wake_lock" + android:summary="@string/web_service_wake_lock_summary" />