MyWebChromeClient.java
7.17 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.polysoft.nafmii.webview;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import com.polysoft.nafmii.fragment.WebViewFragment;
import com.tencent.smtt.export.external.interfaces.ConsoleMessage;
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient;
import com.tencent.smtt.export.external.interfaces.JsResult;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebView;
import java.util.concurrent.atomic.AtomicBoolean;
public class MyWebChromeClient extends WebChromeClient {
private final WebviewLoadListener listener;
private AtomicBoolean mIsLoadFinish = new AtomicBoolean();
private boolean mIsFinish = false;
private Activity activity;
/**
* 视频全屏参数
*/
protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
private View customView;
private FrameLayout fullscreenContainer;
private IX5WebChromeClient.CustomViewCallback customViewCallback;
private WebView webView;
private int systemVisbility;
public MyWebChromeClient(WebViewFragment webViewFragment) {
this.listener = webViewFragment;
this.activity = webViewFragment.getActivity();
this.webView = webViewFragment.getWebview().getWebview();
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
@Override
public void onShowCustomView(View view, IX5WebChromeClient.CustomViewCallback customViewCallback) {
if (null != webView.getX5WebViewExtension()) {
super.onShowCustomView(view, customViewCallback);
} else {
if (this.activity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
showCustomView(view, customViewCallback);
}
}
@Override
public void onHideCustomView() {
if (null != webView.getX5WebViewExtension()) {
super.onHideCustomView();
} else {
if (this.activity.getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
hideCustomView();
}
}
/**
* 视频播放全屏
**/
private void showCustomView(View view, IX5WebChromeClient.CustomViewCallback callback) {
// if a view already exists then immediately terminate the new one
if (customView != null) {
callback.onCustomViewHidden();
return;
}
View decorView = this.activity.getWindow().getDecorView();
this.systemVisbility = decorView.getSystemUiVisibility();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_IMMERSIVE);
if (decorView instanceof FrameLayout) {
FrameLayout decor = (FrameLayout) decorView;
fullscreenContainer = new FullscreenHolder(this.activity);
fullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
decor.addView(fullscreenContainer, COVER_SCREEN_PARAMS);
customView = view;
setStatusBarVisibility(false);
customViewCallback = callback;
webView.setVisibility(View.GONE);
}
}
/**
* 隐藏视频全屏
*/
private void hideCustomView() {
if (customView == null) {
return;
}
View decorView = this.activity.getWindow().getDecorView();
decorView.setSystemUiVisibility(systemVisbility);
if (decorView instanceof FrameLayout) {
FrameLayout decor = (FrameLayout) decorView;
decor.removeView(fullscreenContainer);
fullscreenContainer = null;
customView = null;
setStatusBarVisibility(true);
customViewCallback.onCustomViewHidden();
webView.setVisibility(View.VISIBLE);
}
}
/**
* 全屏容器界面
*/
static class FullscreenHolder extends FrameLayout {
public FullscreenHolder(Context ctx) {
super(ctx);
setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
}
@Override
public boolean onTouchEvent(MotionEvent evt) {
return true;
}
}
private void setStatusBarVisibility(boolean visible) {
Window window = this.activity.getWindow();
if (visible) {
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (null != this.listener) {
if (newProgress == 100) {
if (!mIsFinish) {
this.mIsFinish = true;
this.listener.onWebviewLoad(WebviewLoadListener.TYPE_FINISH, null);
return;
}
}
this.listener.onWebviewLoad(WebviewLoadListener.TYPE_PROGRESS, newProgress);
}
super.onProgressChanged(view, newProgress);
}
/**
* 16(Android 4.1.2) <= API <= 20(Android 4.4W.2)回调此方法
*/
public void openFileChooser(final ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
if (null == this.listener) {
return;
}
this.listener.onShowFileChooser((ValueCallback<Uri[]>) value -> {
for (Uri uri : value) {
uploadMsg.onReceiveValue(uri);
}
}, new String[]{acceptType}, TextUtils.isEmpty(capture));
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if (null == this.listener) {
return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
}
String[] acceptTypes = fileChooserParams.getAcceptTypes();
boolean captureEnabled = fileChooserParams.isCaptureEnabled();
this.listener.onShowFileChooser(filePathCallback, acceptTypes, captureEnabled);
return true;
}
@Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
}
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
return super.onConsoleMessage(consoleMessage);
}
}