Commit 39a030db authored by wanglei's avatar wanglei

...

parent 97eceac0
......@@ -52,8 +52,8 @@ class WebBrowserActivity : BaseActivity<ActivityWebBrowserBinding>() {
binding.editWeb.setOnEditorActionListener(object : TextView.OnEditorActionListener {
override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
if (actionId == EditorInfo.IME_ACTION_DONE) {
val url = v.toString()
currentFragment?.jumpUrl(url)
val url = v?.text.toString()
currentFragment?.loadWebView(url)
return true; // 返回true表示事件已处理
}
return false
......
......@@ -2,10 +2,14 @@ package com.base.browserwhite.ui.fragment
import android.annotation.SuppressLint
import android.net.Uri
import android.net.http.SslError
import android.view.View
import android.webkit.CookieManager
import android.webkit.JsResult
import android.webkit.SslErrorHandler
import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebStorage
......@@ -13,10 +17,12 @@ import android.webkit.WebView
import android.webkit.WebViewClient
import com.base.browserwhite.databinding.FragmentWebViewBinding
import com.base.browserwhite.utils.LogEx
import kotlinx.coroutines.Job
class WebViewFragment(val uri: String) : BaseFragment<FragmentWebViewBinding>() {
class WebViewFragment(val url: String) : BaseFragment<FragmentWebViewBinding>() {
private val TAG = "WebViewFragment"
private var job: Job? = null
override val binding: FragmentWebViewBinding by lazy {
FragmentWebViewBinding.inflate(layoutInflater)
......@@ -27,13 +33,16 @@ class WebViewFragment(val uri: String) : BaseFragment<FragmentWebViewBinding>()
override fun setView() {
initWebSettings()
binding.webView.loadUrl(uri)
loadWebView(url)
}
override fun setListener() {
super.setListener()
}
fun loadWebView(loadUrl: String) {
binding.webView.visibility = View.VISIBLE
binding.webView.loadUrl(loadUrl)
}
@SuppressLint("SetJavaScriptEnabled")
......@@ -54,8 +63,6 @@ class WebViewFragment(val uri: String) : BaseFragment<FragmentWebViewBinding>()
// 关键性代码,这里要给webView添加这行代码,才可以点击之后正常播放音频。记录一下。
webSettings.mediaPlaybackRequiresUserGesture = false
// Android13后不支持
// webSettings.setAppCacheEnabled(false)
//设置WebView属性,能够执行Javascript脚本
webSettings.javaScriptEnabled = true
......@@ -63,7 +70,11 @@ class WebViewFragment(val uri: String) : BaseFragment<FragmentWebViewBinding>()
//设置WebChromeClient
binding.webView.webChromeClient = object : WebChromeClient() {
override fun onProgressChanged(view: WebView, newProgress: Int) {
// LogEx.logDebug(TAG, )
LogEx.logDebug(TAG, "onProgressChanged newProgress=$newProgress")
binding.progressBar.progress = newProgress + 15
if (binding.progressBar.progress >= 100) {
binding.progressBar.visibility = View.GONE
}
}
override fun onReceivedTitle(view: WebView?, title: String?) {
......@@ -87,14 +98,54 @@ class WebViewFragment(val uri: String) : BaseFragment<FragmentWebViewBinding>()
}
binding.webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
LogEx.logDebug(TAG, "shouldOverrideUrlLoading")
val url = request?.url.toString()
binding.llError.visibility = View.GONE
view?.loadUrl(url)
return true
}
override fun onPageFinished(view: WebView?, url: String?) {
LogEx.logDebug(TAG, "onPageFinished")
super.onPageFinished(view, url)
onPageFinished?.invoke(url)
job?.cancel()
}
override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
LogEx.logDebug(TAG, "onReceivedSslError")
super.onReceivedSslError(view, handler, error)
job?.cancel()
binding.webView.visibility = View.GONE
binding.llError.visibility = View.VISIBLE
if (error != null) {
val domain = error.url
// 获取错误码
val primaryError = error.primaryError
// 根据错误码获取错误描述的中英文对照文本
val errorString = getSslErrorString(primaryError)
binding.tvErrorReason.text = errorString
}
}
override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
LogEx.logDebug(TAG, "onReceivedError")
super.onReceivedError(view, request, error)
job?.cancel()
binding.webView.visibility = View.GONE
binding.llError.visibility = View.VISIBLE
if (error != null) {
// 获取错误码
val errorCode = error.errorCode
// 获取错误描述
val description = error.description
// 创建错误信息文本
val errorText = "Error ${errorCode}: $description"
// 打印错误信息,或者根据需要进行其他处理
binding.tvErrorReason.text = errorText
}
}
}
......@@ -126,8 +177,18 @@ class WebViewFragment(val uri: String) : BaseFragment<FragmentWebViewBinding>()
binding.webView.evaluateJavascript("window.history.back();", null)
}
fun jumpUrl(url: String) {
binding.webView.loadUrl(url)
private fun getSslErrorString(error: Int): String {
return when (error) {
SslError.SSL_DATE_INVALID -> "Date Invalid: The certificate's date is incorrect or the certificate has expired."
SslError.SSL_EXPIRED -> "Expired: The SSL certificate has expired."
SslError.SSL_IDMISMATCH -> "Hostname Mismatch: The hostname in the certificate does not match the requested hostname."
SslError.SSL_INVALID -> "Invalid: The certificate is invalid."
SslError.SSL_NOTYETVALID -> "Not Yet Valid: The certificate is not yet valid."
SslError.SSL_UNTRUSTED -> "Untrusted: The certificate authority is not trusted."
else -> "Unknown Error"
}
}
}
\ No newline at end of file
......@@ -13,10 +13,13 @@
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginHorizontal="15dp"
android:layout_marginVertical="15dp"
android:layout_marginVertical="10dp"
android:background="@drawable/bg_stroke_070709"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@id/container"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
......@@ -63,6 +66,7 @@
android:background="@null"
android:gravity="center_vertical"
android:hint="Search or enter website address"
android:imeOptions="actionDone"
android:paddingHorizontal="15dp"
android:singleLine="true"
android:textColorHint="#858587"
......@@ -84,10 +88,10 @@
tools:ignore="ContentDescription">
<ImageView
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/saoyisao" />
android:src="@mipmap/saoyisao"
android:visibility="gone" />
</FrameLayout>
......@@ -144,19 +148,19 @@
android:id="@+id/tv_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:background="@mipmap/biaoqian"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/iv_more"
app:layout_constraintStart_toEndOf="@id/iv_home"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:visibility="gone"
android:id="@+id/iv_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/gengduo"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tv_label"
......
......@@ -7,17 +7,63 @@
tools:context=".ui.fragment.WebViewFragment">
<ProgressBar
android:id="@+id/progress"
android:id="@+id/progress_bar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="3dp"
android:layout_marginVertical="3dp"
android:max="100"
android:progress="50"
android:progressDrawable="@drawable/progress_bar_web" />
android:progressDrawable="@drawable/progress_bar_web"
tools:progress="50" />
<WebView
android:id="@+id/webView"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/ll_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:visibility="gone">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="@mipmap/wufafangwen"
tools:ignore="ContentDescription" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:text="Unable to access the website"
android:textColor="@color/black"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/tv_error_reason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="18dp"
android:gravity="center"
android:textColor="#545456"
android:textSize="16sp"
tools:text="net::ERR CONNECTION TIMED OUT" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment