Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
B
Browser White
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wanglei
Browser White
Commits
39a030db
Commit
39a030db
authored
Aug 16, 2024
by
wanglei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
...
parent
97eceac0
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
132 additions
and
21 deletions
+132
-21
WebBrowserActivity.kt
...browserwhite/ui/activity/webbrowser/WebBrowserActivity.kt
+2
-2
WebViewFragment.kt
...java/com/base/browserwhite/ui/fragment/WebViewFragment.kt
+69
-8
activity_web_browser.xml
app/src/main/res/layout/activity_web_browser.xml
+9
-5
fragment_web_view.xml
app/src/main/res/layout/fragment_web_view.xml
+52
-6
wufafangwen.png
app/src/main/res/mipmap-xxhdpi/wufafangwen.png
+0
-0
No files found.
app/src/main/java/com/base/browserwhite/ui/activity/webbrowser/WebBrowserActivity.kt
View file @
39a030db
...
...
@@ -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
...
...
app/src/main/java/com/base/browserwhite/ui/fragment/WebViewFragment.kt
View file @
39a030db
...
...
@@ -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
ur
i
:
String
)
:
BaseFragment
<
FragmentWebViewBinding
>()
{
class
WebViewFragment
(
val
ur
l
:
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
app/src/main/res/layout/activity_web_browser.xml
View file @
39a030db
...
...
@@ -13,10 +13,13 @@
android:layout_width=
"match_parent"
android:layout_height=
"50dp"
android:layout_marginHorizontal=
"15dp"
android:layout_marginVertical=
"1
5
dp"
android:layout_marginVertical=
"1
0
dp"
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"
...
...
app/src/main/res/layout/fragment_web_view.xml
View file @
39a030db
...
...
@@ -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:progress
Drawable=
"@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
app/src/main/res/mipmap-xxhdpi/wufafangwen.png
0 → 100644
View file @
39a030db
10.5 KB
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment