StandAPP/app/src/main/java/com/stand/standapp/MainActivity.kt

145 lines
4.7 KiB
Kotlin

package com.stand.standapp
import android.os.Bundle
import android.view.KeyEvent
import android.view.ViewGroup
import android.widget.LinearLayout
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.just.agentweb.AgentWeb
import com.stand.standapp.R
class MainActivity : AppCompatActivity() {
private var mAgentWeb: AgentWeb? = null
private val mHandler = android.os.Handler(android.os.Looper.getMainLooper())
private val TIMEOUT_MS = 30000L
private val timeoutRunnable = Runnable {
showErrorPage()
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
try {
setContentView(R.layout.activity_main)
val container = findViewById<LinearLayout>(R.id.container)
if (container == null) return
findViewById<android.widget.Button>(R.id.btn_retry)?.setOnClickListener {
hideErrorPage()
mAgentWeb?.webCreator?.webView?.reload()
startTimeoutTimer()
}
// 从配置中获取服务器地址
val serverUrl = AppConfig.getServerUrl(this)
val finalUrl = "$serverUrl/example/print_demo.html?t=${System.currentTimeMillis()}"
Log.d("MainActivity", "Loading URL: $finalUrl")
mAgentWeb = AgentWeb.with(this)
.setAgentWebParent(container, LinearLayout.LayoutParams(-1, -1))
.useDefaultIndicator()
.addJavascriptInterface("android", AndroidInterface(this))
.createAgentWeb()
.ready()
.go(finalUrl)
startTimeoutTimer()
// 配置 WebView 属性以支持 SSE 和跨域
mAgentWeb?.let { agent ->
try {
val webView = agent.webCreator.webView
val settings = webView.settings
// 允许混合内容 (HTTP/HTTPS)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
settings.mixedContentMode = android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
}
// 允许跨域和存储
settings.allowUniversalAccessFromFileURLs = true
settings.allowFileAccessFromFileURLs = true
settings.domStorageEnabled = true
settings.javaScriptEnabled = true
} catch (e: Exception) {
Log.e("MainActivity", "WebView settings error: ${e.message}")
}
}
// .go("http://152.32.186.199/local/print_demo.html")
// 启动时检查更新
UpdateManager(this).checkUpdate()
} catch (e: Exception) {
Log.e("MainActivity", "Fatal Crash: ${e.message}")
e.printStackTrace()
}
}
fun getAgentWeb(): AgentWeb? {
return mAgentWeb
}
fun showExitDialog() {
// 调用 JS 函数
mAgentWeb?.jsAccessEntrace?.quickCallJs("addExitText")
androidx.appcompat.app.AlertDialog.Builder(this)
.setTitle("提示")
.setMessage("确定要退出应用吗?")
.setPositiveButton("确定") { _, _ -> finish() }
.setNegativeButton("取消", null)
.show()
}
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
if (mAgentWeb?.handleKeyEvent(keyCode, event) == true) {
return true
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
showExitDialog()
return true
}
return super.onKeyDown(keyCode, event)
}
override fun onPause() {
mAgentWeb?.webLifeCycle?.onPause()
super.onPause()
}
override fun onResume() {
mAgentWeb?.webLifeCycle?.onResume()
super.onResume()
}
override fun onDestroy() {
mAgentWeb?.webLifeCycle?.onDestroy()
cancelTimeoutTimer()
super.onDestroy()
}
private fun startTimeoutTimer() {
cancelTimeoutTimer()
mHandler.postDelayed(timeoutRunnable, TIMEOUT_MS)
}
fun cancelTimeoutTimer() {
mHandler.removeCallbacks(timeoutRunnable)
}
private fun showErrorPage() {
findViewById<android.view.View>(R.id.loading_layout)?.visibility = android.view.View.GONE
findViewById<android.view.View>(R.id.error_layout)?.visibility = android.view.View.VISIBLE
}
private fun hideErrorPage() {
findViewById<android.view.View>(R.id.error_layout)?.visibility = android.view.View.GONE
findViewById<android.view.View>(R.id.loading_layout)?.visibility = android.view.View.VISIBLE
}
}