/** * Retreives "sourceId" constraint value. * @param mediaConstraints a <tt>ReadableMap</tt> which represents "GUM" * constraints argument * @return String value of "sourceId" optional "GUM" constraint or * <tt>null</tt> if not specified in the given map. */ private String getSourceIdConstraint(ReadableMap mediaConstraints) { if (mediaConstraints.hasKey("optional") && mediaConstraints.getType("optional") == ReadableType.Array) { ReadableArray optional = mediaConstraints.getArray("optional"); for (int i = 0, size = optional.size(); i < size; i++) { if (optional.getType(i) == ReadableType.Map) { ReadableMap option = optional.getMap(i); if (option.hasKey("sourceId") && option.getType("sourceId") == ReadableType.String) { return option.getString("sourceId"); } } } } return null; }
/** * Reads a value from given <tt>ReadableMap</tt> and returns it as * a <tt>String</tt>. Note that integer value is converted to double, before * it gets converted to a string. * @param map the <tt>ReadableMap</tt> from which the value will be obtained * @param key the map's key under which the value has been mapped. * @return a <tt>String</tt> representation of the value if exists or * <tt>null</tt> if there is no value mapped for given <tt>key</tt>. */ public static String getMapStrValue(ReadableMap map, String key) { if(!map.hasKey(key)){ return null; } ReadableType type = map.getType(key); switch (type) { case Boolean: return String.valueOf(map.getBoolean(key)); case Number: // Don't know how to distinguish between Int and Double from // ReadableType.Number. 'getInt' will fail on double value, // while 'getDouble' works for both. // return String.valueOf(map.getInt(key)); return String.valueOf(map.getDouble(key)); case String: return map.getString(key); default: return null; } }
private Intent createIntent(String action, ReadableMap extra) { final Intent intent = new Intent(action); for ( ReadableMapKeySetIterator it = extra.keySetIterator(); it.hasNextKey(); ) { String key = it.nextKey(); ReadableType type = extra.getType(key); switch(type) { case Null: break; case Boolean: intent.putExtra(key, extra.getBoolean(key)); break; case Number: intent.putExtra(key, extra.getInt(key)); break; case String: intent.putExtra(key, extra.getString(key)); break; default: throw new IllegalArgumentException("Unsupported type " + type); } } return intent; }
public static HashMap<String, Object> readableMapToHashMap(ReadableMap readableMap) { if (readableMap == null) { return null; } HashMap map = new HashMap<String, Object>(); ReadableMapKeySetIterator keySetIterator = readableMap.keySetIterator(); while (keySetIterator.hasNextKey()) { String key = keySetIterator.nextKey(); ReadableType type = readableMap.getType(key); switch(type) { case String: map.put(key, readableMap.getString(key)); break; case Map: HashMap<String, Object> attributes = new RCTConvert().readableMapToHashMap(readableMap.getMap(key)); map.put(key, attributes); break; default: // do nothing } } return map; }
private String[] getStringArray(ReadableMap map, String key) { ReadableArray array = getArray(map, key); if (array == null) return null; ArrayList<String> list = new ArrayList<>(); for (int i = 0; i < array.size(); i++) { if (array.getType(i) == ReadableType.String) { String str = array.getString(i); if (!isEmpty(str)) { list.add(str); } } } String[] arr = new String[list.size()]; return list.toArray(arr); }
private StringBuilder buildCreateQuery(String tableName, ReadableMap props, List<String> keys) { final StringBuilder queryBuilder = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(tableName).append(" "); queryBuilder.append("("); queryBuilder.append("id INTEGER PRIMARY KEY AUTOINCREMENT"); for (String key : keys) { String propType = null; if(props.getType(key) == ReadableType.Boolean) { propType = "BOOLEAN"; } else if(props.getType(key) == ReadableType.String) { propType = "TEXT"; } else if(props.getType(key) == ReadableType.Number) { propType = "DOUBLE"; } if(propType != null) { queryBuilder.append(", ").append(key).append(" ").append(propType); } } queryBuilder.append(")"); return queryBuilder; }
private ArrayList<String> buildQueryArgument(List<String> keys, ReadableMap query) { final ArrayList<String> arguments = new ArrayList<>(); for (String key : keys) { ReadableType type = query.getType(key); if(type == ReadableType.Boolean) { arguments.add(String.valueOf(query.getBoolean(key))); } else if(type == ReadableType.Number) { arguments.add(String.valueOf(query.getDouble(key))); } else if(type == ReadableType.String) { arguments.add(String.valueOf(query.getString(key))); } } return arguments; }
private String stackTraceToString(String message, ReadableArray stack) { StringBuilder stringBuilder = new StringBuilder(message).append(", stack:\n"); for (int i = 0; i < stack.size(); i++) { ReadableMap frame = stack.getMap(i); stringBuilder .append(frame.getString("methodName")) .append("@") .append(stackFrameToModuleId(frame)) .append(frame.getInt("lineNumber")); if (frame.hasKey("column") && !frame.isNull("column") && frame.getType("column") == ReadableType.Number) { stringBuilder .append(":") .append(frame.getInt("column")); } stringBuilder.append("\n"); } return stringBuilder.toString(); }
void setFromDynamic(Dynamic dynamic) { if (dynamic.isNull()) { unit = YogaUnit.UNDEFINED; value = YogaConstants.UNDEFINED; } else if (dynamic.getType() == ReadableType.String) { final String s = dynamic.asString(); if (s.equals("auto")) { unit = YogaUnit.AUTO; value = YogaConstants.UNDEFINED; } else if (s.endsWith("%")) { unit = YogaUnit.PERCENT; value = Float.parseFloat(s.substring(0, s.length() - 1)); } else { throw new IllegalArgumentException("Unknown value: " + s); } } else { unit = YogaUnit.POINT; value = PixelUtil.toPixelFromDIP(dynamic.asDouble()); } }
private static double convertToRadians(ReadableMap transformMap, String key) { double value; boolean inRadians = true; if (transformMap.getType(key) == ReadableType.String) { String stringValue = transformMap.getString(key); if (stringValue.endsWith("rad")) { stringValue = stringValue.substring(0, stringValue.length() - 3); } else if (stringValue.endsWith("deg")) { inRadians = false; stringValue = stringValue.substring(0, stringValue.length() - 3); } value = Float.parseFloat(stringValue); } else { value = transformMap.getDouble(key); } return inRadians ? value : MatrixMathHelper.degreesToRadians(value); }
public void testGetTypeFromArray() { mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class) .returnArrayWithStringDoubleIntMapArrayBooleanNull(); waitForBridgeAndUIIdle(); List<ReadableArray> calls = mRecordingTestModule.getArrayCalls(); assertEquals(1, calls.size()); ReadableArray array = calls.get(0); assertEquals(ReadableType.String, array.getType(0)); assertEquals(ReadableType.Number, array.getType(1)); assertEquals(ReadableType.Number, array.getType(2)); assertEquals(ReadableType.Map, array.getType(3)); assertEquals(ReadableType.Array, array.getType(4)); assertEquals(ReadableType.Boolean, array.getType(5)); assertEquals(ReadableType.Null, array.getType(6)); }
public void testGetTypeFromMap() { mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class) .returnMapWithStringDoubleIntMapArrayBooleanNull(); waitForBridgeAndUIIdle(); List<ReadableMap> calls = mRecordingTestModule.getMapCalls(); assertEquals(1, calls.size()); ReadableMap map = calls.get(0); assertEquals(ReadableType.String, map.getType("string")); assertEquals(ReadableType.Number, map.getType("double")); assertEquals(ReadableType.Number, map.getType("int")); assertEquals(ReadableType.Map, map.getType("map")); assertEquals(ReadableType.Array, map.getType("array")); assertEquals(ReadableType.Boolean, map.getType("boolean")); assertEquals(ReadableType.Null, map.getType("null")); }
public void testIntOutOfRangeThrown() { mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnArrayWithLargeInts(); mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnMapWithLargeInts(); waitForBridgeAndUIIdle(); assertEquals(1, mRecordingTestModule.getArrayCalls().size()); assertEquals(1, mRecordingTestModule.getMapCalls().size()); ReadableArray array = mRecordingTestModule.getArrayCalls().get(0); assertNotNull(array); ReadableMap map = mRecordingTestModule.getMapCalls().get(0); assertNotNull(map); assertEquals(ReadableType.Number, array.getType(0)); assertUnexpectedTypeExceptionThrown(array, 0, "int"); assertEquals(ReadableType.Number, array.getType(1)); assertUnexpectedTypeExceptionThrown(array, 1, "int"); assertEquals(ReadableType.Number, map.getType("first")); assertUnexpectedTypeExceptionThrown(map, "first", "int"); assertEquals(ReadableType.Number, map.getType("second")); assertUnexpectedTypeExceptionThrown(map, "second", "int"); }
private String stackTraceToString(String message, ReadableArray stack) { StringBuilder stringBuilder = new StringBuilder(message).append(", stack:\n"); for (int i = 0; i < stack.size(); i++) { ReadableMap frame = stack.getMap(i); stringBuilder .append(frame.getString("methodName")) .append("@") .append(frame.getInt("lineNumber")); if (frame.hasKey("column") && !frame.isNull("column") && frame.getType("column") == ReadableType.Number) { stringBuilder .append(":") .append(frame.getInt("column")); } stringBuilder.append("\n"); } return stringBuilder.toString(); }
@Override BubbleEntry createEntry(ReadableArray yValues, int index) { if (!ReadableType.Map.equals(yValues.getType(index))) { throw new IllegalArgumentException("Invalid BubbleEntry data"); } ReadableMap entry = yValues.getMap(index); if(!BridgeUtils.validate(entry, ReadableType.Number, "value") || !BridgeUtils.validate(entry, ReadableType.Number, "size")) { throw new IllegalArgumentException("Invalid BubbleEntry data"); } float value = (float) entry.getDouble("value"); float size = (float) entry.getDouble("size"); return new BubbleEntry(index, value, size); }
@Override void dataSetConfig(IDataSet<BarEntry> dataSet, ReadableMap config) { BarDataSet barDataSet = (BarDataSet) dataSet; ChartDataSetConfigUtils.commonConfig(barDataSet, config); ChartDataSetConfigUtils.commonBarLineScatterCandleBubbleConfig(barDataSet, config); if (BridgeUtils.validate(config, ReadableType.Number, "barSpacePercent")) { barDataSet.setBarSpacePercent((float) config.getDouble("barSpacePercent")); } if (BridgeUtils.validate(config, ReadableType.String, "barShadowColor")) { barDataSet.setBarShadowColor(Color.parseColor(config.getString("barShadowColor"))); } if (BridgeUtils.validate(config, ReadableType.Number, "highlightAlpha")) { barDataSet.setHighLightAlpha(config.getInt("highlightAlpha")); } if (BridgeUtils.validate(config, ReadableType.Array, "stackLabels")) { barDataSet.setStackLabels(BridgeUtils.convertToStringArray(config.getArray("stackLabels"))); } }
@Override CandleEntry createEntry(ReadableArray yValues, int index) { if (!ReadableType.Map.equals(yValues.getType(index))) { throw new IllegalArgumentException(); } ReadableMap entryData = yValues.getMap(index); if (!BridgeUtils.validate(entryData, ReadableType.Number, "shadowH") || !BridgeUtils.validate(entryData, ReadableType.Number, "shadowL") || !BridgeUtils.validate(entryData, ReadableType.Number, "open") || !BridgeUtils.validate(entryData, ReadableType.Number, "close")) { throw new IllegalArgumentException("CandleStick data must contain: shadowH, shadowL, open and close values"); } float shadowH = (float) entryData.getDouble("shadowH"); float shadowL = (float) entryData.getDouble("shadowL"); float open = (float) entryData.getDouble("open"); float close = (float) entryData.getDouble("close"); return new CandleEntry(index, shadowH, shadowL, open, close); }
@ReactProp(name = "description") public void setDescription(Chart chart, ReadableMap propMap) { if (BridgeUtils.validate(propMap, ReadableType.String, "text")) { chart.setDescription(propMap.getString("text")); } if (BridgeUtils.validate(propMap, ReadableType.String, "textColor")) { chart.setDescriptionColor(Color.parseColor(propMap.getString("textColor"))); } if (BridgeUtils.validate(propMap, ReadableType.Number, "textSize")) { chart.setDescriptionTextSize((float) propMap.getDouble("textSize")); } if (BridgeUtils.validate(propMap, ReadableType.Number, "positionX") && BridgeUtils.validate(propMap, ReadableType.Number, "positionY")) { chart.setDescriptionPosition((float) propMap.getDouble("positionX"), (float) propMap.getDouble("positionY")); } if (BridgeUtils.validate(propMap, ReadableType.String, "fontFamily") || BridgeUtils.validate(propMap, ReadableType.Number, "fontStyle")) { chart.setDescriptionTypeface(BridgeUtils.parseTypeface(chart.getContext(), propMap, "fontStyle", "fontFamily")); } }
/** * xAxis config details: https://github.com/PhilJay/MPAndroidChart/wiki/XAxis */ @ReactProp(name = "xAxis") public void setXAxis(Chart chart, ReadableMap propMap) { XAxis axis = chart.getXAxis(); setCommonAxisConfig(chart, axis, propMap); if (BridgeUtils.validate(propMap, ReadableType.Number, "labelsToSkip")) { axis.setLabelsToSkip(propMap.getInt("labelsToSkip")); } if (BridgeUtils.validate(propMap, ReadableType.Boolean, "avoidFirstLastClipping")) { axis.setAvoidFirstLastClipping(propMap.getBoolean("avoidFirstLastClipping")); } if (BridgeUtils.validate(propMap, ReadableType.Number, "spaceBetweenLabels")) { axis.setSpaceBetweenLabels(propMap.getInt("spaceBetweenLabels")); } if (BridgeUtils.validate(propMap, ReadableType.String, "position")) { axis.setPosition(XAxisPosition.valueOf(propMap.getString("position"))); } }
@ReactProp(name = "zoom") public void setZoom(BarLineChartBase chart, ReadableMap propMap) { if (BridgeUtils.validate(propMap, ReadableType.Number, "scaleX") && BridgeUtils.validate(propMap, ReadableType.Number, "scaleY") && BridgeUtils.validate(propMap, ReadableType.Number, "xValue") && BridgeUtils.validate(propMap, ReadableType.Number, "yValue")) { YAxis.AxisDependency axisDependency = YAxis.AxisDependency.LEFT; if (propMap.hasKey("axisDependency") && propMap.getString("axisDependency").equalsIgnoreCase("RIGHT")) { axisDependency = YAxis.AxisDependency.RIGHT; } chart.zoom( (float) propMap.getDouble("scaleX"), (float) propMap.getDouble("scaleY"), (float) propMap.getDouble("xValue"), (float) propMap.getDouble("yValue"), axisDependency ); } }
@Override void dataSetConfig(IDataSet<Entry> dataSet, ReadableMap config) { ScatterDataSet scatterDataSet = (ScatterDataSet) dataSet; ChartDataSetConfigUtils.commonConfig(scatterDataSet, config); ChartDataSetConfigUtils.commonBarLineScatterCandleBubbleConfig(scatterDataSet, config); ChartDataSetConfigUtils.commonLineScatterCandleRadarConfig(scatterDataSet, config); // ScatterDataSet only config if (BridgeUtils.validate(config, ReadableType.Number, "scatterShapeSize")) { scatterDataSet.setScatterShapeSize((float) config.getDouble("scatterShapeSize")); } if (BridgeUtils.validate(config, ReadableType.String, "scatterShape")) { scatterDataSet.setScatterShape(ScatterShape.valueOf(config.getString("scatterShape").toUpperCase())); } if (BridgeUtils.validate(config, ReadableType.String, "scatterShapeHoleColor")) { scatterDataSet.setScatterShapeHoleColor(Color.parseColor(config.getString("scatterShapeHoleColor"))); } if (BridgeUtils.validate(config, ReadableType.Number, "scatterShapeHoleRadius")) { scatterDataSet.setScatterShapeHoleRadius((float) config.getDouble("scatterShapeHoleRadius")); } }
public static void commonLineRadarConfig(LineRadarDataSet dataSet, ReadableMap config) { if (BridgeUtils.validate(config, ReadableType.String, "fillColor")) { dataSet.setFillColor(Color.parseColor(config.getString("fillColor"))); } if (BridgeUtils.validate(config, ReadableType.Number, "fillAlpha")) { dataSet.setFillAlpha(config.getInt("fillAlpha")); } // TODO setFillDrawable android.graphics.drawable.Drawable if (BridgeUtils.validate(config, ReadableType.Boolean, "drawFilled")) { dataSet.setDrawFilled(config.getBoolean("drawFilled")); } if (BridgeUtils.validate(config, ReadableType.Number, "lineWidth")) { float lineWidth = (float) config.getDouble("lineWidth"); if (lineWidth >= 0.2f && lineWidth < 10f) { dataSet.setLineWidth(lineWidth); } } }
@NonNull private JSONObject createJsonObject(final ReadableMap properties) throws JSONException { JSONObject jsonPayload = new JSONObject(); for (final ReadableMapKeySetIterator iterator = properties.keySetIterator(); iterator.hasNextKey();) { final String key = iterator.nextKey(); final ReadableType type = properties.getType(key); switch (type) { case String: jsonPayload.put(key, properties.getString(key)); break; case Array: jsonPayload.put(key, properties.getArray(key)); break; case Boolean: jsonPayload.put(key, properties.getBoolean(key)); break; case Number: jsonPayload.put(key, properties.getDouble(key)); break; case Map: jsonPayload.put(key, createJsonObject(properties.getMap(key))); break; } jsonPayload.put(key, type); } return jsonPayload; }
static private OAuthRequest addParametersToRequest( OAuthRequest request, final String access_token, @Nullable final ReadableMap params ) { if (params != null && params.hasKey("params")) { ReadableMapKeySetIterator iterator = params.keySetIterator(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); ReadableType readableType = params.getType(key); switch(readableType) { case String: String val = params.getString(key); // String escapedVal = Uri.encode(val); if (val.equals("access_token")) { val = access_token; } request.addParameter(key, val); break; default: throw new IllegalArgumentException("Could not read object with key: " + key); } } } return request; }
public void exec(final String sql, final ReadableArray values) { SQLiteStatement statement = Assertions.assertNotNull(db).compileStatement(sql); db.beginTransaction(); try { for (int i=0; i < values.size(); i++) { if (values.getType(i) == ReadableType.Number) { statement.bindLong(i+1, values.getInt(i)); } else { statement.bindString(i+1, values.getString(i)); } } statement.execute(); db.setTransactionSuccessful(); } catch (Exception e) { throw e; } finally { db.endTransaction(); } }