Example usage for org.apache.cordova PluginResult PluginResult

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

Introduction

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

Prototype

public PluginResult(Status status, List<PluginResult> multipartMessages) 

Source Link

Usage

From source file:com.heyzap.cordova.ads.CDVHeyzapAbstractPlugin.java

License:Open Source License

public void addEventListener(final JSONArray args, final CallbackContext callbackContext) {

    JSONArray callbackData = new JSONArray();
    callbackData.put("OK");

    if (listener == null) {
        listener = new CDVListener(callbackContext);

        PluginResult result = new PluginResult(PluginResult.Status.OK, callbackData);
        result.setKeepCallback(true);/*  www .j  av  a 2s  .c o  m*/
        callbackContext.sendPluginResult(result);

        setListener(listener);

    } else {
        callbackContext.success(callbackData);
    }
}

From source file:com.heyzap.cordova.ads.CDVListener.java

License:Open Source License

private void dispatchCallback(String callbackId, JSONArray data) {
    if (context != null) {
        JSONArray callbackData = new JSONArray();
        callbackData.put(callbackId);/*w  ww. j  a  va 2s  . com*/

        for (int i = 0; i < data.length(); i++) {
            try {
                callbackData.put(data.get(i));

            } catch (JSONException e) {
                Log.e(TAG, "Could not add data to callback.");
                e.printStackTrace();
            }
        }

        PluginResult result = new PluginResult(PluginResult.Status.OK, callbackData);
        result.setKeepCallback(true);
        context.sendPluginResult(result);
    }
}

From source file:com.ibm.mobilefirstplatform.clientsdk.cordovaplugins.core.CDVBMSAuthorizationManager.java

License:Apache License

/**
 * Use the native SDK API to invoke process for obtaining authorization header.
 * @param callbackContext Callback that will indicate whether the request succeeded or failed
 *//*from  w  w w  .j a  v a 2  s . c  o  m*/
private void obtainAuthorizationHeader(final CallbackContext callbackContext) throws JSONException {

    final Context currentContext = this.cordova.getActivity();

    cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            MCAAuthorizationManager.getInstance().obtainAuthorization(currentContext, new ResponseListener() {
                @Override
                public void onSuccess(Response response) {
                    try {
                        PluginResult result = new PluginResult(PluginResult.Status.OK,
                                CDVBMSRequest.packJavaResponseToJSON(response));
                        amLogger.debug("ObtainAuthorizationHeader: request successful.");
                        callbackContext.sendPluginResult(result);
                    } catch (JSONException e) {
                        callbackContext.error(e.getMessage());
                    }
                }

                @Override
                public void onFailure(Response response, Throwable t, JSONObject extendedInfo) {
                    try {
                        PluginResult result = new PluginResult(PluginResult.Status.ERROR,
                                CDVBMSRequest.packJavaResponseToJSON(response));
                        amLogger.error("Failed to send request obtainAuthorizationHeader.");
                        callbackContext.sendPluginResult(result);
                    } catch (JSONException e) {
                        callbackContext.error(e.getMessage());
                    }
                }
            });

        }
    });

}

From source file:com.ibm.mobilefirstplatform.clientsdk.cordovaplugins.core.CDVBMSAuthorizationManager.java

License:Apache License

/**
 * Use the native SDK API to invoke process to logout.
 * @param callbackContext Callback that will indicate whether the request succeeded or failed
 *//*from w  w w.j a  va 2  s  .co m*/
private void logout(final CallbackContext callbackContext) throws JSONException {

    final Context currentContext = this.cordova.getActivity();

    cordova.getThreadPool().execute(new Runnable() {
        public void run() {

            MCAAuthorizationManager.getInstance().logout(currentContext, new ResponseListener() {
                @Override
                public void onSuccess(Response response) {
                    try {
                        PluginResult result = new PluginResult(PluginResult.Status.OK,
                                CDVBMSRequest.packJavaResponseToJSON(response));
                        amLogger.debug("Logout: request successful.");
                        callbackContext.sendPluginResult(result);
                    } catch (JSONException e) {
                        callbackContext.error(e.getMessage());
                    }
                }

                @Override
                public void onFailure(Response failResponse, Throwable t, JSONObject extendedInfo) {
                    try {
                        PluginResult result = new PluginResult(PluginResult.Status.ERROR,
                                CDVBMSRequest.packJavaResponseToJSON(failResponse));
                        amLogger.error("Failed to logout.");
                        callbackContext.sendPluginResult(result);
                    } catch (JSONException e) {
                        callbackContext.error(e.getMessage());
                    }
                }
            });

        }
    });

}

From source file:com.ibm.mobilefirstplatform.clientsdk.cordovaplugins.core.CDVBMSRequest.java

License:Apache License

/**
 * Responsible for converting a JSON request to a Bluemix Request, sending the request, receiving a Response, and converting it to a
 * JSON object that is sent back to the Javascript layer.
 *
 * @param args            A JSONArray that contains the JSONObject with the request
 * @param callbackContext Callback that will indicate whether the request succeeded or failed
 */// w  w w.  j ava  2s.co m
public void send(JSONArray args, final CallbackContext callbackContext) throws JSONException {
    JSONObject myrequest = args.getJSONObject(0);

    final Context currentContext = this.cordova.getActivity();
    final Request nativeRequest = unpackJSONRequest(myrequest);
    final String bodyText = myrequest.optString("body", "");

    cordova.getThreadPool().execute(new Runnable() {
        public void run() {

            nativeRequest.send(currentContext, bodyText, new ResponseListener() {
                @Override
                public void onSuccess(Response response) {
                    try {
                        PluginResult result = new PluginResult(PluginResult.Status.OK,
                                packJavaResponseToJSON(response));
                        mfpRequestLogger.debug("Request successful.");
                        callbackContext.sendPluginResult(result);
                    } catch (JSONException e) {
                        callbackContext.error(e.getMessage());
                    }
                }

                @Override
                public void onFailure(Response failResponse, Throwable t, JSONObject extendedInfo) {
                    try {
                        PluginResult result = new PluginResult(PluginResult.Status.ERROR,
                                packJavaResponseToJSON(failResponse, t, extendedInfo));
                        mfpRequestLogger.error("Failed to send request.");
                        callbackContext.sendPluginResult(result);
                    } catch (JSONException e) {
                        callbackContext.error(e.getMessage());
                    }
                }
            });

        }
    });
}

From source file:com.ibm.mobilefirstplatform.clientsdk.cordovaplugins.core.CDVMFPAuthorizationManager.java

License:Apache License

/**
 * Use the native SDK API to invoke process for obtaining authorization header.
 * @param callbackContext Callback that will indicate whether the request succeeded or failed
 */// w ww. jav  a 2 s  .c o m
private void obtainAuthorizationHeader(final CallbackContext callbackContext) throws JSONException {

    final Context currentContext = this.cordova.getActivity();

    cordova.getThreadPool().execute(new Runnable() {
        public void run() {

            AuthorizationManager.getInstance().obtainAuthorizationHeader(currentContext,
                    new ResponseListener() {
                        @Override
                        public void onSuccess(Response response) {
                            try {
                                PluginResult result = new PluginResult(PluginResult.Status.OK,
                                        CDVMFPRequest.packJavaResponseToJSON(response));
                                amLogger.debug("ObtainAuthorizationHeader: request successful.");
                                callbackContext.sendPluginResult(result);
                            } catch (JSONException e) {
                                callbackContext.error(e.getMessage());
                            }
                        }

                        @Override
                        public void onFailure(Response failResponse, Throwable t, JSONObject extendedInfo) {
                            try {
                                PluginResult result = new PluginResult(PluginResult.Status.ERROR,
                                        CDVMFPRequest.packJavaResponseToJSON(failResponse));
                                amLogger.error("Failed to send request obtainAuthorizationHeader.");
                                callbackContext.sendPluginResult(result);
                            } catch (JSONException e) {
                                callbackContext.error(e.getMessage());
                            }
                        }
                    });

        }
    });

}

From source file:com.ibm.mobilefirstplatform.clientsdk.cordovaplugins.core.CDVMFPAuthorizationManager.java

License:Apache License

/**
 * Use the native SDK API to invoke process to logout.
 * @param callbackContext Callback that will indicate whether the request succeeded or failed
 *///w  ww  . j a  v  a  2 s.  c o m
private void logout(final CallbackContext callbackContext) throws JSONException {

    final Context currentContext = this.cordova.getActivity();

    cordova.getThreadPool().execute(new Runnable() {
        public void run() {

            AuthorizationManager.getInstance().logout(currentContext, new ResponseListener() {
                @Override
                public void onSuccess(Response response) {
                    try {
                        PluginResult result = new PluginResult(PluginResult.Status.OK,
                                CDVMFPRequest.packJavaResponseToJSON(response));
                        amLogger.debug("Logout: request successful.");
                        callbackContext.sendPluginResult(result);
                    } catch (JSONException e) {
                        callbackContext.error(e.getMessage());
                    }
                }

                @Override
                public void onFailure(Response failResponse, Throwable t, JSONObject extendedInfo) {
                    try {
                        PluginResult result = new PluginResult(PluginResult.Status.ERROR,
                                CDVMFPRequest.packJavaResponseToJSON(failResponse));
                        amLogger.error("Failed to logout.");
                        callbackContext.sendPluginResult(result);
                    } catch (JSONException e) {
                        callbackContext.error(e.getMessage());
                    }
                }
            });

        }
    });

}

From source file:com.ibm.mobilefirstplatform.clientsdk.cordovaplugins.push.CDVBMSPush.java

License:Apache License

private void registerNotificationsCallback(final CallbackContext callbackContext) {

    notificationCallback = callbackContext;

    if (!ignoreIncomingNotifications) {

        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                notificationListener = new MFPPushNotificationListener() {
                    @Override//from  w  w  w. java 2s.c o m
                    public void onReceive(final MFPSimplePushNotification message) {
                        try {

                            JSONObject notification = new JSONObject();

                            notification.put("message", message.getAlert());
                            notification.put("payload", message.getPayload());

                            PluginResult result = new PluginResult(PluginResult.Status.OK, notification);
                            result.setKeepCallback(true);
                            notificationCallback.sendPluginResult(result);
                        } catch (JSONException e) {
                            PluginResult result = new PluginResult(PluginResult.Status.ERROR, e.toString());
                            result.setKeepCallback(true);
                            notificationCallback.sendPluginResult(result);
                        }
                    }
                };

                MFPPush.getInstance().listen(notificationListener);
            }
        });

    } else {
        callbackContext.error(
                "Error: Called registerNotificationsCallback() after IgnoreIncomingNotifications was set");
    }
}

From source file:com.ibm.mobilefirstplatform.clientsdk.cordovaplugins.push.CDVMFPPush.java

License:Apache License

private void registerNotificationsCallback(final CallbackContext callbackContext) {
    pushLogger.debug("In registerNotificationsCallback");

    notificationCallback = callbackContext;

    if (!ignoreIncomingNotifications) {

        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                notificationListener = new MFPPushNotificationListener() {
                    @Override//  ww w.ja  v  a2 s.  c o m
                    public void onReceive(final MFPSimplePushNotification message) {
                        try {
                            pushLogger.debug("Push notification received: " + message.toString());

                            JSONObject notification = new JSONObject();

                            notification.put("message", message.getAlert());
                            notification.put("payload", message.getPayload());

                            PluginResult result = new PluginResult(PluginResult.Status.OK, notification);
                            result.setKeepCallback(true);
                            notificationCallback.sendPluginResult(result);
                        } catch (JSONException e) {
                            PluginResult result = new PluginResult(PluginResult.Status.ERROR, e.toString());
                            result.setKeepCallback(true);
                            notificationCallback.sendPluginResult(result);
                        }
                    }
                };

                MFPPush.getInstance().listen(notificationListener);
            }
        });

    } else {
        pushLogger.warn(
                "Notification handling is currently off. Turn it back on by calling setIgnoreIncomingNotifications(true)");
        callbackContext.error(
                "Error: Called registerNotificationsCallback() after IgnoreIncomingNotifications was set");
    }
}

From source file:com.ibm.mqtt.android.cordova.plugin.MqttPlugin.java

License:Open Source License

private void makeTraceCallback(Status status, String message, int errorCode, String severity,
        CallbackContext callbackContext) {
    if ((traceCallbackId != null) && (traceEnabled)) {
        JSONObject callbackResult = new JSONObject();
        try {/* w w w . j  av a2s. com*/
            callbackResult.put("severity", severity);
            callbackResult.put("message", message);
            callbackResult.put("errorCode", errorCode);
        } catch (JSONException e) {
            Log.e(TAG, "failed to build callback result", e);
        }
        PluginResult pluginResult = new PluginResult(status, callbackResult);
        pluginResult.setKeepCallback(true);

        callbackContext.success(traceCallbackId);
    }
}