Search in sources :

Example 11 with WebResourceError

use of android.webkit.WebResourceError in project incubator-weex by apache.

the class WXWebView method initWebView.

private void initWebView(WebView wv) {
    WebSettings settings = wv.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setAppCacheEnabled(true);
    settings.setUseWideViewPort(true);
    settings.setDomStorageEnabled(true);
    settings.setSupportZoom(false);
    settings.setBuiltInZoomControls(false);
    wv.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            WXLogUtils.v("tag", "onPageOverride " + url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            WXLogUtils.v("tag", "onPageStarted " + url);
            if (mOnPageListener != null) {
                mOnPageListener.onPageStart(url);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            WXLogUtils.v("tag", "onPageFinished " + url);
            if (mOnPageListener != null) {
                mOnPageListener.onPageFinish(url, view.canGoBack(), view.canGoForward());
            }
            if (mOnMessageListener != null) {
                evaluateJS("javascript:(window.postMessage = function(message, targetOrigin) {" + "if (message == null || !targetOrigin) return;" + (DOWNGRADE_JS_INTERFACE ? "prompt('" + BRIDGE_NAME + "://postMessage?message=' + JSON.stringify(message) + '&targetOrigin=' + targetOrigin)" : BRIDGE_NAME + ".postMessage(JSON.stringify(message), targetOrigin);") + "})");
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            if (mOnErrorListener != null) {
                // mOnErrorListener.onError("error", "page error code:" + error.getErrorCode() + ", desc:" + error.getDescription() + ", url:" + request.getUrl());
                mOnErrorListener.onError("error", "page error");
            }
        }

        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);
            if (mOnErrorListener != null) {
                mOnErrorListener.onError("error", "http error");
            }
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
            if (mOnErrorListener != null) {
                mOnErrorListener.onError("error", "ssl error");
            }
        }
    });
    wv.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            showWebView(newProgress == 100);
            showProgressBar(newProgress != 100);
            WXLogUtils.v("tag", "onPageProgressChanged " + newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if (mOnPageListener != null) {
                mOnPageListener.onReceivedTitle(view.getTitle());
            }
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String text, String defaultValue, JsPromptResult result) {
            Uri uri = Uri.parse(text);
            String scheme = uri.getScheme();
            if (TextUtils.equals(scheme, BRIDGE_NAME)) {
                if (TextUtils.equals(uri.getAuthority(), "postMessage")) {
                    String message = uri.getQueryParameter("message");
                    String targetOrigin = uri.getQueryParameter("targetOrigin");
                    onMessage(message, targetOrigin);
                    result.confirm("success");
                } else {
                    result.confirm("fail");
                }
                return true;
            }
            return super.onJsPrompt(view, url, text, defaultValue, result);
        }
    });
    if (!DOWNGRADE_JS_INTERFACE) {
        wv.addJavascriptInterface(new Object() {

            @JavascriptInterface
            public void postMessage(String message, String targetOrigin) {
                onMessage(message, targetOrigin);
            }
        }, BRIDGE_NAME);
    }
}
Also used : SslErrorHandler(android.webkit.SslErrorHandler) WebResourceRequest(android.webkit.WebResourceRequest) SslError(android.net.http.SslError) JavascriptInterface(android.webkit.JavascriptInterface) Uri(android.net.Uri) Bitmap(android.graphics.Bitmap) WebResourceResponse(android.webkit.WebResourceResponse) WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebResourceError(android.webkit.WebResourceError) JSONObject(com.alibaba.fastjson.JSONObject) WebView(android.webkit.WebView) JsPromptResult(android.webkit.JsPromptResult) WebViewClient(android.webkit.WebViewClient)

Example 12 with WebResourceError

use of android.webkit.WebResourceError in project incubator-weex by apache.

the class WXWebView method initWebView.

@SuppressLint({ "SetJavaScriptEnabled", "AddJavascriptInterface" })
private void initWebView(WebView wv) {
    WebSettings settings = wv.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setAppCacheEnabled(true);
    settings.setUseWideViewPort(true);
    settings.setDomStorageEnabled(true);
    settings.setSupportZoom(false);
    settings.setBuiltInZoomControls(false);
    settings.setAllowFileAccess(false);
    wv.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            WXLogUtils.v("tag", "onPageOverride " + url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            WXLogUtils.v("tag", "onPageStarted " + url);
            if (mOnPageListener != null) {
                mOnPageListener.onPageStart(url);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            WXLogUtils.v("tag", "onPageFinished " + url);
            if (mOnPageListener != null) {
                mOnPageListener.onPageFinish(url, view.canGoBack(), view.canGoForward());
            }
            if (mOnMessageListener != null) {
                evaluateJS("javascript:(window.postMessage = function(message, targetOrigin) {" + "if (message == null || !targetOrigin) return;" + (DOWNGRADE_JS_INTERFACE ? "prompt('" + BRIDGE_NAME + "://postMessage?message=' + JSON.stringify(message) + '&targetOrigin=' + targetOrigin)" : BRIDGE_NAME + ".postMessage(JSON.stringify(message), targetOrigin);") + "})");
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            if (mOnErrorListener != null) {
                // mOnErrorListener.onError("error", "page error code:" + error.getErrorCode() + ", desc:" + error.getDescription() + ", url:" + request.getUrl());
                mOnErrorListener.onError("error", "page error");
            }
        }

        @Override
        public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
            super.onReceivedHttpError(view, request, errorResponse);
            if (mOnErrorListener != null) {
                mOnErrorListener.onError("error", "http error");
            }
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
            if (mOnErrorListener != null) {
                mOnErrorListener.onError("error", "ssl error");
            }
        }
    });
    wv.setWebChromeClient(new WebChromeClient() {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            showWebView(newProgress == 100);
            showProgressBar(newProgress != 100);
            WXLogUtils.v("tag", "onPageProgressChanged " + newProgress);
        }

        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            if (mOnPageListener != null) {
                mOnPageListener.onReceivedTitle(view.getTitle());
            }
        }

        @Override
        public boolean onJsPrompt(WebView view, String url, String text, String defaultValue, JsPromptResult result) {
            Uri uri = Uri.parse(text);
            String scheme = uri.getScheme();
            if (TextUtils.equals(scheme, BRIDGE_NAME)) {
                if (TextUtils.equals(uri.getAuthority(), "postMessage")) {
                    String message = uri.getQueryParameter("message");
                    String targetOrigin = uri.getQueryParameter("targetOrigin");
                    onMessage(message, targetOrigin);
                    result.confirm("success");
                } else {
                    result.confirm("fail");
                }
                return true;
            }
            return super.onJsPrompt(view, url, text, defaultValue, result);
        }
    });
    if (!DOWNGRADE_JS_INTERFACE) {
        wv.addJavascriptInterface(new Object() {

            @JavascriptInterface
            public void postMessage(String message, String targetOrigin) {
                onMessage(message, targetOrigin);
            }
        }, BRIDGE_NAME);
    }
}
Also used : SslErrorHandler(android.webkit.SslErrorHandler) WebResourceRequest(android.webkit.WebResourceRequest) SslError(android.net.http.SslError) JavascriptInterface(android.webkit.JavascriptInterface) Uri(android.net.Uri) SuppressLint(android.annotation.SuppressLint) Bitmap(android.graphics.Bitmap) WebResourceResponse(android.webkit.WebResourceResponse) WebSettings(android.webkit.WebSettings) WebChromeClient(android.webkit.WebChromeClient) WebResourceError(android.webkit.WebResourceError) JSONObject(com.alibaba.fastjson.JSONObject) WebView(android.webkit.WebView) JsPromptResult(android.webkit.JsPromptResult) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Example 13 with WebResourceError

use of android.webkit.WebResourceError in project AppCenter-SDK-Android by Microsoft.

the class MSALoginActivity method signIn.

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

        @Override
        public void onPageFinished(WebView view, String url) {
            checkSignInCompletion(url);
        }

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

        @Override
        @TargetApi(Build.VERSION_CODES.M)
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            failSignIn(error.getErrorCode(), error.getDescription());
        }
    });
    boolean compactTicket = mAuthType == AuthenticationProvider.Type.MSA_COMPACT;
    String responseType = compactTicket ? "token" : "code";
    String[] scopes = compactTicket ? SCOPES_COMPACT : SCOPES_DELEGATE;
    mWebView.loadUrl(AUTHORIZE_URL + REDIRECT_URI_PARAM + CLIENT_ID_PARAM + "&response_type=" + responseType + "&scope=" + TextUtils.join("+", scopes));
}
Also used : WebResourceRequest(android.webkit.WebResourceRequest) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) TargetApi(android.annotation.TargetApi) SuppressLint(android.annotation.SuppressLint) WebViewClient(android.webkit.WebViewClient)

Example 14 with WebResourceError

use of android.webkit.WebResourceError in project AppCenter-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 15 with WebResourceError

use of android.webkit.WebResourceError in project android-sdk by testpress.

the class WebViewUtils method initWebViewAndPostUrl.

@SuppressLint({ "AddJavascriptInterface", "SetJavaScriptEnabled" })
public void initWebViewAndPostUrl(String url, String json, final Activity activity) {
    initWebView(webView);
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            WebViewUtils.this.onPageStarted();
            hasError = false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (!hasError) {
                onLoadFinished();
            }
        }

        @SuppressWarnings("deprecation")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return WebViewUtils.this.shouldOverrideUrlLoading(activity, url);
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            if (InternetConnectivityChecker.isConnected(activity)) {
                return;
            }
            onNetworkError();
            hasError = true;
        }
    });
    postUrl(url, json);
}
Also used : Bitmap(android.graphics.Bitmap) WebResourceRequest(android.webkit.WebResourceRequest) WebResourceError(android.webkit.WebResourceError) WebView(android.webkit.WebView) WebViewClient(android.webkit.WebViewClient) SuppressLint(android.annotation.SuppressLint)

Aggregations

WebResourceError (android.webkit.WebResourceError)27 WebResourceRequest (android.webkit.WebResourceRequest)27 WebView (android.webkit.WebView)27 WebViewClient (android.webkit.WebViewClient)26 Bitmap (android.graphics.Bitmap)18 SuppressLint (android.annotation.SuppressLint)11 WebSettings (android.webkit.WebSettings)10 WebChromeClient (android.webkit.WebChromeClient)9 SslError (android.net.http.SslError)8 SslErrorHandler (android.webkit.SslErrorHandler)8 WebResourceResponse (android.webkit.WebResourceResponse)7 View (android.view.View)6 Intent (android.content.Intent)5 Uri (android.net.Uri)5 TargetApi (android.annotation.TargetApi)4 TextView (android.widget.TextView)3 Nullable (android.support.annotation.Nullable)2 MotionEvent (android.view.MotionEvent)2 JavascriptInterface (android.webkit.JavascriptInterface)2 JsPromptResult (android.webkit.JsPromptResult)2