Example usage for android.webkit WebView post

List of usage examples for android.webkit WebView post

Introduction

In this page you can find the example usage for android.webkit WebView post.

Prototype

public boolean post(Runnable action) 

Source Link

Document

Causes the Runnable to be added to the message queue.

Usage

From source file:Main.java

public static void execIsEnabledCallbackCommand(final WebView webView, final String commandName,
        final String isEnabled) {
    if (webView == null) {
        return;/*from  w w w.ja v  a  2s.c  o  m*/
    }

    webView.post(new Runnable() {
        @Override
        public void run() {
            String command = "javascript:" + commandName + "(" + isEnabled + ");";
            webView.loadUrl(command);
        }
    });
}

From source file:com.pdi.hybridge.HybridgeBroadcaster.java

public void fireJavascriptEvent(final WebView view, final Event event, final JSONObject data) {
    if (mIsInitialized) {
        view.post(new Runnable() {
            @Override//from  w w  w .j a  v  a2s .  c o  m
            public void run() {
                final WebView.HitTestResult hitTestResult = view.getHitTestResult();
                String prejs = "";
                final String json = data != null ? data.toString() : "{}";
                final StringBuffer js = new StringBuffer("HybridgeGlobal.fireEvent(\"");
                js.append(event.getJsName()).append("\",").append(json).append(");");

                if (hitTestResult == null || hitTestResult.getType() != HitTestResult.EDIT_TEXT_TYPE) {
                    if (mJsBuffer.length() != 0) {
                        prejs = mJsBuffer.append(js.toString()).toString();
                        runJsInWebView(view, prejs);
                        mJsBuffer = new StringBuffer("");
                    } else {
                        runJsInWebView(view, js.toString());
                    }
                } else {
                    Log.d(TAG, "Defer javascript message, user is entering text");
                    mJsBuffer.append(js.toString());
                }
            }
        });
    }
}

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

/**
 * @param webView/*from  w w w  . jav  a  2s.  c o m*/
 * @param javaScript
 */
public void hookWebElements(final WebView webView, final String javaScript) {
    webView.post(new Runnable() {
        public void run() {
            if (webView != null) {
                webView.loadUrl("javascript:" + javaScript);
            }
        }
    });
    // viewRecorder.getLocalLib().getCurrentActivity().setProgress(10000);
}

From source file:org.mozilla.focus.webkit.FocusWebViewClient.java

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
    // Only update the user visible URL if:
    // 1. The purported site URL has actually been requested
    // 2. And it's being loaded for the main frame (and not a fake/hidden/iframe request)
    // Note also: shouldInterceptRequest() runs on a background thread, so we can't actually
    // query WebView.getURL().
    // We update the URL when loading has finished too (redirects can happen after a request has been
    // made in which case we don't get shouldInterceptRequest with the final URL), but this
    // allows us to update the URL during loading.
    if (request.isForMainFrame()) {

        // WebView will always add a trailing / to the request URL, but currentPageURL may or may
        // not have a trailing URL (usually no trailing / when a link is entered via UrlInputFragment),
        // hence we do a somewhat convoluted test:
        final String requestURL = request.getUrl().toString();
        if (UrlUtils.urlsMatchExceptForTrailingSlash(currentPageURL, requestURL)) {
            view.post(new Runnable() {
                @Override/*from  w w w.ja  v a  2 s .  c o  m*/
                public void run() {
                    if (callback != null) {
                        callback.onURLChanged(currentPageURL);
                    }
                }
            });
        }
    }

    return super.shouldInterceptRequest(view, request);
}

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

/**
 * set hook listener on WebView body/*  w  ww  . j a  v a 2 s.  c  o m*/
 * 
 * @param webView
 * @return
 */
public void handleWebView(final WebView webView) {
    if (webView == null) {
        return;
    }
    print("start monitor WebView: " + webView);
    webView.post(new Runnable() {

        public void run() {
            print("webView getURL: " + webView.getUrl());
        }

    });
    this.webView = webView;
    // monitor WebView
    hookWebView(webView);
    handleWebElementRecordEventQueue();
}

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/*from  w  w w .  j  av a 2s  .c om*/
                    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);
                        }
                    }
                });
            }
        }
    });
}

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/*from w w  w.  j  av a  2  s .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);
                        }

                    });
                }
            }
        });
    }
}

From source file:im.vector.adapters.ImagesSliderAdapter.java

private void downloadHighResPict(final View view, final int position) {
    final WebView webView = (WebView) view.findViewById(R.id.image_webview);
    final PieFractionView pieFractionView = (PieFractionView) view
            .findViewById(R.id.download_zoomed_image_piechart);
    final MXMediasCache mediasCache = Matrix.getInstance(this.context).getMediasCache();
    final SlidableImageInfo imageInfo = mListImageMessages.get(position);
    final String viewportContent = "width=640";
    final String loadingUri = imageInfo.mImageUrl;
    final String downloadId = mediasCache.loadBitmap(this.context, loadingUri, imageInfo.mRotationAngle,
            imageInfo.mOrientation, imageInfo.mMimeType);

    if (null != downloadId) {
        pieFractionView.setVisibility(View.VISIBLE);

        pieFractionView.setFraction(mediasCache.progressValueForDownloadId(downloadId));

        mediasCache.addDownloadListener(downloadId, new MXMediasCache.DownloadCallback() {
            @Override/*  w w w. ja  v a2  s  .c o  m*/
            public void onDownloadProgress(String aDownloadId, int percentageProgress) {
                if (aDownloadId.equals(downloadId)) {
                    pieFractionView.setFraction(percentageProgress);
                }
            }

            @Override
            public void onDownloadComplete(String aDownloadId) {
                if (aDownloadId.equals(downloadId)) {
                    pieFractionView.setVisibility(View.GONE);

                    final File mediaFile = mediasCache.mediaCacheFile(loadingUri, imageInfo.mMimeType);

                    if (null != mediaFile) {
                        mHighResMediaIndex.add(position);

                        Uri uri = Uri.fromFile(mediaFile);
                        final String newHighResUri = uri.toString();

                        webView.post(new Runnable() {
                            @Override
                            public void run() {
                                Uri mediaUri = Uri.parse(newHighResUri);

                                // save in the gallery
                                //CommonActivityUtils.saveImageIntoGallery(ImagesSliderAdapter.this.context, mediaFile);

                                // refresh the UI
                                loadImage(webView, mediaUri, viewportContent,
                                        computeCss(newHighResUri, ImagesSliderAdapter.this.mMaxImageWidth,
                                                ImagesSliderAdapter.this.mMaxImageHeight,
                                                imageInfo.mRotationAngle));
                            }
                        });
                    }
                }
            }
        });
    }
}

From source file:org.matrix.console.adapters.ImagesSliderAdapter.java

private void downloadHighResPict(final View view, final int position) {
    final WebView webView = (WebView) view.findViewById(R.id.image_webview);
    final PieFractionView pieFractionView = (PieFractionView) view
            .findViewById(R.id.download_zoomed_image_piechart);
    final MXMediasCache mediasCache = Matrix.getInstance(this.context).getMediasCache();
    final SlidableImageInfo imageInfo = mListImageMessages.get(position);
    final String viewportContent = "width=640";
    final String loadingUri = imageInfo.mImageUrl;
    final String downloadId = mediasCache.loadBitmap(this.context, mHsConfig, loadingUri,
            imageInfo.mRotationAngle, imageInfo.mOrientation, imageInfo.mMimeType);

    if (null != downloadId) {
        pieFractionView.setVisibility(View.VISIBLE);

        pieFractionView.setFraction(mediasCache.progressValueForDownloadId(downloadId));

        mediasCache.addDownloadListener(downloadId, new MXMediasCache.DownloadCallback() {
            @Override/*from w ww  .  ja  v  a2s. c  o  m*/
            public void onDownloadStart(String aDownloadId) {
            }

            @Override
            public void onDownloadProgress(String aDownloadId, int percentageProgress) {
                if (aDownloadId.equals(downloadId)) {
                    pieFractionView.setFraction(percentageProgress);
                }
            }

            @Override
            public void onDownloadComplete(String aDownloadId) {
                if (aDownloadId.equals(downloadId)) {
                    pieFractionView.setVisibility(View.GONE);

                    final File mediaFile = mediasCache.mediaCacheFile(loadingUri, imageInfo.mMimeType);

                    if (null != mediaFile) {
                        mHighResMediaIndex.add(position);

                        Uri uri = Uri.fromFile(mediaFile);
                        final String newHighResUri = uri.toString();

                        webView.post(new Runnable() {
                            @Override
                            public void run() {
                                Uri mediaUri = Uri.parse(newHighResUri);

                                // save in the gallery
                                //CommonActivityUtils.saveImageIntoGallery(ImagesSliderAdapter.this.context, mediaFile);

                                // refresh the UI
                                loadImage(webView, mediaUri, viewportContent,
                                        computeCss(newHighResUri, ImagesSliderAdapter.this.mMaxImageWidth,
                                                ImagesSliderAdapter.this.mMaxImageHeight,
                                                imageInfo.mRotationAngle));
                            }
                        });
                    }
                }
            }
        });
    }
}

From source file:im.vector.adapters.VectorMediasViewerAdapter.java

/**
 * Download the high res image//  ww  w  . j a v  a 2  s.com
 * @param view the slider page view
 * @param position the item position
 */
private void downloadHighResPict(final View view, final int position) {
    final WebView webView = (WebView) view.findViewById(R.id.media_slider_image_webview);
    final PieFractionView pieFractionView = (PieFractionView) view.findViewById(R.id.media_slider_piechart);
    final SlidableMediaInfo imageInfo = mMediasMessagesList.get(position);
    final String viewportContent = "width=640";
    final String loadingUri = imageInfo.mMediaUrl;
    final String downloadId = mMediasCache.loadBitmap(mContext, mSession.getHomeserverConfig(), loadingUri,
            imageInfo.mRotationAngle, imageInfo.mOrientation, imageInfo.mMimeType);

    webView.getSettings().setDisplayZoomControls(false);

    if (null != downloadId) {
        pieFractionView.setVisibility(View.VISIBLE);
        pieFractionView.setFraction(mMediasCache.getProgressValueForDownloadId(downloadId));
        mMediasCache.addDownloadListener(downloadId, new MXMediaDownloadListener() {
            @Override
            public void onDownloadError(String downloadId, JsonElement jsonElement) {
                MatrixError error = JsonUtils.toMatrixError(jsonElement);

                if ((null != error) && error.isSupportedErrorCode()) {
                    Toast.makeText(VectorMediasViewerAdapter.this.mContext, error.getLocalizedMessage(),
                            Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onDownloadProgress(String aDownloadId, DownloadStats stats) {
                if (aDownloadId.equals(downloadId)) {
                    pieFractionView.setFraction(stats.mProgress);
                }
            }

            @Override
            public void onDownloadComplete(String aDownloadId) {
                if (aDownloadId.equals(downloadId)) {
                    pieFractionView.setVisibility(View.GONE);
                    final File mediaFile = mMediasCache.mediaCacheFile(loadingUri, imageInfo.mMimeType);

                    if (null != mediaFile) {
                        mHighResMediaIndex.add(position);

                        Uri uri = Uri.fromFile(mediaFile);
                        final String newHighResUri = uri.toString();

                        webView.post(new Runnable() {
                            @Override
                            public void run() {
                                Uri mediaUri = Uri.parse(newHighResUri);
                                // refresh the UI
                                loadImage(webView, mediaUri, viewportContent,
                                        computeCss(newHighResUri, VectorMediasViewerAdapter.this.mMaxImageWidth,
                                                VectorMediasViewerAdapter.this.mMaxImageHeight,
                                                imageInfo.mRotationAngle));
                            }
                        });
                    }
                }
            }
        });
    }
}