|
@@ -12,13 +12,22 @@ import android.os.Build
|
|
|
import android.os.Environment
|
|
import android.os.Environment
|
|
|
import android.os.SystemClock
|
|
import android.os.SystemClock
|
|
|
import android.text.TextUtils
|
|
import android.text.TextUtils
|
|
|
|
|
+import android.util.Log
|
|
|
import android.view.KeyEvent
|
|
import android.view.KeyEvent
|
|
|
import android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
|
|
import android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
|
|
|
import android.widget.Toast
|
|
import android.widget.Toast
|
|
|
import androidx.core.content.FileProvider
|
|
import androidx.core.content.FileProvider
|
|
|
import androidx.lifecycle.Observer
|
|
import androidx.lifecycle.Observer
|
|
|
|
|
+import com.tencent.smtt.export.external.interfaces.ConsoleMessage
|
|
|
|
|
+import com.tencent.smtt.export.external.interfaces.SslError
|
|
|
|
|
+import com.tencent.smtt.export.external.interfaces.SslErrorHandler
|
|
|
|
|
+import com.tencent.smtt.export.external.interfaces.WebResourceError
|
|
|
|
|
+import com.tencent.smtt.export.external.interfaces.WebResourceRequest
|
|
|
|
|
+import com.tencent.smtt.export.external.interfaces.WebResourceResponse
|
|
|
import com.tencent.smtt.sdk.ValueCallback
|
|
import com.tencent.smtt.sdk.ValueCallback
|
|
|
import com.tencent.smtt.sdk.WebChromeClient
|
|
import com.tencent.smtt.sdk.WebChromeClient
|
|
|
|
|
+import com.tencent.smtt.sdk.WebView
|
|
|
|
|
+import com.tencent.smtt.sdk.WebViewClient
|
|
|
import com.xueyu.kotlinextlibrary.string
|
|
import com.xueyu.kotlinextlibrary.string
|
|
|
import com.yc.baselibrary.R
|
|
import com.yc.baselibrary.R
|
|
|
import com.yc.baselibrary.event.LiveBusEvent
|
|
import com.yc.baselibrary.event.LiveBusEvent
|
|
@@ -37,6 +46,7 @@ import java.io.File
|
|
|
class WebViewActivity : BaseActivity<WebViewVm>(), OpenFileChooserCallBack {
|
|
class WebViewActivity : BaseActivity<WebViewVm>(), OpenFileChooserCallBack {
|
|
|
|
|
|
|
|
override fun getLayoutId() = R.layout.base_activity_webview
|
|
override fun getLayoutId() = R.layout.base_activity_webview
|
|
|
|
|
+ private val TAG = "WebViewActivity"
|
|
|
private val REQUEST_CODE_PICK_IMAGE = 1
|
|
private val REQUEST_CODE_PICK_IMAGE = 1
|
|
|
private val REQUEST_CODE_TAKE_PHOTO = 2
|
|
private val REQUEST_CODE_TAKE_PHOTO = 2
|
|
|
private val REQUEST_CODE_PICK_FILE = 3
|
|
private val REQUEST_CODE_PICK_FILE = 3
|
|
@@ -51,6 +61,13 @@ class WebViewActivity : BaseActivity<WebViewVm>(), OpenFileChooserCallBack {
|
|
|
private var imageUri: Uri? = null
|
|
private var imageUri: Uri? = null
|
|
|
private var fromType: Int = 0
|
|
private var fromType: Int = 0
|
|
|
|
|
|
|
|
|
|
+ private fun maskUrl(raw: String?): String {
|
|
|
|
|
+ if (raw.isNullOrBlank()) return raw.orEmpty()
|
|
|
|
|
+ return raw
|
|
|
|
|
+ .replace(Regex("([?&])token=[^&#]*"), "$1token=***")
|
|
|
|
|
+ .replace(Regex("([?&])gtoken=[^&#]*"), "$1gtoken=***")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
override fun initView() {
|
|
override fun initView() {
|
|
|
this.window.decorView.systemUiVisibility = android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
|
|
this.window.decorView.systemUiVisibility = android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
|
|
|
titleBar.setTitle(viewModel.h5Title)
|
|
titleBar.setTitle(viewModel.h5Title)
|
|
@@ -61,6 +78,8 @@ class WebViewActivity : BaseActivity<WebViewVm>(), OpenFileChooserCallBack {
|
|
|
val url = intent?.getStringExtra(WebViewVm.H5_URL)
|
|
val url = intent?.getStringExtra(WebViewVm.H5_URL)
|
|
|
val title = intent?.getStringExtra(WebViewVm.H5_TITLE)
|
|
val title = intent?.getStringExtra(WebViewVm.H5_TITLE)
|
|
|
fromType = intent?.getIntExtra(WebViewVm.FROM_TYPE, 0) ?: 0
|
|
fromType = intent?.getIntExtra(WebViewVm.FROM_TYPE, 0) ?: 0
|
|
|
|
|
+ val isNoToken = intent?.getBooleanExtra(WebViewVm.IS_NO_TOKEN, false) ?: false
|
|
|
|
|
+ Log.i(TAG, "openWebView title=$title fromType=$fromType isNoToken=$isNoToken url=${maskUrl(url)}")
|
|
|
setValue(WebViewVm.H5_URL to url, WebViewVm.H5_TITLE to title)
|
|
setValue(WebViewVm.H5_URL to url, WebViewVm.H5_TITLE to title)
|
|
|
titleBar.setWebCloseOnClickListener {
|
|
titleBar.setWebCloseOnClickListener {
|
|
|
if (webView.canGoBack()) {
|
|
if (webView.canGoBack()) {
|
|
@@ -85,7 +104,79 @@ class WebViewActivity : BaseActivity<WebViewVm>(), OpenFileChooserCallBack {
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
webView.settings.setMixedContentMode(MIXED_CONTENT_ALWAYS_ALLOW);
|
|
webView.settings.setMixedContentMode(MIXED_CONTENT_ALWAYS_ALLOW);
|
|
|
}
|
|
}
|
|
|
- webView.webChromeClient = ReWebChromeClient(this)
|
|
|
|
|
|
|
+ webView.webChromeClient = object : ReWebChromeClient(this) {
|
|
|
|
|
+ override fun onConsoleMessage(consoleMessage: ConsoleMessage?): Boolean {
|
|
|
|
|
+ val msg = consoleMessage?.message().orEmpty()
|
|
|
|
|
+ val source = consoleMessage?.sourceId().orEmpty()
|
|
|
|
|
+ val line = consoleMessage?.lineNumber() ?: -1
|
|
|
|
|
+ Log.d(TAG, "console line=$line source=$source msg=$msg")
|
|
|
|
|
+ return super.onConsoleMessage(consoleMessage)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onReceivedTitle(view: WebView?, title: String?) {
|
|
|
|
|
+ Log.i(TAG, "onReceivedTitle title=$title url=${maskUrl(view?.url)}")
|
|
|
|
|
+ super.onReceivedTitle(view, title)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onProgressChanged(view: WebView?, newProgress: Int) {
|
|
|
|
|
+ if (newProgress == 10 || newProgress == 30 || newProgress == 50 || newProgress == 80 || newProgress == 100) {
|
|
|
|
|
+ Log.d(TAG, "onProgressChanged progress=$newProgress url=${maskUrl(view?.url)}")
|
|
|
|
|
+ }
|
|
|
|
|
+ super.onProgressChanged(view, newProgress)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ webView.webViewClient = object : WebViewClient() {
|
|
|
|
|
+ override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
|
|
|
|
|
+ Log.i(TAG, "shouldOverrideUrlLoading url=${maskUrl(url)}")
|
|
|
|
|
+ return super.shouldOverrideUrlLoading(view, url)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
|
|
|
|
|
+ val reqUrl = request?.url?.toString()
|
|
|
|
|
+ Log.i(TAG, "shouldOverrideUrlLoading(request) url=${maskUrl(reqUrl)} method=${request?.method}")
|
|
|
|
|
+ return super.shouldOverrideUrlLoading(view, request)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onPageStarted(view: WebView?, url: String?, favicon: android.graphics.Bitmap?) {
|
|
|
|
|
+ Log.i(TAG, "onPageStarted url=${maskUrl(url)}")
|
|
|
|
|
+ super.onPageStarted(view, url, favicon)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onPageFinished(view: WebView?, url: String?) {
|
|
|
|
|
+ Log.i(TAG, "onPageFinished url=${maskUrl(url)}")
|
|
|
|
|
+ super.onPageFinished(view, url)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
|
|
|
|
|
+ val reqUrl = request?.url?.toString()
|
|
|
|
|
+ val isMainFrame = request?.isForMainFrame ?: false
|
|
|
|
|
+ Log.e(
|
|
|
|
|
+ TAG,
|
|
|
|
|
+ "onReceivedError(main=$isMainFrame) url=${maskUrl(reqUrl)} code=${error?.errorCode} desc=${error?.description}"
|
|
|
|
|
+ )
|
|
|
|
|
+ super.onReceivedError(view, request, error)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onReceivedError(view: WebView?, errorCode: Int, description: String?, failingUrl: String?) {
|
|
|
|
|
+ Log.e(TAG, "onReceivedError(deprecated) code=$errorCode desc=$description url=${maskUrl(failingUrl)}")
|
|
|
|
|
+ super.onReceivedError(view, errorCode, description, failingUrl)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onReceivedHttpError(view: WebView?, request: WebResourceRequest?, errorResponse: WebResourceResponse?) {
|
|
|
|
|
+ val reqUrl = request?.url?.toString()
|
|
|
|
|
+ val isMainFrame = request?.isForMainFrame ?: false
|
|
|
|
|
+ Log.e(
|
|
|
|
|
+ TAG,
|
|
|
|
|
+ "onReceivedHttpError(main=$isMainFrame) url=${maskUrl(reqUrl)} status=${errorResponse?.statusCode} reason=${errorResponse?.reasonPhrase}"
|
|
|
|
|
+ )
|
|
|
|
|
+ super.onReceivedHttpError(view, request, errorResponse)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
|
|
|
|
|
+ Log.e(TAG, "onReceivedSslError url=${maskUrl(view?.url)} primaryError=${error?.primaryError}")
|
|
|
|
|
+ super.onReceivedSslError(view, handler, error)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -95,7 +186,8 @@ class WebViewActivity : BaseActivity<WebViewVm>(), OpenFileChooserCallBack {
|
|
|
|
|
|
|
|
override fun observe() {
|
|
override fun observe() {
|
|
|
viewModel.isFetch.observe(this, Observer {
|
|
viewModel.isFetch.observe(this, Observer {
|
|
|
- webView.loadUrl(viewModel.h5Url)
|
|
|
|
|
|
|
+ Log.i(TAG, "loadUrl url=${maskUrl(viewModel.h5Url)}")
|
|
|
|
|
+ webView.loadUrl(viewModel.h5Url ?: "")
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -494,4 +586,4 @@ class WebViewActivity : BaseActivity<WebViewVm>(), OpenFileChooserCallBack {
|
|
|
return true
|
|
return true
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
+}
|