Example usage for android.webkit ValueCallback onReceiveValue

List of usage examples for android.webkit ValueCallback onReceiveValue

Introduction

In this page you can find the example usage for android.webkit ValueCallback onReceiveValue.

Prototype

public void onReceiveValue(T value);

Source Link

Document

Invoked when the value is available.

Usage

From source file:com.just.agentweb.AgentWebUtils.java

static boolean showFileChooserCompat(Activity activity, WebView webView, ValueCallback<Uri[]> valueCallbacks,
        WebChromeClient.FileChooserParams fileChooserParams, PermissionInterceptor permissionInterceptor,
        ValueCallback valueCallback, String mimeType, Handler.Callback jsChannelCallback) {

    try {//w w  w  .jav a  2  s . co m

        Class<?> clz = Class.forName("com.just.agentweb.filechooser.FileChooser");
        Object mFileChooser$Builder = clz.getDeclaredMethod("newBuilder", Activity.class, WebView.class)
                .invoke(null, activity, webView);
        clz = mFileChooser$Builder.getClass();
        Method mMethod = null;
        if (valueCallbacks != null) {
            mMethod = clz.getDeclaredMethod("setUriValueCallbacks", ValueCallback.class);
            mMethod.setAccessible(true);
            mMethod.invoke(mFileChooser$Builder, valueCallbacks);
        }

        if (fileChooserParams != null) {
            mMethod = clz.getDeclaredMethod("setFileChooserParams", WebChromeClient.FileChooserParams.class);
            mMethod.setAccessible(true);
            mMethod.invoke(mFileChooser$Builder, fileChooserParams);
        }

        if (valueCallback != null) {
            mMethod = clz.getDeclaredMethod("setUriValueCallback", ValueCallback.class);
            mMethod.setAccessible(true);
            mMethod.invoke(mFileChooser$Builder, valueCallback);
        }

        if (!TextUtils.isEmpty(mimeType)) {
            //                LogUtils.i(TAG, Arrays.toString(clz.getDeclaredMethods()));
            mMethod = clz.getDeclaredMethod("setAcceptType", String.class);
            mMethod.setAccessible(true);
            mMethod.invoke(mFileChooser$Builder, mimeType);
        }

        if (jsChannelCallback != null) {
            mMethod = clz.getDeclaredMethod("setJsChannelCallback", Handler.Callback.class);
            mMethod.setAccessible(true);
            mMethod.invoke(mFileChooser$Builder, jsChannelCallback);
        }

        mMethod = clz.getDeclaredMethod("setPermissionInterceptor", PermissionInterceptor.class);
        mMethod.setAccessible(true);
        mMethod.invoke(mFileChooser$Builder, permissionInterceptor);

        mMethod = clz.getDeclaredMethod("build");
        mMethod.setAccessible(true);
        Object mFileChooser = mMethod.invoke(mFileChooser$Builder);

        mMethod = mFileChooser.getClass().getDeclaredMethod("openFileChooser");
        mMethod.setAccessible(true);
        mMethod.invoke(mFileChooser);

    } catch (Throwable throwable) {
        if (LogUtils.isDebug()) {
            throwable.printStackTrace();
        }
        if (valueCallbacks != null) {
            LogUtils.i(TAG, "onReceiveValue empty");
            return false;
        }
        if (valueCallback != null) {
            valueCallback.onReceiveValue(null);
        }
    }
    return true;
}

From source file:org.chromium.android_webview.test.AwSettingsTest.java

@SmallTest
@Feature({ "AndroidWebView", "Preferences" })
public void testAllowMixedMode() throws Throwable {
    final TestAwContentsClient contentClient = new TestAwContentsClient() {
        @Override/*from   www.  j ava2 s. c om*/
        public void onReceivedSslError(ValueCallback<Boolean> callback, SslError error) {
            callback.onReceiveValue(true);
        }
    };
    final AwTestContainerView testContainerView = createAwTestContainerViewOnMainSync(contentClient);
    final AwContents awContents = testContainerView.getAwContents();
    final AwSettings awSettings = getAwSettingsOnUiThread(awContents);

    awSettings.setJavaScriptEnabled(true);

    TestWebServer httpsServer = new TestWebServer(true);
    TestWebServer httpServer = new TestWebServer(false);

    final String JS_URL = "/insecure.js";
    final String IMG_URL = "/insecure.png";
    final String SECURE_URL = "/secure.html";
    httpServer.setResponse(JS_URL, "window.loaded_js = 42;", null);
    httpServer.setResponseBase64(IMG_URL, CommonResources.FAVICON_DATA_BASE64, null);

    final String JS_HTML = "<script src=\"" + httpServer.getResponseUrl(JS_URL) + "\"></script>";
    final String IMG_HTML = "<img src=\"" + httpServer.getResponseUrl(IMG_URL) + "\" />";
    final String SECURE_HTML = "<body>" + IMG_HTML + " " + JS_HTML + "</body>";

    String secureUrl = httpsServer.setResponse(SECURE_URL, SECURE_HTML, null);

    awSettings.setMixedContentMode(AwSettings.MIXED_CONTENT_NEVER_ALLOW);
    loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), secureUrl);
    assertEquals(1, httpsServer.getRequestCount(SECURE_URL));
    assertEquals(0, httpServer.getRequestCount(JS_URL));
    assertEquals(0, httpServer.getRequestCount(IMG_URL));

    awSettings.setMixedContentMode(AwSettings.MIXED_CONTENT_ALWAYS_ALLOW);
    loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), secureUrl);
    assertEquals(2, httpsServer.getRequestCount(SECURE_URL));
    assertEquals(1, httpServer.getRequestCount(JS_URL));
    assertEquals(1, httpServer.getRequestCount(IMG_URL));

    awSettings.setMixedContentMode(AwSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
    loadUrlSync(awContents, contentClient.getOnPageFinishedHelper(), secureUrl);
    assertEquals(3, httpsServer.getRequestCount(SECURE_URL));
    assertEquals(1, httpServer.getRequestCount(JS_URL));
    assertEquals(2, httpServer.getRequestCount(IMG_URL));

    httpServer.shutdown();
    httpsServer.shutdown();
}

From source file:com.codename1.impl.android.AndroidImplementation.java

private void execJSUnsafe(final AndroidBrowserComponent bc, final String javaScript,
        final ValueCallback<String> resultCallback) {
    if (useEvaluateJavascript()) {
        try {//from  w  ww. j a v a2s . co m
            bc.web.evaluateJavascript(javaScript, resultCallback);
        } catch (Throwable t) {
            com.codename1.io.Log.e(t);
            resultCallback.onReceiveValue(null);
        }
    } else {
        jsCallbackIndex = (++jsCallbackIndex) % 1024;
        int index = jsCallbackIndex;

        // The jsCallback is a special java object exposed to javascript that we use
        // to return values from javascript to java.
        synchronized (bc.jsCallback) {
            // Initialize the return value to null
            while (!bc.jsCallback.isIndexAvailable(index)) {
                index++;
            }
            jsCallbackIndex = index + 1;
        }
        final int fIndex = index;
        // We are placing the javascript inside eval() so we need to escape
        // the input.
        String escaped = StringUtil.replaceAll(javaScript, "\\", "\\\\");
        escaped = StringUtil.replaceAll(escaped, "'", "\\'");

        final String js = "javascript:(function(){"

                + "try{" + bc.jsCallback.jsInit() + bc.jsCallback.jsCleanup()
                + AndroidBrowserComponentCallback.JS_RETURNVAL_VARNAME + "[" + index + "]" + "=eval('" + escaped
                + "');} catch (e){console.log(e)};" + AndroidBrowserComponentCallback.JS_VAR_NAME
                + ".addReturnValue(" + index + ", ''+"

                + AndroidBrowserComponentCallback.JS_RETURNVAL_VARNAME + "[" + index + "]" + ");})()";

        // Send the Javascript string via SetURL.
        // NOTE!! This is sent asynchronously so we will need to wait for
        // the result to come in.
        bc.setURL(js, null);
        if (resultCallback == null) {
            return;
        }
        Thread t = new Thread(new Runnable() {
            public void run() {
                int maxTries = 500;
                int tryCounter = 0;

                // If we are not on the EDT, then it is safe to just loop and wait.
                while (!bc.jsCallback.isValueSet(fIndex) && tryCounter++ < maxTries) {
                    synchronized (bc.jsCallback) {
                        Util.wait(bc.jsCallback, 20);
                    }
                }

                if (bc.jsCallback.isValueSet(fIndex)) {
                    String retval = bc.jsCallback.getReturnValue(fIndex);
                    bc.jsCallback.remove(fIndex);
                    resultCallback.onReceiveValue(retval != null ? JSONObject.quote(retval) : null);

                } else {
                    com.codename1.io.Log.e(new RuntimeException(
                            "Failed to execute javascript " + js + " after maximum wait time."));
                    resultCallback.onReceiveValue(null);
                }
            }
        });
        t.start();

    }
}