package com.sd.shupathwebview import android.Manifest import android.annotation.SuppressLint import android.content.DialogInterface import android.content.Intent import android.net.http.SslError import android.os.Bundle import android.view.KeyEvent import android.webkit.ConsoleMessage import android.webkit.SslErrorHandler import android.webkit.WebChromeClient import android.webkit.WebSettings import android.webkit.WebView import android.webkit.WebViewClient import androidx.activity.viewModels import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import com.amap.api.location.AMapLocationClient import com.google.gson.Gson import com.jeremyliao.liveeventbus.LiveEventBus import com.permissionx.guolindev.PermissionX import com.sd.shupathwebview.databinding.ActivityMainBinding import com.sd.shupathwebview.dsbridge.DWebView import com.sd.shupathwebview.dsbridge.OnReturnValue import com.sd.shupathwebview.network.MyTcpService import com.sd.shupathwebview.utils.CustomLog import com.sd.shupathwebview.utils.MyContants import com.sd.shupathwebview.utils.ToastHelper import com.sd.shupathwebview.viewmodels.AMapLoc import com.sd.shupathwebview.works.TcpConntctVM import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint class MainActivity : AppCompatActivity() { private lateinit var jsApi: JsApi private val gson = Gson() private lateinit var binding: ActivityMainBinding private val tcpConntctVM: TcpConntctVM by viewModels() // private val myLocVM: MyLocVM by viewModels() private val aMapLoc: AMapLoc by viewModels() @SuppressLint("MissingPermission") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // enableEdgeToEdge() binding = ActivityMainBinding.inflate(layoutInflater) AMapLocationClient.updatePrivacyShow(this,true,true) AMapLocationClient.updatePrivacyAgree(this,true) aMapLoc.initLocation() CustomLog.clearLog() setContentView(binding.root) requestPers() initWeb() readMockData() subcribeObu() } fun readMockData() { tcpConntctVM.readMockData() } fun subcribeObu() { //webview 点击PC-5 渲染完成回调 jsApi.setOnStartPageData { CustomLog.d("---pxl", "webview 点击PC-5 完成回调") binding.dwebview.hasJavascriptMethod( MyContants.getOBUData, { retValue -> CustomLog.d("---pxl", "-------getOBUData 函数存在吗 = ${retValue}") }) binding.dwebview.hasJavascriptMethod( MyContants.getDeviceData, { retValue -> CustomLog.d("---pxl", "-------getDeviceData 函数存在吗 = ${retValue}") }) // tcpConntctVM.obuStr = // "CmQSCDAxMjM0NTY3KhIJdwcLwoINPkARYvEdfEEYWkBAAkmYbhKDwKowQFGbOZK1YoMvQGoAcgB6GwkAAAAAAAAAQBEAAAAAAAAQQBkAAAAAAAAAQIIBAggKigEECAAgAJIBAggA" tcpConntctVM.sendMockData(binding.dwebview) // tcpConntctVM.mockList.forEach { // tcpConntctVM.obuStr = it // CustomLog.d("---pxl ", "准备给webview的模拟数据是 = ${tcpConntctVM.obuStr}") // binding.dwebview.callHandler( // MyContants.getOBUData, // arrayOf(tcpConntctVM.obuStr), // object : OnReturnValue { // override fun onValue(retValue: String?) { // CustomLog.d( // "---pxl ", // "getOBUData Android 收到webview的回调 onValue= ${retValue.toString()}" // ) // } // }) // CustomLog.d("---pxl ", "getOBUData 睡200ms") // Thread.sleep(200) // } } aMapLoc.myLocValue.observe(this) { CustomLog.d( "---pxl ", " Android 发给web getDeviceData 的数据是 = ${gson.toJson(it)}" ) binding.dwebview.callHandler( MyContants.getDeviceData, arrayOf(gson.toJson(it)), object : OnReturnValue { override fun onValue(retValue: String?) { CustomLog.d( "---pxl ", "getDeviceData Android 收到webview的回调 onValue= ${retValue.toString()}" ) } }) } LiveEventBus .get(MyContants.obu_data, String::class.java) .observe(this) { serverObu -> tcpConntctVM.obuStr = serverObu tcpConntctVM.obuStr = tcpConntctVM.obuStr.run { var index = lastIndexOf("??") substring(0..index - 1) replace("[\n\r]+".toRegex(), "") trim() return@run this } // tcpConntctVM.obuStr = tcpConntctVM.obuStr.substring(0..index - 1) // tcpConntctVM.obuStr = tcpConntctVM.obuStr.replace("[\n\r]+".toRegex(), "").trim() println("--------MainActivity.subcribeObu obu=" + tcpConntctVM.obuStr + " 当前线程= " + Thread.currentThread()) } } fun startConnectTcpServer() { var ser = Intent(this, MyTcpService::class.java) startForegroundService(ser) } fun initWeb() { jsApi = JsApi() DWebView.setWebContentsDebuggingEnabled(true) binding.dwebview.addJavascriptObject(jsApi, null) binding.dwebview.settings.run { javaScriptEnabled = true javaScriptCanOpenWindowsAutomatically = true loadWithOverviewMode = true pluginState = WebSettings.PluginState.ON domStorageEnabled = true setAllowFileAccess(true) setAllowContentAccess(true) databaseEnabled = true //解决跨域问题 setAllowFileAccessFromFileURLs(true) setAllowUniversalAccessFromFileURLs(true) } // val url = "file:///android_asset/dist/index.html" val url = "http://82.156.182.48:8088/3dmap/" // val url = "https://172.29.128.38:13443/shudao-navigation3d-app/index" binding.dwebview.loadUrl(url) binding.dwebview.webViewClient = object : WebViewClient() { override fun onReceivedSslError( view: WebView?, handler: SslErrorHandler?, error: SslError? ) { // super.onReceivedSslError(view, handler, error) handler?.proceed() } } binding.dwebview.webChromeClient = object : WebChromeClient() { override fun onConsoleMessage(message: ConsoleMessage?): Boolean { CustomLog.d( "---pxl ", "${message?.message()} -- From line " + "${message?.lineNumber()} of ${message?.sourceId()}" ) return true } } } @SuppressLint("MissingPermission") fun requestPers() { var list = listOf( Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE, ) PermissionX.init(this) .permissions(list) .onExplainRequestReason { scope, deniedList -> scope.showRequestReasonDialog( deniedList, "蜀道 需要同意以下授权才能正常使用", "好的", "取消" ) } // .onForwardToSettings { scope, deniedList -> // scope.showForwardToSettingsDialog(deniedList, "您需要手动在‘设置’中允许必要的权限", "OK", "Cancel") // } .request { allGranted, grantedList, deniedList -> if (allGranted) { // myLocVM.getLocation() aMapLoc.startLocation() startConnectTcpServer() } else { ToastHelper.showShort(this, "权限被拒") exitApp() binding.root.postDelayed({ finish() }, 150) } } } override fun onStop() { super.onStop() aMapLoc.stopLocation() } override fun onDestroy() { super.onDestroy() exitApp() } fun exitApp() { aMapLoc.destroyLocation() var ser = Intent(this, MyTcpService::class.java) stopService(ser) } override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean { if (keyCode == KeyEvent.KEYCODE_BACK) { AlertDialog.Builder(this) .setMessage("你确定要退出吗?") .setCancelable(false) .setPositiveButton("Yes", DialogInterface.OnClickListener { dialog, id -> exitApp() finish() }) .setNegativeButton("No", null) .show() return true } return super.onKeyDown(keyCode, event) } }