Example usage for com.facebook.react.bridge ReadableType String

List of usage examples for com.facebook.react.bridge ReadableType String

Introduction

In this page you can find the example usage for com.facebook.react.bridge ReadableType String.

Prototype

ReadableType String

To view the source code for com.facebook.react.bridge ReadableType String.

Click Source Link

Usage

From source file:com.hijridatepicker.HijriDatePickerAndroidModule.java

License:Open Source License

private boolean parseOptionsWithKey(String ARG_KEY, ReadableMap options, Bundle args, Promise promise) {
    ReadableType argDateType = options.getType(ARG_KEY);
    if (ReadableType.String.equals(argDateType)) {
        try {//from ww  w . ja v a 2 s .c om
            long milliSeconds = 0;
            milliSeconds = (long) convertHijriDateToGregorianMilliseconds(options.getString(ARG_KEY));
            args.putLong(ARG_KEY, milliSeconds);
            return true;
        } catch (PatternSyntaxException | IndexOutOfBoundsException | NumberFormatException e) {
            promise.reject(ERROR_PARSING_OPTIONS, "Exception happened while parsing " + ARG_KEY
                    + " we only accept object of Date or String with the format \"dd-MM-yyyy\" in Hijri");
            return false;
        }
    } else if (ReadableType.Number.equals(argDateType)) {
        args.putLong(ARG_KEY, (long) options.getDouble(ARG_KEY));
        return true;
    } else {
        promise.reject(ERROR_PARSING_OPTIONS, "Exception happened while parsing " + ARG_KEY
                + " we only accept object of Date or String with the format \"dd-MM-yyyy\" in Hijri");
        return false;
    }
}

From source file:com.microsoft.appcenter.reactnative.analytics.ReactNativeUtils.java

License:Open Source License

public static Map<String, String> convertReadableMapToStringMap(ReadableMap map) throws JSONException {
    Map<String, String> stringMap = new HashMap<>();
    ReadableMapKeySetIterator it = map.keySetIterator();
    while (it.hasNextKey()) {
        String key = it.nextKey();
        ReadableType type = map.getType(key);
        // Only support storing strings. Non-string data must be stringified in JS.
        if (type == ReadableType.String) {
            stringMap.put(key, map.getString(key));
        }//w w w  .j ava2  s  . c o  m
    }

    return stringMap;
}

From source file:com.silklabs.react.blobs.WebSocketModule.java

License:Open Source License

@ReactMethod
public void connect(final String url, @Nullable final ReadableArray protocols,
        @Nullable final ReadableMap headers, final int id) {
    OkHttpClient client = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS).readTimeout(0, TimeUnit.MINUTES) // Disable timeouts for read
            .build();/*from  w w  w .j  a va  2s  .c o m*/

    Request.Builder builder = new Request.Builder().tag(id).url(url);

    if (headers != null) {
        ReadableMapKeySetIterator iterator = headers.keySetIterator();

        if (!headers.hasKey("origin")) {
            builder.addHeader("origin", setDefaultOrigin(url));
        }

        while (iterator.hasNextKey()) {
            String key = iterator.nextKey();
            if (ReadableType.String.equals(headers.getType(key))) {
                builder.addHeader(key, headers.getString(key));
            } else {
                FLog.w(ReactConstants.TAG, "Ignoring: requested " + key + ", value not a string");
            }
        }
    } else {
        builder.addHeader("origin", setDefaultOrigin(url));
    }

    if (protocols != null && protocols.size() > 0) {
        StringBuilder protocolsValue = new StringBuilder("");
        for (int i = 0; i < protocols.size(); i++) {
            String v = protocols.getString(i).trim();
            if (!v.isEmpty() && !v.contains(",")) {
                protocolsValue.append(v);
                protocolsValue.append(",");
            }
        }
        if (protocolsValue.length() > 0) {
            protocolsValue.replace(protocolsValue.length() - 1, protocolsValue.length(), "");
            builder.addHeader("Sec-WebSocket-Protocol", protocolsValue.toString());
        }
    }

    WebSocketCall.create(client, builder.build()).enqueue(new WebSocketListener() {

        @Override
        public void onOpen(WebSocket webSocket, Response response) {
            mWebSocketConnections.put(id, webSocket);
            WritableMap params = Arguments.createMap();
            params.putInt("id", id);
            sendEvent("websocketOpen", params);
        }

        @Override
        public void onClose(int code, String reason) {
            WritableMap params = Arguments.createMap();
            params.putInt("id", id);
            params.putInt("code", code);
            params.putString("reason", reason);
            sendEvent("websocketClosed", params);
        }

        @Override
        public void onFailure(IOException e, Response response) {
            notifyWebSocketFailed(id, e.getMessage());
        }

        @Override
        public void onPong(Buffer buffer) {
        }

        @Override
        public void onMessage(ResponseBody response) throws IOException {
            WritableMap params = Arguments.createMap();
            params.putInt("id", id);

            if (mBlobsEnabled.containsKey(id) && mBlobsEnabled.get(id)
                    && response.contentType() == WebSocket.BINARY) {
                byte[] data;
                try {
                    data = response.source().readByteArray();
                } catch (IOException e) {
                    notifyWebSocketFailed(id, e.getMessage());
                    return;
                }
                WritableMap blob = Arguments.createMap();
                blob.putString("blobId", BlobModule.store(data));
                blob.putInt("offset", 0);
                blob.putInt("size", data.length);
                params.putMap("data", blob);
                params.putString("type", "blob");
            } else {
                String message;
                try {
                    if (response.contentType() == WebSocket.BINARY) {
                        message = Base64.encodeToString(response.source().readByteArray(), Base64.NO_WRAP);
                    } else {
                        message = response.source().readUtf8();
                    }
                } catch (IOException e) {
                    notifyWebSocketFailed(id, e.getMessage());
                    return;
                }
                params.putString("data", message);
                params.putString("type", response.contentType() == WebSocket.BINARY ? "binary" : "text");
            }

            try {
                response.source().close();
            } catch (IOException e) {
                FLog.e(ReactConstants.TAG, "Could not close BufferedSource for WebSocket id " + id, e);
            }

            sendEvent("websocketMessage", params);
        }
    });

    // Trigger shutdown of the dispatcher's executor so this process can exit cleanly
    client.dispatcher().executorService().shutdown();
}