From 1a3a6250e018872d538b5c731f2f9ab69ee00585 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 24 Sep 2020 12:25:21 +0800 Subject: [PATCH] =?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()