优化webService

pull/397/head
gedoor 4 years ago
parent ee3d6dadc8
commit 1a3a6250e0
  1. 2
      app/src/main/java/io/legado/app/constant/EventBus.kt
  2. 8
      app/src/main/java/io/legado/app/service/WebService.kt
  3. 18
      app/src/main/java/io/legado/app/ui/main/my/MyFragment.kt

@ -17,7 +17,7 @@ object EventBus {
const val AUDIO_SIZE = "audioSize" const val AUDIO_SIZE = "audioSize"
const val AUDIO_SPEED = "audioSpeed" const val AUDIO_SPEED = "audioSpeed"
const val SHOW_RSS = "showRss" const val SHOW_RSS = "showRss"
const val WEB_SERVICE_STOP = "webServiceStop" const val WEB_SERVICE = "webService"
const val UP_DOWNLOAD = "upDownload" const val UP_DOWNLOAD = "upDownload"
const val SAVE_CONTENT = "saveContent" const val SAVE_CONTENT = "saveContent"
const val CHECK_INIT = "checkInit" const val CHECK_INIT = "checkInit"

@ -24,6 +24,7 @@ class WebService : BaseService() {
companion object { companion object {
var isRun = false var isRun = false
var hostAddress = ""
fun start(context: Context) { fun start(context: Context) {
context.startService<WebService>() context.startService<WebService>()
@ -36,6 +37,7 @@ class WebService : BaseService() {
context.startService(intent) context.startService(intent)
} }
} }
} }
private var httpServer: HttpServer? = null private var httpServer: HttpServer? = null
@ -56,7 +58,7 @@ class WebService : BaseService() {
if (webSocketServer?.isAlive == true) { if (webSocketServer?.isAlive == true) {
webSocketServer?.stop() webSocketServer?.stop()
} }
postEvent(EventBus.WEB_SERVICE_STOP, true) postEvent(EventBus.WEB_SERVICE, "")
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
@ -82,8 +84,10 @@ class WebService : BaseService() {
try { try {
httpServer?.start() httpServer?.start()
webSocketServer?.start(1000 * 30) // 通信超时设置 webSocketServer?.start(1000 * 30) // 通信超时设置
hostAddress = getString(R.string.http_ip, address.hostAddress, port)
isRun = true isRun = true
updateNotification(getString(R.string.http_ip, address.hostAddress, port)) postEvent(EventBus.WEB_SERVICE, hostAddress)
updateNotification(hostAddress)
} catch (e: IOException) { } catch (e: IOException) {
launch { launch {
toast(e.localizedMessage ?: "") toast(e.localizedMessage ?: "")

@ -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.NameListPreference
import io.legado.app.ui.widget.prefs.PreferenceCategory import io.legado.app.ui.widget.prefs.PreferenceCategory
import io.legado.app.ui.widget.prefs.SwitchPreference 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 kotlinx.android.synthetic.main.view_title_bar.*
import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivity
@ -80,8 +83,15 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config), FileChooserDialog.
} }
addPreferencesFromResource(R.xml.pref_main) addPreferencesFromResource(R.xml.pref_main)
val webServicePre = findPreference<SwitchPreference>(PreferKey.webService) val webServicePre = findPreference<SwitchPreference>(PreferKey.webService)
observeEvent<Boolean>(EventBus.WEB_SERVICE_STOP) { observeEventSticky<String>(EventBus.WEB_SERVICE) {
webServicePre?.isChecked = false webServicePre?.let {
it.isChecked = WebService.isRun
it.summary = if (WebService.isRun) {
WebService.hostAddress
} else {
getString(R.string.web_service_desc)
}
}
} }
findPreference<NameListPreference>(PreferKey.themeMode)?.let { findPreference<NameListPreference>(PreferKey.themeMode)?.let {
it.setOnPreferenceChangeListener { _, _ -> it.setOnPreferenceChangeListener { _, _ ->
@ -118,10 +128,8 @@ class MyFragment : BaseFragment(R.layout.fragment_my_config), FileChooserDialog.
PreferKey.webService -> { PreferKey.webService -> {
if (requireContext().getPrefBoolean("webService")) { if (requireContext().getPrefBoolean("webService")) {
WebService.start(requireContext()) WebService.start(requireContext())
toast(R.string.service_start)
} else { } else {
WebService.stop(requireContext()) WebService.stop(requireContext())
toast(R.string.service_stop)
} }
} }
"recordLog" -> LogUtils.upLevel() "recordLog" -> LogUtils.upLevel()

Loading…
Cancel
Save