BrowserPresentation.java
5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.studymachine.www.ui.presentation;
import static com.hjq.http.EasyUtils.post;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Message;
import android.view.Display;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import com.studymachine.www.R;
import com.studymachine.www.action.StatusAction;
import com.studymachine.www.aop.CheckNet;
import com.studymachine.www.app.AppPresentation;
import com.studymachine.www.app.ModeHandler;
import com.studymachine.www.http.glide.GlideApp;
import com.studymachine.www.js.MainJavaScriptInterface;
import com.studymachine.www.ui.activity.BrowserActivity;
import com.studymachine.www.widget.StatusLayout;
import com.studymachine.www.widget.X5WebView;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
import java.util.TimerTask;
import timber.log.Timber;
public class BrowserPresentation extends AppPresentation implements StatusAction {
private String url;
private StatusLayout mStatusLayout;
private ProgressBar mProgressBar;
private X5WebView mX5WebView;
private ImageView mImgView;
public BrowserPresentation(Context outerContext, Display display) {
super(outerContext, display);
}
public BrowserPresentation(Context outerContext, Display display, String url) {
super(outerContext, display);
this.url = url;
}
public X5WebView getmX5WebView() {
return this.mX5WebView;
}
@Override
public int getLayout() {
return R.layout.browser_activity;
}
@Override
public void initData() {
final ModeHandler handler = new ModeHandler(this);
mStatusLayout = findViewById(R.id.hl_browser_hint);
mProgressBar = findViewById(R.id.pb_browser_progress);
mX5WebView = findViewById(R.id.wv_browser_view);
mImgView = findViewById(R.id.loadview);
mX5WebView.addJavascriptInterface(new MainJavaScriptInterface(getOwnerActivity(), mX5WebView, handler), "studyMachine");
GlideApp.with(getOwnerActivity())
.asGif()
.load(R.drawable.common_load)
.into(mImgView);
mX5WebView.setWebViewClient(new AppBrowserViewClient());
mX5WebView.setWebChromeClient(new AppBrowserChromeClient());
mX5WebView.loadUrl(url);
// handler.sendEmptyMessage(0);
// Runnable task = new Runnable() {
// public void run() {
// mX5WebView.evaluateJavascript("javascript:answerExist()", new ValueCallback<String>() {
// @Override
// public void onReceiveValue(String s) {
// try {
// mode = Integer.valueOf(s);
// } catch (Exception e) {
// mode = 0;
// }
//
// }
// });
//
// handler.sendEmptyMessage(0);//设置循环时间,此处是5秒
// handler.postDelayed(this, 100);
// //需要执行的代码
// }
// };
//
// handler.post(task);
}
private class AppBrowserViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(com.tencent.smtt.sdk.WebView webView, String url) {
Timber.tag("网页UrlLoading").d(url);
return super.shouldOverrideUrlLoading(webView, url);
}
/**
* 网页加载错误时回调,这个方法会在 onPageFinished 之前调用
*/
@Override
public void onReceivedError(WebView webView, int i, String s, String s1) {
// 这里为什么要用延迟呢?因为加载出错之后会先调用 onReceivedError 再调用 onPageFinished
post(() -> showError(listener -> reload()));
}
/**
* 开始加载网页
*/
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
/**
* 完成加载网页
*/
@Override
public void onPageFinished(WebView view, String url) {
// showComplete();
mImgView.setVisibility(View.GONE);
}
}
private class AppBrowserChromeClient extends WebChromeClient {
@Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
Timber.tag("网页").d("look");
return false;
}
/**
* 收到网页标题
*/
@Override
public void onReceivedTitle(WebView view, String title) {
if (title == null) {
return;
}
setTitle(title);
}
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
if (icon == null) {
return;
}
setRightIcon(new BitmapDrawable(getResources(), icon));
}
/**
* 收到加载进度变化
*/
@Override
public void onProgressChanged(WebView view, int newProgress) {
mProgressBar.setProgress(newProgress);
}
}
/**
* 重新加载当前页
*/
@CheckNet
private void reload() {
mX5WebView.reload();
}
}