Example usage for android.webkit JsPromptResult confirm

List of usage examples for android.webkit JsPromptResult confirm

Introduction

In this page you can find the example usage for android.webkit JsPromptResult confirm.

Prototype

public void confirm(String result) 

Source Link

Document

Handle a confirmation response from the user.

Usage

From source file:com.polyvi.xface.view.XWebChromeClient.java

@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
        JsPromptResult result) {
    boolean reqOk = false;
    if (url.startsWith("file://") || Config.isUrlWhiteListed(url)) {
        reqOk = true;//ww w .  ja v a  2s  .com
    }
    if (reqOk && defaultValue != null && defaultValue.equals("xFace_close_application:")) {
        XAppWebView appView = ((XAppWebView) view);
        int viewId = appView.getViewId();
        XEvent evt = XEvent.createEvent(XEventType.CLOSE_APP, viewId);
        ((XFaceMainActivity) mInterface.getActivity()).getEventCenter().sendEventSync(evt);
        result.confirm("");
        return true;
    } else if (reqOk && defaultValue != null && defaultValue.equals("xFace_app_send_message:")) {
        try {
            JSONArray args = new JSONArray(message);
            Pair<XAppWebView, String> appMessage = new Pair<XAppWebView, String>((XAppWebView) view,
                    args.getString(0));
            XEvent evt = XEvent.createEvent(XEventType.XAPP_MESSAGE, appMessage);
            ((XFaceMainActivity) mInterface.getActivity()).getEventCenter().sendEventSync(evt);
        } catch (JSONException e) {
            XLog.e(CLASS_NAME, "");
            XLog.e(CLASS_NAME, e.getMessage());
        }
        result.confirm("");
        return true;
    }
    return super.onJsPrompt(view, url, message, defaultValue, result);
}

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

@SuppressLint("DefaultLocale")
@SuppressWarnings({ "unchecked", "rawtypes" })
private void executeJSONTask(String action, JSONObject json, JsPromptResult result,
        HybridgeBroadcaster hybridge, Activity activity) {
    final Class clazz = mActions.get(action);
    if (clazz != null && hybridge != null) {
        AsyncTask task = null;//  www  . j  av  a 2  s  .  c  o  m
        try {
            task = (AsyncTask<JSONObject, Void, JSONObject>) clazz
                    .getDeclaredConstructor(new Class[] { android.app.Activity.class }).newInstance(activity);
        } catch (final InstantiationException e) {
            e.printStackTrace();
        } catch (final IllegalAccessException e) {
            e.printStackTrace();
        } catch (final IllegalArgumentException e) {
            e.printStackTrace();
        } catch (final InvocationTargetException e) {
            e.printStackTrace();
        } catch (final NoSuchMethodException e) {
            e.printStackTrace();
        }
        Log.v(mTag, "Execute action " + action);
        task.execute(json, result, hybridge);
    } else {
        result.confirm(json.toString());
        Log.d(mTag, "Hybridge action not implemented: " + action);
    }
}

From source file:com.dtworkshop.inappcrossbrowser.InAppChromeClient.java

/**
 * Tell the client to display a prompt dialog to the user.
 * If the client returns true, WebView will assume that the client will
 * handle the prompt dialog and call the appropriate JsPromptResult method.
 *
 * The prompt bridge provided for the InAppBrowser is capable of executing any
 * oustanding callback belonging to the InAppBrowser plugin. Care has been
 * taken that other callbacks cannot be triggered, and that no other code
 * execution is possible.// w  w  w.  j  a va  2  s .c  om
 *
 * To trigger the bridge, the prompt default value should be of the form:
 *
 * gap-iab://<callbackId>
 *
 * where <callbackId> is the string id of the callback to trigger (something
 * like "InAppBrowser0123456789")
 *
 * If present, the prompt message is expected to be a JSON-encoded value to
 * pass to the callback. A JSON_EXCEPTION is returned if the JSON is invalid.
 *
 * @param view
 * @param url
 * @param message
 * @param defaultValue
 * @param result
 */
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
        JsPromptResult result) {
    // See if the prompt string uses the 'gap-iab' protocol. If so, the remainder should be the id of a callback to execute.
    if (defaultValue != null && defaultValue.startsWith("gap")) {
        if (defaultValue.startsWith("gap-iab://")) {
            PluginResult scriptResult;
            String scriptCallbackId = defaultValue.substring(10);
            if (scriptCallbackId.startsWith("InAppBrowser")) {
                if (message == null || message.length() == 0) {
                    scriptResult = new PluginResult(PluginResult.Status.OK, new JSONArray());
                } else {
                    try {
                        scriptResult = new PluginResult(PluginResult.Status.OK, new JSONArray(message));
                    } catch (JSONException e) {
                        scriptResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
                    }
                }
                this.webView.sendPluginResult(scriptResult, scriptCallbackId);
                result.confirm("");
                return true;
            }
        } else {
            // Anything else with a gap: prefix should get this message
            LOG.w(LOG_TAG, "InAppBrowser does not support Cordova API calls: " + url + " " + defaultValue);
            result.cancel();
            return true;
        }
    }
    return false;
}

From source file:com.initialxy.cordova.themeablebrowser.InAppChromeClient.java

/**
 * Tell the client to display a prompt dialog to the user.
 * If the client returns true, WebView will assume that the client will
 * handle the prompt dialog and call the appropriate JsPromptResult method.
 *
 * The prompt bridge provided for the ThemeableBrowser is capable of executing any
 * oustanding callback belonging to the ThemeableBrowser plugin. Care has been
 * taken that other callbacks cannot be triggered, and that no other code
 * execution is possible./*ww  w  . j a va2  s .co  m*/
 *
 * To trigger the bridge, the prompt default value should be of the form:
 *
 * gap-iab://<callbackId>
 *
 * where <callbackId> is the string id of the callback to trigger (something
 * like "ThemeableBrowser0123456789")
 *
 * If present, the prompt message is expected to be a JSON-encoded value to
 * pass to the callback. A JSON_EXCEPTION is returned if the JSON is invalid.
 *
 * @param view
 * @param url
 * @param message
 * @param defaultValue
 * @param result
 */
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
        JsPromptResult result) {
    // See if the prompt string uses the 'gap-iab' protocol. If so, the remainder should be the id of a callback to execute.
    if (defaultValue != null && defaultValue.startsWith("gap")) {
        if (defaultValue.startsWith("gap-iab://")) {
            PluginResult scriptResult;
            String scriptCallbackId = defaultValue.substring(10);
            if (scriptCallbackId.startsWith("ThemeableBrowser")) {
                if (message == null || message.length() == 0) {
                    scriptResult = new PluginResult(PluginResult.Status.OK, new JSONArray());
                } else {
                    try {
                        scriptResult = new PluginResult(PluginResult.Status.OK, new JSONArray(message));
                    } catch (JSONException e) {
                        scriptResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage());
                    }
                }
                this.webView.sendPluginResult(scriptResult, scriptCallbackId);
                result.confirm("");
                return true;
            }
        } else {
            // Anything else with a gap: prefix should get this message
            LOG.w(LOG_TAG, "ThemeableBrowser does not support Cordova API calls: " + url + " " + defaultValue);
            result.cancel();
            return true;
        }
    }
    return false;
}

From source file:com.hellofyc.base.widget.BaseWebView.java

private boolean invokeJSInterfaceMethod(JsPromptResult result, String interfaceName, String methodName,
        Object[] args) {/*  w  w  w.  j av a  2s. c o m*/

    boolean succeed = false;
    final Object obj = mJsInterfaceMap.get(interfaceName);
    if (null == obj) {
        result.cancel();
        return false;
    }

    Class<?>[] parameterTypes = null;
    int count = 0;
    if (args != null) {
        count = args.length;
    }

    if (count > 0) {
        parameterTypes = new Class[count];
        for (int i = 0; i < count; ++i) {
            parameterTypes[i] = getClassFromJsonObject(args[i]);
        }
    }

    try {
        Method method = obj.getClass().getMethod(methodName, parameterTypes);
        Object returnObj = method.invoke(obj, args); // ?
        boolean isVoid = returnObj == null || returnObj.getClass() == void.class;
        String returnValue = isVoid ? "" : returnObj.toString();
        result.confirm(returnValue); // prompt
        succeed = true;
    } catch (Exception e) {
        e.printStackTrace();
    }

    result.cancel();
    return succeed;
}

From source file:org.apache.cordova.CordovaChromeClient.java

/**
 * Tell the client to display a prompt dialog to the user.
 * If the client returns true, WebView will assume that the client will
 * handle the prompt dialog and call the appropriate JsPromptResult method.
 *
 * Since we are hacking prompts for our own purposes, we should not be using them for
 * this purpose, perhaps we should hack console.log to do this instead!
 *
 * @param view/*from  w  ww. j  a v  a 2s. c o  m*/
 * @param url
 * @param message
 * @param defaultValue
 * @param result
 */
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
        JsPromptResult result) {

    // Security check to make sure any requests are coming from the page initially
    // loaded in webview and not another loaded in an iframe.
    boolean reqOk = false;
    if (url.startsWith("file://") || Config.isUrlWhiteListed(url)) {
        reqOk = true;
    }

    // Calling PluginManager.exec() to call a native service using 
    // prompt(this.stringify(args), "gap:"+this.stringify([service, action, callbackId, true]));
    if (reqOk && defaultValue != null && defaultValue.length() > 3
            && defaultValue.substring(0, 4).equals("gap:")) {
        JSONArray array;
        try {
            array = new JSONArray(defaultValue.substring(4));
            String service = array.getString(0);
            String action = array.getString(1);
            String callbackId = array.getString(2);
            String r = this.appView.exposedJsApi.exec(service, action, callbackId, message);
            result.confirm(r == null ? "" : r);
        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }
    }

    // Sets the native->JS bridge mode. 
    else if (reqOk && defaultValue != null && defaultValue.equals("gap_bridge_mode:")) {
        try {
            this.appView.exposedJsApi.setNativeToJsBridgeMode(Integer.parseInt(message));
            result.confirm("");
        } catch (NumberFormatException e) {
            result.confirm("");
            e.printStackTrace();
        }
    }

    // Polling for JavaScript messages 
    else if (reqOk && defaultValue != null && defaultValue.equals("gap_poll:")) {
        String r = this.appView.exposedJsApi.retrieveJsMessages("1".equals(message));
        result.confirm(r == null ? "" : r);
    }

    // Do NO-OP so older code doesn't display dialog
    else if (defaultValue != null && defaultValue.equals("gap_init:")) {
        result.confirm("OK");
    }

    // Show dialog
    else {
        final JsPromptResult res = result;
        AlertDialog.Builder dlg = new AlertDialog.Builder(this.cordova.getActivity());
        dlg.setMessage(message);
        final EditText input = new EditText(this.cordova.getActivity());
        if (defaultValue != null) {
            input.setText(defaultValue);
        }
        dlg.setView(input);
        dlg.setCancelable(false);
        dlg.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String usertext = input.getText().toString();
                res.confirm(usertext);
            }
        });
        dlg.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                res.cancel();
            }
        });
        dlg.show();
    }
    return true;
}

From source file:org.apache.cordova.AndroidChromeClient.java

/**
 * Tell the client to display a prompt dialog to the user.
 * If the client returns true, WebView will assume that the client will
 * handle the prompt dialog and call the appropriate JsPromptResult method.
 *
 * Since we are hacking prompts for our own purposes, we should not be using them for
 * this purpose, perhaps we should hack console.log to do this instead!
 *
 * @param view/*  ww w  . ja v a 2s  .com*/
 * @param url
 * @param message
 * @param defaultValue
 * @param result
 */
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
        JsPromptResult result) {

    // Security check to make sure any requests are coming from the page initially
    // loaded in webview and not another loaded in an iframe.
    boolean reqOk = false;
    if (url.startsWith("file://") || Config.isUrlWhiteListed(url)) {
        reqOk = true;
    }

    // Calling PluginManager.exec() to call a native service using 
    // prompt(this.stringify(args), "gap:"+this.stringify([service, action, callbackId, true]));
    if (reqOk && defaultValue != null && defaultValue.length() > 3
            && defaultValue.substring(0, 4).equals("gap:")) {
        JSONArray array;
        try {
            array = new JSONArray(defaultValue.substring(4));
            String service = array.getString(0);
            String action = array.getString(1);
            String callbackId = array.getString(2);

            //String r = this.appView.exposedJsApi.exec(service, action, callbackId, message);
            String r = this.appView.exec(service, action, callbackId, message);
            result.confirm(r == null ? "" : r);
        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }
    }

    // Sets the native->JS bridge mode. 
    else if (reqOk && defaultValue != null && defaultValue.equals("gap_bridge_mode:")) {
        try {
            //this.appView.exposedJsApi.setNativeToJsBridgeMode(Integer.parseInt(message));
            this.appView.setNativeToJsBridgeMode(Integer.parseInt(message));
            result.confirm("");
        } catch (NumberFormatException e) {
            result.confirm("");
            e.printStackTrace();
        }
    }

    // Polling for JavaScript messages 
    else if (reqOk && defaultValue != null && defaultValue.equals("gap_poll:")) {
        //String r = this.appView.exposedJsApi.retrieveJsMessages("1".equals(message));
        String r = this.appView.retrieveJsMessages("1".equals(message));
        result.confirm(r == null ? "" : r);
    }

    // Do NO-OP so older code doesn't display dialog
    else if (defaultValue != null && defaultValue.equals("gap_init:")) {
        result.confirm("OK");
    }

    // Show dialog
    else {
        final JsPromptResult res = result;
        AlertDialog.Builder dlg = new AlertDialog.Builder(this.cordova.getActivity());
        dlg.setMessage(message);
        final EditText input = new EditText(this.cordova.getActivity());
        if (defaultValue != null) {
            input.setText(defaultValue);
        }
        dlg.setView(input);
        dlg.setCancelable(false);
        dlg.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String usertext = input.getText().toString();
                res.confirm(usertext);
            }
        });
        dlg.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                res.cancel();
            }
        });
        dlg.create();
        dlg.show();
    }
    return true;
}

From source file:org.skt.runtime.RuntimeChromeClient.java

/**
 * Tell the client to display a prompt dialog to the user. 
 * If the client returns true, WebView will assume that the client will 
 * handle the prompt dialog and call the appropriate JsPromptResult method.
 * //w  ww  . jav a  2s .  c  om
 * Since we are hacking prompts for our own purposes, we should not be using them for 
 * this purpose, perhaps we should hack console.log to do this instead!
 * 
 * @param view
 * @param url
 * @param message
 * @param defaultValue
 * @param result
 */
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue,
        JsPromptResult result) {

    // Security check to make sure any requests are coming from the page initially
    // loaded in webview and not another loaded in an iframe.
    boolean reqOk = false;
    if (url.startsWith("file://") || url.indexOf(this.ctx.baseUrl) == 0 || ctx.isUrlWhiteListed(url)) {
        reqOk = true;
    }

    // Calling PluginManager.exec() to call a native service using 
    // prompt(this.stringify(args), "gap:"+this.stringify([service, action, callbackId, true]));
    if (reqOk && defaultValue != null && defaultValue.length() > 3
            && defaultValue.substring(0, 4).equals("gap:")) {
        JSONArray array;
        try {
            array = new JSONArray(defaultValue.substring(4));
            String service = array.getString(0);
            String action = array.getString(1);
            String callbackId = array.getString(2);
            boolean async = array.getBoolean(3);
            String r = ctx.pluginManager.exec(service, action, callbackId, message, async);
            result.confirm(r);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    // Polling for JavaScript messages 
    else if (reqOk && defaultValue != null && defaultValue.equals("gap_poll:")) {
        String r = ctx.callbackServer.getJavascript();
        result.confirm(r);
    }

    // Calling into CallbackServer
    else if (reqOk && defaultValue != null && defaultValue.equals("gap_callbackServer:")) {
        String r = "";
        if (message.equals("usePolling")) {
            r = "" + ctx.callbackServer.usePolling();
        } else if (message.equals("restartServer")) {
            ctx.callbackServer.restartServer();
        } else if (message.equals("getPort")) {
            r = Integer.toString(ctx.callbackServer.getPort());
        } else if (message.equals("getToken")) {
            r = ctx.callbackServer.getToken();
        }
        result.confirm(r);
    }

    // SKTRuntime JS has initialized, so show webview
    // (This solves white flash seen when rendering HTML)
    else if (reqOk && defaultValue != null && defaultValue.equals("gap_init:")) {
        if (ctx.splashscreen != 0) {
            //ctx.root.setBackgroundResource(0);
        }
        ctx.appView.setVisibility(View.VISIBLE);
        ctx.spinnerStop();
        result.confirm("OK");
    }

    // Show dialog
    else {
        final JsPromptResult res = result;
        AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx);
        dlg.setMessage(message);
        final EditText input = new EditText(this.ctx);
        if (defaultValue != null) {
            input.setText(defaultValue);
        }
        dlg.setView(input);
        dlg.setCancelable(false);
        dlg.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                String usertext = input.getText().toString();
                res.confirm(usertext);
            }
        });
        dlg.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                res.cancel();
            }
        });
        dlg.create();
        dlg.show();
    }
    return true;
}