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.dtworkshop.inappcrossbrowser.WebViewBrowser.java

License:Apache License

/**
 * Create a new plugin result and send it back to JavaScript
 *
 * @param obj a JSONObject contain event payload information
 * @param status the status code to return to the JavaScript environment
 *///ww  w. j  a v a2  s.com
protected void sendUpdate(JSONObject obj, boolean keepCallback, PluginResult.Status status) {
    if (callbackContext != null) {
        PluginResult result = new PluginResult(status, obj);
        result.setKeepCallback(keepCallback);
        callbackContext.sendPluginResult(result);
        if (!keepCallback) {
            callbackContext = null;
        }
    }
}

From source file:com.dtz.plugins.azurehubnotification.AzureHubNotification.java

License:Apache License

protected void sendNotificationCallback(CallbackContext callbackContext, String result, Status status) {
    PluginResult progressResult = new PluginResult(status, result);
    progressResult.setKeepCallback(true);
    callbackContext.sendPluginResult(progressResult);
}

From source file:com.ecor.MDNS.java

License:Open Source License

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    Log.d(TAG, "Executing an action...");
    this.callback = callbackContext;

    Log.d(TAG, "Action called: " + action);
    Log.d(TAG, args.toString());/*from  ww  w.ja v a 2  s. c  o  m*/
    if (action.equals("macaddress")) {
        Log.d(TAG, "Mac Address: " + macaddress);
        final CallbackContext cb = callbackContext;
        cordova.getThreadPool().execute(new Runnable() {
            public void run() {
                JSONObject json = new JSONObject();
                try {
                    json.put("action", new String("macaddress"));
                    json.put("address", macaddress);
                    PluginResult result = new PluginResult(PluginResult.Status.OK, json);
                    result.setKeepCallback(true);
                    cb.sendPluginResult(result);
                } catch (Exception e) {
                    e.printStackTrace();
                    cb.error("JSON Error retrieving Mac Address.");
                }
            }
        });
    } else if (action.equals("monitor")) {
        final String type = args.optString(0);
        if (type != null) {
            Log.d(TAG, "Monitor type: " + type);
            cordova.getThreadPool().execute(new Runnable() {
                public void run() {
                    watch(type); // Thread-safe.
                }
            });
        } 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 {
        Log.e(TAG, "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.ecor.MDNS.java

License:Open Source License

public void sendCallback(String action, ServiceInfo info) {
    JSONObject status = new JSONObject();
    try {/*from   w  w w  .j ava2 s .c  o  m*/
        status.put("action", action);
        status.put("service", jsonifyService(info));
        Log.d(TAG, "Sending result: " + status.toString());

        PluginResult result = new PluginResult(PluginResult.Status.OK, status);

        result.setKeepCallback(true);
        callback.sendPluginResult(result);

    } catch (JSONException e) {

        e.printStackTrace();
    }

}

From source file:com.emesonsantana.cordova.pedometer.PedometerListener.java

License:Open Source License

/**
 * Executes the request./*w ww  .  ja  va2  s  .c  o  m*/
 *
 * @param action the action to execute.
 * @param args the exec() arguments.
 * @param callbackContext the callback context used when calling back into JavaScript.
 * @return whether the action was valid.
 */
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
    this.callbackContext = callbackContext;

    if (action.equals("isStepCountingAvailable")) {
        Sensor stepCounter = this.sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        Sensor accel = this.sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (accel != null || stepCounter != null) {
            this.win(true);
            return true;
        } else {
            this.setStatus(PedometerListener.ERROR_NO_SENSOR_FOUND);
            this.win(false);
            return true;
        }
    } else if (action.equals("isDistanceAvailable")) {
        //distance is never available in Android
        this.win(false);
        return true;
    } else if (action.equals("isFloorCountingAvailable")) {
        //floor counting is never available in Android
        this.win(false);
        return true;
    } else if (action.equals("startPedometerUpdates")) {
        if (this.status != PedometerListener.RUNNING) {
            // If not running, then this is an async call, so don't worry about waiting
            // We drop the callback onto our stack, call start, and let start and the sensor callback fire off the callback down the road
            this.start();
        }
        PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT, "");
        result.setKeepCallback(true);
        callbackContext.sendPluginResult(result);
        return true;
    } else if (action.equals("stopPedometerUpdates")) {
        if (this.status == PedometerListener.RUNNING) {
            this.stop();
        }
        this.win(null);
        return true;
    } else {
        // Unsupported action
        return false;
    }
}

From source file:com.emesonsantana.cordova.pedometer.PedometerListener.java

License:Open Source License

private void win(JSONObject message) {
    // Success return object
    PluginResult result;
    if (message != null)
        result = new PluginResult(PluginResult.Status.OK, message);
    else/* ww w . j a v  a2  s  .co  m*/
        result = new PluginResult(PluginResult.Status.OK);

    result.setKeepCallback(true);
    callbackContext.sendPluginResult(result);
}

From source file:com.emesonsantana.cordova.pedometer.PedometerListener.java

License:Open Source License

private void win(boolean success) {
    // Success return object
    PluginResult result;
    result = new PluginResult(PluginResult.Status.OK, success);

    result.setKeepCallback(true);
    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();) {//from  w w w. j a  v a  2  s  .  com
        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.BLECentralPlugin.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  www. j  a  va2  s. c o  m*/

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

    // TODO offer option to return duplicates

}

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;/*from  w  w w. j  ava  2  s . co m*/
    gatt = device.connectGatt(activity, false, this);

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