Example usage for org.apache.cordova PluginResult setKeepCallback

List of usage examples for org.apache.cordova PluginResult setKeepCallback

Introduction

In this page you can find the example usage for org.apache.cordova PluginResult setKeepCallback.

Prototype

public void setKeepCallback(boolean b) 

Source Link

Usage

From source file:rdx.andro.forexcapplugins.childBrowser.ChildBrowser.java

License:BSD License

/**
 * Executes the request and returns PluginResult.
 * /*from w ww.  j  a  v a  2s  .  com*/
 * @param action
 *            The action to execute.
 * @param args
 *            JSONArry of arguments for the plugin.
 * @param callbackId
 *            The callback id used when calling back into JavaScript.
 * @return A PluginResult object with a status and message.
 */
public boolean execute(String action, JSONArray args, CallbackContext callbackId) {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";

    try {
        if (action.equals("showWebPage")) {
            this.browserCallbackId = callbackId;

            // If the ChildBrowser is already open then throw an error
            if (dialog != null && dialog.isShowing()) {

                browserCallbackId.sendPluginResult(
                        new PluginResult(PluginResult.Status.ERROR, "ChildBrowser is already open"));
                return false;
            }

            result = this.showWebPage(args.getString(0), args.optJSONObject(1));

            if (result.length() > 0) {
                status = PluginResult.Status.ERROR;
                browserCallbackId.sendPluginResult(new PluginResult(status, result));
                return true;
            } else {
                PluginResult pluginResult = new PluginResult(status, result);
                pluginResult.setKeepCallback(true);
                browserCallbackId.sendPluginResult(pluginResult);
                return true;
            }
        } else if (action.equals("close")) {
            closeDialog();

            JSONObject obj = new JSONObject();
            obj.put("type", CLOSE_EVENT);

            PluginResult pluginResult = new PluginResult(status, obj);
            pluginResult.setKeepCallback(false);
            browserCallbackId.sendPluginResult(pluginResult);
            return true;
        } else if (action.equals("openExternal")) {
            result = this.openExternal(args.getString(0), args.optBoolean(1));
            if (result.length() > 0) {
                status = PluginResult.Status.ERROR;
            }
        } else {
            status = PluginResult.Status.INVALID_ACTION;
        }
        browserCallbackId.sendPluginResult(new PluginResult(status, result));
        return true;
    } catch (JSONException e) {
        browserCallbackId.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
        return false;
    }
}

From source file:rdx.andro.forexcapplugins.childBrowser.ChildBrowser.java

License:BSD License

/**
 * Create a new plugin result and send it back to JavaScript
 * /*from  w  w  w  .j av a 2s.  com*/
 * @param obj
 *            a JSONObject contain event payload information
 */
private void sendUpdate(JSONObject obj, boolean keepCallback) {
    if (this.browserCallbackId != null) {
        PluginResult result = new PluginResult(PluginResult.Status.OK, obj);
        result.setKeepCallback(keepCallback);
        browserCallbackId.sendPluginResult(result);
        // this.success(result, this.browserCallbackId);
    }
}

From source file:run.ace.OutgoingMessages.java

License:Open Source License

static void send(Object[] data) {
    PluginResult r = null;
    try {/*from ww w .  j  a v  a 2 s  .c  om*/
        r = new PluginResult(PluginResult.Status.OK, new JSONArray(data));
    } catch (JSONException ex) {
        r = new PluginResult(PluginResult.Status.ERROR,
                "Could not send back native data " + data + ": " + ex.toString());
    }
    r.setKeepCallback(true);
    _callbackContext.sendPluginResult(r);
}

From source file:salvatejero.cordova.liferay.LiferayPlugin.java

License:Apache License

private void getObjectModel(final CallbackContext callbackContext, String className, String methodName,
        JSONArray values) throws Exception {

    JSONArray jsonArrayInstance = new JSONArray();
    JSONObject jsonObjectInstance = new JSONObject();

    JSONObjectAsyncTaskCallback callBackJSONObject = new JSONObjectAsyncTaskCallback() {

        @Override/*from   w  w  w.j  a v  a 2s.co  m*/
        public void onFailure(Exception arg0) {
            callbackContext.error(arg0.getMessage());
        }

        @Override
        public void onSuccess(JSONObject arg0) {
            // TODO Auto-generated method stub
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, arg0);
            pluginResult.setKeepCallback(true);
            callbackContext.sendPluginResult(pluginResult);
        }

    };

    JSONArrayAsyncTaskCallback callbackJSONArray = new JSONArrayAsyncTaskCallback() {

        @Override
        public void onFailure(Exception arg0) {
            callbackContext.error(arg0.getMessage());
        }

        @Override
        public void onSuccess(JSONArray arg0) {
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, arg0);
            pluginResult.setKeepCallback(true);
            callbackContext.sendPluginResult(pluginResult);

        }
    };

    Method methodToExecute = null;
    Object[] params = null;
    BaseService service = getService(className);

    if (service == null) {
        throw new LiferayPluginException("Service not implemented");
    }
    Method[] methods = service.getClass().getMethods();
    for (Method m : methods) {
        if (m.getName().toLowerCase().equals(methodName.toLowerCase())) {

            if (values.length() != m.getParameterTypes().length) {
                throw new LiferayPluginException("Number of params error for the method " + methodName);
            }
            params = getListOfParam(m, values);
            if (m.getReturnType().isInstance(jsonArrayInstance)) {
                session.setCallback(callbackJSONArray);
            } else if (m.getReturnType().isInstance(jsonObjectInstance)) {
                session.setCallback(callBackJSONObject);
            } else if (m.getReturnType().equals(Void.TYPE)) {
                callbackContext.success();
            }

            methodToExecute = m;
            break;
        }
    }
    if (methodToExecute == null) {
        for (Method m : methods) {
            if (methodName.indexOf(m.getName().toLowerCase()) >= 0) {

                if (values.length() != m.getParameterTypes().length) {
                    throw new LiferayPluginException("Number of params error for the method " + methodName);
                }
                params = getListOfParam(m, values);
                if (m.getReturnType().isInstance(jsonArrayInstance)) {
                    session.setCallback(callbackJSONArray);
                } else if (m.getReturnType().isInstance(jsonObjectInstance)) {
                    session.setCallback(callBackJSONObject);
                } else if (m.getReturnType().equals(Void.TYPE)) {
                    callbackContext.success();
                }

                methodToExecute = m;
                break;
            }
        }
    }
    if (methodToExecute == null) {
        throw new LiferayPluginException("Method " + methodName + "not found");
    }

    try {
        methodToExecute.invoke(service, params);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new LiferayPluginException("Error invoking -- " + e.getMessage());
    }

}

From source file:salvatejero.cordova.liferay.LiferayPlugin.java

License:Apache License

private void doConnect(final CallbackContext callbackContext, final String urlServer, final String userName,
        final String password) {

    cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            session = new SessionImpl(urlServer, new BasicAuthentication(userName, password));
            try {
                JSONObject user = getUser(session, userName);
                PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, user);
                pluginResult.setKeepCallback(true);
                callbackContext.sendPluginResult(pluginResult);
            } catch (Exception e) {
                callbackContext.error(e.getMessage());

            }/*from ww w .  j  a  v a  2s. c o m*/

        }
    });

}

From source file:tech.gnb.test.Test.java

License:Apache License

/**
 * Executes the request and returns PluginResult.
 *
 * @param action            The action to execute.
 * @param args              JSONArry of arguments for the plugin.
 * @param callbackContext   The callback id used when calling back into JavaScript.
 * @return                  A PluginResult object with a status and message.
 *//*from  w  w w. j av a 2 s  . c  om*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    JSONObject r = new JSONObject();
    PluginResult result;
    boolean b = true;
    try {
        if (action.equalsIgnoreCase("test")) {
            String str = args.getString(0);
            r.put("response", str);
            //callbackContext.success(r);
            result = new PluginResult(PluginResult.Status.OK, r);
        } else {
            b = false;
            result = new PluginResult(PluginResult.Status.ERROR, "Invalid Action");
        }
    } catch (JSONException e) {
        e.printStackTrace();
        b = false;
        result = new PluginResult(PluginResult.Status.ERROR, "JSON Error");
    }

    result.setKeepCallback(true);
    callbackContext.sendPluginResult(result);

    /*
    cordova.getActivity().runOnUiThread(new Runnable() {
    public void run() {
        ...
        callbackContext.success(); // Thread-safe.
    }
    });
            
    cordova.getThreadPool().execute(new Runnable() {
    public void run() {
        ...
        callbackContext.success(); // Thread-safe.
    }
    });
    */
    return b;
}

From source file:tv.ouya.sdk.CordovaOuyaPlugin.java

License:Apache License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    JSONObject result = null;//from w w w  . j  av a  2 s .co  m
    if (sEnableLogging) {
        Log.i(TAG, "********************* execute action=" + action);
    }
    if (action.equals("setCallbackOnGenericMotionEvent")) {
        sCallbackOnGenericMotionEvent = callbackContext;
        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "");
        pluginResult.setKeepCallback(true);
        sCallbackOnGenericMotionEvent.sendPluginResult(pluginResult);
        return true;
    } else if (action.equals("setCallbackOnKeyUp")) {
        sCallbackOnKeyUp = callbackContext;
        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "");
        pluginResult.setKeepCallback(true);
        sCallbackOnKeyUp.sendPluginResult(pluginResult);
        return true;
    } else if (action.equals("setCallbackOnKeyDown")) {
        sCallbackOnKeyDown = callbackContext;
        PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "");
        pluginResult.setKeepCallback(true);
        sCallbackOnKeyDown.sendPluginResult(pluginResult);
        return true;
    } else if (action.equals("initOuyaPlugin")) {
        sCallbackInitOuyaPlugin = callbackContext;
        JSONArray jsonArray = null;
        if (args.length() > 0) {
            try {
                jsonArray = new JSONArray(args.get(0).toString());
            } catch (Exception e) {
                result = createError(0, "initOuyaPlugin failed to read argument!");
                sCallbackInitOuyaPlugin.error(result);
                return true;
            }
        } else {
            result = createError(0, "initOuyaPlugin arg1 is null!");
            sCallbackInitOuyaPlugin.error(result);
            return true;
        }
        initOuyaPlugin(jsonArray);
        return true;
    } else if (action.equals("requestGamerInfo")) {
        sCallbackRequestGamerInfo = callbackContext;
        requestGamerInfo();
        return true;
    } else if (action.equals("requestProducts")) {
        sCallbackRequestProducts = callbackContext;
        JSONArray jsonArray = null;
        if (args.length() > 0) {
            try {
                jsonArray = new JSONArray(args.get(0).toString());
            } catch (Exception e) {
                result = createError(0, "requestProducts failed to read argument!");
                sCallbackRequestProducts.error(result);
                return true;
            }
        } else {
            result = createError(0, "requestProducts arg1 is null!");
            sCallbackRequestProducts.error(result);
            return true;
        }
        requestProducts(jsonArray);
        return true;
    } else if (action.equals("requestPurchase")) {
        sCallbackRequestPurchase = callbackContext;
        String identifier = null;
        if (args.length() > 0) {
            try {
                Log.i(TAG, "requestPurchase identifier=" + args.getString(0));
                identifier = args.getString(0);
            } catch (Exception e) {
                result = createError(0, "requestPurchase failed to read argument!");
                sCallbackRequestPurchase.error(result);
                return true;
            }
        } else {
            result = createError(0, "requestPurchase arg1 is null!");
            sCallbackRequestPurchase.error(result);
            return true;
        }
        requestPurchase(identifier);
        return true;
    } else if (action.equals("requestReceipts")) {
        sCallbackRequestReceipts = callbackContext;
        requestReceipts();
        return true;
    } else if (action.equals("setSafeArea")) {
        float amount = 0f;
        if (args.length() > 0) {
            try {
                Log.i(TAG, "setSafeArea identifier=" + args.getString(0));
                amount = (float) args.getDouble(0);
            } catch (Exception e) {
                result = createError(0, "setSafeArea failed to read argument!");
                callbackContext.error(result);
                return true;
            }
        } else {
            result = createError(0, "setSafeArea arg1 is null!");
            callbackContext.error(result);
            return true;
        }
        setSafeArea(callbackContext, amount);
        return true;
    } else if (action.equals("getDeviceHardware")) {
        getDeviceHardware(callbackContext);
        return true;
    } else if (action.equals("shutdown")) {
        shutdown(callbackContext);
        return true;
    }

    return false;
}

From source file:tv.ouya.sdk.CordovaOuyaPlugin.java

License:Apache License

public static void onGenericMotionEvent(final int playerNum, final int axis, final float val) {
    if (null == sCallbackOnGenericMotionEvent) {
        if (sEnableLogging) {
            Log.e(TAG, "sCallbackOnGenericMotionEvent is null!");
        }/*  w w  w. j ava  2  s.com*/
        return;
    }
    if (null == sInstance) {
        if (sEnableLogging) {
            Log.e(TAG, "sInstance is null!");
        }
        return;
    }
    sInstance.cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("playerNum", playerNum);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                jsonObject.put("axis", axis);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                jsonObject.put("val", val);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonObject.toString());
            pluginResult.setKeepCallback(true);
            sCallbackOnGenericMotionEvent.sendPluginResult(pluginResult); // Thread-safe.
        }
    });
}

From source file:tv.ouya.sdk.CordovaOuyaPlugin.java

License:Apache License

public static void onKeyUp(final int playerNum, final int button) {
    if (null == sCallbackOnKeyUp) {
        if (sEnableLogging) {
            Log.e(TAG, "sCallbackOnKeyUp is null!");
        }//from   www . j  a  v a 2s . c  om
        return;
    }
    if (null == sInstance) {
        if (sEnableLogging) {
            Log.e(TAG, "sInstance is null!");
        }
        return;
    }
    sInstance.cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("playerNum", playerNum);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                jsonObject.put("button", button);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonObject.toString());
            pluginResult.setKeepCallback(true);
            sCallbackOnKeyUp.sendPluginResult(pluginResult); // Thread-safe.
        }
    });
}

From source file:tv.ouya.sdk.CordovaOuyaPlugin.java

License:Apache License

public static void onKeyDown(final int playerNum, final int button) {
    if (null == sCallbackOnKeyDown) {
        if (sEnableLogging) {
            Log.e(TAG, "sCallbackOnKeyDown is null!");
        }//from  www  . j  av  a2s  .c o  m
        return;
    }
    if (null == sInstance) {
        if (sEnableLogging) {
            Log.e(TAG, "sInstance is null!");
        }
        return;
    }
    sInstance.cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("playerNum", playerNum);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            try {
                jsonObject.put("button", button);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonObject.toString());
            pluginResult.setKeepCallback(true);
            sCallbackOnKeyDown.sendPluginResult(pluginResult); // Thread-safe.
        }
    });
}