From 4a67405303d248ccbacec70d37b78731c4c176bd Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 26 Aug 2021 16:41:47 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/api/controller/SourceController.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/api/controller/SourceController.kt b/app/src/main/java/io/legado/app/api/controller/SourceController.kt index f4a92cd6d..80dd7d783 100644 --- a/app/src/main/java/io/legado/app/api/controller/SourceController.kt +++ b/app/src/main/java/io/legado/app/api/controller/SourceController.kt @@ -5,9 +5,9 @@ import android.text.TextUtils import io.legado.app.api.ReturnData import io.legado.app.data.appDb import io.legado.app.data.entities.BookSource +import io.legado.app.help.BookSourceAnalyzer import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonArray -import io.legado.app.utils.fromJsonObject import io.legado.app.utils.msg object SourceController { @@ -23,8 +23,9 @@ object SourceController { fun saveSource(postData: String?): ReturnData { val returnData = ReturnData() + postData ?: return returnData.setErrorMsg("数据不能为空") kotlin.runCatching { - val bookSource = GSON.fromJsonObject(postData) + val bookSource = BookSourceAnalyzer.jsonToBookSource(postData) if (bookSource != null) { if (TextUtils.isEmpty(bookSource.bookSourceName) || TextUtils.isEmpty(bookSource.bookSourceUrl)) { returnData.setErrorMsg("书源名称和URL不能为空") From 84fa663f7b83d482c1715c7abbde01292b5eb734 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 26 Aug 2021 22:02:44 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=9C=97=E8=AF=BB?= =?UTF-8?q?=E5=AE=9A=E6=97=B6=E8=AE=A1=E6=97=B6=E6=96=B9=E6=B3=95,handler.?= =?UTF-8?q?removeCallbacks=E6=9C=89=E6=97=B6=E5=80=99=E4=BC=9A=E5=A4=B1?= =?UTF-8?q?=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/service/BaseReadAloudService.kt | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) 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 7f50e1546..a30358f6b 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -29,6 +29,10 @@ import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.utils.getPrefBoolean import io.legado.app.utils.postEvent import io.legado.app.utils.toastOnUi +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import splitties.init.appCtx abstract class BaseReadAloudService : BaseService(), @@ -56,7 +60,7 @@ abstract class BaseReadAloudService : BaseService(), internal var readAloudNumber: Int = 0 internal var textChapter: TextChapter? = null internal var pageIndex = 0 - private val dsRunnable: Runnable = Runnable { doDs() } + private var dsJob: Job? = null override fun onCreate() { super.onCreate() @@ -69,7 +73,7 @@ abstract class BaseReadAloudService : BaseService(), initBroadcastReceiver() upNotification() upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PLAYING) - startDs() + doDs() } override fun onDestroy() { @@ -149,7 +153,7 @@ abstract class BaseReadAloudService : BaseService(), upMediaSessionPlaybackState(PlaybackStateCompat.STATE_PAUSED) postEvent(EventBus.ALOUD_STATE, Status.PAUSE) ReadBook.uploadProgress() - startDs() + doDs() } @CallSuper @@ -184,7 +188,7 @@ abstract class BaseReadAloudService : BaseService(), private fun setTimer(minute: Int) { timeMinute = minute - startDs() + doDs() } private fun addTimer() { @@ -194,32 +198,34 @@ abstract class BaseReadAloudService : BaseService(), timeMinute += 10 if (timeMinute > 180) timeMinute = 180 } - startDs() - } - - private fun startDs() { - postEvent(EventBus.TTS_DS, timeMinute) - upNotification() - handler.removeCallbacks(dsRunnable) - handler.postDelayed(dsRunnable, 60000) + doDs() } /** * 定时 */ + @Synchronized private fun doDs() { - handler.removeCallbacks(dsRunnable) - if (!pause) { - if (timeMinute >= 0) { - timeMinute-- - } - if (timeMinute == 0) { - ReadAloud.stop(this) - } - } postEvent(EventBus.TTS_DS, timeMinute) upNotification() - handler.postDelayed(dsRunnable, 60000) + dsJob?.cancel() + dsJob = launch { + while (isActive) { + delay(60000) + if (isActive) { + if (!pause) { + if (timeMinute >= 0) { + timeMinute-- + } + if (timeMinute == 0) { + ReadAloud.stop(this@BaseReadAloudService) + } + } + postEvent(EventBus.TTS_DS, timeMinute) + upNotification() + } + } + } } /** From 5e362db480c3d327bbca85d84aff60b6e5e52599 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 26 Aug 2021 23:01:35 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E8=AE=A1=E6=97=B6=E6=96=B9=E6=B3=95,handler.removeCallbacks?= =?UTF-8?q?=E6=9C=89=E6=97=B6=E5=80=99=E4=BC=9A=E5=A4=B1=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/service/BaseReadAloudService.kt | 3 - .../app/service/HttpReadAloudService.kt | 7 +- .../app/ui/book/read/ReadBookActivity.kt | 101 ++++++++++-------- 3 files changed, 59 insertions(+), 52 deletions(-) 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 a30358f6b..d31efc649 100644 --- a/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/BaseReadAloudService.kt @@ -8,8 +8,6 @@ import android.content.Intent import android.content.IntentFilter import android.graphics.BitmapFactory import android.media.AudioManager -import android.os.Handler -import android.os.Looper import android.support.v4.media.session.MediaSessionCompat import android.support.v4.media.session.PlaybackStateCompat import androidx.annotation.CallSuper @@ -48,7 +46,6 @@ abstract class BaseReadAloudService : BaseService(), } } - internal val handler = Handler(Looper.getMainLooper()) private lateinit var audioManager: AudioManager private var mFocusRequest: AudioFocusRequestCompat? = null private var broadcastReceiver: BroadcastReceiver? = null diff --git a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt index 0e75eb04e..18788fa4a 100644 --- a/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt +++ b/app/src/main/java/io/legado/app/service/HttpReadAloudService.kt @@ -10,8 +10,10 @@ import io.legado.app.model.ReadBook import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.service.help.ReadAloud import io.legado.app.utils.* +import kotlinx.coroutines.delay import kotlinx.coroutines.ensureActive import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import java.io.File import java.io.FileDescriptor import java.io.FileInputStream @@ -238,7 +240,8 @@ class HttpReadAloudService : BaseReadAloudService(), if (what == -38 && extra == 0) { return true } - handler.postDelayed({ + launch { + delay(100) readAloudNumber += contentList[nowSpeak].length + 1 if (nowSpeak < contentList.lastIndex) { nowSpeak++ @@ -246,7 +249,7 @@ class HttpReadAloudService : BaseReadAloudService(), } else { nextChapter() } - }, 50) + } return true } diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 7025b6442..b57bc6987 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -5,8 +5,6 @@ import android.app.Activity import android.content.Intent import android.content.res.Configuration import android.os.Bundle -import android.os.Handler -import android.os.Looper import android.view.* import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import androidx.activity.result.contract.ActivityResultContracts @@ -53,7 +51,9 @@ import io.legado.app.ui.replace.edit.ReplaceEditActivity import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.* import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.Job import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive import kotlinx.coroutines.launch class ReadBookActivity : ReadBookBaseActivity(), @@ -109,15 +109,9 @@ class ReadBookActivity : ReadBookBaseActivity(), override val isInitFinish: Boolean get() = viewModel.isInitFinish override val isScroll: Boolean get() = binding.readView.isScroll - private val mHandler = Handler(Looper.getMainLooper()) - private val keepScreenRunnable = Runnable { keepScreenOn(false) } - private val autoPageRunnable = Runnable { autoPagePlus() } - private val backupRunnable = Runnable { - if (!BuildConfig.DEBUG) { - ReadBook.uploadProgress() - Backup.autoBack(this) - } - } + private var keepScreenJon: Job? = null + private var autoPageJob: Job? = null + private var backupJob: Job? = null override var autoPageProgress = 0 override var isAutoPage = false private var screenTimeOut: Long = 0 @@ -169,7 +163,7 @@ class ReadBookActivity : ReadBookBaseActivity(), override fun onPause() { super.onPause() autoPageStop() - mHandler.removeCallbacks(backupRunnable) + backupJob?.cancel() ReadBook.saveRead() timeBatteryReceiver?.let { unregisterReceiver(it) @@ -629,7 +623,7 @@ class ReadBookActivity : ReadBookBaseActivity(), launch { autoPageProgress = 0 binding.readMenu.setSeekPage(ReadBook.durPageIndex()) - mHandler.postDelayed(backupRunnable, 600000) + startBackupJob() } } @@ -692,38 +686,42 @@ class ReadBookActivity : ReadBookBaseActivity(), override fun autoPageStop() { if (isAutoPage) { isAutoPage = false - mHandler.removeCallbacks(autoPageRunnable) + autoPageJob?.cancel() binding.readMenu.setAutoPage(false) upScreenTimeOut() } } private fun autoPagePlus() { - var delayMillis = ReadBookConfig.autoReadSpeed * 1000L / binding.readView.height - var scrollOffset = 1 - if (delayMillis < 20) { - var delayInt = delayMillis.toInt() - if (delayInt == 0) delayInt = 1 - scrollOffset = 20 / delayInt - delayMillis = 20 - } - mHandler.removeCallbacks(autoPageRunnable) - if (!menuLayoutIsVisible) { - if (binding.readView.isScroll) { - binding.readView.curPage.scroll(-scrollOffset) - } else { - autoPageProgress += scrollOffset - if (autoPageProgress >= binding.readView.height) { - autoPageProgress = 0 - if (!binding.readView.fillPage(PageDirection.NEXT)) { - autoPageStop() + autoPageJob?.cancel() + autoPageJob = launch { + while (isActive) { + var delayMillis = ReadBookConfig.autoReadSpeed * 1000L / binding.readView.height + var scrollOffset = 1 + if (delayMillis < 20) { + var delayInt = delayMillis.toInt() + if (delayInt == 0) delayInt = 1 + scrollOffset = 20 / delayInt + delayMillis = 20 + } + delay(delayMillis) + if (!menuLayoutIsVisible && isActive) { + if (binding.readView.isScroll) { + binding.readView.curPage.scroll(-scrollOffset) + } else { + autoPageProgress += scrollOffset + if (autoPageProgress >= binding.readView.height) { + autoPageProgress = 0 + if (!binding.readView.fillPage(PageDirection.NEXT)) { + autoPageStop() + } + } else { + binding.readView.invalidate() + } } - } else { - binding.readView.invalidate() } } } - mHandler.postDelayed(autoPageRunnable, delayMillis) } override fun openSourceEditActivity() { @@ -878,6 +876,13 @@ class ReadBookActivity : ReadBookBaseActivity(), } } + private fun startBackupJob() { + backupJob?.cancel() + backupJob = launch { + delay(120000) + } + } + override fun finish() { ReadBook.book?.let { if (!ReadBook.inBookshelf) { @@ -897,7 +902,6 @@ class ReadBookActivity : ReadBookBaseActivity(), override fun onDestroy() { super.onDestroy() - mHandler.removeCallbacks(keepScreenRunnable) textActionMenu.dismiss() binding.readView.onDestroy() ReadBook.msg = null @@ -975,17 +979,20 @@ class ReadBookActivity : ReadBookBaseActivity(), * 重置黑屏时间 */ override fun screenOffTimerStart() { - if (screenTimeOut < 0) { - keepScreenOn(true) - return - } - val t = screenTimeOut - sysScreenOffTime - if (t > 0) { - mHandler.removeCallbacks(keepScreenRunnable) - keepScreenOn(true) - mHandler.postDelayed(keepScreenRunnable, screenTimeOut) - } else { - keepScreenOn(false) + keepScreenJon?.cancel() + keepScreenJon = launch { + if (screenTimeOut < 0) { + keepScreenOn(true) + return@launch + } + val t = screenTimeOut - sysScreenOffTime + if (t > 0) { + keepScreenOn(true) + delay(screenTimeOut) + keepScreenOn(false) + } else { + keepScreenOn(false) + } } } } \ No newline at end of file From 58b8ec8f0bc5469923b2e1d70996f2c21077103e Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 26 Aug 2021 23:07:48 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index b57bc6987..5dbaa9de3 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -687,6 +687,7 @@ class ReadBookActivity : ReadBookBaseActivity(), if (isAutoPage) { isAutoPage = false autoPageJob?.cancel() + binding.readView.invalidate() binding.readMenu.setAutoPage(false) upScreenTimeOut() }