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.phonegap.plugins.speech.XSpeechRecognizer.java

License:Open Source License

private void fireErrorEvent(Integer code) {
    JSONObject event = new JSONObject();
    try {//from  w  ww. ja  va  2s  .c  om
        event.put("type", "error");
        event.put("code", code.toString());
    } catch (JSONException e) {
        // this will never happen
    }

    PluginResult pr = new PluginResult(PluginResult.Status.ERROR, event);
    pr.setKeepCallback(false);
    this.callbackContext.sendPluginResult(pr);
}

From source file:com.phonegap.plugins.speech.XSpeechRecognizer.java

License:Open Source License

private void fireEvent(String type) {
    // callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "Event"));
    JSONObject event = new JSONObject();
    try {/*from  w  w  w .j  a  v a  2s. c o  m*/
        event.put("type", type);
    } catch (JSONException e) {
        // this will never happen
    }
    // PluginResult pr = new PluginResult(PluginResult.Status.OK, "event");
    PluginResult pr = new PluginResult(PluginResult.Status.OK, event);
    pr.setKeepCallback(true);
    this.callbackContext.sendPluginResult(pr);
}

From source file:com.physicaroid.pocketduino.cordova.PocketDuino.java

License:Apache License

private void setCallbackTest(CallbackContext callbackContext) {
    try {//  w  ww  .  j a  va 2 s.  c o  m
        String json = "{\"message\": \"conntected\"}";
        JSONObject parameter = new JSONObject(json);
        PluginResult dataResult = new PluginResult(PluginResult.Status.OK, parameter);
        dataResult.setKeepCallback(true);
        callbackContext.sendPluginResult(dataResult);
    } catch (JSONException e) {
        Log.e(POCKETDUINO, "Exception: " + e.getMessage());
        callbackContext.error(e.toString());
    }
}

From source file:com.physicaroid.pocketduino.cordova.PocketDuino.java

License:Apache License

/**
 * USB???/*from ww  w.  ja v a  2  s .co  m*/
 */
private void listenUsbDevice(CallbackContext callbackContext) {
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
    intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
    this.usbCallbackContext = callbackContext;
    if (this.mUsbReceiver == null) {
        this.mUsbReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String handlerName = null;
                String action = intent.getAction();
                if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                    // plugin result of USB attached
                    handlerName = HANDLER_PREFIX + "." + "attached";
                }
                if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                    // plugin result of USB detached
                    handlerName = HANDLER_PREFIX + "." + "detached";
                }
                try {
                    String json = "{\"handlerName\":" + handlerName + " }";
                    JSONObject parameter = new JSONObject(json);
                    PluginResult dataResult = new PluginResult(PluginResult.Status.OK, parameter);
                    dataResult.setKeepCallback(true);
                    usbCallbackContext.sendPluginResult(dataResult);
                } catch (JSONException e) {
                    Log.e(POCKETDUINO, "Exception: " + e.getMessage());
                    usbCallbackContext.error(e.toString());
                }
            }
        };
        webView.getContext().registerReceiver(this.mUsbReceiver, intentFilter);
    }
}

From source file:com.physicaroid.pocketduino.cordova.PocketDuino.java

License:Apache License

/**
 * PocketDuino??//w ww  .  j a  v  a  2 s .c o m
 */
private void openDevice(CallbackContext callbackContext) {
    try {
        if (this.mPhysicaloid.open()) {
            // ??????upload????????
            PluginResult dataResult = new PluginResult(PluginResult.Status.OK);
            dataResult.setKeepCallback(true);
            callbackContext.sendPluginResult(dataResult);
        } else {
            PluginResult dataResult = new PluginResult(PluginResult.Status.ERROR);
            dataResult.setKeepCallback(true);
            callbackContext.sendPluginResult(dataResult);
        }
    } catch (RuntimeException e) {
        PluginResult dataResult = new PluginResult(PluginResult.Status.ERROR);
        dataResult.setKeepCallback(true);
        callbackContext.sendPluginResult(dataResult);
    }
}

From source file:com.physicaroid.pocketduino.cordova.PocketDuino.java

License:Apache License

/**
 * PocketDuino??/*w w  w  .jav a 2  s.  co  m*/
 */
private void closeDevice(CallbackContext callbackContext) {
    try {
        if (this.mPhysicaloid.close()) {
            PluginResult dataResult = new PluginResult(PluginResult.Status.OK);
            dataResult.setKeepCallback(true);
            callbackContext.sendPluginResult(dataResult);
        } else {
            PluginResult dataResult = new PluginResult(PluginResult.Status.ERROR);
            dataResult.setKeepCallback(true);
            callbackContext.sendPluginResult(dataResult);
        }
    } catch (RuntimeException e) {
        PluginResult dataResult = new PluginResult(PluginResult.Status.ERROR);
        dataResult.setKeepCallback(true);
        callbackContext.sendPluginResult(dataResult);
    }
}

From source file:com.physicaroid.pocketduino.cordova.PocketDuino.java

License:Apache License

/**
 *
 *///from  w w  w.  ja  va 2  s.  co m
private void writeSerial(CallbackContext callbackContext, JSONArray args) {
    try {
        String command = args.getString(0);
        byte[] buf = command.getBytes();
        Log.d(POCKETDUINO, command);
        this.mPhysicaloid.write(buf, buf.length);
        PluginResult dataResult = new PluginResult(PluginResult.Status.OK);
        dataResult.setKeepCallback(true);
        callbackContext.sendPluginResult(dataResult);
    } catch (Exception e) {
        try {
            String json = "{\"message\":" + e.toString() + " }";
            JSONObject parameter = new JSONObject(json);
            PluginResult dataResult = new PluginResult(PluginResult.Status.ERROR, parameter);
            dataResult.setKeepCallback(true);
            callbackContext.sendPluginResult(dataResult);
            String hoge;
        } catch (Exception ex) {
            Log.e(POCKETDUINO, ex.toString());
        }
    }
}

From source file:com.physicaroid.pocketduino.cordova.PocketDuino.java

License:Apache License

/**
 * hex?/*w  w w .  j av  a  2s. c o m*/
 */
private void uploadHexFile(CallbackContext callbackContext, JSONArray args) {
    try {
        String fileName = args.getString(0);
        Log.d(POCKETDUINO, "!!!--- " + fileName + " ---!!!");
        // upload()?3? callback ?????????
        // ???????????????
        mPhysicaloid.upload(Boards.POCKETDUINO, cordova.getActivity().getResources().getAssets().open(fileName),
                null);
        PluginResult dataResult = new PluginResult(PluginResult.Status.OK);
        dataResult.setKeepCallback(true);
        callbackContext.sendPluginResult(dataResult);
    } catch (RuntimeException e) {
        try {
            String json = "{\"message\":" + e.toString() + " }";
            JSONObject parameter = new JSONObject(json);
            PluginResult dataResult = new PluginResult(PluginResult.Status.ERROR, parameter);
            dataResult.setKeepCallback(true);
            callbackContext.sendPluginResult(dataResult);
        } catch (JSONException exc) {
            Log.e(POCKETDUINO, exc.toString());
        }
    } catch (IOException e) {
        try {
            String json = "{\"message\":" + e.toString() + " }";
            JSONObject parameter = new JSONObject(json);
            PluginResult dataResult = new PluginResult(PluginResult.Status.ERROR, parameter);
            dataResult.setKeepCallback(true);
            callbackContext.sendPluginResult(dataResult);
        } catch (JSONException exc) {
            Log.e(POCKETDUINO, exc.toString());
        }
    } catch (JSONException e) {
        try {
            String json = "{\"message\":" + e.toString() + " }";
            JSONObject parameter = new JSONObject(json);
            PluginResult dataResult = new PluginResult(PluginResult.Status.ERROR, parameter);
            dataResult.setKeepCallback(true);
            callbackContext.sendPluginResult(dataResult);
        } catch (JSONException exc) {
            Log.e(POCKETDUINO, exc.toString());
        }
    }
}

From source file:com.physicaroid.pocketduino.cordova.PocketDuino.java

License:Apache License

private void listenData(CallbackContext callbackContext) {
    this.dataCallbackContext = callbackContext;
    if (mPhysicaloid.isOpened()) {
        mPhysicaloid.addReadListener(new ReadLisener() {
            // callback when reading one or more size buffer
            @Override/*from   ww  w  .jav  a 2s  . c  o  m*/
            public void onRead(int size) {
                byte[] buf = new byte[size];
                int readSize = mPhysicaloid.read(buf, size);
                //Log.d(POCKETDUINO, String.format("%02d ", size));
                if (readSize > 2) {
                    try {
                        String hexString = bytesToHex(buf);
                        String handlerName = "pocketduino.receive";
                        String json = "{\"handlerName\":" + handlerName + ", \"data\":" + hexString + " }";
                        //String json = "{\"handlerName\":" + handlerName + " }";
                        JSONObject parameter = new JSONObject(json);
                        PluginResult dataResult = new PluginResult(PluginResult.Status.OK, parameter);
                        dataResult.setKeepCallback(true);
                        dataCallbackContext.sendPluginResult(dataResult);
                    } catch (JSONException e) {
                        Log.e(POCKETDUINO, "Exception: " + e.getMessage());
                        dataCallbackContext.error(e.toString());
                    }
                }
            }
        });
    } else {
        PluginResult dataResult = new PluginResult(PluginResult.Status.ERROR);
        dataResult.setKeepCallback(true);
        this.dataCallbackContext.sendPluginResult(dataResult);
        this.dataCallbackContext = null;
    }
}

From source file:com.plugin.camera.ForegroundCameraLauncher.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.
 *//*w  ww  . j  a  v  a 2  s .  com*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    this.callbackContext = callbackContext;
    if (action.equals("takePicture")) {
        this.targetHeight = 0;
        this.targetWidth = 0;
        this.mQuality = 50;
        try {
            this.mQuality = args.getInt(1);
            this.targetWidth = args.getInt(3);
            this.targetHeight = args.getInt(4);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // If the user specifies a 0 or smaller width/height
        // make it -1 so later comparisons succeed
        if (this.targetWidth < 1) {
            this.targetWidth = -1;
        }
        if (this.targetHeight < 1) {
            this.targetHeight = -1;
        }
        this.takePicture();

        PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
        r.setKeepCallback(true);
        callbackContext.sendPluginResult(r);
        return true;
    }
    return false;
}