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 static boolean isLayoutOnlyAndCollapsable(@Nullable ReactStylesDiffMap props) { if (props == null) { return true; } if (props.hasKey(ViewProps.COLLAPSABLE) && !props.getBoolean(ViewProps.COLLAPSABLE, true)) { return false; } ReadableMapKeySetIterator keyIterator = props.mBackingMap.keySetIterator(); while (keyIterator.hasNextKey()) { if (!ViewProps.isLayoutOnly(props.mBackingMap, keyIterator.nextKey())) { return false; } } return true; }
public void testMapIterateOverMapWithBasicTypes() { mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnMapWithBasicTypes(); waitForBridgeAndUIIdle(); List<ReadableMap> calls = mRecordingTestModule.getMapCalls(); assertEquals(1, calls.size()); ReadableNativeMap map = (ReadableNativeMap) calls.get(0); assertNotNull(map); ReadableMapKeySetIterator mapIterator = map.keySetIterator(); Set<String> keys = new HashSet<String>(); while (mapIterator.hasNextKey()) { keys.add(mapIterator.nextKey()); } Set<String> expectedKeys = new HashSet<String>( Arrays.asList("stringKey", "doubleKey", "intKey", "booleanKey", "nullKey")); assertEquals(keys, expectedKeys); }
public void testMapIterateOverNestedMaps() { mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnNestedMap(); waitForBridgeAndUIIdle(); List<ReadableMap> calls = mRecordingTestModule.getMapCalls(); assertEquals(1, calls.size()); ReadableNativeMap map = (ReadableNativeMap) calls.get(0); assertNotNull(map); ReadableMapKeySetIterator firstLevelIterator = map.keySetIterator(); String firstLevelKey = firstLevelIterator.nextKey(); assertEquals(firstLevelKey, "weHaveToGoDeeper"); ReadableNativeMap secondMap = map.getMap("weHaveToGoDeeper"); ReadableMapKeySetIterator secondLevelIterator = secondMap.keySetIterator(); String secondLevelKey = secondLevelIterator.nextKey(); assertEquals(secondLevelKey, "inception"); assertTrue(secondMap.getBoolean(secondLevelKey)); }
private static boolean isLayoutOnlyAndCollapsable(@Nullable ReactStylesDiffMap props) { if (props == null) { return true; } if (props.hasKey(ViewProps.COLLAPSABLE) && !props.getBoolean(ViewProps.COLLAPSABLE, true)) { return false; } ReadableMapKeySetIterator keyIterator = props.mBackingMap.keySetIterator(); while (keyIterator.hasNextKey()) { if (!ViewProps.isLayoutOnly(keyIterator.nextKey())) { return false; } } return true; }
private static Map<String, String> toMap(@Nullable ReadableMap readableMap) { if (readableMap == null) { return null; } ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); if (!iterator.hasNextKey()) { return null; } Map<String, String> result = new HashMap<>(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); result.put(key, readableMap.getString(key)); } return result; }
@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; }
private Map<String, Object> getUserProperties(ReadableMap properties) { ReadableMapKeySetIterator iterator = properties.keySetIterator(); Map<String, Object> userProperties = new HashMap<>(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); ReadableType type = properties.getType(key); if (type == ReadableType.Boolean) { userProperties.put(key, properties.getBoolean(key)); } else if (type == ReadableType.Number) { userProperties.put(key, properties.getDouble(key)); } else if (type == ReadableType.String) { userProperties.put(key, properties.getString(key)); } } return userProperties; }
/** * Parses a constraint set specified in the form of a JavaScript object into * a specific <tt>List</tt> of <tt>MediaConstraints.KeyValuePair</tt>s. * * @param src The constraint set in the form of a JavaScript object to * parse. * @param dst The <tt>List</tt> of <tt>MediaConstraints.KeyValuePair</tt>s * into which the specified <tt>src</tt> is to be parsed. */ private void parseConstraints( ReadableMap src, List<MediaConstraints.KeyValuePair> dst) { ReadableMapKeySetIterator keyIterator = src.keySetIterator(); while (keyIterator.hasNextKey()) { String key = keyIterator.nextKey(); String value = ReactBridgeUtil.getMapStrValue(src, key); dst.add(new MediaConstraints.KeyValuePair(key, value)); } }
public static JSONObject toJSONObject(ReadableMap readableMap) throws JSONException { JSONObject jsonObject = new JSONObject(); ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); ReadableType type = readableMap.getType(key); switch (type) { case Null: jsonObject.put(key, null); break; case Boolean: jsonObject.put(key, readableMap.getBoolean(key)); break; case Number: jsonObject.put(key, readableMap.getDouble(key)); break; case String: jsonObject.put(key, readableMap.getString(key)); break; case Map: jsonObject.put(key, MapUtil.toJSONObject(readableMap.getMap(key))); break; case Array: jsonObject.put(key, ArrayUtil.toJSONArray(readableMap.getArray(key))); break; } } return jsonObject; }
public static Map<String, Object> toMap(ReadableMap readableMap) { Map<String, Object> map = new HashMap<>(); ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); ReadableType type = readableMap.getType(key); switch (type) { case Null: map.put(key, null); break; case Boolean: map.put(key, readableMap.getBoolean(key)); break; case Number: map.put(key, readableMap.getDouble(key)); break; case String: map.put(key, readableMap.getString(key)); break; case Map: map.put(key, MapUtil.toMap(readableMap.getMap(key))); break; case Array: map.put(key, ArrayUtil.toArray(readableMap.getArray(key))); break; } } return map; }
public static JSONObject reactToJSON(ReadableMap readableMap) throws JSONException { JSONObject jsonObject = new JSONObject(); ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); while(iterator.hasNextKey()){ String key = iterator.nextKey(); ReadableType valueType = readableMap.getType(key); switch (valueType){ case Null: jsonObject.put(key,JSONObject.NULL); break; case Boolean: jsonObject.put(key, readableMap.getBoolean(key)); break; case Number: try { jsonObject.put(key, readableMap.getInt(key)); } catch(Exception e) { jsonObject.put(key, readableMap.getDouble(key)); } break; case String: jsonObject.put(key, readableMap.getString(key)); break; case Map: jsonObject.put(key, reactToJSON(readableMap.getMap(key))); break; case Array: jsonObject.put(key, reactToJSON(readableMap.getArray(key))); break; } } return jsonObject; }
public DownloadManager.Request createRequest(String url, ReadableMap headers, ReadableMap requestConfig) { String downloadTitle = requestConfig.getString("downloadTitle"); String downloadDescription = requestConfig.getString("downloadTitle"); String saveAsName = requestConfig.getString("saveAsName"); Boolean allowedInRoaming = requestConfig.getBoolean("allowedInRoaming"); Boolean allowedInMetered = requestConfig.getBoolean("allowedInMetered"); Boolean showInDownloads = requestConfig.getBoolean("showInDownloads"); Uri downloadUri = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(downloadUri); ReadableMapKeySetIterator iterator = headers.keySetIterator(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); request.addRequestHeader(key, headers.getString(key)); } request.setTitle(downloadTitle); request.setDescription(downloadDescription); request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, saveAsName); request.setAllowedOverRoaming(allowedInRoaming); request.setAllowedOverMetered(allowedInMetered); request.setVisibleInDownloadsUi(showInDownloads); request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); return request; }
private List<String> getPropKeys(ReadableMap props) { List<String> keys = new ArrayList<>(); ReadableMapKeySetIterator iterator = props.keySetIterator(); while(iterator.hasNextKey()) { keys.add(iterator.nextKey()); } return keys; }
private List<String> getQueryKeys(ReadableMap query) { List<String> keys = new ArrayList<>(); ReadableMapKeySetIterator iterator = query.keySetIterator(); while(iterator.hasNextKey()) { keys.add(iterator.nextKey()); } return keys; }
@ReactProp(name = "source") public void setSource(WebView view, @Nullable ReadableMap source) { if (source != null) { if (source.hasKey("html")) { String html = source.getString("html"); if (source.hasKey("baseUrl")) { view.loadDataWithBaseURL( source.getString("baseUrl"), html, HTML_MIME_TYPE, HTML_ENCODING, null); } else { view.loadData(html, HTML_MIME_TYPE, HTML_ENCODING); } return; } if (source.hasKey("uri")) { String url = source.getString("uri"); String previousUrl = view.getUrl(); if (previousUrl != null && previousUrl.equals(url)) { return; } if (source.hasKey("method")) { String method = source.getString("method"); if (method.equals(HTTP_METHOD_POST)) { byte[] postData = null; if (source.hasKey("body")) { String body = source.getString("body"); try { postData = body.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { postData = body.getBytes(); } } if (postData == null) { postData = new byte[0]; } view.postUrl(url, postData); return; } } HashMap<String, String> headerMap = new HashMap<>(); if (source.hasKey("headers")) { ReadableMap headers = source.getMap("headers"); ReadableMapKeySetIterator iter = headers.keySetIterator(); while (iter.hasNextKey()) { String key = iter.nextKey(); if ("user-agent".equals(key.toLowerCase(Locale.ENGLISH))) { if (view.getSettings() != null) { view.getSettings().setUserAgentString(headers.getString(key)); } } else { headerMap.put(key, headers.getString(key)); } } } view.loadUrl(url, headerMap); return; } } view.loadUrl(BLANK_URL); }
private Map<String, String> getHeaders(ReadableMap readableMap) { if (readableMap == null) { return null; } ReadableMapKeySetIterator iterator = readableMap.keySetIterator(); Map<String, String> map = new HashMap<>(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); String value = readableMap.getString(key); map.put(key, value); } return map; }
public static <T extends ViewManager, V extends View> void updateProps( T manager, V v, ReactStylesDiffMap props) { ViewManagerSetter<T, V> setter = findManagerSetter(manager.getClass()); ReadableMap propMap = props.mBackingMap; ReadableMapKeySetIterator iterator = propMap.keySetIterator(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); setter.setProperty(manager, v, key, props); } }
public static <T extends ReactShadowNode> void updateProps(T node, ReactStylesDiffMap props) { ShadowNodeSetter<T> setter = findNodeSetter(node.getClass()); ReadableMap propMap = props.mBackingMap; ReadableMapKeySetIterator iterator = propMap.keySetIterator(); while (iterator.hasNextKey()) { String key = iterator.nextKey(); setter.setProperty(node, key, props); } }
StyleAnimatedNode(ReadableMap config, NativeAnimatedNodesManager nativeAnimatedNodesManager) { ReadableMap style = config.getMap("style"); ReadableMapKeySetIterator iter = style.keySetIterator(); mPropMapping = new HashMap<>(); while (iter.hasNextKey()) { String propKey = iter.nextKey(); int nodeIndex = style.getInt(propKey); mPropMapping.put(propKey, nodeIndex); } mNativeAnimatedNodesManager = nativeAnimatedNodesManager; }
PropsAnimatedNode(ReadableMap config, NativeAnimatedNodesManager nativeAnimatedNodesManager) { ReadableMap props = config.getMap("props"); ReadableMapKeySetIterator iter = props.keySetIterator(); mPropMapping = new HashMap<>(); while (iter.hasNextKey()) { String propKey = iter.nextKey(); int nodeIndex = props.getInt(propKey); mPropMapping.put(propKey, nodeIndex); } mNativeAnimatedNodesManager = nativeAnimatedNodesManager; }
public void testInvalidIteratorExceptionThrown() { mCatalystInstance.getJSModule(TestJSToJavaParametersModule.class).returnMapWithBasicTypes(); waitForBridgeAndUIIdle(); List<ReadableMap> calls = mRecordingTestModule.getMapCalls(); assertEquals(1, calls.size()); ReadableNativeMap map = (ReadableNativeMap) calls.get(0); assertNotNull(map); ReadableMapKeySetIterator mapIterator = map.keySetIterator(); while (mapIterator.hasNextKey()) { mapIterator.nextKey(); } assertInvalidIteratorExceptionThrown(mapIterator); }
private static void assertInvalidIteratorExceptionThrown( ReadableMapKeySetIterator iterator) { boolean gotException = false; try { iterator.nextKey(); } catch (InvalidIteratorException expected) { gotException = true; } assertTrue(gotException); }