Search in sources :

Example 16 with SslError

use of android.net.http.SslError in project FBReaderJ by geometer.

the class WebAuthorisationScreen method onCreate.

@Override
protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    requestWindowFeature(Window.FEATURE_PROGRESS);
    SQLiteCookieDatabase.init(this);
    CookieSyncManager.createInstance(getApplicationContext());
    CookieManager.getInstance().removeAllCookie();
    final Intent intent = getIntent();
    final Uri data = intent.getData();
    if (data == null || data.getHost() == null) {
        finish();
        return;
    }
    final String completeUrl = intent.getStringExtra(COMPLETE_URL_KEY);
    OrientationUtil.setOrientation(this, intent);
    final WebView view = new WebView(this);
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int progress) {
            setProgress(progress * 100);
        }
    });
    view.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            setTitle(url);
            if (url != null && url.startsWith(completeUrl)) {
                final HashMap<String, String> cookies = new HashMap<String, String>();
                final String cookieString = CookieManager.getInstance().getCookie(url);
                if (cookieString != null) {
                    // cookieString is a string like NAME=VALUE [; NAME=VALUE]
                    for (String pair : cookieString.split(";")) {
                        final String[] parts = pair.split("=", 2);
                        if (parts.length != 2) {
                            continue;
                        }
                        cookies.put(parts[0].trim(), parts[1].trim());
                    }
                }
                storeCookies(data.getHost(), cookies);
                WebAuthorisationScreen.this.setResult(RESULT_OK);
                finish();
            }
        }

        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.ECLAIR_MR1) {
                // hack for auth problem in android 2.1
                handler.proceed();
            } else {
                super.onReceivedSslError(view, handler, error);
            }
        }
    });
    setContentView(view);
    view.loadUrl(intent.getDataString());
}
Also used : SslError(android.net.http.SslError) Intent(android.content.Intent) Uri(android.net.Uri) Bitmap(android.graphics.Bitmap)

Example 17 with SslError

use of android.net.http.SslError 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

SslError (android.net.http.SslError)17 SslErrorHandler (android.webkit.SslErrorHandler)12 WebView (android.webkit.WebView)12 WebViewClient (android.webkit.WebViewClient)11 Bitmap (android.graphics.Bitmap)8 View (android.view.View)7 WebSettings (android.webkit.WebSettings)7 Intent (android.content.Intent)6 WebChromeClient (android.webkit.WebChromeClient)6 WebResourceRequest (android.webkit.WebResourceRequest)6 SslCertificate (android.net.http.SslCertificate)5 WebResourceError (android.webkit.WebResourceError)5 WebResourceResponse (android.webkit.WebResourceResponse)5 Uri (android.net.Uri)4 HttpAuthHandler (android.webkit.HttpAuthHandler)4 SuppressLint (android.annotation.SuppressLint)3 X509Certificate (java.security.cert.X509Certificate)3 Activity (android.app.Activity)2 Build (android.os.Build)2 Handler (android.os.Handler)2