优化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_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"

@ -24,6 +24,7 @@ class WebService : BaseService() {
companion object {
var isRun = false
var hostAddress = ""
fun start(context: Context) {
context.startService<WebService>()
@ -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 ?: "")

@ -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<SwitchPreference>(PreferKey.webService)
observeEvent<Boolean>(EventBus.WEB_SERVICE_STOP) {
webServicePre?.isChecked = false
observeEventSticky<String>(EventBus.WEB_SERVICE) {
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 {
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()

Loading…
Cancel
Save