Example usage for com.facebook.react.bridge ReadableMap getString

List of usage examples for com.facebook.react.bridge ReadableMap getString

Introduction

In this page you can find the example usage for com.facebook.react.bridge ReadableMap getString.

Prototype

@Nullable
    String getString(@NonNull String name);

Source Link

Usage

From source file:com.boundlessgeo.spatialconnect.jsbridge.SCBridge.java

License:Apache License

private static JSONObject convertMapToJson(ReadableMap readableMap) {
    JSONObject object = new JSONObject();
    ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
    try {/*  w  w  w.ja va 2 s  .  c o  m*/

        while (iterator.hasNextKey()) {
            String key = iterator.nextKey();
            switch (readableMap.getType(key)) {
            case Null:
                object.put(key, JSONObject.NULL);
                break;
            case Boolean:
                object.put(key, readableMap.getBoolean(key));
                break;
            case Number:
                object.put(key, readableMap.getDouble(key));
                break;
            case String:
                object.put(key, readableMap.getString(key));
                break;
            case Map:
                object.put(key, convertMapToJson(readableMap.getMap(key)));
                break;
            case Array:
                object.put(key, convertArrayToJson(readableMap.getArray(key)));
                break;
            }
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Could not convert to json");
        e.printStackTrace();
    }
    return object;
}

From source file:com.devialab.camerarollextended.CameraRoll.java

License:Open Source License

/**
 * Save an image to the gallery (i.e. {@link MediaStore.Images}). This copies the original file
 * from wherever it may be to the external storage pictures directory, so that it can be scanned
 * by the MediaScanner.//from  ww w .j  a  v  a 2  s  .c o m
 *
 * @param uri the file:// URI of the image to save
 * @param promise to be resolved or rejected
 */
@ReactMethod
public void saveToCameraRoll(ReadableMap tag, String type, Promise promise) {
    MediaType parsedType = type.equals("video") ? MediaType.VIDEO : MediaType.PHOTO;
    new SaveToCameraRoll(getReactApplicationContext(), Uri.parse(tag.getString("uri")), tag.getString("album"),
            parsedType, promise).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

From source file:com.dylanvann.cameraroll.CameraRollManager.java

License:Open Source License

/**
 * Get photos from {@link MediaStore.Images}, most recent first.
 *
 * @param params a map containing the following keys:
 *        <ul>//from w w w .j av  a2  s . c o m
 *          <li>first (mandatory): a number representing the number of photos to fetch</li>
 *          <li>
 *            after (optional): a cursor that matches page_info[end_cursor] returned by a
 *            previous call to {@link #getPhotos}
 *          </li>
 *          <li>groupName (optional): an album name</li>
 *          <li>
 *            mimeType (optional): restrict returned images to a specific mimetype (e.g.
 *            image/jpeg)
 *          </li>
 *        </ul>
 * @param promise the Promise to be resolved when the photos are loaded; for a format of the
 *        parameters passed to this callback, see {@code getPhotosReturnChecker} in CameraRoll.js
 */
@ReactMethod
public void getPhotos(final ReadableMap params, final Promise promise) {
    int first = params.getInt("first");
    String after = params.hasKey("after") ? params.getString("after") : null;
    String albumId = params.hasKey("albumId") ? params.getString("albumId") : null;
    ReadableArray mimeTypes = params.hasKey("mimeTypes") ? params.getArray("mimeTypes") : null;
    if (params.hasKey("groupTypes")) {
        throw new JSApplicationIllegalArgumentException("groupTypes is not supported on Android");
    }

    new GetPhotosTask(getReactApplicationContext(), first, after, albumId, mimeTypes, promise)
            .executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

From source file:com.farmisen.react_native_file_uploader.RCTFileUploaderModule.java

License:Open Source License

@ReactMethod
public void upload(final ReadableMap settings, final Callback callback) {
    String uri = settings.getString(URI_FIELD);
    String path = null;//  w  w w.jav a 2 s .com
    if (uri.startsWith("file:") || uri.startsWith("content:")) {
        path = (Uri.parse(uri)).getPath();
    } else if (this.isAbsolutePath(uri)) {
        path = uri;
    } else {
        callback.invoke("Can't handle " + uri, null);
    }
    this.uploadFile(path, settings, callback);
}

From source file:com.farmisen.react_native_file_uploader.RCTFileUploaderModule.java

License:Open Source License

private void uploadFile(String path, ReadableMap settings, Callback callback) {
    HttpURLConnection connection = null;
    FileUploadCountingOutputStream outputStream = null;
    InputStream inputStream = null;

    String boundary = "*****" + UUID.randomUUID().toString() + "*****";

    try {//  www. ja va 2 s  .com
        URL url = new URL(settings.getString(UPLOAD_URL_FIELD));
        connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        String method = getStringParam(settings, METHOD_FIELD, "POST");
        connection.setRequestMethod(method);
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("User-Agent", "React Native File Uploader Android HTTP Client");
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

        String contentType = getStringParam(settings, CONTENT_TYPE_FIELD, "application/octet-stream");
        String fileName = getStringParam(settings, FILE_NAME_FIELD, this.filenameForContentType(contentType));
        String fieldName = getStringParam(settings, FIELD_NAME_FIELD, "file");

        File file = new File(path);
        FileInputStream fileInputStream = new FileInputStream(file);
        int bytesRead, bytesAvailable, bufferSize;
        byte[] buffer;
        int maxBufferSize = MAX_BUFFER_SIZE;

        outputStream = new FileUploadCountingOutputStream(new DataOutputStream(connection.getOutputStream()),
                file.length(), settings.getString(URI_FIELD), this);
        outputStream.writeBytes(TWO_HYPHENS + boundary + LINE_END);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"" + fieldName + "\"; filename=\""
                + fileName + "\"" + LINE_END);
        outputStream.writeBytes("Content-Type: " + contentType + LINE_END);
        outputStream.writeBytes("Content-Transfer-Encoding: binary" + LINE_END);

        outputStream.writeBytes(LINE_END);
        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];
        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        while (bytesRead > 0) {
            outputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }
        outputStream.writeBytes(LINE_END);

        ReadableMap params = getMapParam(settings, "data", Arguments.createMap());
        ReadableMapKeySetIterator keys = params.keySetIterator();
        while (keys.hasNextKey()) {
            String key = keys.nextKey();
            ReadableType type = params.getType(key);
            String value = null;
            switch (type) {
            case String:
                value = params.getString(key);
                break;
            case Number:
                value = Integer.toString(params.getInt(key));
                break;
            default:
                callback.invoke(type.toString() + " type not supported.", null);
                break;
            }

            outputStream.writeBytes(TWO_HYPHENS + boundary + LINE_END);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + LINE_END);
            outputStream.writeBytes("Content-Type: text/plain" + LINE_END);
            outputStream.writeBytes(LINE_END + value + LINE_END);
        }

        outputStream.writeBytes(TWO_HYPHENS + boundary + TWO_HYPHENS + LINE_END);

        inputStream = connection.getInputStream();
        String responseBody = this.streamToString(inputStream);

        fileInputStream.close();
        inputStream.close();
        outputStream.flush();
        outputStream.close();

        WritableMap result = Arguments.createMap();
        result.putString("data", responseBody);
        result.putInt("status", connection.getResponseCode());
        callback.invoke(null, result);
    } catch (Exception e) {
        callback.invoke(e.getLocalizedMessage(), null);
    }
}

From source file:com.farmisen.react_native_file_uploader.RCTFileUploaderModule.java

License:Open Source License

private String getStringParam(ReadableMap map, String key, String defaultValue) {
    if (map.hasKey(key)) {
        return map.getString(key);
    } else {//from www.  j ava  2s  . c  om
        return defaultValue;
    }
}

From source file:com.hijridatepicker.HijriDatePickerAndroidModule.java

License:Open Source License

private Bundle createFragmentArguments(ReadableMap options, Promise promise) {
    final Bundle args = new Bundle();
    try {/*  w  ww.j a  va2 s .  co  m*/
        if (options.hasKey(ARG_DATE) && !options.isNull(ARG_DATE)) {
            if (!parseOptionsWithKey(ARG_DATE, options, args, promise))
                return null;
        }
        if (options.hasKey(ARG_MINDATE) && !options.isNull(ARG_MINDATE)) {
            if (!parseOptionsWithKey(ARG_MINDATE, options, args, promise))
                return null;
        }
        if (options.hasKey(ARG_MAXDATE) && !options.isNull(ARG_MAXDATE)) {
            if (!parseOptionsWithKey(ARG_MAXDATE, options, args, promise))
                return null;
        }
        if (options.hasKey(ARG_MODE) && !options.isNull(ARG_MODE)) {
            args.putString(ARG_MODE, options.getString(ARG_MODE));
        }
        if (options.hasKey(ARG_WEEK_DAY_LABELS) && !options.isNull(ARG_WEEK_DAY_LABELS)) {
            args.putStringArrayList(ARG_WEEK_DAY_LABELS,
                    toStringArrayList(options.getArray(ARG_WEEK_DAY_LABELS)));
        }
    } catch (Exception e) {
        promise.reject(ERROR_PARSING_OPTIONS,
                "Exception happened while parsing options, details: " + e.getMessage());
        return null;
    }
    return args;
}

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 {//  w  w  w . j ava  2  s . co m
            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.horcrux.svg.GlyphContext.java

License:Open Source License

public ReadableMap getGlyphFont() {
    String fontFamily = null;/*  w  w w  . j av  a  2s .  c  o  m*/
    float fontSize = DEFAULT_FONT_SIZE;
    boolean fontSizeSet = false;
    String fontWeight = null;
    String fontStyle = null;

    int index = mContextLength - 1;

    for (; index >= 0; index--) {
        ReadableMap font = mFontContext.get(index);

        if (fontFamily == null && font.hasKey("fontFamily")) {
            fontFamily = font.getString("fontFamily");
        }

        if (!fontSizeSet && font.hasKey("fontSize")) {
            fontSize = (float) font.getDouble("fontSize");
            fontSizeSet = true;
        }

        if (fontWeight == null && font.hasKey("fontWeight")) {
            fontWeight = font.getString("fontWeight");
        }
        if (fontStyle == null && font.hasKey("fontStyle")) {
            fontStyle = font.getString("fontStyle");
        }

        if (fontFamily != null && fontSizeSet && fontWeight != null && fontStyle != null) {
            break;
        }
    }

    WritableMap map = Arguments.createMap();
    map.putString("fontFamily", fontFamily);
    map.putDouble("fontSize", fontSize);
    map.putString("fontWeight", fontWeight);
    map.putString("fontStyle", fontStyle);

    return map;
}

From source file:com.horcrux.svg.ImageShadowNode.java

License:Open Source License

@ReactProp(name = "src")
public void setSrc(@Nullable ReadableMap src) {
    if (src != null) {
        String uriString = src.getString("uri");

        if (uriString == null || uriString.isEmpty()) {
            //TODO: give warning about this
            return;
        }//from   www. ja v  a  2  s .co  m

        mImageRatio = (float) src.getInt("width") / (float) src.getInt("height");
        mUri = Uri.parse(uriString);
    }
}