Example usage for android.webkit WebChromeClient onJsAlert

List of usage examples for android.webkit WebChromeClient onJsAlert

Introduction

In this page you can find the example usage for android.webkit WebChromeClient onJsAlert.

Prototype

public boolean onJsAlert(WebView view, String url, String message, JsResult result) 

Source Link

Document

Tell the client to display a javascript alert dialog.

Usage

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

public void setHookedWebChromeClient(final WebView webView) {
    webElementEventCreator.prepareForStart();
    if (webView != null) {
        webView.post(new Runnable() {
            public void run() {
                webView.getSettings().setJavaScriptEnabled(true);
                final WebChromeClient originalWebChromeClient = getOriginalWebChromeClient(webView);
                if (originalWebChromeClient != null) {
                    webView.setWebChromeClient(new WebChromeClient() {
                        HashMap<String, Boolean> invoke = new HashMap<String, Boolean>();

                        /**
                         * Overrides onJsPrompt in order to create
                         * {@code WebElement} objects based on the web
                         * elements attributes prompted by the injections of
                         * JavaScript// ww w .  j av  a  2s.  co  m
                         */

                        @Override
                        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
                                JsPromptResult r) {

                            if (message != null) {
                                if (message.endsWith("WebElementRecorder-finished")) {
                                    // Log.i("onJsPrompt : " + message);
                                    webElementEventCreator.setFinished(true);
                                } else {
                                    webElementEventCreator.createWebElementEvent(message, view);
                                }
                            }
                            r.confirm();
                            return true;
                        }

                        @Override
                        public Bitmap getDefaultVideoPoster() {
                            Bitmap ret = null;
                            String funcName = new Throwable().getStackTrace()[1].getMethodName();
                            if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                                invoke.put(funcName, true);
                                ret = originalWebChromeClient.getDefaultVideoPoster();
                                invoke.put(funcName, false);
                            }
                            return ret;
                        }

                        @Override
                        public View getVideoLoadingProgressView() {
                            View ret = null;
                            String funcName = new Throwable().getStackTrace()[1].getMethodName();
                            if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                                invoke.put(funcName, true);
                                ret = originalWebChromeClient.getVideoLoadingProgressView();
                                invoke.put(funcName, false);
                            }
                            return ret;
                        }

                        @Override
                        public void getVisitedHistory(ValueCallback<String[]> callback) {
                            String funcName = new Throwable().getStackTrace()[1].getMethodName();
                            if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                                invoke.put(funcName, true);
                                originalWebChromeClient.getVisitedHistory(callback);
                                invoke.put(funcName, false);
                            }
                        }

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

                        @Override
                        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                            boolean ret = false;
                            String funcName = new Throwable().getStackTrace()[1].getMethodName();
                            if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                                invoke.put(funcName, true);
                                ret = originalWebChromeClient.onConsoleMessage(consoleMessage);
                                invoke.put(funcName, false);
                            }
                            return ret;
                        }

                        @Override
                        public void onConsoleMessage(String message, int lineNumber, String sourceID) {
                            String funcName = new Throwable().getStackTrace()[1].getMethodName();
                            if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                                invoke.put(funcName, true);
                                originalWebChromeClient.onConsoleMessage(message, lineNumber, sourceID);
                                invoke.put(funcName, false);
                            }
                        }

                        @Override
                        public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture,
                                Message resultMsg) {
                            boolean ret = false;
                            String funcName = new Throwable().getStackTrace()[1].getMethodName();
                            if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                                invoke.put(funcName, true);
                                ret = originalWebChromeClient.onCreateWindow(view, isDialog, isUserGesture,
                                        resultMsg);
                                invoke.put(funcName, false);
                            }
                            return ret;
                        }

                        @Override
                        public void onExceededDatabaseQuota(String url, String databaseIdentifier, long quota,
                                long estimatedDatabaseSize, long totalQuota, QuotaUpdater quotaUpdater) {
                            String funcName = new Throwable().getStackTrace()[1].getMethodName();
                            if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                                invoke.put(funcName, true);
                                originalWebChromeClient.onExceededDatabaseQuota(url, databaseIdentifier, quota,
                                        estimatedDatabaseSize, totalQuota, quotaUpdater);
                                invoke.put(funcName, false);
                            }
                        }

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

                        @Override
                        public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
                            String funcName = new Throwable().getStackTrace()[1].getMethodName();
                            if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                                invoke.put(funcName, true);
                                originalWebChromeClient.onGeolocationPermissionsShowPrompt(origin, callback);
                                invoke.put(funcName, false);
                            }
                        }

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

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

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

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

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

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

                        @Override
                        public void onReachedMaxAppCacheSize(long requiredStorage, long quota,
                                QuotaUpdater quotaUpdater) {
                            String funcName = new Throwable().getStackTrace()[1].getMethodName();
                            if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                                invoke.put(funcName, true);
                                originalWebChromeClient.onReachedMaxAppCacheSize(requiredStorage, quota,
                                        quotaUpdater);
                                invoke.put(funcName, false);
                            }
                        }

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

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

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

                        }

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

                        @Override
                        public void onShowCustomView(View view, CustomViewCallback callback) {
                            String funcName = new Throwable().getStackTrace()[1].getMethodName();
                            if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                                invoke.put(funcName, true);
                                originalWebChromeClient.onShowCustomView(view, callback);
                                invoke.put(funcName, false);
                            }
                        }

                        public void onShowCustomView(View view, int requestedOrientation,
                                CustomViewCallback callback) {
                            if (Build.VERSION.SDK_INT >= 14) {
                                String funcName = new Throwable().getStackTrace()[1].getMethodName();
                                if (invoke.get(funcName) == null || !invoke.get(funcName)) {
                                    invoke.put(funcName, true);
                                    originalWebChromeClient.onShowCustomView(view, requestedOrientation,
                                            callback);
                                    invoke.put(funcName, false);
                                }
                            }
                        }
                    });
                } else {
                    webView.setWebChromeClient(new WebChromeClient() {

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#getDefaultVideoPoster()
                         */
                        @Override
                        public Bitmap getDefaultVideoPoster() {
                            // TODO Auto-generated method stub
                            return super.getDefaultVideoPoster();
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#getVideoLoadingProgressView()
                         */
                        @Override
                        public View getVideoLoadingProgressView() {
                            // TODO Auto-generated method stub
                            return super.getVideoLoadingProgressView();
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#getVisitedHistory(android.webkit.ValueCallback)
                         */
                        @Override
                        public void getVisitedHistory(ValueCallback<String[]> callback) {
                            // TODO Auto-generated method stub
                            super.getVisitedHistory(callback);
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onCloseWindow(android.webkit.WebView)
                         */
                        @Override
                        public void onCloseWindow(WebView window) {
                            // TODO Auto-generated method stub
                            super.onCloseWindow(window);
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onConsoleMessage(android.webkit.ConsoleMessage)
                         */
                        @Override
                        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                            // TODO Auto-generated method stub
                            return super.onConsoleMessage(consoleMessage);
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onConsoleMessage(java.lang.String, int, java.lang.String)
                         */
                        @Override
                        public void onConsoleMessage(String message, int lineNumber, String sourceID) {
                            // TODO Auto-generated method stub
                            super.onConsoleMessage(message, lineNumber, sourceID);
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onCreateWindow(android.webkit.WebView, boolean, boolean, android.os.Message)
                         */
                        @Override
                        public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture,
                                Message resultMsg) {
                            // TODO Auto-generated method stub
                            return super.onCreateWindow(view, isDialog, isUserGesture, resultMsg);
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onExceededDatabaseQuota(java.lang.String, java.lang.String, long, long, long, android.webkit.WebStorage.QuotaUpdater)
                         */
                        @Override
                        public void onExceededDatabaseQuota(String url, String databaseIdentifier, long quota,
                                long estimatedDatabaseSize, long totalQuota, QuotaUpdater quotaUpdater) {
                            // TODO Auto-generated method stub
                            super.onExceededDatabaseQuota(url, databaseIdentifier, quota, estimatedDatabaseSize,
                                    totalQuota, quotaUpdater);
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onGeolocationPermissionsHidePrompt()
                         */
                        @Override
                        public void onGeolocationPermissionsHidePrompt() {
                            // TODO Auto-generated method stub
                            super.onGeolocationPermissionsHidePrompt();
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onGeolocationPermissionsShowPrompt(java.lang.String, android.webkit.GeolocationPermissions.Callback)
                         */
                        @Override
                        public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) {
                            // TODO Auto-generated method stub
                            super.onGeolocationPermissionsShowPrompt(origin, callback);
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onHideCustomView()
                         */
                        @Override
                        public void onHideCustomView() {
                            // TODO Auto-generated method stub
                            super.onHideCustomView();
                        }

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

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

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

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onJsPrompt(android.webkit.WebView, java.lang.String, java.lang.String, java.lang.String, android.webkit.JsPromptResult)
                         */
                        @Override
                        public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
                                JsPromptResult result) {
                            if (message != null) {
                                if (message.endsWith("WebElementRecorder-finished")) {
                                    // Log.i("onJsPrompt : " + message);
                                    webElementEventCreator.setFinished(true);
                                } else {
                                    webElementEventCreator.createWebElementEvent(message, view);
                                }
                            }
                            result.confirm();
                            return true;
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onJsTimeout()
                         */
                        @Override
                        public boolean onJsTimeout() {
                            // TODO Auto-generated method stub
                            return super.onJsTimeout();
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onProgressChanged(android.webkit.WebView, int)
                         */
                        @Override
                        public void onProgressChanged(WebView view, int newProgress) {
                            // TODO Auto-generated method stub
                            super.onProgressChanged(view, newProgress);
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onReachedMaxAppCacheSize(long, long, android.webkit.WebStorage.QuotaUpdater)
                         */
                        @Override
                        public void onReachedMaxAppCacheSize(long requiredStorage, long quota,
                                QuotaUpdater quotaUpdater) {
                            // TODO Auto-generated method stub
                            super.onReachedMaxAppCacheSize(requiredStorage, quota, quotaUpdater);
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onReceivedIcon(android.webkit.WebView, android.graphics.Bitmap)
                         */
                        @Override
                        public void onReceivedIcon(WebView view, Bitmap icon) {
                            // TODO Auto-generated method stub
                            super.onReceivedIcon(view, icon);
                        }

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

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

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onRequestFocus(android.webkit.WebView)
                         */
                        @Override
                        public void onRequestFocus(WebView view) {
                            // TODO Auto-generated method stub
                            super.onRequestFocus(view);
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onShowCustomView(android.view.View, android.webkit.WebChromeClient.CustomViewCallback)
                         */
                        @Override
                        public void onShowCustomView(View view, CustomViewCallback callback) {
                            // TODO Auto-generated method stub
                            super.onShowCustomView(view, callback);
                        }

                        /* (non-Javadoc)
                         * @see android.webkit.WebChromeClient#onShowCustomView(android.view.View, int, android.webkit.WebChromeClient.CustomViewCallback)
                         */
                        @Override
                        public void onShowCustomView(View view, int requestedOrientation,
                                CustomViewCallback callback) {
                            // TODO Auto-generated method stub
                            super.onShowCustomView(view, requestedOrientation, callback);
                        }

                    });
                }
            }
        });
    }
}