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) 

Source Link

Usage

From source file:com.dileepindia.cordova.sms.SMSPlugin.java

public boolean execute(String action, JSONArray inputs, CallbackContext callbackContext) throws JSONException {
    PluginResult result = null;// w w w. j  a  va2  s . c  om

    if ("startWatch".equals(action)) {
        result = startWatch(callbackContext);
    } else if ("stopWatch".equals(action)) {
        result = stopWatch(callbackContext);
    } else if ("enableIntercept".equals(action)) {
        boolean on_off = inputs.optBoolean(0);
        result = enableIntercept(on_off, callbackContext);
    } else if ("deleteSMS".equals(action)) {
        JSONObject msg = inputs.optJSONObject(0);
        result = deleteSMS(msg, callbackContext);
    } else if ("restoreSMS".equals(action)) {
        JSONArray smsList = inputs.optJSONArray(0);
        result = restoreSMS(smsList, callbackContext);
    } else if ("listSMS".equals(action)) {
        JSONObject filters = inputs.optJSONObject(0);
        result = listSMS(filters, callbackContext);
    }

    //******************************************************
    else if ("sendSMS".equals(action)) {
        String Phone = inputs.optString(0);
        String mess = inputs.optString(1);
        String method = inputs.optString(2);
        result = sendSMS(Phone, mess, method, callbackContext);
    }

    //********************************************************

    else {
        Log.d("SMSPlugin", String.format("Invalid action passed: %s", new Object[] { action }));
        result = new PluginResult(PluginResult.Status.INVALID_ACTION);
    }

    if (result != null) {
        callbackContext.sendPluginResult(result);
    }
    return true;
}

From source file:com.dileepindia.cordova.sms.SMSPlugin.java

private PluginResult sendSMS(String Phone, String messages, String methods, CallbackContext callbackContext) {
    String phoneNumber = Phone;/* w w  w.j a va  2s  .c o m*/
    String message = messages;
    String method = methods;
    SmsSender smsSender = new SmsSender(this.cordova.getActivity());
    if (method.equalsIgnoreCase("INTENT")) {
        smsSender.invokeSMSIntent(phoneNumber, message);
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.NO_RESULT));
    } else {
        smsSender.sendSMS(phoneNumber, message);
    }

    callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK));

    // TODO Auto-generated method stub
    return null;
}

From source file:com.doylestowncoder.plugin.mobileaccessibility.MobileAccessibility.java

License:Apache License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    try {/*ww w  . j a  v  a2 s . c  o  m*/
        if (action.equals("isScreenReaderRunning")) {
            isScreenReaderRunning(callbackContext);
            return true;
        } else if (action.equals("isClosedCaptioningEnabled")) {
            isClosedCaptioningEnabled(callbackContext);
            return true;
        } else if (action.equals("isTouchExplorationEnabled")) {
            isTouchExplorationEnabled(callbackContext);
            return true;
        } else if (action.equals("postNotification")) {
            if (args.length() > 1) {
                String string = args.getString(1);
                if (!string.isEmpty()) {
                    announceForAccessibility(string, callbackContext);
                }
            }
            return true;
        } else if (action.equals("getTextZoom")) {
            getTextZoom(callbackContext);
            return true;
        } else if (action.equals("setTextZoom")) {
            if (args.length() > 0) {
                double textZoom = args.getDouble(0);
                if (textZoom > 0) {
                    setTextZoom(textZoom, callbackContext);
                }
            }
            return true;
        } else if (action.equals("updateTextZoom")) {
            updateTextZoom(callbackContext);
            return true;
        } else if (action.equals("start")) {
            start(callbackContext);
            return true;
        } else if (action.equals("stop")) {
            stop();
            return true;
        }
    } catch (JSONException e) {
        e.printStackTrace();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return false;
}

From source file:com.dude.plugins.sms.SMSPlugin.java

private void executeHelper() {
    PluginResult result = null;/*from   w  w  w  .j  a va2 s  .  c  o  m*/
    if (ACTION_SET_OPTIONS.equals(action)) {
        JSONObject options = inputs.optJSONObject(0);
        this.setOptions(options);
        result = new PluginResult(PluginResult.Status.OK);
    } else if (ACTION_START_WATCH.equals(action)) {
        result = this.startWatch(callbackContext);
    } else if (ACTION_STOP_WATCH.equals(action)) {
        result = this.stopWatch(callbackContext);
    } else if (ACTION_ENABLE_INTERCEPT.equals(action)) {
        boolean on_off = inputs.optBoolean(0);
        result = this.enableIntercept(on_off, callbackContext);
    } else if (ACTION_DELETE_SMS.equals(action)) {
        JSONObject msg = inputs.optJSONObject(0);
        result = this.deleteSMS(msg, callbackContext);
    } else if (ACTION_RESTORE_SMS.equals(action)) {
        JSONArray smsList = inputs.optJSONArray(0);
        result = this.restoreSMS(smsList, callbackContext);
    } else if (ACTION_LIST_SMS.equals(action)) {
        JSONObject filters = inputs.optJSONObject(0);
        result = this.listSMS(filters, callbackContext);
    } else if (ACTION_SEND_SMS.equals(action)) {
        JSONArray addressList = inputs.optJSONArray(0);
        String message = inputs.optString(1);
        result = this.sendSMS(addressList, message, callbackContext);
    } else {
        Log.d(LOGTAG, String.format("Invalid action passed: %s", action));
        result = new PluginResult(PluginResult.Status.INVALID_ACTION);
    }
    if (result != null) {
        callbackContext.sendPluginResult(result);
    }
}

From source file:com.emeth.cordova.ble.central.BLECentralPlugin.java

License:Apache License

private void findLowEnergyDevices(CallbackContext callbackContext, UUID[] serviceUUIDs, int scanSeconds) {

    // TODO skip if currently scanning

    // clear non-connected cached peripherals
    for (Iterator<Map.Entry<String, Peripheral>> iterator = peripherals.entrySet().iterator(); iterator
            .hasNext();) {// ww w .j  av  a  2 s .  c  o  m
        Map.Entry<String, Peripheral> entry = iterator.next();
        if (!entry.getValue().isConnected()) {
            iterator.remove();
        }
    }

    discoverCallback = callbackContext;

    if (serviceUUIDs.length > 0) {
        bluetoothAdapter.startLeScan(serviceUUIDs, this);
    } else {
        bluetoothAdapter.startLeScan(this);
    }

    if (scanSeconds > 0) {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                LOG.d(TAG, "Stopping Scan");
                BLECentralPlugin.this.bluetoothAdapter.stopLeScan(BLECentralPlugin.this);
            }
        }, scanSeconds * 1000);
    }

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

From source file:com.emeth.cordova.ble.central.Peripheral.java

License:Apache License

public void connect(CallbackContext callbackContext, Activity activity) {
    BluetoothDevice device = getDevice();
    connectCallback = callbackContext;//  www  . j  av a2 s.c  om
    gatt = device.connectGatt(activity, false, this);

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

From source file:com.emeth.cordova.ble.central.Peripheral.java

License:Apache License

private void queueCommand(BLECommand command) {
    LOG.d(TAG, "Queuing Command " + command);
    commandQueue.add(command);/*from w w w .j  a v a2 s.c  om*/

    PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
    result.setKeepCallback(true);
    command.getCallbackContext().sendPluginResult(result);

    if (!bleProcessing) {
        processCommands();
    }
}

From source file:com.example.plugin.PhoneListener.java

License:Open Source License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {

    if (action.equals("startMonitoringPhoneState")) {
        this.phoneListenerCallbackId = callbackContext;
        PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
        pluginResult.setKeepCallback(true);
        callbackContext.sendPluginResult(pluginResult);
        return true;
    } else if (action.equals("stopMonitoringPhoneState")) {
        removePhoneListener();/*from  w  w w . java2  s. c om*/
        this.updatePhoneState("", false); // release status callback
        this.phoneListenerCallbackId = null;
        PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
        callbackContext.sendPluginResult(pluginResult);
    }

    return false; // no valid action called
}

From source file:com.facilityhero.plugin.camera.CameraLauncher.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  a v  a 2s  .c  om*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
    this.callbackContext = callbackContext;

    if (action.equals("takePicture")) {
        int srcType = CAMERA;
        int destType = FILE_URI;
        this.saveToPhotoAlbum = false;
        this.targetHeight = 0;
        this.targetWidth = 0;
        this.encodingType = JPEG;
        this.mediaType = PICTURE;
        this.mQuality = 80;

        this.mQuality = args.getInt(0);
        destType = args.getInt(1);
        srcType = args.getInt(2);
        this.targetWidth = args.getInt(3);
        this.targetHeight = args.getInt(4);
        this.encodingType = args.getInt(5);
        this.mediaType = args.getInt(6);
        //this.allowEdit = args.getBoolean(7); // This field is unused.
        this.correctOrientation = args.getBoolean(8);
        this.saveToPhotoAlbum = args.getBoolean(9);

        // 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;
        }

        try {
            if (srcType == CAMERA) {
                this.takePicture(destType, encodingType);
            } else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
                this.getImage(srcType, destType);
            }
        } catch (IllegalArgumentException e) {
            callbackContext.error("Illegal Argument Exception");
            PluginResult r = new PluginResult(PluginResult.Status.ERROR);
            callbackContext.sendPluginResult(r);
            return true;
        }

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

        return true;
    }
    return false;
}

From source file:com.firerunner.cordova.TTS.java

License:BSD License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    PluginResult.Status status = PluginResult.Status.OK;
    String result = "";
    this.callbackContext = callbackContext;

    try {/* ww  w  . j  a  v a 2  s  .c  o m*/
        if (action.equals("speak")) {
            String text = args.getString(0);
            if (isReady()) {
                HashMap<String, String> map = null;
                map = new HashMap<String, String>();
                map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackContext.getCallbackId());
                mTts.speak(text, TextToSpeech.QUEUE_ADD, map);
                PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
                pr.setKeepCallback(true);
                callbackContext.sendPluginResult(pr);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("interrupt")) {
            String text = args.getString(0);
            if (isReady()) {
                HashMap<String, String> map = null;
                map = new HashMap<String, String>();
                //map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackId);
                mTts.speak(text, TextToSpeech.QUEUE_FLUSH, map);
                PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
                pr.setKeepCallback(true);
                callbackContext.sendPluginResult(pr);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("stop")) {
            if (isReady()) {
                mTts.stop();
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("silence")) {
            if (isReady()) {
                HashMap<String, String> map = null;
                map = new HashMap<String, String>();
                map.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, callbackContext.getCallbackId());
                mTts.playSilence(args.getLong(0), TextToSpeech.QUEUE_ADD, map);
                PluginResult pr = new PluginResult(PluginResult.Status.NO_RESULT);
                pr.setKeepCallback(true);
                callbackContext.sendPluginResult(pr);
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("speed")) {
            if (isReady()) {
                float speed = (float) (args.optLong(0, 100)) / (float) 100.0;
                mTts.setSpeechRate(speed);
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("pitch")) {
            if (isReady()) {
                float pitch = (float) (args.optLong(0, 100)) / (float) 100.0;
                mTts.setPitch(pitch);
                callbackContext.sendPluginResult(new PluginResult(status, result));
            } else {
                JSONObject error = new JSONObject();
                error.put("message", "TTS service is still initialzing.");
                error.put("code", TTS.INITIALIZING);
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, error));
            }
        } else if (action.equals("startup")) {

            this.startupCallbackContext = callbackContext;
            if (mTts == null) {
                state = TTS.INITIALIZING;
                mTts = new TextToSpeech(cordova.getActivity().getApplicationContext(), this);
                PluginResult pluginResult = new PluginResult(status, TTS.INITIALIZING);
                pluginResult.setKeepCallback(true);
                // do not send this as onInit is more reliable: domaemon
                // startupCallbackContext.sendPluginResult(pluginResult);
            } else {
                PluginResult pluginResult = new PluginResult(status, TTS.INITIALIZING);
                pluginResult.setKeepCallback(true);
                startupCallbackContext.sendPluginResult(pluginResult);
            }
        } else if (action.equals("shutdown")) {
            if (mTts != null) {
                mTts.shutdown();
                mTts = null;
            }
            callbackContext.sendPluginResult(new PluginResult(status, result));
        } else if (action.equals("getLanguage")) {
            if (mTts != null) {
                result = mTts.getLanguage().toString();
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        } else if (action.equals("isLanguageAvailable")) {
            if (mTts != null) {
                Locale loc = new Locale(args.getString(0));
                int available = mTts.isLanguageAvailable(loc);
                result = (available < 0) ? "false" : "true";
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        } else if (action.equals("setLanguage")) {
            if (mTts != null) {
                Locale loc = new Locale(args.getString(0));
                int available = mTts.setLanguage(loc);
                result = (available < 0) ? "false" : "true";
                callbackContext.sendPluginResult(new PluginResult(status, result));
            }
        }
        return true;
    } catch (JSONException e) {
        e.printStackTrace();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return false;
}