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.unit11apps.tts.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 {/*w w  w .  j a  v a 2  s .  co  m*/
        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();
            }
            callbackContext.sendPluginResult(new PluginResult(status, result));
        } else 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("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));
            }
        }
        return true;
    } catch (JSONException e) {
        e.printStackTrace();
        callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
    }
    return false;
}

From source file:com.unit11apps.tts.TTS.java

License:BSD License

/**
 * Called when the TTS service is initialized.
 *
 * @param status//  w  w w . ja  v  a2 s  . c  o  m
 */
public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        state = TTS.STARTED;
        PluginResult result = new PluginResult(PluginResult.Status.OK, TTS.STARTED);
        result.setKeepCallback(false);
        //this.success(result, this.startupCallbackId);
        this.startupCallbackContext.sendPluginResult(result);
        mTts.setOnUtteranceCompletedListener(this);
        //            Putting this code in hear as a place holder. When everything moves to API level 15 or greater
        //            we'll switch over to this way of trackign progress.
        //            mTts.setOnUtteranceProgressListener(new UtteranceProgressListener() {
        //
        //                @Override
        //                public void onDone(String utteranceId) {
        //                    Log.d(LOG_TAG, "got completed utterance");
        //                    PluginResult result = new PluginResult(PluginResult.Status.OK);
        //                    result.setKeepCallback(false);
        //                    callbackContext.sendPluginResult(result);        
        //                }
        //
        //                @Override
        //                public void onError(String utteranceId) {
        //                    Log.d(LOG_TAG, "got utterance error");
        //                    PluginResult result = new PluginResult(PluginResult.Status.ERROR);
        //                    result.setKeepCallback(false);
        //                    callbackContext.sendPluginResult(result);        
        //                }
        //
        //                @Override
        //                public void onStart(String utteranceId) {
        //                    Log.d(LOG_TAG, "started talking");
        //                }
        //                
        //            });
    } else if (status == TextToSpeech.ERROR) {
        state = TTS.STOPPED;
        PluginResult result = new PluginResult(PluginResult.Status.ERROR, TTS.STOPPED);
        result.setKeepCallback(false);
        this.startupCallbackContext.sendPluginResult(result);
    }
}

From source file:com.vidinoti.pixlive.PixLive.java

@Override
public void onCodesRecognized(ArrayList<com.vidinoti.android.vdarsdk.VDARCode> arrayList) {
    if (this.eventHandler != null) {
        for (VDARCode code : arrayList) {
            if (!code.isSpecialCode()) {
                JSONObject o = new JSONObject();

                try {
                    o.put("type", "codeRecognize");
                    o.put("codeType", getCodeTypeAsString(code.getCodeType()));
                    o.put("code", code.getCodeData());
                } catch (JSONException e) {

                }//from w w w.  jav  a  2  s. com

                PluginResult p = new PluginResult(PluginResult.Status.OK, o);
                p.setKeepCallback(true);

                try {
                    PixLive.this.eventHandler.sendPluginResult(p);
                } catch (Exception e) {
                    //To avoid webview crashes
                }
            }
        }
    }
}

From source file:com.vidinoti.pixlive.PixLive.java

@Override
public void onPresentAnnotations() {
    if (this.eventHandler != null) {
        JSONObject o = new JSONObject();

        try {//w ww  .  j  av a  2s . c  o m
            o.put("type", "presentAnnotations");
        } catch (JSONException e) {

        }

        PluginResult p = new PluginResult(PluginResult.Status.OK, o);
        p.setKeepCallback(true);

        try {
            PixLive.this.eventHandler.sendPluginResult(p);
        } catch (Exception e) {
            //To avoid webview crashes
        }
    }
}

From source file:com.vidinoti.pixlive.PixLive.java

@Override
public void onAnnotationsHidden() {
    if (this.eventHandler != null) {
        JSONObject o = new JSONObject();

        try {/*from  w w  w  .j a  va 2s. c  o m*/
            o.put("type", "hideAnnotations");
        } catch (JSONException e) {

        }

        PluginResult p = new PluginResult(PluginResult.Status.OK, o);
        p.setKeepCallback(true);

        try {
            PixLive.this.eventHandler.sendPluginResult(p);
        } catch (Exception e) {
            //To avoid webview crashes
        }
    }
}

From source file:com.vidinoti.pixlive.PixLive.java

@Override
public void onEnterContext(com.vidinoti.android.vdarsdk.VDARContext vdarContext) {
    if (this.eventHandler != null) {
        JSONObject o = new JSONObject();

        try {//from   w  ww .  j  ava2s .  c om
            o.put("type", "enterContext");
            o.put("context", vdarContext.getRemoteID());
        } catch (JSONException e) {

        }

        PluginResult p = new PluginResult(PluginResult.Status.OK, o);
        p.setKeepCallback(true);

        try {
            PixLive.this.eventHandler.sendPluginResult(p);
        } catch (Exception e) {
            //To avoid webview crashes
        }
    }
}

From source file:com.vidinoti.pixlive.PixLive.java

@Override
public void onExitContext(com.vidinoti.android.vdarsdk.VDARContext vdarContext) {
    if (this.eventHandler != null) {
        JSONObject o = new JSONObject();

        try {// w w w  .  j av  a 2  s  . c  om
            o.put("type", "exitContext");
            o.put("context", vdarContext.getRemoteID());
        } catch (JSONException e) {

        }

        PluginResult p = new PluginResult(PluginResult.Status.OK, o);
        p.setKeepCallback(true);

        try {
            PixLive.this.eventHandler.sendPluginResult(p);
        } catch (Exception e) {
            //To avoid webview crashes
        }
    }
}

From source file:com.vidinoti.pixlive.PixLive.java

@Override
public void onReceiveContentEvent(final String eventName, final String eventParams) {
    if (this.eventHandler != null) {
        JSONObject o = new JSONObject();

        try {/*from   w  w  w . j a  va2 s  .  c o m*/
            o.put("type", "eventFromContent");
            o.put("eventName", eventName);
            o.put("eventParams", eventParams);
        } catch (JSONException e) {

        }

        PluginResult p = new PluginResult(PluginResult.Status.OK, o);
        p.setKeepCallback(true);

        try {
            PixLive.this.eventHandler.sendPluginResult(p);
        } catch (Exception e) {
            //To avoid webview crashes
        }
    }
}

From source file:com.vidinoti.pixlive.PixLive.java

@Override
public void onSensorTriggered(Sensor sensor, VDARContext context) {
    if (this.eventHandler != null) {
        JSONObject o = new JSONObject();

        try {//  www  . j  a va2s .c om
            o.put("type", sensor.isTriggered() ? "sensorTriggered" : "sensorUntriggered");
            o.put("sensorId", sensor.getSensorId());
            o.put("sensorType", sensor.getType());
            o.put("context", createJSONForContext(context));

            if (sensor.isTriggered()) {
                if (sensor instanceof VidiBeaconSensor) {
                    o.put("rssi", ((VidiBeaconSensor) sensor).getRssi());
                } else if (sensor instanceof IBeaconSensor) {
                    o.put("rssi", ((IBeaconSensor) sensor).getRssi());
                    o.put("distance", ((IBeaconSensor) sensor).getDistance());
                }
            }

        } catch (JSONException e) {

        }

        PluginResult p = new PluginResult(PluginResult.Status.OK, o);
        p.setKeepCallback(true);

        try {
            PixLive.this.eventHandler.sendPluginResult(p);
        } catch (Exception e) {
            //To avoid webview crashes
        }
    }
}

From source file:com.vidinoti.pixlive.PixLive.java

@Override
public void onSensorUpdated(Sensor sensor, VDARContext context) {
    if (this.eventHandler != null) {

        JSONObject o = new JSONObject();

        try {//from  w w w. ja  v a 2s.  c  o m
            o.put("type", "sensorUpdate");
            o.put("sensorId", sensor.getSensorId());
            o.put("sensorType", sensor.getType());
            o.put("context", createJSONForContext(context));

            if (sensor instanceof VidiBeaconSensor) {
                o.put("rssi", ((VidiBeaconSensor) sensor).getRssi());
            } else if (sensor instanceof IBeaconSensor) {
                o.put("rssi", ((IBeaconSensor) sensor).getRssi());
                o.put("distance", ((IBeaconSensor) sensor).getDistance());
            }
        } catch (JSONException e) {

        }

        PluginResult p = new PluginResult(PluginResult.Status.OK, o);
        p.setKeepCallback(true);

        try {
            PixLive.this.eventHandler.sendPluginResult(p);
        } catch (Exception e) {
            //To avoid webview crashes
        }
    }
}