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.manueldeveloper.VolumeButtonsListener.java

License:Apache License

/**
*    Method which executes the Javascript request
*
*   @param      action: String object with the action to execute
*   @param      args: JSONArray object with the arguments of the request
*   @param      callbackContext: CallbackContext object for call back into Javascript
*
*   @return      "boolean" which indicates if the action is valid (true) or not (false)
* 
*    @date      10/12/2016//from   ww  w .  java 2 s .c o m
*    @version   0.0.3
*    @author   ManuelDeveloper(manueldeveloper@gmail.com) 
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

    // Check the action
    if (action.equals("start")) {

        // Check if the plugin is listening the volume button events
        if (this.volumeCallbackContext != null) {

            callbackContext.error("Volume buttons listener already running");
            return true;
        }

        // Get the reference to the callbacks and start the listening process
        this.volumeCallbackContext = callbackContext;
        this.webView.getView().setOnKeyListener(this);

        // Don't return any result now
        PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT);
        pluginResult.setKeepCallback(true);
        this.volumeCallbackContext.sendPluginResult(pluginResult);
        return true;
    } else if (action.equals("stop")) {

        // Erase the callbacks reference and stop the listening process
        sendSignal(new JSONObject(), false); // release status callback in Javascript side
        this.volumeCallbackContext = null;
        this.webView.getView().setOnKeyListener(null);
        callbackContext.success();
        return true;
    }

    return false;
}

From source file:com.manueldeveloper.VolumeButtonsListener.java

License:Apache License

/**
*    Method which sends back a new PluginResult to Javascript
*
*   @param      info: JSONObject object with the information to send back
*   @param      keepCallback: boolean which indicates if there will be more results
* 
*    @date      27/02/2014//from  w ww  .  j  av a  2s .c om
*    @version   0.0.1
*    @author   ManuelDeveloper(manueldeveloper@gmail.com) 
*/
private void sendSignal(JSONObject info, boolean keepCallback) {
    if (this.volumeCallbackContext != null) {
        PluginResult result = new PluginResult(PluginResult.Status.OK, info);
        result.setKeepCallback(keepCallback);
        this.volumeCallbackContext.sendPluginResult(result);
    }
}

From source file:com.marianhello.cordova.bgloc.BackgroundGpsPlugin.java

License:Apache License

private void handleMessage(Intent msg) {
    Bundle data = msg.getExtras();/*www.j  a v  a 2 s .  c  o  m*/
    switch (data.getInt(Constant.COMMAND, 0)) {
    case Constant.UPDATE_PROGRESS:
        try {
            JSONObject location = new JSONObject(data.getString(Constant.DATA));
            PluginResult result = new PluginResult(PluginResult.Status.OK, location);
            result.setKeepCallback(true);
            callbackContext.sendPluginResult(result);
            Log.d(TAG, "Sending plugin result");
        } catch (JSONException e) {
            Log.w(TAG, "Error converting message to json");
        }
        break;
    default:
        break;
    }
}

From source file:com.mattrayner.vuforia.VuforiaPlugin.java

public static void sendImageFoundUpdate(String imageName) {
    Log.d(LOGTAG, "Attempting to send an update for image: " + imageName);

    // Create an object to hold our response
    JSONObject jsonObj = new JSONObject();

    // Try to build a JSON response to send to Cordova
    try {/*from  ww  w  . ja va 2s.  c o  m*/
        JSONObject jsonStatus = new JSONObject();
        jsonStatus.put("imageFound", true);
        jsonStatus.put("message", "An image was found.");

        JSONObject jsonResult = new JSONObject();
        jsonResult.put("imageName", imageName);

        jsonObj.put("status", jsonStatus);
        jsonObj.put("result", jsonResult);
    } catch (JSONException e) {
        Log.d(LOGTAG, "JSON ERROR: " + e);
    }

    // Build our response
    PluginResult result = new PluginResult(PluginResult.Status.OK, jsonObj);
    result.setKeepCallback(true); // Don't clean up our callback (we intend on sending more messages to it)

    // Send the result to our PERSISTANT callback
    persistantVuforiaStartCallback.sendPluginResult(result);
}

From source file:com.megster.cordova.rfduino.Peripheral.java

License:Apache License

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.d(TAG, "gatt " + gatt);
    Log.d(TAG, "status " + status);
    super.onServicesDiscovered(gatt, status);

    BluetoothGattService service = gatt.getService(RFDUINO_SERVICE_UUID);
    Log.d(TAG, "service " + service);

    BluetoothGattCharacteristic receiveCharacteristic = service.getCharacteristic(RECEIVE_CHARACTERISTIC_UUID);
    sendCharacteristic = service.getCharacteristic(SEND_CHARACTERISTIC_UUID);
    disconnectCharacteristic = service.getCharacteristic(DISCONNECT_CHARACTERISTIC_UUID);

    if (receiveCharacteristic != null) {
        gatt.setCharacteristicNotification(receiveCharacteristic, true);

        BluetoothGattDescriptor receiveConfigDescriptor = receiveCharacteristic
                .getDescriptor(CLIENT_CHARACTERISTIC_CONFIGURATION_UUID);
        if (receiveConfigDescriptor != null) {
            receiveConfigDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            gatt.writeDescriptor(receiveConfigDescriptor);
        } else {// w ww  . ja va2 s .  c  o  m
            LOG.e(TAG, "Receive Characteristic can not be configured.");
        }
    } else {
        LOG.e(TAG, "Receive Characteristic is missing.");
    }

    // call the success callback for connect
    PluginResult result = new PluginResult(PluginResult.Status.OK);
    result.setKeepCallback(true);
    connectCallback.sendPluginResult(result);
}

From source file:com.megster.cordova.rfduino.Peripheral.java

License:Apache License

@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

    super.onCharacteristicChanged(gatt, characteristic);
    if (characteristic.getUuid().equals(RECEIVE_CHARACTERISTIC_UUID)) {
        PluginResult result = new PluginResult(PluginResult.Status.OK, characteristic.getValue());
        result.setKeepCallback(true);
        onDataCallback.sendPluginResult(result);
    }//from w  w w.  j  ava 2 s . c o  m
}

From source file:com.megster.cordova.rfduino.RFduinoPlugin.java

License:Apache License

private void connect(CallbackContext callbackContext, String uuid) {

    Peripheral peripheral = peripherals.get(uuid); // note uuid is mac address on android
    BluetoothDevice device = peripheral.getDevice();
    peripheral.setConnectCallback(callbackContext);
    gatt = device.connectGatt(cordova.getActivity(), false, peripheral);

    activePeripheral = peripheral;//from  w  w  w  .j ava  2s  .c o  m

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

From source file:com.megster.cordova.rfduino.RFduinoPlugin.java

License:Apache License

private void registerOnDataCallback(CallbackContext callbackContext) {

    // TODO this should accept a MAC address and allow a listener to be added to
    // any device, not just the active device. Requires JS and ObjC updates.

    if (activePeripheral != null) {
        activePeripheral.setOnDataCallback(callbackContext);

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

    } else {// w  w w  .ja  v  a2s . c o m

        callbackContext.error("No connected device");

    }

}

From source file:com.megster.cordova.rfduino.RFduinoPlugin.java

License:Apache License

private void findLowEnergyDevices(CallbackContext callbackContext, int scanSeconds) {

    // TODO skip if currently scanning
    peripherals.clear();/*from www  . ja  va  2  s.  c  o  m*/

    discoverCallback = callbackContext;
    bluetoothAdapter.startLeScan(new UUID[] { Peripheral.RFDUINO_SERVICE_UUID }, this);

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

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

From source file:com.megster.cordova.rfduino.RFduinoPlugin.java

License:Apache License

@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {

    String address = device.getAddress();

    if (!peripherals.containsKey(address)) {

        Peripheral peripheral = new Peripheral(device, rssi, scanRecord);
        peripherals.put(device.getAddress(), peripheral);

        if (discoverCallback != null) {
            PluginResult result = new PluginResult(PluginResult.Status.OK, peripheral.asJSONObject());
            result.setKeepCallback(true);
            discoverCallback.sendPluginResult(result);
        }/*from   w  w w .ja v  a 2s . c  o  m*/

    } else {
        // this isn't necessary
        Peripheral peripheral = peripherals.get(address);
        peripheral.updateRssi(rssi);
    }

}