From 1a3a6250e018872d538b5c731f2f9ab69ee00585 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 24 Sep 2020 12:25:21 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BC=98=E5=8C=96webService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/constant/EventBus.kt | 2 +- .../java/io/legado/app/service/WebService.kt | 8 ++++++-- .../io/legado/app/ui/main/my/MyFragment.kt | 18 +++++++++++++----- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/EventBus.kt b/app/src/main/java/io/legado/app/constant/EventBus.kt index 8ea4122b9..1ef2e9b44 100644 --- a/app/src/main/java/io/legado/app/constant/EventBus.kt +++ b/app/src/main/java/io/legado/app/constant/EventBus.kt @@ -17,7 +17,7 @@ object EventBus { const val AUDIO_SIZE = "audioSize" const val AUDIO_SPEED = "audioSpeed" const val SHOW_RSS = "showRss" - const val WEB_SERVICE_STOP = "webServiceStop" + const val WEB_SERVICE = "webService" const val UP_DOWNLOAD = "upDownload" const val SAVE_CONTENT = "saveContent" const val CHECK_INIT = "checkInit" 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 bbf317066..83168593c 100644 --- a/app/src/main/java/io/legado/app/service/WebService.kt +++ b/app/src/main/java/io/legado/app/service/WebService.kt @@ -24,6 +24,7 @@ class WebService : BaseService() { companion object { var isRun = false + var hostAddress = "" fun start(context: Context) { context.startService() @@ -36,6 +37,7 @@ class WebService : BaseService() { context.startService(intent) } } + } private var httpServer: HttpServer? = null @@ -56,7 +58,7 @@ class WebService : BaseService() { if (webSocketServer?.isAlive == true) { webSocketServer?.stop() } - postEvent(EventBus.WEB_SERVICE_STOP, true) + postEvent(EventBus.WEB_SERVICE, "") } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { @@ -82,8 +84,10 @@ class WebService : BaseService() { try { httpServer?.start() webSocketServer?.start(1000 * 30) // 通信超时设置 + hostAddress = getString(R.string.http_ip, address.hostAddress, port) isRun = true - updateNotification(getString(R.string.http_ip, address.hostAddress, port)) + postEvent(EventBus.WEB_SERVICE, hostAddress) + updateNotification(hostAddress) } catch (e: IOException) { launch { toast(e.localizedMessage ?: "") diff --git a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt index e4bc131ba..8ceb76b48 100644 --- a/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt @@ -29,7 +29,10 @@ import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.ui.widget.prefs.NameListPreference import io.legado.app.ui.widget.prefs.PreferenceCategory import io.legado.app.ui.widget.prefs.SwitchPreference -import io.legado.app.utils.* +import io.legado.app.utils.LogUtils +import io.legado.app.utils.getPrefBoolean +import io.legado.app.utils.observeEventSticky +import io.legado.app.utils.putPrefBoolean import kotlinx.android.synthetic.main.view_title_bar.* import org.jetbrains.anko.startActivity @@ -80,8 +83,15 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config), FileChooserDialog. } addPreferencesFromResource(R.xml.pref_main) val webServicePre = findPreference(PreferKey.webService) - observeEvent(EventBus.WEB_SERVICE_STOP) { - webServicePre?.isChecked = false + observeEventSticky(EventBus.WEB_SERVICE) { + webServicePre?.let { + it.isChecked = WebService.isRun + it.summary = if (WebService.isRun) { + WebService.hostAddress + } else { + getString(R.string.web_service_desc) + } + } } findPreference(PreferKey.themeMode)?.let { it.setOnPreferenceChangeListener { _, _ -> @@ -118,10 +128,8 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config), FileChooserDialog. PreferKey.webService -> { if (requireContext().getPrefBoolean("webService")) { WebService.start(requireContext()) - toast(R.string.service_start) } else { WebService.stop(requireContext()) - toast(R.string.service_stop) } } "recordLog" -> LogUtils.upLevel() From 4f9aa56148472217e95534f6002d83d6793f9f0e Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 24 Sep 2020 12:29:57 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96webService?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/res/values-zh-rHK/strings.xml | 2 +- app/src/main/res/values-zh-rTW/strings.xml | 2 +- app/src/main/res/values-zh/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 4d38941b5..de31599a4 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -82,7 +82,7 @@ 載入中… 重試 Web 服務 - 啟用 Web 服務 + 瀏覽器寫源,看書 web 編輯書源 http://%1$s:%2$d 離線緩存 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 6335186ca..0d44a8fae 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -82,7 +82,7 @@ 載入中… 重試 Web 服務 - 啟用Web服務 + 瀏覽器寫源,看書 web編輯書源 http://%1$s:%2$d 離線緩存 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 92977d3f5..e3cc6c436 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -84,7 +84,7 @@ 加载中… 重试 Web 服务 - 启用Web服务 + 浏览器写源,看书 web编辑书源 http://%1$s:%2$d 离线缓存 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4779b5909..b7fe8a37f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -84,7 +84,7 @@ Loading… Retry Web service - Enable web service + Web edit source and read book Edit book sources on the web http://%1$s:%2$d Offline cache