Search in sources :

Example 16 with SslErrorHandler

use of android.webkit.SslErrorHandler in project IbookerEditorAndroid by zrunker.

the class IbookerEditorWebView method init.

// 初始化
@SuppressLint({ "AddJavascriptInterface", "SetJavaScriptEnabled" })
private void init() {
    webSettings = this.getSettings();
    // 支持内容重新布局
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    // 允许JS
    webSettings.setJavaScriptEnabled(true);
    // 支持插件
    webSettings.setPluginState(WebSettings.PluginState.ON);
    // 设置允许JS弹窗
    webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
    // access Assets and resources
    webSettings.setAllowFileAccess(true);
    webSettings.setAppCacheEnabled(false);
    // 提高渲染优先级
    webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
    // 设置编码格式
    webSettings.setDefaultTextEncodingName("utf-8");
    // 支持自动加载图片
    webSettings.setLoadsImagesAutomatically(true);
    // 关闭webview中缓存
    webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    // 将图片调整到适合webview的大小
    webSettings.setUseWideViewPort(true);
    // 缩放至屏幕的大小
    webSettings.setLoadWithOverviewMode(true);
    // 支持缩放,默认为true。
    webSettings.setSupportZoom(true);
    // 设置内置的缩放控件。若为false,则该WebView不可缩放
    webSettings.setBuiltInZoomControls(true);
    // 隐藏原生的缩放控件
    webSettings.setDisplayZoomControls(false);
    // 获取当前字体大小
    if (webSettings.getTextSize() == WebSettings.TextSize.SMALLEST) {
        currentFontSize = 1;
    } else if (webSettings.getTextSize() == WebSettings.TextSize.SMALLER) {
        currentFontSize = 2;
    } else if (webSettings.getTextSize() == WebSettings.TextSize.NORMAL) {
        currentFontSize = 3;
    } else if (webSettings.getTextSize() == WebSettings.TextSize.LARGER) {
        currentFontSize = 4;
    } else if (webSettings.getTextSize() == WebSettings.TextSize.LARGEST) {
        currentFontSize = 5;
    }
    // 隐藏滚动条
    setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    // 使页面获取焦点,防止点击无响应
    requestFocus();
    // 设置WebViewClient
    setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (ibookerEditorWebViewUrlLoadingListener != null)
                return ibookerEditorWebViewUrlLoadingListener.shouldOverrideUrlLoading(view, url);
            else {
                // view.loadUrl(url, additionalHttpHeaders);
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                getContext().startActivity(intent);
                return true;
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            if (ibookerEditorWebViewUrlLoadingListener != null)
                return ibookerEditorWebViewUrlLoadingListener.shouldOverrideUrlLoading(view, request);
            else {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    // view.loadUrl(request.getUrl().toString(), additionalHttpHeaders);
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(request.getUrl().toString()));
                    getContext().startActivity(intent);
                }
                return true;
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            if (ibookerEditorWebViewUrlLoadingListener != null)
                ibookerEditorWebViewUrlLoadingListener.onReceivedError(view, request, error);
            else
                // 当网页加载出错时,加载本地错误文件
                IbookerEditorWebView.this.loadUrl("file:///android_asset/error.html", additionalHttpHeaders);
            isLoadError = true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            if (error.getPrimaryError() == SslError.SSL_DATE_INVALID || error.getPrimaryError() == SslError.SSL_EXPIRED || error.getPrimaryError() == SslError.SSL_INVALID || error.getPrimaryError() == SslError.SSL_UNTRUSTED) {
                handler.proceed();
            } else {
                handler.cancel();
            }
            if (ibookerEditorWebViewUrlLoadingListener != null)
                ibookerEditorWebViewUrlLoadingListener.onReceivedSslError(view, handler, error);
            else
                // 当网页加载出错时,加载本地错误文件
                IbookerEditorWebView.this.loadUrl("file:///android_asset/error.html", additionalHttpHeaders);
            isLoadError = true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (ibookerEditorWebViewUrlLoadingListener != null)
                ibookerEditorWebViewUrlLoadingListener.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            isLoadFinished = true;
            if (isExecuteCompile) {
                ibookerCompile(ibookerEditorText);
            } else if (isExecuteHtmlCompile) {
                ibookerHtmlCompile(ibookerEditorHtml);
            } else {
                addWebViewListener();
            }
            if (ibookerEditorWebViewUrlLoadingListener != null)
                ibookerEditorWebViewUrlLoadingListener.onPageFinished(view, url);
        }
    });
    // 添加js
    ibookerEditorJsCheckImgEvent = new IbookerEditorJsCheckImgEvent();
    addJavascriptInterface(ibookerEditorJsCheckImgEvent, "ibookerEditorJsCheckImgEvent");
    // 加载本地HTML
    loadUrl("file:///android_asset/ibooker_editor_index.html", additionalHttpHeaders);
    isLoadError = false;
}
Also used : SslErrorHandler(android.webkit.SslErrorHandler) Bitmap(android.graphics.Bitmap) WebResourceRequest(android.webkit.WebResourceRequest) SslError(android.net.http.SslError) WebResourceError(android.webkit.WebResourceError) Intent(android.content.Intent) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Aggregations

SslErrorHandler (android.webkit.SslErrorHandler)16 SslError (android.net.http.SslError)15 WebView (android.webkit.WebView)14 WebViewClient (android.webkit.WebViewClient)13 Bitmap (android.graphics.Bitmap)9 WebSettings (android.webkit.WebSettings)9 WebChromeClient (android.webkit.WebChromeClient)8 WebResourceRequest (android.webkit.WebResourceRequest)8 View (android.view.View)7 WebResourceError (android.webkit.WebResourceError)7 WebResourceResponse (android.webkit.WebResourceResponse)7 SuppressLint (android.annotation.SuppressLint)5 Intent (android.content.Intent)5 HttpAuthHandler (android.webkit.HttpAuthHandler)4 Uri (android.net.Uri)3 Log (android.util.Log)3 Activity (android.app.Activity)2 SslCertificate (android.net.http.SslCertificate)2 Build (android.os.Build)2 Bundle (android.os.Bundle)2