Skip to content

Commit 616977b

Browse files
author
lemon
committed
enhance demo,show webview performace data
1 parent 0fe919a commit 616977b

File tree

6 files changed

+72
-8
lines changed

6 files changed

+72
-8
lines changed

app/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ android {
4949
zipAlignEnabled false
5050
minifyEnabled false
5151
shrinkResources false
52-
debuggable false
52+
debuggable true
5353
}
5454
}
5555

@@ -67,4 +67,5 @@ dependencies {
6767
implementation project(':easybridge')
6868
implementation project(':easybridge-annotation')
6969
kapt project(':easybridge-processor')
70+
implementation 'com.google.code.gson:gson:2.8.4'
7071
}

app/src/main/assets/demo.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
<body>
4545

46-
<input type="text" value="http://google.com" id="tv_url">
46+
<input type="text" value="https://google.com" id="tv_url">
4747
<input type="button" value="jump" onclick="jumpToPage()">
4848

4949
<a href="javascript:toast('click toast')"><h3>测试Toast</h3></a>

app/src/main/java/tech/easily/easybridge/MainActivity.kt

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
package tech.easily.easybridge
22

3+
import android.annotation.SuppressLint
4+
import android.graphics.Bitmap
35
import android.net.Uri
46
import android.os.Build
57
import android.os.Bundle
68
import android.support.v7.app.AppCompatActivity
9+
import android.view.View
710
import android.webkit.*
811
import android.widget.Toast
12+
import com.google.gson.Gson
913
import tech.easily.easybridge.lib.EBHandlerManager
1014
import tech.easily.easybridge.lib.EasyBridgeWebChromeClient
1115
import kotlinx.android.synthetic.main.activity_main.*
16+
import kotlinx.android.synthetic.main.layout_performance.*
1217
import tech.easily.easybridge.lib.ResultCallBack
18+
import tech.easily.easybridge.model.PerformanceTiming
19+
import java.lang.Exception
1320

1421
class MainActivity : AppCompatActivity() {
1522

23+
private val injectPerformanceHandler = "window.easyBridge.registerHandler('getPerformance', function (parameters, callback) {\n" +
24+
" if (typeof callback == 'function') {\n" +
25+
" callback(window.performance.timing);\n" +
26+
" }\n" +
27+
" });"
28+
1629
override fun onCreate(savedInstanceState: Bundle?) {
1730
super.onCreate(savedInstanceState)
1831
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
@@ -35,9 +48,21 @@ class MainActivity : AppCompatActivity() {
3548
EBHandlerManager.register(webView)
3649
webView.setDebuggable(BuildConfig.DEBUG)
3750
webView.webViewClient = object : WebViewClient() {
51+
52+
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
53+
llPerformance.visibility = View.GONE
54+
}
55+
3856
override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
3957
return false
4058
}
59+
60+
override fun onPageFinished(view: WebView?, url: String?) {
61+
super.onPageFinished(view, url)
62+
webView.postDelayed({
63+
getPerformanceData()
64+
},500)
65+
}
4166
}
4267
// set a global security checker,all the url with scheme https/http is not allowed to using the bridge
4368
webView.setPolicyChecker(({ url, parameters ->
@@ -46,12 +71,40 @@ class MainActivity : AppCompatActivity() {
4671
// control the bridge inject action
4772
EasyBridgeWebChromeClient.SECURITY_CHECK_PARAMETERS -> when (page.scheme) {
4873
"http" -> false
49-
"https" -> false
74+
"https" -> true
5075
else -> true
5176
}
5277
else -> true
5378
}
5479
}))
80+
// inject a performance js interface
81+
webView.setBridgeInjectedListener {
82+
webView.loadUrl("javascript:$injectPerformanceHandler")
83+
}
84+
}
85+
86+
private fun getPerformanceData() {
87+
webView.callHandler("getPerformance", null, object : ResultCallBack() {
88+
override fun onResult(result: Any?) {
89+
try {
90+
val performanceTimingJson = result as String
91+
val performanceTiming = Gson().fromJson<PerformanceTiming>(performanceTimingJson, PerformanceTiming::class.java)
92+
renderPerformance(performanceTiming)
93+
} catch (e: Exception) {
94+
e.printStackTrace()
95+
}
96+
}
97+
})
98+
}
99+
100+
@SuppressLint("SetTextI18n")
101+
private fun renderPerformance(performanceTiming: PerformanceTiming) {
102+
llPerformance.visibility = View.VISIBLE
103+
with(performanceTiming) {
104+
tvNet.text = (responseEnd - fetchStart).toString() + " ms"
105+
tvRender.text = (loadEventEnd - responseEnd).toString() + " ms"
106+
tvTotal.text = (loadEventEnd - fetchStart).toString() + " ms"
107+
}
55108
}
56109

57110
override fun onBackPressed() {

app/src/main/java/tech/easily/easybridge/model/PerformanceTiming.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package tech.easily.webviewperformancedemo
1+
package tech.easily.easybridge.model
22

33
data class PerformanceTiming(val connectEnd: Long, val connectStart: Long, val domComplete: Long, val domContentLoadedEventEnd: Long,
44
val domContentLoadedEventStart: Long, val domInteractive: Long, val domLoading: Long, val domainLookupEnd: Long,

app/src/main/res/layout/activity_main.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@
2222
android:text="callJS"
2323
android:textColor="#fff" />
2424

25+
26+
<include
27+
android:layout_margin="16dp"
28+
android:id="@+id/llPerformance"
29+
layout="@layout/layout_performance"
30+
android:layout_width="wrap_content"
31+
android:layout_height="wrap_content"
32+
android:layout_gravity="bottom|left"
33+
android:visibility="gone" />
34+
2535
</FrameLayout>

app/src/main/res/layout/layout_performance.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="wrap_content"
44
android:layout_height="wrap_content"
5-
android:background="#50000000"
5+
android:background="#60000000"
66
android:orientation="vertical">
77

88
<LinearLayout
@@ -17,7 +17,7 @@
1717
android:text="网络耗时:" />
1818

1919
<TextView
20-
android:id="@+id/tv_net"
20+
android:id="@+id/tvNet"
2121
android:layout_width="wrap_content"
2222
android:layout_height="wrap_content" />
2323
</LinearLayout>
@@ -34,7 +34,7 @@
3434
android:text="渲染耗时:" />
3535

3636
<TextView
37-
android:id="@+id/tv_render"
37+
android:id="@+id/tvRender"
3838
android:layout_width="wrap_content"
3939
android:layout_height="wrap_content" />
4040
</LinearLayout>
@@ -51,7 +51,7 @@
5151
android:text="总耗时长:" />
5252

5353
<TextView
54-
android:id="@+id/tv_total"
54+
android:id="@+id/tvTotal"
5555
android:layout_width="wrap_content"
5656
android:layout_height="wrap_content" />
5757
</LinearLayout>

0 commit comments

Comments
 (0)