Example usage for android.webkit WebViewClient onReceivedHttpAuthRequest

List of usage examples for android.webkit WebViewClient onReceivedHttpAuthRequest

Introduction

In this page you can find the example usage for android.webkit WebViewClient onReceivedHttpAuthRequest.

Prototype

public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) 

Source Link

Document

Notifies the host application that the WebView received an HTTP authentication request.

Usage

From source file:com.baidu.cafe.local.record.WebElementRecorder.java

public void setHookedWebViewClient(final WebView webView, final String javaScript) {
    webView.post(new Runnable() {
        // @Override
        public void run() {
            final WebViewClient orginalWebViewClient = getOriginalWebViewClient(webView);
            if (orginalWebViewClient != null) {
                webView.setWebViewClient(new WebViewClient() {

                    HashMap<String, Boolean> invoke = new HashMap<String, Boolean>();

                    @Override/* ww w .j  a va 2 s  .co  m*/
                    public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
                        orginalWebViewClient.doUpdateVisitedHistory(view, url, isReload);
                    }

                    @Override
                    public void onFormResubmission(WebView view, Message dontResend, Message resend) {
                        orginalWebViewClient.onFormResubmission(view, dontResend, resend);
                    }

                    @Override
                    public void onLoadResource(WebView view, String url) {
                        String funcName = new Throwable().getStackTrace()[1].getMethodName();
                        if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                            invoke.put(funcName, true);
                            orginalWebViewClient.onLoadResource(view, url);
                            invoke.put(funcName, false);
                        }
                    }

                    @Override
                    public void onPageFinished(WebView view, String url) {
                        String funcName = new Throwable().getStackTrace()[1].getMethodName();
                        if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                            invoke.put(funcName, true);
                            orginalWebViewClient.onPageFinished(view, url);
                            if (url != null) {
                                hookWebElements(view, javaScript);
                            }
                            invoke.put(funcName, false);
                        }
                    }

                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon) {
                        String funcName = new Throwable().getStackTrace()[1].getMethodName();
                        if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                            invoke.put(funcName, true);
                            orginalWebViewClient.onPageStarted(view, url, favicon);
                            invoke.put(funcName, false);
                        }
                    }

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

                    @Override
                    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host,
                            String realm) {
                        orginalWebViewClient.onReceivedHttpAuthRequest(view, handler, host, realm);
                    }

                    public void onReceivedLoginRequest(WebView view, String realm, String account,
                            String args) {
                        // do support onReceivedLoginRequest since the
                        // version 4.0
                        if (Build.VERSION.SDK_INT >= 14) {
                            orginalWebViewClient.onReceivedLoginRequest(view, realm, account, args);
                        }
                    }

                    @Override
                    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                        orginalWebViewClient.onReceivedSslError(view, handler, error);
                    }

                    @Override
                    public void onScaleChanged(WebView view, float oldScale, float newScale) {
                        orginalWebViewClient.onScaleChanged(view, oldScale, newScale);
                    }

                    @Override
                    public void onTooManyRedirects(WebView view, Message cancelMsg, Message continueMsg) {
                        orginalWebViewClient.onTooManyRedirects(view, cancelMsg, continueMsg);
                    }

                    @Override
                    public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
                        orginalWebViewClient.onUnhandledKeyEvent(view, event);
                    }

                    @Override
                    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                        if (Build.VERSION.SDK_INT >= 14) {
                            return orginalWebViewClient.shouldInterceptRequest(view, url);
                        } else {
                            return null;
                        }
                    }

                    @Override
                    public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
                        return orginalWebViewClient.shouldOverrideKeyEvent(view, event);
                    }

                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        boolean ret = false;
                        String funcName = new Throwable().getStackTrace()[1].getMethodName();
                        if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                            invoke.put(funcName, true);
                            ret = orginalWebViewClient.shouldOverrideUrlLoading(view, url);
                            invoke.put(funcName, false);
                        }
                        return ret;
                    }

                });
            } else {
                // set hook WebViewClient
                webView.setWebViewClient(new WebViewClient() {

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#doUpdateVisitedHistory(android.webkit.WebView, java.lang.String, boolean)
                     */
                    @Override
                    public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
                        // TODO Auto-generated method stub
                        super.doUpdateVisitedHistory(view, url, isReload);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#onFormResubmission(android.webkit.WebView, android.os.Message, android.os.Message)
                     */
                    @Override
                    public void onFormResubmission(WebView view, Message dontResend, Message resend) {
                        // TODO Auto-generated method stub
                        super.onFormResubmission(view, dontResend, resend);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#onLoadResource(android.webkit.WebView, java.lang.String)
                     */
                    @Override
                    public void onLoadResource(WebView view, String url) {
                        // TODO Auto-generated method stub
                        super.onLoadResource(view, url);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#onPageStarted(android.webkit.WebView, java.lang.String, android.graphics.Bitmap)
                     */
                    @Override
                    public void onPageStarted(WebView view, String url, Bitmap favicon) {
                        // TODO Auto-generated method stub
                        super.onPageStarted(view, url, favicon);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#onReceivedError(android.webkit.WebView, int, java.lang.String, java.lang.String)
                     */
                    @Override
                    public void onReceivedError(WebView view, int errorCode, String description,
                            String failingUrl) {
                        // TODO Auto-generated method stub
                        super.onReceivedError(view, errorCode, description, failingUrl);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#onReceivedHttpAuthRequest(android.webkit.WebView, android.webkit.HttpAuthHandler, java.lang.String, java.lang.String)
                     */
                    @Override
                    public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host,
                            String realm) {
                        // TODO Auto-generated method stub
                        super.onReceivedHttpAuthRequest(view, handler, host, realm);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#onReceivedLoginRequest(android.webkit.WebView, java.lang.String, java.lang.String, java.lang.String)
                     */
                    @Override
                    public void onReceivedLoginRequest(WebView view, String realm, String account,
                            String args) {
                        // TODO Auto-generated method stub
                        super.onReceivedLoginRequest(view, realm, account, args);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#onReceivedSslError(android.webkit.WebView, android.webkit.SslErrorHandler, android.net.http.SslError)
                     */
                    @Override
                    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                        // TODO Auto-generated method stub
                        super.onReceivedSslError(view, handler, error);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#onScaleChanged(android.webkit.WebView, float, float)
                     */
                    @Override
                    public void onScaleChanged(WebView view, float oldScale, float newScale) {
                        // TODO Auto-generated method stub
                        super.onScaleChanged(view, oldScale, newScale);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#onTooManyRedirects(android.webkit.WebView, android.os.Message, android.os.Message)
                     */
                    @Override
                    public void onTooManyRedirects(WebView view, Message cancelMsg, Message continueMsg) {
                        // TODO Auto-generated method stub
                        super.onTooManyRedirects(view, cancelMsg, continueMsg);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#onUnhandledKeyEvent(android.webkit.WebView, android.view.KeyEvent)
                     */
                    @Override
                    public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
                        // TODO Auto-generated method stub
                        super.onUnhandledKeyEvent(view, event);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#shouldInterceptRequest(android.webkit.WebView, java.lang.String)
                     */
                    @Override
                    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                        // TODO Auto-generated method stub
                        return super.shouldInterceptRequest(view, url);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#shouldOverrideKeyEvent(android.webkit.WebView, android.view.KeyEvent)
                     */
                    @Override
                    public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
                        // TODO Auto-generated method stub
                        return super.shouldOverrideKeyEvent(view, event);
                    }

                    /* (non-Javadoc)
                     * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
                     */
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        // TODO Auto-generated method stub
                        return super.shouldOverrideUrlLoading(view, url);
                    }

                    @Override
                    public void onPageFinished(WebView view, String url) {
                        super.onPageFinished(webView, url);
                        // print("webView onPageFinished: " + url);
                        if (url != null) {
                            hookWebElements(view, javaScript);
                        }
                    }
                });
            }
        }
    });
}