Search in sources :

Example 36 with WebResourceRequest

use of android.webkit.WebResourceRequest in project SwipeToLoadLayout by Aspsine.

the class TwitterWebViewFragment method onViewCreated.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    swipeToLoadLayout = (SwipeToLoadLayout) view.findViewById(R.id.swipeToLoadLayout);
    webView = (WebView) view.findViewById(R.id.swipe_target);
    swipeToLoadLayout.setOnRefreshListener(this);
    swipeToLoadLayout.setOnLoadMoreListener(this);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            swipeToLoadLayout.setRefreshing(false);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            swipeToLoadLayout.setRefreshing(false);
        }

        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);
            swipeToLoadLayout.setRefreshing(false);
        }
    });
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) WebResourceResponse(android.webkit.WebResourceResponse) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient)

Example 37 with WebResourceRequest

use of android.webkit.WebResourceRequest in project mobile-center-sdk-android by Microsoft.

the class MSALoginActivity method signOut.

@SuppressWarnings("unused")
public void signOut(View view) {
    mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            if (url.startsWith(REDIRECT_URL)) {
                clearCookies();
                Uri uri = Uri.parse(url);
                sSharedPreferences.edit().remove(MSA_REFRESH_TOKEN_SCOPE_KEY).apply();
                sSharedPreferences.edit().remove(MSA_REFRESH_TOKEN_KEY).apply();
                sSharedPreferences.edit().remove(MSA_AUTH_TYPE_KEY).apply();
                sSharedPreferences.edit().remove(MSA_TOKEN_KEY).apply();
                String error = uri.getQueryParameter("error");
                if (error != null) {
                    failSignOut(0, error);
                } else {
                    signIn(null);
                }
            }
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            clearCookies();
            failSignOut(errorCode, description);
        }

        @Override
        @TargetApi(Build.VERSION_CODES.M)
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            clearCookies();
            failSignOut(error.getErrorCode(), error.getDescription());
        }
    });
    mWebView.loadUrl(SIGN_OUT_URL + REDIRECT_URI_PARAM + CLIENT_ID_PARAM);
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) Uri(android.net.Uri) TargetApi(android.annotation.TargetApi) SuppressLint(android.annotation.SuppressLint) WebViewClient(android.webkit.WebViewClient)

Example 38 with WebResourceRequest

use of android.webkit.WebResourceRequest 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

WebResourceRequest (android.webkit.WebResourceRequest)38 WebView (android.webkit.WebView)37 WebViewClient (android.webkit.WebViewClient)34 Bitmap (android.graphics.Bitmap)22 WebResourceError (android.webkit.WebResourceError)21 WebResourceResponse (android.webkit.WebResourceResponse)16 WebSettings (android.webkit.WebSettings)14 Intent (android.content.Intent)13 View (android.view.View)12 WebChromeClient (android.webkit.WebChromeClient)12 SuppressLint (android.annotation.SuppressLint)11 TextView (android.widget.TextView)9 SslError (android.net.http.SslError)8 SslErrorHandler (android.webkit.SslErrorHandler)8 Uri (android.net.Uri)7 TargetApi (android.annotation.TargetApi)6 IOException (java.io.IOException)5 Nullable (android.support.annotation.Nullable)3 SearchView (android.support.v7.widget.SearchView)3 Toolbar (android.support.v7.widget.Toolbar)3