public static void sendError(String message) { PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, message); pluginResult.setKeepCallback(true); if (pushContext != null) { pushContext.sendPluginResult(pluginResult); } }
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { if (action.equals("getBluetoothMacAddress")) { String macAddress = this.getBluetoothMacAddress(); if (macAddress != null) { JSONObject JSONresult = new JSONObject(); try { JSONresult.put("mac", macAddress); PluginResult r = new PluginResult(PluginResult.Status.OK, JSONresult); callbackContext.success(macAddress); r.setKeepCallback(true); callbackContext.sendPluginResult(r); return true; } catch (JSONException jsonEx) { PluginResult r = new PluginResult(PluginResult.Status.JSON_EXCEPTION); callbackContext.error("error"); r.setKeepCallback(true); callbackContext.sendPluginResult(r); return true; } } } return false; }
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { PluginResult.Status status = PluginResult.Status.OK; String result = ""; this.callbackContext = callbackContext; try { if (action.equals("takePicture")) { this.targetHeight = 0; this.targetWidth = 0; this.mQuality = 80; this.targetHeight = args.getInt(4); this.targetWidth = args.getInt(3); this.mQuality = args.getInt(0); this.takePicture(); PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); r.setKeepCallback(true); callbackContext.sendPluginResult(r); return true; } return false; } catch (JSONException e) { e.printStackTrace(); callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION)); return true; } }
public static void sendEvent(JSONObject _json) { PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, _json); pluginResult.setKeepCallback(true); if (pushContext != null) { pushContext.sendPluginResult(pluginResult); } }
/** 插件主入口 */ @Override public boolean execute(String action, final JSONArray args, CallbackContext callbackContext) throws JSONException { LOG.d(LOG_TAG, "BaiduPush#execute"); boolean ret = false; if ("getCurrentPosition".equalsIgnoreCase(action)) { cbCtx = callbackContext; PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT); pluginResult.setKeepCallback(true); cbCtx.sendPluginResult(pluginResult); if (mLocationClient == null) { mLocationClient = new LocationClient(this.webView.getContext()); mLocationClient.registerLocationListener(myListener); // 配置定位SDK参数 initLocation(); } mLocationClient.start(); ret = true; } return ret; }
/** Send a new plugin result with no result. Use to keep callback channel open for events */ private void sendNoResult() { if (this.eventCallbackContext != null) { PluginResult pluginResult = new PluginResult(PluginResult.Status.NO_RESULT); pluginResult.setKeepCallback(true); this.eventCallbackContext.sendPluginResult(pluginResult); } }
public void onCallStateChanged(int state, String incomingNumber) { super.onCallStateChanged(state, incomingNumber); if (callbackContext == null) return; String msg = ""; switch (state) { case TelephonyManager.CALL_STATE_IDLE: msg = "IDLE"; Log.d(TAG, "IDLE"); break; case TelephonyManager.CALL_STATE_OFFHOOK: msg = "OFFHOOK"; Log.d(TAG, "OFFHOOK"); break; case TelephonyManager.CALL_STATE_RINGING: msg = "RINGING"; Log.d(TAG, "RINGING"); break; } PluginResult result = new PluginResult(PluginResult.Status.OK, msg); result.setKeepCallback(true); callbackContext.sendPluginResult(result); Log.d(TAG, "Result: " + result); }
/** * Send a new plugin result back to JavaScript, without closing callback * * @param data InBeacon event result containing message and extras */ private void sendUpdate(JSONObject data) { if (this.eventCallbackContext != null) { PluginResult result = new PluginResult(PluginResult.Status.OK, data); result.setKeepCallback(true); this.eventCallbackContext.sendPluginResult(result); } }
@Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { this.callbackContext = callbackContext; Log.d("WIFI", action); if(ACTION_START.equals(action)){ PluginResult pgRes = new PluginResult(PluginResult.Status.OK, "Registered"); pgRes.setKeepCallback(true); mWifiManager = (WifiManager) this.cordova.getActivity().getSystemService(Context.WIFI_SERVICE); this.reciever = new BroadcastReceiver() { @Override public void onReceive(Context c, Intent intent) { List<ScanResult> scanResults = mWifiManager.getScanResults(); handleResults(scanResults); } }; this.cordova.getActivity().registerReceiver(this.reciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)); callbackContext.sendPluginResult(pgRes); Log.d("WIFI", "inside " + action); return true; } else if (ACTION_SCAN.equals(action)){ PluginResult pgRes = new PluginResult(PluginResult.Status.OK, []); pgRes.setKeepCallback(true); mWifiManager = (WifiManager) this.cordova.getActivity().getSystemService(Context.WIFI_SERVICE); mWifiManager.startScan(); callbackContext.sendPluginResult(pgRes); Log.d("WIFI", "inside " + action); return true; } else if (ACTION_STOP.equals(action)){
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if (action.equals("start")) { this.callbackContext = callbackContext; if (this.status != Magnetometer.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(); } } else if (action.equals("stop")) { if (this.status == Magnetometer.RUNNING) { this.stop(); } } else { // Unsupported action return false; } // Only actions are start and stop. Start is asynchronous, but has no // return value. Stop has no return value PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT, ""); result.setKeepCallback(true); callbackContext.sendPluginResult(result); return true; }
private void performNotificationClicked(String offerData) { // These lines can be reused anywhere in your app to send data to the javascript PluginResult result = new PluginResult(PluginResult.Status.OK, offerData); result.setKeepCallback( true); // This is the important part that allows executing the callback more than once, // change to false if you want the callbacks to stop firing this.webView.sendPluginResult(result, callbackIds.get(ACTION_SETONNOTIFICATIONCLICKEDCALLBACK)); }
/** * Create a new plugin result and send it back to JavaScript * * @param connection the network info to set as navigator.connection */ private void sendUpdate(String type) { if (connectionCallbackContext != null) { PluginResult result = new PluginResult(PluginResult.Status.OK, type); result.setKeepCallback(true); connectionCallbackContext.sendPluginResult(result); } webView.postMessage("networkconnection", type); }
public void setPushReceiveCallbackChannel(JSONArray data, CallbackContext callbackContext) { Log.i(TAG, "setPushReceiveCallbackChannel:" + callbackContext.getCallbackId()); mJPushReceiveCallback = callbackContext; PluginResult dataResult = new PluginResult(PluginResult.Status.OK, "js call init ok"); dataResult.setKeepCallback(true); // 必要 mJPushReceiveCallback.sendPluginResult(dataResult); }
protected void sendMobileAccessibilityStatusChangedCallback() { if (this.mCallbackContext != null) { PluginResult result = new PluginResult(PluginResult.Status.OK, getMobileAccessibilityStatus()); result.setKeepCallback(true); this.mCallbackContext.sendPluginResult(result); } }
private void sendNoResultPluginResult(CallbackContext callbackContext) { // save current callback context currentCallbackContext = callbackContext; // send no result and keep callback PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT); result.setKeepCallback(true); callbackContext.sendPluginResult(result); }
/** * 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 */ private 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; } } }
/** * 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. */ 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; }
void onReceivePushMessage(JSONObject jsonData) { Log.i(TAG, "onReceivePushMessage"); if (mJPushReceiveCallback == null) { Log.i(TAG, "mJPushReceiveCallback is null"); return; } PluginResult dataResult = new PluginResult(PluginResult.Status.OK, jsonData); dataResult.setKeepCallback(true); mJPushReceiveCallback.sendPluginResult(dataResult); }
private void _sendResultOfCommand(CallbackContext callbackContext, PluginResult pluginResult) { // debugLog("Send result: " + pluginResult.getMessage()); if (pluginResult.getStatus() != PluginResult.Status.OK.ordinal()) debugWarn("WARNING: " + PluginResult.StatusMessages[pluginResult.getStatus()]); // When calling without a callback from the client side the command can be null. if (callbackContext == null) { return; } callbackContext.sendPluginResult(pluginResult); }
// Sends an error back to JS private void fail(int code, String message) { // Error object JSONObject errorObj = new JSONObject(); try { errorObj.put("code", code); errorObj.put("message", message); } catch (JSONException e) { e.printStackTrace(); } PluginResult err = new PluginResult(PluginResult.Status.ERROR, errorObj); err.setKeepCallback(true); callbackContext.sendPluginResult(err); }
private void sendNotification(CallbackContext callBack, JSONObject notification) { if (callBack != null) { PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, notification); pluginResult.setKeepCallback(true); callBack.sendPluginResult(pluginResult); mNotifications.add(notification); } else { Log.v(TAG, "No Notification callback - webview: " + getWebView()); } }
@Override public void onTextMessage(Context arg0, XGPushTextMessage message) { JSONObject data = new JSONObject(); try { data.put("content", message.getContent()); data.put("title", message.getTitle()); data.put("customContent", message.getCustomContent()); } catch (JSONException e) { e.printStackTrace(); } PluginResult results = new PluginResult(PluginResult.Status.OK, data); results.setKeepCallback(true); this.callback.sendPluginResult(results); }
/** * 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 True if the action was valid, false otherwise. */ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) { if (action.equals("getConnectionInfo")) { this.connectionCallbackContext = callbackContext; NetworkInfo info = sockMan.getActiveNetworkInfo(); String connectionType = ""; try { connectionType = this.getConnectionInfo(info).get("type").toString(); } catch (JSONException e) { } PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, connectionType); pluginResult.setKeepCallback(true); callbackContext.sendPluginResult(pluginResult); return true; } return false; }
int calculateEncodedLength() { if (pluginResult == null) { return jsPayloadOrCallbackId.length() + 1; } int statusLen = String.valueOf(pluginResult.getStatus()).length(); int ret = 2 + statusLen + 1 + jsPayloadOrCallbackId.length() + 1; switch (pluginResult.getMessageType()) { case PluginResult.MESSAGE_TYPE_BOOLEAN: // f or t case PluginResult.MESSAGE_TYPE_NULL: // N ret += 1; break; case PluginResult.MESSAGE_TYPE_NUMBER: // n ret += 1 + pluginResult.getMessage().length(); break; case PluginResult.MESSAGE_TYPE_STRING: // s ret += 1 + pluginResult.getStrMessage().length(); break; case PluginResult.MESSAGE_TYPE_BINARYSTRING: ret += 1 + pluginResult.getMessage().length(); break; case PluginResult.MESSAGE_TYPE_ARRAYBUFFER: ret += 1 + pluginResult.getMessage().length(); break; case PluginResult.MESSAGE_TYPE_JSON: default: ret += pluginResult.getMessage().length(); } return ret; }
/** * Executes the request. * * <p>This method is called from the WebView thread. To do a non-trivial amount of work, use: * cordova.getThreadPool().execute(runnable); * * <p>To run on the UI thread, use: cordova.getActivity().runOnUiThread(runnable); * * @param action The action to execute. * @param args The exec() arguments, wrapped with some Cordova helpers. * @param callbackContext The callback context used when calling back into JavaScript. * @return Whether the action was valid. */ public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException { this.callbackContext = callbackContext; if (action.equals("startTouchPad")) { startTouchPad(); } if (action.equals("publishCard")) { pushCard(args); } if (action.equals("speechField")) { speechField(args); } else { return false; } PluginResult result = new PluginResult(PluginResult.Status.OK, "START"); result.setKeepCallback(true); callbackContext.sendPluginResult(result); return true; }
public void onEvent(MessageEvent event) { final Message msg = event.getMessage(); Log.i(TAG, "onEvent:" + msg.toString()); // 可以在这里创建Notification if (msg.getTargetType() == ConversationType.single) { JSONObject obj = this.getJSonFormMessage(msg); Log.i(TAG, "@@@" + obj.toString()); PluginResult dataResult = new PluginResult(PluginResult.Status.OK, obj); dataResult.setKeepCallback(true); if (mJMessageReceiveCallback != null) { mJMessageReceiveCallback.sendPluginResult(dataResult); } } else { LOG.w(TAG, "message is not singleTyle"); } }
@Override public boolean execute(String action, JSONArray data, CallbackContext callbackContext) { this.callbackContext = callbackContext; this.context = cordova.getActivity().getApplicationContext(); if (action.equals("chooseContact")) { Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); cordova.startActivityForResult(this, intent, CHOOSE_CONTACT); PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); r.setKeepCallback(true); callbackContext.sendPluginResult(r); return true; } return false; }
/** The final call you receive before your activity is destroyed. */ public void onDestroy() { if (broadcastReceiver != null) { // unRegister our receiver that was registered on localbroadcastmanager // wrong: cordova.getActivity().unregisterReceiver(broadcastReceiver); LocalBroadcastManager.getInstance(cordova.getActivity().getApplicationContext()) .unregisterReceiver(broadcastReceiver); broadcastReceiver = null; } // release events in JS side if (eventCallbackContext != null) { PluginResult result = new PluginResult(PluginResult.Status.OK); result.setKeepCallback(false); eventCallbackContext.sendPluginResult(result); eventCallbackContext = null; } super.onDestroy(); }
void encodeAsJsMessage(StringBuilder sb) { if (pluginResult == null) { sb.append(jsPayloadOrCallbackId); } else { int status = pluginResult.getStatus(); boolean success = (status == PluginResult.Status.OK.ordinal()) || (status == PluginResult.Status.NO_RESULT.ordinal()); sb.append("cordova.callbackFromNative('") .append(jsPayloadOrCallbackId) .append("',") .append(success) .append(",") .append(status) .append(",[") .append(pluginResult.getMessage()) .append("],") .append(pluginResult.getKeepCallback()) .append(");"); } }
/** Add a JavaScript statement to the list. */ public void addPluginResult(PluginResult result, String callbackId) { if (callbackId == null) { Log.e(LOG_TAG, "Got plugin result with no callbackId", new Throwable()); return; } // Don't send anything if there is no result and there is no need to // clear the callbacks. boolean noResult = result.getStatus() == PluginResult.Status.NO_RESULT.ordinal(); boolean keepCallback = result.getKeepCallback(); if (noResult && keepCallback) { return; } JsMessage message = new JsMessage(result, callbackId); if (FORCE_ENCODE_USING_EVAL) { StringBuilder sb = new StringBuilder(message.calculateEncodedLength() + 50); message.encodeAsJsMessage(sb); message = new JsMessage(sb.toString()); } enqueueMessage(message); }