List of usage examples for org.apache.cordova.api CallbackContext getCallbackId
public String getCallbackId()
From source file:com.Trigger.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 {//from w w w . ja v a 2s.co 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); startupCallbackContext.sendPluginResult(pluginResult); } else if (action.equals("shutdown")) { if (mTts != null) { mTts.shutdown(); } 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; }