private WritableMap createRangingResponse(Collection<Beacon> beacons, Region region) { WritableMap map = new WritableNativeMap(); map.putString("identifier", region.getUniqueId()); map.putString("uuid", region.getId1() != null ? region.getId1().toString() : ""); WritableArray a = new WritableNativeArray(); for (Beacon beacon : beacons) { WritableMap b = new WritableNativeMap(); b.putString("uuid", beacon.getId1().toString()); b.putInt("major", beacon.getId2().toInt()); b.putInt("minor", beacon.getId3().toInt()); b.putInt("rssi", beacon.getRssi()); b.putDouble("distance", beacon.getDistance()); b.putString("proximity", getProximity(beacon.getDistance())); a.pushMap(b); } map.putArray("beacons", a); return map; }
protected void writeStaticNodesFile(String enodes) { try { File dir = new File(this.reactContext .getFilesDir() + STATIC_NODES_FILES_PATH); if (dir.exists() == false) dir.mkdirs(); File f = new File(dir, STATIC_NODES_FILES_NAME); if (f.exists() == false) { if (f.createNewFile() == true) { WritableArray staticNodes = new WritableNativeArray(); staticNodes.pushString(enodes); Writer output = new BufferedWriter(new FileWriter(f)); output.write(staticNodes.toString()); output.close(); } } } catch (Exception e) { e.printStackTrace(); } }
@ReactMethod void callJavaScript() { Activity activity = getCurrentActivity(); if (activity != null) { MainApplication application = (MainApplication) activity.getApplication(); ReactNativeHost reactNativeHost = application.getReactNativeHost(); ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager(); ReactContext reactContext = reactInstanceManager.getCurrentReactContext(); if (reactContext != null) { CatalystInstance catalystInstance = reactContext.getCatalystInstance(); WritableNativeArray params = new WritableNativeArray(); params.pushString("Hello, JavaScript!"); catalystInstance.callFunction("JavaScriptVisibleToJava", "alert", params); } } }
/** * This overload is like the above, but uses reflection to operate on any * primitive or object type. */ public static <T> WritableNativeArray makeNativeArray(final Object objects) { if (objects == null) { return new WritableNativeArray(); } // No explicit check for objects's type here. If it's not an array, the // Array methods will throw IllegalArgumentException. return makeNativeArray(new AbstractList() { public int size() { return Array.getLength(objects); } public Object get(int index) { return Array.get(objects, index); } }); }
private static void addEntry(WritableNativeMap nativeMap, String key, Object value) { value = makeNativeObject(value); if (value == null) { nativeMap.putNull(key); } else if (value instanceof Boolean) { nativeMap.putBoolean(key, (Boolean) value); } else if (value instanceof Integer) { nativeMap.putInt(key, (Integer) value); } else if (value instanceof Number) { nativeMap.putDouble(key, ((Number) value).doubleValue()); } else if (value instanceof String) { nativeMap.putString(key, (String) value); } else if (value instanceof WritableNativeArray) { nativeMap.putArray(key, (WritableNativeArray) value); } else if (value instanceof WritableNativeMap) { nativeMap.putMap(key, (WritableNativeMap) value); } else { throw new IllegalArgumentException("Could not convert " + value.getClass()); } }
public void testMapWithArrays() { WritableNativeMap map = new WritableNativeMap(); WritableNativeArray a1 = new WritableNativeArray(); WritableNativeArray a2 = new WritableNativeArray(); a1.pushDouble(3); a1.pushDouble(1); a1.pushDouble(4); a2.pushDouble(1); a2.pushDouble(9); map.putArray("array1", a1); map.putArray("array2", a2); mInstance.getJSModule(TestJavaToJSArgumentsModule.class).receiveMapWithArrays(map); waitForBridgeAndUIIdle(); mAssertModule.verifyAssertsAndReset(); }
public void testMapWithNullStringValue() { WritableNativeMap map = new WritableNativeMap(); map.putString("string", null); map.putArray("array", null); map.putMap("map", null); WritableNativeArray array = new WritableNativeArray(); array.pushString(null); array.pushArray(null); array.pushMap(null); mInstance.getJSModule(TestJavaToJSArgumentsModule.class) .receiveMapAndArrayWithNullValues(map, array); waitForBridgeAndUIIdle(); mAssertModule.verifyAssertsAndReset(); }
public void testStringWithMultibyteUTF8Characters() { TestJavaToJSArgumentsModule jsModule = mInstance.getJSModule(TestJavaToJSArgumentsModule.class); WritableNativeMap map = new WritableNativeMap(); map.putString("two-bytes", "\u00A2"); map.putString("three-bytes", "\u20AC"); map.putString("four-bytes", "\uD83D\uDE1C"); map.putString( "mixed", "\u017C\u00F3\u0142\u0107 g\u0119\u015Bl\u0105 \u6211 \uD83D\uDE0E ja\u017A\u0107"); jsModule.receiveMapWithMultibyteUTF8CharacterString(map); waitForBridgeAndUIIdle(); mAssertModule.verifyAssertsAndReset(); WritableArray array = new WritableNativeArray(); array.pushString("\u00A2"); array.pushString("\u20AC"); array.pushString("\uD83D\uDE1C"); array.pushString( "\u017C\u00F3\u0142\u0107 g\u0119\u015Bl\u0105 \u6211 \uD83D\uDE0E ja\u017A\u0107"); jsModule.receiveArrayWithMultibyteUTF8CharacterString(array); waitForBridgeAndUIIdle(); mAssertModule.verifyAssertsAndReset(); }
@DoNotStrip public NativeArray getConstants() { BaseJavaModule baseJavaModule = getModule(); ReactMarker.logMarker(GET_CONSTANTS_START, getName()); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "Map constants") .arg("moduleName", getName()) .flush(); Map<String, Object> map = baseJavaModule.getConstants(); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "WritableNativeMap constants") .arg("moduleName", getName()) .flush(); ReactMarker.logMarker(CONVERT_CONSTANTS_START, getName()); WritableNativeMap writableNativeMap; try { writableNativeMap = Arguments.makeNativeMap(map); } finally { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); } WritableNativeArray array = new WritableNativeArray(); array.pushMap(writableNativeMap); ReactMarker.logMarker(CONVERT_CONSTANTS_END); ReactMarker.logMarker(GET_CONSTANTS_END); return array; }
/** * React method to get all notification ids. */ @ReactMethod public void rGetIDs( Callback errorCallback, Callback successCallback ) { try { ArrayList<Integer> ids = mNotificationManager.getIDs(); WritableArray rids = new WritableNativeArray(); for (Integer id: ids) { rids.pushInt(id); } successCallback.invoke((ReadableArray) rids); } catch (Exception e) { errorCallback.invoke(e.getMessage()); Log.e("ReactSystemNotification", "NotificationModule: rGetIDs Error: " + Log.getStackTraceString(e)); } }
@DoNotStrip public NativeArray getConstants() { SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "Map constants") .arg("moduleName", getName()) .flush(); Map<String, Object> map = getModule().getConstants(); Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "WritableNativeMap constants") .arg("moduleName", getName()) .flush(); WritableNativeMap writableNativeMap; try { writableNativeMap = Arguments.makeNativeMap(map); } finally { Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE); } WritableNativeArray array = new WritableNativeArray(); array.pushMap(writableNativeMap); return array; }
@ReactMethod public void getInstalledApps(Promise promise) { WritableNativeArray params = new WritableNativeArray(); PackageManager pm = reactContext.getPackageManager(); List<ApplicationInfo> apps = pm.getInstalledApplications(0); for(ApplicationInfo app : apps) { if ((pm.getLaunchIntentForPackage(app.packageName) != null) && !thisApp.equals(app.packageName)) { WritableNativeMap param = new WritableNativeMap(); String appName = app.loadLabel(pm).toString(); String appPackage = app.packageName; String appIcon = saveIcon(appPackage, app.loadIcon(pm)); //Log.d(TAG, "name: "+appName); //Log.d(TAG, "app: "+appPackage); //Log.d(TAG, "icon: "+appIcon); param.putString("name", appName); param.putString("app", appPackage); param.putString("icon", appIcon); //sendEvent("installedApps", param); params.pushMap(param); } } promise.resolve(params); }
static WritableArray reactArrayFromJson(JSONArray json) throws JSONException { WritableArray result = new WritableNativeArray(); for (int i = 0; i < json.length(); i++) { Object value = json.get(i); if (value == null || value == JSONObject.NULL) { result.pushNull(); } else if (value instanceof Boolean) { result.pushBoolean((Boolean)value); } else if (value instanceof Integer) { result.pushInt((Integer)value); } else if (value instanceof Double) { result.pushDouble((Double)value); } else if (value instanceof String) { result.pushString((String)value); } else if (value instanceof JSONObject) { result.pushMap(reactMapFromJson((JSONObject)value)); } else if (value instanceof JSONArray) { result.pushArray(reactArrayFromJson((JSONArray)value)); } else { throw new RuntimeException("Bad value in JSON array: " + value.toString()); } } return result; }
private WritableNativeArray findEventCalendars() { Cursor cursor; ContentResolver cr = reactContext.getContentResolver(); Uri uri = CalendarContract.Calendars.CONTENT_URI; cursor = cr.query(uri, new String[]{ CalendarContract.Calendars._ID, CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, CalendarContract.Calendars.ACCOUNT_NAME, CalendarContract.Calendars.IS_PRIMARY, CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.ALLOWED_AVAILABILITY }, null, null, null); return serializeEventCalendars(cursor); }
private WritableNativeArray calendarAllowedAvailabilitiesFromDBString(String dbString) { WritableNativeArray availabilitiesStrings = new WritableNativeArray(); for(String availabilityId: dbString.split(",")) { switch(Integer.parseInt(availabilityId)) { case CalendarContract.Events.AVAILABILITY_BUSY: availabilitiesStrings.pushString("busy"); break; case CalendarContract.Events.AVAILABILITY_FREE: availabilitiesStrings.pushString("free"); break; case CalendarContract.Events.AVAILABILITY_TENTATIVE: availabilitiesStrings.pushString("tentative"); break; } } return availabilitiesStrings; }
@ReactMethod public void findAllEvents(final Dynamic startDate, final Dynamic endDate, final ReadableArray calendars, final Promise promise) { if (this.haveCalendarReadWritePermissions()) { try { Thread thread = new Thread(new Runnable(){ @Override public void run() { WritableNativeArray results = findEvents(startDate, endDate, calendars); promise.resolve(results); } }); thread.start(); } catch (Exception e) { promise.reject("find event error", e.getMessage()); } } else { promise.reject("find event error", "you don't have permissions to read an event from the users calendar"); } }
public static WritableArray convertJsonToArray(JSONArray jsonArray) throws JSONException { WritableArray array = new WritableNativeArray(); for (int i = 0; i < jsonArray.length(); i++) { Object value = jsonArray.get(i); if (value instanceof JSONObject) { array.pushMap(jsonToMap((JSONObject) value)); } else if (value instanceof JSONArray) { array.pushArray(convertJsonToArray((JSONArray) value)); } else if (value instanceof Boolean) { array.pushBoolean((Boolean) value); } else if (value instanceof Integer) { array.pushInt((Integer) value); } else if (value instanceof Double) { array.pushDouble((Double) value); } else if (value instanceof String) { array.pushString((String) value); } else { array.pushString(value.toString()); } } return array; }
public static WritableArray convertJsonToArray(JSONArray jsonArray) throws JSONException { WritableArray array = new WritableNativeArray(); for (int i = 0; i < jsonArray.length(); i++) { Object value = jsonArray.get(i); if (value instanceof JSONObject) { array.pushMap(convertJsonToMap((JSONObject) value)); } else if (value instanceof JSONArray) { array.pushArray(convertJsonToArray((JSONArray) value)); } else if (value instanceof Boolean) { array.pushBoolean((Boolean) value); } else if (value instanceof Integer) { array.pushInt((Integer) value); } else if (value instanceof Double) { array.pushDouble((Double) value); } else if (value instanceof String) { array.pushString((String) value); } else { array.pushString(value.toString()); } } return array; }
@ReactMethod public void getCameraImages(ReadableMap data, Callback onSuccess) { WritableNativeArray result = new WritableNativeArray(); for(CameraImage imageData : CameraImagesManager.getCameraImages(this.context, data.getInt("first"), data.hasKey("after") ? data.getString("after") : "")) { WritableMap imageDataMap = new WritableNativeMap(); imageDataMap.putString("uri", imageData.getLocalPath()); imageDataMap.putInt("width", imageData.getWidth()); imageDataMap.putInt("height", imageData.getHeight()); imageDataMap.putInt("orientation", imageData.getOrientation()); imageDataMap.putString("timestamp", Long.toString(imageData.getTimestamp())); result.pushMap(imageDataMap); } onSuccess.invoke(result); }
public static ReadableArray doubleArrayToReadableArray(double[] arr) { WritableArray writableArray = new WritableNativeArray(); for (double d : arr) { writableArray.pushDouble(d); } return writableArray; }
public static ReadableArray floatArrayToReadableArray(float[] arr) { WritableArray writableArray = new WritableNativeArray(); for (float f : arr) { writableArray.pushDouble(f); } return writableArray; }
public static ReadableArray intArrayToReadableArray(int[] arr) { WritableArray writableArray = new WritableNativeArray(); for (int i : arr) { writableArray.pushInt(i); } return writableArray; }
public static ReadableArray byteArrayToBoolReadableArray(byte[] arr) { WritableArray writableArray = new WritableNativeArray(); byte[] pos = new byte[]{(byte)0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1}; for(int i = 0; i < arr.length; i++){ for(int k = 0; k < 8; k++){ boolean res = (arr[i] & pos[k]) != 0; writableArray.pushBoolean(res); } } return writableArray; }
public static WritableArray jsonArrayToWritableArray(JSONArray jsonArray) { WritableArray writableArray = new WritableNativeArray(); try { if (jsonArray == null) { return null; } if (jsonArray.length() <= 0) { return null; } for (int i = 0 ; i < jsonArray.length(); i++) { Object value = jsonArray.get(i); if (value == null) { writableArray.pushNull(); } else if (value instanceof Boolean) { writableArray.pushBoolean((Boolean) value); } else if (value instanceof Integer) { writableArray.pushInt((Integer) value); } else if (value instanceof Double) { writableArray.pushDouble((Double) value); } else if (value instanceof String) { writableArray.pushString((String) value); } else if (value instanceof JSONObject) { writableArray.pushMap(jsonToWritableMap((JSONObject) value)); } else if (value instanceof JSONArray) { writableArray.pushArray(jsonArrayToWritableArray((JSONArray) value)); } } } catch (JSONException e) { // Do nothing and fail silently } return writableArray; }
@Override public void onEventCallback(final String name, final ReadableMap info, final MSREventBridgeReceiverCallback callback) { Log.d(ReactConstants.TAG, this.getClass().getName() + ": Received event with callback: ".concat(name)); final String activityClassName = this.getClass().getSimpleName(); // Example how to load some async data if (name.equals(LoadDataEventName)) { final int count = info.getInt("count"); // Simulate some data loading delay final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { WritableArray array = new WritableNativeArray(); for (int i = 0; i < count; i++) { array.pushString("Row " + i + " - " + activityClassName); } callback.onSuccess(array); } }, 2000); return; } // Emit callback WritableMap map = new WritableNativeMap(); map.putString("key", "value"); callback.onSuccess(map); }
@Override public void onEventCallback(final String name, final ReadableMap info, final MSREventBridgeReceiverCallback callback) { Log.d(ReactConstants.TAG, this.getClass().getName() + ": Received event with callback: ".concat(name)); final String activityClassName = this.getClass().getSimpleName(); // Example how to load some async data if (name.equals(LoadDataEventName)) { final int count = info.getInt("count"); // Simulate some data loading delay final Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { WritableArray array = new WritableNativeArray(); for (int i = 0; i < count; i++) { // getSimpleName() does not work here for some reason String className = this.getClass().getName(); array.pushString("Row " + i + " - " + activityClassName); } callback.onSuccess(array); } }, 2000); return; } // Emit callback WritableMap map = new WritableNativeMap(); map.putString("key", "value"); callback.onSuccess(map); }
@ReactMethod public void getMonitoredRegions(Callback callback) { WritableArray array = new WritableNativeArray(); for (Region region: mBeaconManager.getMonitoredRegions()) { WritableMap map = new WritableNativeMap(); map.putString("identifier", region.getUniqueId()); map.putString("uuid", region.getId1().toString()); map.putInt("major", region.getId2() != null ? region.getId2().toInt() : 0); map.putInt("minor", region.getId3() != null ? region.getId3().toInt() : 0); array.pushMap(map); } callback.invoke(array); }
@ReactMethod public void getRangedRegions(Callback callback) { WritableArray array = new WritableNativeArray(); for (Region region: mBeaconManager.getRangedRegions()) { WritableMap map = new WritableNativeMap(); map.putString("region", region.getUniqueId()); map.putString("uuid", region.getId1().toString()); array.pushMap(map); } callback.invoke(array); }
private void onByteArraysStream(byte[] bytes) { WritableMap params = Arguments.createMap(); WritableArray data = new WritableNativeArray(); for (byte b : bytes) { data.pushInt(UnsignedBytes.toInt(b)); } params.putArray("payload", data); sendEventToJs(BT_RECEIVED_DATA, params); }