Java Code Examples for com.facebook.react.bridge.ReadableMapKeySetIterator#nextKey()

The following examples show how to use com.facebook.react.bridge.ReadableMapKeySetIterator#nextKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SegmentAnalyticsModule.java    From react-native-segment-analytics with MIT License 5 votes vote down vote up
/**
 * Transforms ReadableMap to java.util.Map object
 * @param readableMap should not be null or empty
 * @return Map object of readableMap
 */
private Map<String, Object> toMap (ReadableMap readableMap) {
    ReadableMapKeySetIterator iterator = readableMap.keySetIterator();

    Map<String, Object> map = new HashMap<>();
    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        ReadableType readableType = readableMap.getType(key);

        switch (readableType) {
            case Null:
                map.put(key, null);
                break;
            case Boolean:
                map.put(key, readableMap.getBoolean(key));
                break;
            case Number:
                // Can be int or double.
                map.put(key, readableMap.getDouble(key));
                break;
            case String:
                map.put(key, readableMap.getString(key));
                break;
            case Map:
                map.put(key, this.toMap(readableMap.getMap(key)));
                break;
            case Array:
                map.put(key, this.toArray(readableMap.getArray(key)));
                break;
            default:
                throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
        }
    }
    return map;
}
 
Example 2
Source File: ReactNativeJson.java    From react-native-fcm with MIT License 5 votes vote down vote up
public static JSONObject convertMapToJson(ReadableMap readableMap) throws JSONException {
    JSONObject object = new JSONObject();
    ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
    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;
        }
    }
    return object;
}
 
Example 3
Source File: CatalystNativeJSToJavaParametersTestCase.java    From react-native-GPay with MIT License 5 votes vote down vote up
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);
}
 
Example 4
Source File: Downloader.java    From react-native-simple-download-manager with MIT License 5 votes vote down vote up
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 external = requestConfig.getBoolean("external");
    String external_path = requestConfig.getString("path");

    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));
    }

    

    if(external){
        request.setDestinationInExternalPublicDir(external_path, saveAsName);

    }else{
        request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, saveAsName);

    }

    request.setTitle(downloadTitle);
    request.setDescription(downloadDescription);
    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;
}
 
Example 5
Source File: MapUtil.java    From react-native-background-geolocation with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 6
Source File: ViewManagerPropertyUpdater.java    From react-native-GPay with MIT License 5 votes vote down vote up
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);
  }
}
 
Example 7
Source File: FirestackCloudMessaging.java    From react-native-firestack with MIT License 5 votes vote down vote up
@ReactMethod
public void send(String senderId, String messageId, String messageType, ReadableMap params, final Callback callback) {
    FirebaseMessaging fm = FirebaseMessaging.getInstance();
    RemoteMessage.Builder remoteMessage = new RemoteMessage.Builder(senderId);
    remoteMessage.setMessageId(messageId);
    remoteMessage.setMessageType(messageType);
    ReadableMapKeySetIterator iterator = params.keySetIterator();
    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        ReadableType type = params.getType(key);
        if (type == ReadableType.String) {
            remoteMessage.addData(key, params.getString(key));
            Log.d(TAG, "Firebase send: " + key);
            Log.d(TAG, "Firebase send: " + params.getString(key));
        }
    }
    try {
        fm.send(remoteMessage.build());
        WritableMap res = Arguments.createMap();
        res.putString("status", "success");
        callback.invoke(null, res);
    } catch(Exception e) {
        Log.e(TAG, "Error sending message", e);
        WritableMap error = Arguments.createMap();
        error.putString("code", e.toString());
        error.putString("message", e.toString());
        callback.invoke(error);
    }
}
 
Example 8
Source File: JsonConvert.java    From react-native-cordova with MIT License 5 votes vote down vote up
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;
}
 
Example 9
Source File: FIRMessagingModule.java    From react-native-fcm with MIT License 5 votes vote down vote up
@ReactMethod
public void send(String senderId, ReadableMap payload) throws Exception {
    FirebaseMessaging fm = FirebaseMessaging.getInstance();
    RemoteMessage.Builder message = new RemoteMessage.Builder(senderId + "@gcm.googleapis.com")
    .setMessageId(UUID.randomUUID().toString());

    ReadableMapKeySetIterator iterator = payload.keySetIterator();
    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        String value = getStringFromReadableMap(payload, key);
        message.addData(key, value);
    }
    fm.send(message.build());
}
 
Example 10
Source File: MapUtil.java    From Instabug-React-Native with MIT License 5 votes vote down vote up
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;
}
 
Example 11
Source File: PjActions.java    From react-native-pjsip with GNU General Public License v3.0 5 votes vote down vote up
private static Map<String, Object> formatMap(ReadableMap map) {
    Map<String, Object> value = new HashMap<>();
    ReadableMapKeySetIterator mapIt = map.keySetIterator();

    while (mapIt.hasNextKey()) {
        String mapKey = mapIt.nextKey();

        switch (map.getType(mapKey)) {
            case Null:
                value.put(mapKey, null);
                break;
            case String:
                value.put(mapKey, map.getString(mapKey));
                break;
            case Number:
                value.put(mapKey, map.getInt(mapKey));
                break;
            case Boolean:
                value.put(mapKey, map.getBoolean(mapKey));
                break;
            case Array:
                value.put(mapKey, map.getArray(mapKey).toArrayList());
                break;
            case Map:
                value.put(mapKey, formatMap(map.getMap(mapKey)));
                break;
            default:
                Log.w(TAG, "Unable to put extra information for intent: unknown type \""+ map.getType(mapKey) +"\"");
                break;
        }
    }

    return value;
}
 
Example 12
Source File: PjActions.java    From react-native-sip with GNU General Public License v3.0 5 votes vote down vote up
private static Map<String, Object> formatMap(ReadableMap map) {
    Map<String, Object> value = new HashMap<>();
    ReadableMapKeySetIterator mapIt = map.keySetIterator();

    while (mapIt.hasNextKey()) {
        String mapKey = mapIt.nextKey();

        switch (map.getType(mapKey)) {
            case Null:
                value.put(mapKey, null);
                break;
            case String:
                value.put(mapKey, map.getString(mapKey));
                break;
            case Number:
                value.put(mapKey, map.getInt(mapKey));
                break;
            case Boolean:
                value.put(mapKey, map.getBoolean(mapKey));
                break;
            case Array:
                value.put(mapKey, map.getArray(mapKey).toArrayList());
                break;
            case Map:
                value.put(mapKey, formatMap(map.getMap(mapKey)));
                break;
            default:
                Log.w(TAG, "Unable to put extra information for intent: unknown type \""+ map.getType(mapKey) +"\"");
                break;
        }
    }

    return value;
}
 
Example 13
Source File: OAuthManagerModule.java    From react-native-oauth with MIT License 5 votes vote down vote up
public static Map<String, Object> recursivelyDeconstructReadableMap(ReadableMap readableMap) {
  Map<String, Object> deconstructedMap = new HashMap<>();
  if (readableMap == null) {
    return deconstructedMap;
  }

  ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
  while (iterator.hasNextKey()) {
    String key = iterator.nextKey();
    ReadableType type = readableMap.getType(key);
    switch (type) {
      case Null:
        deconstructedMap.put(key, null);
        break;
      case Boolean:
        deconstructedMap.put(key, readableMap.getBoolean(key));
        break;
      case Number:
        deconstructedMap.put(key, readableMap.getDouble(key));
        break;
      case String:
        deconstructedMap.put(key, readableMap.getString(key));
        break;
      case Map:
        deconstructedMap.put(key, OAuthManagerModule.recursivelyDeconstructReadableMap(readableMap.getMap(key)));
        break;
      case Array:
        deconstructedMap.put(key, OAuthManagerModule.recursivelyDeconstructReadableArray(readableMap.getArray(key)));
        break;
      default:
        throw new IllegalArgumentException("Could not convert object with key: " + key + ".");
    }

  }
  return deconstructedMap;
}
 
Example 14
Source File: SipMessageDTO.java    From react-native-sip with GNU General Public License v3.0 5 votes vote down vote up
public static SipMessageDTO fromReadableMap(ReadableMap data) {
    SipMessageDTO result = new SipMessageDTO();

    if (data.hasKey("targetURI")) {
        result.setTargetUri(data.getString("targetURI"));
    }
    if (data.hasKey("headers")) {
        ReadableMap headersData = data.getMap("headers");
        ReadableMapKeySetIterator headersIt = headersData.keySetIterator();
        Map<String, String> headers = new HashMap<>();

        while (headersIt.hasNextKey()) {
            String key = headersIt.nextKey();
            headers.put(key, headersData.getString(key));
        }

        result.setHeaders(headers);
    }
    if (data.hasKey("contentType")) {
        result.setContentType(data.getString("contentType"));
    }
    if (data.hasKey("body")) {
        result.setBody(data.getString("body"));
    }

    return result;
}
 
Example 15
Source File: Jsons.java    From pili-react-native with MIT License 4 votes vote down vote up
public static JSONObject readableMapToJson(ReadableMap readableMap) {
    JSONObject jsonObject = new JSONObject();

    if (readableMap == null) {
        return null;
    }

    ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
    if (!iterator.hasNextKey()) {
        return null;
    }

    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        ReadableType readableType = readableMap.getType(key);

        try {
            switch (readableType) {
                case Null:
                    jsonObject.put(key, null);
                    break;
                case Boolean:
                    jsonObject.put(key, readableMap.getBoolean(key));
                    break;
                case Number:
                    // Can be int or double.
                    jsonObject.put(key, readableMap.getInt(key));
                    break;
                case String:
                    jsonObject.put(key, readableMap.getString(key));
                    break;
                case Map:
                    jsonObject.put(key, readableMapToJson(readableMap.getMap(key)));
                    break;
                case Array:
                    jsonObject.put(key, readableMap.getArray(key));
                default:
                    // Do nothing and fail silently
            }
        } catch (JSONException ex) {
            // Do nothing and fail silently
        }
    }

    return jsonObject;
}
 
Example 16
Source File: PhotoViewWrapper.java    From react-native-image-zoom with MIT License 4 votes vote down vote up
public void setSource(ReadableMap params) {
    mParams = params;
    @Nullable Uri mUri = null;

    String source = params.hasKey("uri") ? params.getString("uri") : null;
    String thumbnail = params.hasKey("thumbnail") ? params.getString("thumbnail") : null;
    ReadableMap headers = params.hasKey("headers") ? params.getMap("headers") : null;

    //handle base64
    if (source.startsWith("data:image/png;base64,")){
        Glide
            .with(this.getContext())
            .load(Base64.decode(source.replaceAll("data:image\\/.*;base64,", ""), Base64.DEFAULT))
            .into(this)
        ;
        return;
    }

    boolean useStorageFile = false ;

    // handle bundled app resources
    try {
        mUri = Uri.parse(source);
        // Verify scheme is set, so that relative uri (used by static resources) are not handled.
        if (mUri.getScheme() == null) {
            mUri = null;
        } else if(
            !mUri.getScheme().equals("http") &&
            !mUri.getScheme().equals("https")
        ){
            useStorageFile = true ;

            if (!mInitialized) {
                this.setImageURI(mUri);
            }
        }
    } catch (Exception e) {
        // ignore malformed uri, then attempt to extract resource ID.
    }

    if (mUri == null) {
        mUri = mResourceDrawableIdHelper.getResourceDrawableUri(
            this.getContext(),
            source
        );
        Glide
            .with(this.getContext())
            .load(mUri)
            .into(this);
    } else if (useStorageFile) {
        Glide
            .with(this.getContext())
            .load(mUri)
            .into(this);
    } else {
        // Handle an http / https address
        RequestListener listener = this.getRequestListener();

        LazyHeaders.Builder lazyHeaders = new LazyHeaders.Builder();
        Log.d("null headers", String.valueOf(headers != null));
        if(headers != null){
            ReadableMapKeySetIterator it = headers.keySetIterator();
            Log.d("next headers", String.valueOf(it.hasNextKey()));
            while(it.hasNextKey()){
                String Key = it.nextKey();
                lazyHeaders.addHeader(Key, headers.getString(Key));
            }
        }

        Log.d("thing", mUri.toString());
        DrawableRequestBuilder builder = Glide
                .with(this.getContext())
                .load(new GlideUrl(mUri.toString(), lazyHeaders.build()))
                .listener(listener);

        //set thumbnails
        if(thumbnail != null) {
            DrawableRequestBuilder<String> thumbnailRequest = Glide
                    .with(this.getContext())
                    .load(thumbnail);
            builder = builder.thumbnail(thumbnailRequest);
        }

        builder.into(this);
    }

    this.setChangeListeners();
}
 
Example 17
Source File: RCTConvert.java    From react-native-twilio-chat with MIT License 4 votes vote down vote up
public static JSONObject readableMapToJson(ReadableMap readableMap) {
    JSONObject jsonObject = new JSONObject();

    if (readableMap == null) {
        return null;
    }

    ReadableMapKeySetIterator iterator = readableMap.keySetIterator();
    if (!iterator.hasNextKey()) {
        return null;
    }

    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        ReadableType readableType = readableMap.getType(key);

        try {
            switch (readableType) {
                case Null:
                    jsonObject.put(key, null);
                    break;
                case Boolean:
                    jsonObject.put(key, readableMap.getBoolean(key));
                    break;
                case Number:
                    // Can be int or double.
                    jsonObject.put(key, readableMap.getInt(key));
                    break;
                case String:
                    jsonObject.put(key, readableMap.getString(key));
                    break;
                case Map:
                    jsonObject.put(key, readableMapToJson(readableMap.getMap(key)));
                    break;
                case Array:
                    jsonObject.put(key, readableMap.getArray(key));
                default:
                    // Do nothing and fail silently
            }
        } catch (JSONException ex) {
            // Do nothing and fail silently
        }
    }

    return jsonObject;
}
 
Example 18
Source File: RNX5WebViewManager.java    From react-native-x5 with MIT License 4 votes vote down vote up
@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);
}
 
Example 19
Source File: RNStripeTerminalModule.java    From react-native-stripe-terminal with MIT License 4 votes vote down vote up
private PaymentIntentParameters.Builder getPaymentParams(ReadableMap options){
    PaymentIntentParameters.Builder paymentIntentParamBuilder = new PaymentIntentParameters.Builder();
    if(options!=null) {
        if (options.hasKey(AMOUNT)) {
            paymentIntentParamBuilder.setAmount(options.getInt(AMOUNT));
        }

        if (options.hasKey(CURRENCY)) {
            paymentIntentParamBuilder.setCurrency(options.getString(CURRENCY));
        }

        if (options.hasKey(APPLICATION_FEE_AMOUNT)) {
            paymentIntentParamBuilder.setApplicationFeeAmount(options.getInt(APPLICATION_FEE_AMOUNT));
        }

        if (options.hasKey(ON_BEHALF_OF)) {
            paymentIntentParamBuilder.setOnBehalfOf(options.getString(ON_BEHALF_OF));
        }

        if (options.hasKey(TRANSFER_DATA_DESTINATION)) {
            paymentIntentParamBuilder.setTransferDataDestination(options.getString(TRANSFER_DATA_DESTINATION));
        }

        if (options.hasKey(TRANSFER_GROUP)) {
            paymentIntentParamBuilder.setTransferGroup(TRANSFER_GROUP);
        }

        if (options.hasKey(CUSTOMER)) {
            paymentIntentParamBuilder.setCustomer(options.getString(CUSTOMER));
        }

        if (options.hasKey(DESCRIPTION)) {
            paymentIntentParamBuilder.setDescription(options.getString(DESCRIPTION));
        }

        if (options.hasKey(STATEMENT_DESCRIPTOR)) {
            paymentIntentParamBuilder.setStatementDescriptor(options.getString(STATEMENT_DESCRIPTOR));
        }

        if (options.hasKey(RECEIPT_EMAIL)) {
            paymentIntentParamBuilder.setReceiptEmail(options.getString(RECEIPT_EMAIL));
        }

        if (options.hasKey(METADATA)) {
            ReadableMap map = options.getMap(METADATA);
            HashMap<String, String> metaDataMap = new HashMap<>();

            if (map != null) {
                ReadableMapKeySetIterator iterator = options.keySetIterator();
                while (iterator.hasNextKey()) {
                    String key = iterator.nextKey();
                    String val = options.getString(key);
                    metaDataMap.put(key, val);
                }
            }

            paymentIntentParamBuilder.setMetadata(metaDataMap);
        }
    }

    return paymentIntentParamBuilder;
}
 
Example 20
Source File: TwilioVoiceModule.java    From react-native-twilio-programmable-voice with MIT License 4 votes vote down vote up
@ReactMethod
public void connect(ReadableMap params) {
    if (BuildConfig.DEBUG) {
        Log.d(TAG, "connect params: "+params);
    }
    WritableMap errParams = Arguments.createMap();
    if (accessToken == null) {
        errParams.putString("err", "Invalid access token");
        eventManager.sendEvent(EVENT_DEVICE_NOT_READY, errParams);
        return;
    }
    if (params == null) {
        errParams.putString("err", "Invalid parameters");
        eventManager.sendEvent(EVENT_CONNECTION_DID_DISCONNECT, errParams);
        return;
    } else if (!params.hasKey("To")) {
        errParams.putString("err", "Invalid To parameter");
        eventManager.sendEvent(EVENT_CONNECTION_DID_DISCONNECT, errParams);
        return;
    }
    toNumber = params.getString("To");
    if (params.hasKey("ToName")) {
        toName = params.getString("ToName");
    }

    twiMLParams.clear();

    ReadableMapKeySetIterator iterator = params.keySetIterator();
    while (iterator.hasNextKey()) {
        String key = iterator.nextKey();
        ReadableType readableType = params.getType(key);
        switch (readableType) {
            case Null:
                twiMLParams.put(key, "");
                break;
            case Boolean:
                twiMLParams.put(key, String.valueOf(params.getBoolean(key)));
                break;
            case Number:
                // Can be int or double.
                twiMLParams.put(key, String.valueOf(params.getDouble(key)));
                break;
            case String:
                twiMLParams.put(key, params.getString(key));
                break;
            default:
                Log.d(TAG, "Could not convert with key: " + key + ".");
                break;
        }
    }

    activeCall = Voice.call(getReactApplicationContext(), accessToken, twiMLParams, callListener);
}