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:com.tribalyte.plugin.myo.MyoEventController.java

License:MIT License

private void callHandler(String evType, Myo myo, long timestamp, Object[] extras) {
    CallbackContext cbc = null;//from   ww  w  .j ava2  s.  c o  m
    synchronized (mHandlersLock) {
        cbc = mEvHandlers.get(evType);
    }
    if (cbc != null) {
        //logd("Sending event " + evType + " to registered caller");
        MyoWithJson myoWithJson = mMyoMap.get(myo.getMacAddress());
        if (myoWithJson != null) {
            try {
                JSONObject res = new JSONObject();
                res.put("eventName", evType);
                res.put("myo", myoWithJson.myoJson);
                res.put("timestamp", Long.valueOf(timestamp));
                for (int i = 0; extras != null && i < extras.length; i += 2) {
                    res.put((String) extras[i], extras[i + 1]);
                }
                PluginResult pResult = new PluginResult(PluginResult.Status.OK, res);
                pResult.setKeepCallback(true);
                cbc.sendPluginResult(pResult);
            } catch (Exception e) {
                loge("Exception while calling handler: ", e);
                cbc.error(e.getLocalizedMessage());
            }
        } else {
            loge("ERROR: JSON Myo not found for MAC " + myo.getMacAddress(), null);
        }
    }
}

From source file:com.tribalyte.plugin.myo.MyoEventController.java

License:MIT License

private void sendRemovedResultToHandler(CallbackContext cbc) {
    PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT, "Event handler removed");
    pluginResult.setKeepCallback(false);
    cbc.sendPluginResult(pluginResult);/* www.  j  a v  a 2 s. c o m*/
}

From source file:com.triggertrap.ZeroConf.java

License:Open Source License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    this.callback = callbackContext;

    if (action.equals("watch")) {
        final String type = args.optString(0);
        if (type != null) {
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    watch(type); // Thread-safe.
                }//from  w  w  w.j ava  2 s. co m
            });
        } else {
            callbackContext.error("Service type not specified.");
            return false;
        }
    } else if (action.equals("unwatch")) {
        final String type = args.optString(0);
        if (type != null) {
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    unwatch(type);
                }
            });
        } else {
            callbackContext.error("Service type not specified.");
            return false;
        }
    } else if (action.equals("register")) {
        JSONObject obj = args.optJSONObject(0);
        if (obj != null) {
            final String type = obj.optString("type");
            final String name = obj.optString("name");
            final int port = obj.optInt("port");
            final String text = obj.optString("text");
            if (type == null) {
                callbackContext.error("Missing required service info.");
                return false;
            }
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    register(type, name, port, text);
                }
            });
        } else {
            callbackContext.error("Missing required service info.");
            return false;

        }

    } else if (action.equals("close")) {
        if (jmdns != null) {
            try {
                jmdns.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else if (action.equals("unregister")) {
        if (jmdns != null) {
            jmdns.unregisterAllServices();
        }

    } else if (action.equals("list")) {
        final String type = args.optString(0);
        final int timeout = args.optInt(1);
        if (type != null && timeout > 0) {
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    list(type, timeout); // Thread-safe.
                }
            });
        } else {
            callbackContext.error("Missing required parameter: type, timeout.");
            return false;

        }
    } else {
        Log.e("ZeroConf", "Invalid action: " + action);
        callbackContext.error("Invalid action.");
        return false;
    }
    PluginResult result = new PluginResult(Status.NO_RESULT);
    result.setKeepCallback(true);
    // return result;
    return true;
}

From source file:com.triggertrap.ZeroConf.java

License:Open Source License

public void sendCallback(String action, ServiceInfo info) {
    JSONObject status = new JSONObject();
    try {//  w w w  .  j ava2s .co m
        status.put("action", action);
        status.put("service", jsonifyService(info));
        Log.d("ZeroConf", "Sending result: " + status.toString());

        PluginResult result = new PluginResult(PluginResult.Status.OK, status);
        result.setKeepCallback(true);
        this.callback.sendPluginResult(result);

    } catch (JSONException e) {

        e.printStackTrace();
    }

}

From source file:com.triggertrap.ZeroConf.java

License:Open Source License

public void sendListCallback(String action, ServiceInfo[] services) {
    JSONObject status = new JSONObject();
    try {//from   www  . j a va  2s. c  o m
        status.put("action", action);
        JSONArray array = new JSONArray();
        for (ServiceInfo service : services) {
            array.put(jsonifyService(service));
        }
        status.put("service", array);
        Log.d("ZeroConf", "Sending result: " + status.toString());

        PluginResult result = new PluginResult(PluginResult.Status.OK, status);
        result.setKeepCallback(true);
        this.callback.sendPluginResult(result);

    } catch (JSONException e) {

        e.printStackTrace();
    }

}

From source file:com.trimble.mcs.cordova.plugin.TrmbMcsRfid.java

License:Apache License

private void setRfidTagListener(CallbackContext clientCb) {
    mScanTagCallback = clientCb;//from  ww w  .jav  a 2  s .  c o m

    cordova.getActivity().registerReceiver(mRfidTagReceiver, mRfidTagFilter);

    PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
    result.setKeepCallback(true);
    clientCb.sendPluginResult(result);
}

From source file:com.trimble.mcs.cordova.plugin.TrmbMcsRfid.java

License:Apache License

private void setRfidStatusListener(CallbackContext clientCb) {
    mScanStatusCallback = clientCb;/*ww  w. j a  va  2  s.  c  om*/

    cordova.getActivity().registerReceiver(mRfidStatusReceiver, mRfidStatusFilter);

    PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
    result.setKeepCallback(true);
    clientCb.sendPluginResult(result);
}

From source file:com.trimble.mcs.cordova.plugin.TrmbMcsRfid.java

License:Apache License

public void handleRfidTagIntent(Context context, Intent intent) {
    CallbackContext cb = mScanTagCallback;
    if (cb == null)
        return;/*from   ww  w. jav a2s  .  c o m*/

    String action = intent.getAction();

    if (action.equals(RfidConstants.ACTION_RFID_TAG_SCANNED)) {
        // Extract tag data from intent
        String tagID = intent.getStringExtra(RfidConstants.RFID_FIELD_ID);
        String memory = intent.getStringExtra(RfidConstants.RFID_FIELD_MEMORY);
        String tagType = intent.getStringExtra(RfidConstants.RFID_FIELD_TAG_TYPE);
        int rssi = intent.getIntExtra(RfidConstants.RFID_FIELD_RSSI, 0);
        int readCount = intent.getIntExtra(RfidConstants.RFID_FIELD_READ_COUNT, 0);
        long timeStamp = intent.getLongExtra(RfidConstants.RFID_FIELD_TIME_STAMP, 0);

        // Store tag data in a JSON object for transmission to javascript
        JSONObject tagInfo = new JSONObject();
        try {
            if (tagID != null)
                tagInfo.put("tagID", tagID);
            if (memory != null)
                tagInfo.put("memory", memory);
            if (tagType != null)
                tagInfo.put("tagType", tagType);
            if (rssi != 0)
                tagInfo.put("rssi", rssi);
            if (readCount != 0)
                tagInfo.put("readCount", readCount);
            if (timeStamp != 0)
                tagInfo.put("timeStamp", timeStamp);
        } catch (JSONException e) {
            Log.d(TAG, "Error populating JSON object with scan data: " + e.getMessage());
        }

        PluginResult result = new PluginResult(PluginResult.Status.OK, tagInfo);
        result.setKeepCallback(true);
        cb.sendPluginResult(result);
    }
}

From source file:com.trimble.mcs.cordova.plugin.TrmbMcsRfid.java

License:Apache License

public void handleRfidStatusIntent(Context context, Intent intent) {
    CallbackContext cb = mScanStatusCallback;
    if (cb == null)
        return;//from w w  w .  j  a  v  a  2s .co m

    String event;
    String action = intent.getAction();

    if (action.equals(RfidConstants.ACTION_RFID_START_SCAN_NOTIFICATION)) {
        event = EVENT_SCAN_STARTED;
    } else if (action.equals(RfidConstants.ACTION_RFID_STOP_SCAN_NOTIFICATION)) {
        event = EVENT_SCAN_STOPPED;
    } else {
        return;
    }

    PluginResult result = new PluginResult(PluginResult.Status.OK, event);
    result.setKeepCallback(true);
    cb.sendPluginResult(result);
}

From source file:com.ugrokit.cordova.ugrokit.UGrokIt.java

License:Apache License

@Override
public boolean firmwareUpdateProgress(int amountDone, int amountTotal, boolean canCancel) {
    JSONObject d = new JSONObject();
    try {/* w w w .  j a  va 2  s .c  o m*/
        d.put("amountDone", amountDone);
        d.put("amountTotal", amountTotal);
        d.put("canCancel", canCancel);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    PluginResult pluginResult = new PluginResult(Status.OK, d);
    pluginResult.setKeepCallback(true);
    this.firmwareCallbackContext.sendPluginResult(pluginResult);
    return false;
}