admin
2021-05-11 e5ca87e89fef96c827ec37d1d91082f626cbb17d
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.tejia.lijin.app.util.x5;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Message;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
 
import com.tencent.smtt.export.external.interfaces.IX5WebChromeClient.CustomViewCallback;
import com.tencent.smtt.export.external.interfaces.JsPromptResult;
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.WebSettings;
import com.tencent.smtt.sdk.WebSettings.LayoutAlgorithm;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;
 
import java.util.HashMap;
import java.util.Map;
 
public class X5WebView extends WebView {
    public static final int FILE_CHOOSER = 0;
    private String resourceUrl = "";
    private WebView smallWebView;
    private static boolean isSmallWebViewDisplayed = false;
    private boolean isClampedY = false;
    private Map<String, Object> mJsBridges;
    private TextView tog;
    RelativeLayout.LayoutParams layoutParams;
    private RelativeLayout refreshRela;
    TextView title;
    private WebViewClient client = new WebViewClient() {
        /**
         * 防止加载网页时调起系统浏览器
         */
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
 
        public void onReceivedHttpAuthRequest(
                WebView webview,
                com.tencent.smtt.export.external.interfaces.HttpAuthHandler httpAuthHandlerhost,
                String host, String realm) {
            boolean flag = httpAuthHandlerhost.useHttpAuthUsernamePassword();
        }
    };
    private WebChromeClient chromeClient = new WebChromeClient() {
 
        @Override
        public boolean onJsConfirm(WebView arg0, String arg1, String arg2,
                                   JsResult arg3) {
            return super.onJsConfirm(arg0, arg1, arg2, arg3);
        }
 
        View myVideoView;
        View myNormalView;
        CustomViewCallback callback;
 
        // /////////////////////////////////////////////////////////
        //
 
        /**
         * 全屏播放配置
         */
        @Override
        public void onShowCustomView(View view,
                                     CustomViewCallback customViewCallback) {
        }
 
        @Override
        public void onHideCustomView() {
            if (callback != null) {
                callback.onCustomViewHidden();
                callback = null;
            }
            if (myVideoView != null) {
                ViewGroup viewGroup = (ViewGroup) myVideoView.getParent();
                viewGroup.removeView(myVideoView);
                viewGroup.addView(myNormalView);
            }
        }
 
        @Override
        public void openFileChooser(ValueCallback<Uri> uploadFile,
                                    String acceptType, String captureType) {
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");
            ((Activity) (X5WebView.this.getContext())).startActivityForResult(
                    Intent.createChooser(i, "choose files"),
                    X5WebView.FILE_CHOOSER);
            super.openFileChooser(uploadFile, acceptType, captureType);
        }
 
        /**
         * webview 的窗口转移
         */
        @Override
        public boolean onCreateWindow(WebView arg0, boolean arg1, boolean arg2,
                                      Message msg) {
            if (X5WebView.isSmallWebViewDisplayed == true) {
 
                WebView.WebViewTransport webViewTransport = (WebView.WebViewTransport) msg.obj;
                WebView webView = new WebView(X5WebView.this.getContext()) {
 
                    protected void onDraw(Canvas canvas) {
                        super.onDraw(canvas);
                        Paint paint = new Paint();
                        paint.setColor(Color.GREEN);
                        paint.setTextSize(15);
                        canvas.drawText("新建窗口", 10, 10, paint);
                    }
 
                };
                webView.setWebViewClient(new WebViewClient() {
                    public boolean shouldOverrideUrlLoading(WebView arg0,
                                                            String arg1) {
                        arg0.loadUrl(arg1);
                        return true;
                    }
 
                });
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(400, 600);
                lp.gravity = Gravity.CENTER_HORIZONTAL
                        | Gravity.CENTER_VERTICAL;
                X5WebView.this.addView(webView, lp);
                webViewTransport.setWebView(webView);
                msg.sendToTarget();
            }
            return true;
        }
 
        @Override
        public boolean onJsAlert(WebView arg0, String arg1, String arg2,
                                 JsResult arg3) {
            /**
             * 这里写入你自定义的window alert
             */
            // AlertDialog.Builder builder = new Builder(getContext());
            // builder.setTitle("X5内核");
            // builder.setPositiveButton("确定", new
            // DialogInterface.OnClickListener() {
            //
            // @Override
            // public void onClick(DialogInterface dialog, int which) {
            // dialog.dismiss();
            // }
            // });
            // builder.show();
            // arg3.confirm();
            // return true;
            return super.onJsAlert(null, "www.baidu.com", "aa", arg3);
        }
 
        /**
         * 对应js 的通知弹框 ,可以用来实现js 和 android之间的通信
         */
        @Override
        public boolean onJsPrompt(WebView arg0, String arg1, String arg2,
                                  String arg3, JsPromptResult arg4) {
            // 在这里可以判定js传过来的数据,用于调起android native 方法
            if (X5WebView.this.isMsgPrompt(arg1)) {
                return X5WebView.this.onJsPrompt(arg2, arg3);
            }
            return super.onJsPrompt(arg0, arg1, arg2, arg3, arg4);
        }
 
        @Override
        public void onReceivedTitle(WebView arg0, final String arg1) {
            super.onReceivedTitle(arg0, arg1);
 
        }
    };
 
    @SuppressLint("SetJavaScriptEnabled")
    public X5WebView(Context arg0, AttributeSet arg1) {
        super(arg0, arg1);
        this.setWebViewClientExtension(new X5WebViewEventHandler(this));// 配置X5webview的事件处理
        this.setWebViewClient(client);
        this.setWebChromeClient(chromeClient);
        initWebViewSettings();
        this.getView().setClickable(true);
        this.getView().setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return false;
            }
        });
    }
 
    private void initWebViewSettings() {
        WebSettings webSetting = this.getSettings();
        webSetting.setJavaScriptEnabled(true);
        webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
        webSetting.setAllowFileAccess(true);
        webSetting.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
        webSetting.setSupportZoom(true);
        webSetting.setBuiltInZoomControls(true);
        webSetting.setUseWideViewPort(true);
        webSetting.setSupportMultipleWindows(true);
        webSetting.setLoadWithOverviewMode(true);
        webSetting.setAppCacheEnabled(true);
        webSetting.setDatabaseEnabled(true);
        webSetting.setDomStorageEnabled(true);
//        webSetting.setGeolocationEnabled(true);//位置权限
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
        webSetting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
 
        // this.getSettingsExtension().setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);//extension
        // settings 的设计
    }
 
    @Override
    protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
        boolean ret = super.drawChild(canvas, child, drawingTime);
        // canvas.save();
        // Paint paint = new Paint();
        // paint.setColor(0x7fff0000);
        // paint.setTextSize(24.f);
        // paint.setAntiAlias(true);
        // if (getX5WebViewExtension() != null) {
        // canvas.drawText(this.getContext().getPackageName() + "-pid:"
        // + android.os.Process.myPid(), 10, 50, paint);
        // canvas.drawText(
        // "X5  Core:" + QbSdk.getTbsVersion(this.getContext()), 10,
        // 100, paint);
        // } else {
        // canvas.drawText(this.getContext().getPackageName() + "-pid:"
        // + android.os.Process.myPid(), 10, 50, paint);
        // canvas.drawText("Sys Core", 10, 100, paint);
        // }
        // canvas.drawText(Build.MANUFACTURER, 10, 150, paint);
        // canvas.drawText(Build.MODEL, 10, 200, paint);
        // canvas.restore();
        return ret;
    }
 
    public X5WebView(Context arg0) {
        super(arg0);
        setBackgroundColor(85621);
    }
 
    public static void setSmallWebViewEnabled(boolean enabled) {
        isSmallWebViewDisplayed = enabled;
    }
 
    public void addJavascriptBridge(SecurityJsBridgeBundle jsBridgeBundle) {
        if (this.mJsBridges == null) {
            this.mJsBridges = new HashMap<String, Object>(5);
        }
 
        if (jsBridgeBundle != null) {
            String tag = SecurityJsBridgeBundle.BLOCK
                    + jsBridgeBundle.getJsBlockName() + "-"
                    + SecurityJsBridgeBundle.METHOD
                    + jsBridgeBundle.getMethodName();
            this.mJsBridges.put(tag, jsBridgeBundle);
        }
    }
 
    /**
     * 当webchromeClient收到 web的prompt请求后进行拦截判断,用于调起本地android方法
     *
     * @param methodName 方法名称
     * @param blockName  区块名称
     * @return true :调用成功 ; false :调用失败
     */
    private boolean onJsPrompt(String methodName, String blockName) {
        String tag = SecurityJsBridgeBundle.BLOCK + blockName + "-"
                + SecurityJsBridgeBundle.METHOD + methodName;
 
        if (this.mJsBridges != null && this.mJsBridges.containsKey(tag)) {
            ((SecurityJsBridgeBundle) this.mJsBridges.get(tag)).onCallMethod();
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 判定当前的prompt消息是否为用于调用native方法的消息
     *
     * @param msg 消息名称
     * @return true 属于prompt消息方法的调用
     */
    private boolean isMsgPrompt(String msg) {
        return msg != null
                && msg.startsWith(SecurityJsBridgeBundle.PROMPT_START_OFFSET);
    }
 
    // TBS: Do not use @Override to avoid false calls
    public boolean tbs_dispatchTouchEvent(MotionEvent ev, View view) {
        boolean r = super.super_dispatchTouchEvent(ev);
        android.util.Log.d("Bran", "dispatchTouchEvent " + ev.getAction() + " "
                + r);
        return r;
    }
 
    // TBS: Do not use @Override to avoid false calls
    public boolean tbs_onInterceptTouchEvent(MotionEvent ev, View view) {
        boolean r = super.super_onInterceptTouchEvent(ev);
        return r;
    }
 
    protected void tbs_onScrollChanged(int l, int t, int oldl, int oldt,
                                       View view) {
        super_onScrollChanged(l, t, oldl, oldt);
    }
 
    protected void tbs_onOverScrolled(int scrollX, int scrollY,
                                      boolean clampedX, boolean clampedY, View view) {
        super_onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    }
 
    protected void tbs_computeScroll(View view) {
        super_computeScroll();
    }
 
    protected boolean tbs_overScrollBy(int deltaX, int deltaY, int scrollX,
                                       int scrollY, int scrollRangeX, int scrollRangeY,
                                       int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent,
                                       View view) {
        if (getContext() instanceof RefreshActivity) {
            if (this.isClampedY) {
                if ((refreshRela.getTop() + (-deltaY)) / 2 < 255) {
                    this.tog.setAlpha((refreshRela.getTop() + (-deltaY)) / 2);
                } else
                    this.tog.setAlpha(255);
                this.refreshRela.layout(refreshRela.getLeft(),
                        refreshRela.getTop() + (-deltaY),
                        refreshRela.getRight(), refreshRela.getBottom()
                                + (-deltaY));
                this.layout(this.getLeft(), this.getTop() + (-deltaY) / 2,
                        this.getRight(), this.getBottom() + (-deltaY) / 2);
            }
        }
        return super_overScrollBy(deltaX, deltaY, scrollX, scrollY,
                scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY,
                isTouchEvent);
    }
 
    public void setTitle(TextView title) {
        this.title = title;
    }
 
    protected boolean tbs_onTouchEvent(MotionEvent event, View view) {
        if (getContext() instanceof RefreshActivity) {
            if (event.getAction() == MotionEvent.ACTION_UP && this.tog != null) {
                this.isClampedY = false;
                this.tog.setAlpha(0);
                this.refreshRela.layout(refreshRela.getLeft(), 0,
                        refreshRela.getRight(), refreshRela.getBottom());
                this.layout(this.getLeft(), 0, this.getRight(),
                        this.getBottom());
            }
 
        }
        return super_onTouchEvent(event);
    }
}