@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)){
@Override public PluginResult execute(String action, JSONArray args, String callbackId) { this.callback = callbackId; if (action.equals("load")) { load(args); } else if (action.equals("fetch")) { fetch(args); } else if (action.equals("remove")) { remove(args); } else { return new PluginResult(PluginResult.Status.INVALID_ACTION); } PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); r.setKeepCallback(true); return r; }
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; }
/** 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); }
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(",["); switch (pluginResult.getMessageType()) { case PluginResult.MESSAGE_TYPE_BINARYSTRING: sb.append("atob('").append(pluginResult.getMessage()).append("')"); break; case PluginResult.MESSAGE_TYPE_ARRAYBUFFER: sb.append("cordova.require('cordova/base64').toArrayBuffer('") .append(pluginResult.getMessage()) .append("')"); break; default: sb.append(pluginResult.getMessage()); } sb.append("],").append(pluginResult.getKeepCallback()).append(");"); } }
void encodeAsMessage(StringBuilder sb) { if (pluginResult == null) { sb.append('J').append(jsPayloadOrCallbackId); return; } int status = pluginResult.getStatus(); boolean noResult = status == PluginResult.Status.NO_RESULT.ordinal(); boolean resultOk = status == PluginResult.Status.OK.ordinal(); boolean keepCallback = pluginResult.getKeepCallback(); sb.append((noResult || resultOk) ? 'S' : 'F') .append(keepCallback ? '1' : '0') .append(status) .append(' ') .append(jsPayloadOrCallbackId) .append(' '); switch (pluginResult.getMessageType()) { case PluginResult.MESSAGE_TYPE_BOOLEAN: sb.append(pluginResult.getMessage().charAt(0)); // t or f. break; case PluginResult.MESSAGE_TYPE_NULL: // N sb.append('N'); break; case PluginResult.MESSAGE_TYPE_NUMBER: // n sb.append('n').append(pluginResult.getMessage()); break; case PluginResult.MESSAGE_TYPE_STRING: // s sb.append('s'); sb.append(pluginResult.getStrMessage()); break; case PluginResult.MESSAGE_TYPE_BINARYSTRING: // S sb.append('S'); sb.append(pluginResult.getMessage()); break; case PluginResult.MESSAGE_TYPE_ARRAYBUFFER: // A sb.append('A'); sb.append(pluginResult.getMessage()); break; case PluginResult.MESSAGE_TYPE_JSON: default: sb.append(pluginResult.getMessage()); // [ or { } }
/** * The invoke method is called when phonegap.pluginManager.exec(...) is used from the script * environment. It instantiates the appropriate plugin and invokes the specified action. * JavaScript arguments are passed in as an array of objects. * * @param service String containing the service to run * @param action String containing the action that the service is supposed to perform. This is * passed to the plugin execute method and it is up to the plugin developer how to deal with * it. * @param callbackId String containing the id of the callback that is executed in JavaScript if * this is an async plugin call. * @param args An Array literal string containing any arguments needed in the plugin execute * method. * @param async Boolean indicating whether the calling JavaScript code is expecting an immediate * return value. If true, either PhoneGap.callbackSuccess(...) or PhoneGap.callbackError(...) * is called once the plugin code has executed. * @return JSON encoded string with a response message and status. * @see net.rim.device.api.script.ScriptableFunction#invoke(java.lang.Object, java.lang.Object[]) */ public Object invoke(Object obj, Object[] oargs) throws Exception { final String service = (String) oargs[ARG_SERVICE]; final String action = (String) oargs[ARG_ACTION]; final String callbackId = (String) oargs[ARG_CALLBACK_ID]; boolean async = (oargs[ARG_ASYNC].toString().equals("true") ? true : false); PluginResult pr = null; try { // action arguments final JSONArray args = new JSONArray((String) oargs[ARG_ARGS]); // get the class for the specified service String clazz = this.pluginManager.getClassForService(service); Class c = null; if (clazz != null) { c = getClassByName(clazz); } if (isPhoneGapPlugin(c)) { // Create a new instance of the plugin and set the context final Plugin plugin = this.addPlugin(clazz, c); async = async && !plugin.isSynch(action); if (async) { // Run this async on a background thread so that JavaScript can continue on Thread thread = new Thread( new Runnable() { public void run() { // Call execute on the plugin so that it can do it's thing final PluginResult result = plugin.execute(action, args, callbackId); if (result != null) { int status = result.getStatus(); // If plugin status is OK, // or plugin is not going to send an immediate result (NO_RESULT) if (status == PluginResult.Status.OK.ordinal() || status == PluginResult.Status.NO_RESULT.ordinal()) { PhoneGapExtension.invokeSuccessCallback(callbackId, result); } // error else { PhoneGapExtension.invokeErrorCallback(callbackId, result); } } } }); thread.start(); return ""; } else { // Call execute on the plugin so that it can do it's thing pr = plugin.execute(action, args, callbackId); } } } catch (ClassNotFoundException e) { pr = new PluginResult( PluginResult.Status.CLASSNOTFOUNDEXCEPTION, "ClassNotFoundException: " + e.getMessage()); } catch (IllegalAccessException e) { pr = new PluginResult( PluginResult.Status.ILLEGALACCESSEXCEPTION, "IllegalAccessException:" + e.getMessage()); } catch (InstantiationException e) { pr = new PluginResult( PluginResult.Status.INSTANTIATIONEXCEPTION, "InstantiationException: " + e.getMessage()); } catch (JSONException e) { pr = new PluginResult(PluginResult.Status.JSONEXCEPTION, "JSONException: " + e.getMessage()); } // if async we have already returned at this point unless there was an error... if (async) { PhoneGapExtension.invokeErrorCallback(callbackId, pr); } return (pr != null ? pr.getJSONString() : "{ status: 0, message: 'all good' }"); }
@Override public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException { reader.start(); am.setStreamVolume( AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0); this.callbackContext = callbackContext; timedOut = false; Log.w(TAG, action); if (mute) { PluginResult dataResult = new PluginResult(PluginResult.Status.OK, "NOTFOUND"); callbackContext.sendPluginResult(dataResult); return true; } final ACR35Controller self = this; final String loadKeyCommand = "FF 82 00 00 06 %s"; final String authCommand = "FF 86 00 00 05 01 00 %s 60 00"; final String defaultKey = "FF FF FF FF FF FF"; String authKeyCommand = "FF 86 00 00 05 01 00 00 60 00"; if (action.equals("getDeviceId")) { cordova .getThreadPool() .execute( new Runnable() { @Override public void run() { reader.setMute(false); reader.reset( new AudioJackReader.OnResetCompleteListener() { @Override public void onResetComplete(AudioJackReader audioJackReader) { reader.getDeviceId(); } }); } }); } if (action.equals("getDeviceStatus")) { cordova .getThreadPool() .execute( new Runnable() { @Override public void run() { reader.setMute(false); reader.reset( new AudioJackReader.OnResetCompleteListener() { @Override public void onResetComplete(AudioJackReader audioJackReader) { reader.getStatus(); } }); } }); } if (action.equals("readIdFromTag")) { executeAPDUCommands(new byte[][] {hexToBytes("FFCA000000")}); } if (action.equals("readDataFromTag")) { executeAPDUCommands( new byte[][] { hexToBytes(String.format(loadKeyCommand, defaultKey)), hexToBytes(String.format(authCommand, "04")), hexToBytes("FF B0 00 04 10"), hexToBytes("FF B0 00 05 10"), hexToBytes("FF B0 00 06 10"), hexToBytes(String.format(authCommand, "08")), hexToBytes("FF B0 00 08 10"), hexToBytes("FF B0 00 09 10"), hexToBytes("FF B0 00 0A 10"), hexToBytes(String.format(authCommand, "10")), hexToBytes("FF B0 00 10 10"), hexToBytes("FF B0 00 11 10") }); } if (action.equals("writeDataIntoTag")) { try { String dataString = data.get(0).toString(); byte[] dataToWrite = new byte[128]; Arrays.fill(dataToWrite, (byte) 0); byte[] dataBytes = hexToBytes(dataString); System.arraycopy(dataBytes, 0, dataToWrite, 0, dataBytes.length); String dataStringToWrite = bytesToHex(dataToWrite).replaceAll("\\s", ""); String commandString1 = "FF D6 00 04 30" + dataStringToWrite.substring(0, 95); String commandString2 = "FF D6 00 08 30" + dataStringToWrite.substring(96, (96 * 2) - 1); String commandString3 = "FF D6 00 10 20" + dataStringToWrite.substring(96 * 2, (96 * 2 + 64) - 1); Log.w(TAG, dataStringToWrite); executeAPDUCommands( new byte[][] { hexToBytes(String.format(loadKeyCommand, defaultKey)), hexToBytes(String.format(authCommand, "04")), hexToBytes(commandString1), hexToBytes(String.format(authCommand, "08")), hexToBytes(commandString2), hexToBytes(String.format(authCommand, "10")), hexToBytes(commandString3), }); } catch (java.lang.Exception e) { Log.w(TAG, e); } } Calendar calendar = Calendar.getInstance(); // gets a calendar using the default time zone and locale. calendar.add(Calendar.SECOND, timeOut); timer = new Timer(); timer.schedule(new TimeoutClass(reader, self), calendar.getTime()); PluginResult dataResult = new PluginResult(PluginResult.Status.OK, "IGNORE"); dataResult.setKeepCallback(true); callbackContext.sendPluginResult(dataResult); return true; }