Java Examples for com.facebook.react.bridge.WritableNativeMap

The following java examples will help you to understand the usage of com.facebook.react.bridge.WritableNativeMap. These source code samples are taken from different open source projects.

Example 1
Project: ReactNativeApp-master  File: XReactInstanceManagerImpl.java View source code
private void attachMeasuredRootViewToInstance(ReactRootView rootView, CatalystInstance catalystInstance) {
    Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachMeasuredRootViewToInstance");
    UiThreadUtil.assertOnUiThread();
    // Reset view content as it's going to be populated by the application content from JS
    rootView.removeAllViews();
    rootView.setId(View.NO_ID);
    UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class);
    int rootTag = uiManagerModule.addMeasuredRootView(rootView);
    rootView.setRootViewTag(rootTag);
    @Nullable Bundle launchOptions = rootView.getLaunchOptions();
    WritableMap initialProps = Arguments.makeNativeMap(launchOptions);
    String jsAppModuleName = rootView.getJSModuleName();
    WritableNativeMap appParams = new WritableNativeMap();
    appParams.putDouble("rootTag", rootTag);
    appParams.putMap("initialProps", initialProps);
    catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
    rootView.onAttachedToReactInstance();
    Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
Example 2
Project: react-native-orientation-listener-master  File: ReactOrientationListenerModule.java View source code
@Override
public void onOrientationChanged(int orientation) {
    WritableNativeMap params = new WritableNativeMap();
    String orientationValue = "";
    if (orientation == 0) {
        orientationValue = "PORTRAIT";
    } else {
        orientationValue = "LANDSCAPE";
    }
    params.putString("orientation", orientationValue);
    params.putString("device", getDeviceName());
    if (thisContext.hasActiveCatalystInstance()) {
        thisContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("orientationDidChange", params);
    }
}
Example 3
Project: react-native-workers-master  File: ReactContextBuilder.java View source code
public ReactApplicationContext build() throws Exception {
    JavaScriptExecutor jsExecutor = new JSCJavaScriptExecutor.Factory().create(new WritableNativeMap());
    // fresh new react context
    final ReactApplicationContext reactContext = new ReactApplicationContext(parentContext);
    if (devSupportManager != null) {
        reactContext.setNativeModuleCallExceptionHandler(devSupportManager);
    }
    // load native modules
    NativeModuleRegistry.Builder nativeRegistryBuilder = new NativeModuleRegistry.Builder();
    addNativeModules(reactContext, nativeRegistryBuilder);
    // load js modules
    JavaScriptModuleRegistry.Builder jsModulesBuilder = new JavaScriptModuleRegistry.Builder();
    addJSModules(jsModulesBuilder);
    CatalystInstanceImpl.Builder catalystInstanceBuilder = new CatalystInstanceImpl.Builder().setReactQueueConfigurationSpec(ReactQueueConfigurationSpec.createDefault()).setJSExecutor(jsExecutor).setRegistry(nativeRegistryBuilder.build()).setJSModuleRegistry(jsModulesBuilder.build()).setJSBundleLoader(jsBundleLoader).setNativeModuleCallExceptionHandler(devSupportManager != null ? devSupportManager : createNativeModuleExceptionHandler());
    final CatalystInstance catalystInstance;
    catalystInstance = catalystInstanceBuilder.build();
    catalystInstance.getReactQueueConfiguration().getJSQueueThread().callOnQueue(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                reactContext.initializeWithInstance(catalystInstance);
                catalystInstance.runJSBundle();
            } catch (Exception e) {
                e.printStackTrace();
                devSupportManager.handleException(e);
            }
            return null;
        }
    }).get();
    catalystInstance.getReactQueueConfiguration().getUIQueueThread().callOnQueue(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                catalystInstance.initialize();
                reactContext.onHostResume(null);
            } catch (Exception e) {
                e.printStackTrace();
                devSupportManager.handleException(e);
            }
            return null;
        }
    }).get();
    return reactContext;
}
Example 4
Project: react-native-charts-wrapper-master  File: EntryToWritableMapUtils.java View source code
public static WritableMap convertEntryToWritableMap(Entry entry) {
    if (entry == null) {
        return null;
    }
    WritableMap map = new WritableNativeMap();
    if (entry.getData() instanceof Map) {
        map.putMap("data", convertMapToWritableMap((Map) entry.getData()));
    }
    if (entry instanceof BarEntry) {
        BarEntry barEntry = (BarEntry) entry;
        map.putDouble("x", entry.getX());
        if (barEntry.getYVals() != null) {
            WritableArray array = new WritableNativeArray();
            for (float f : barEntry.getYVals()) {
                array.pushDouble(f);
            }
            map.putArray("yValues", array);
        } else {
            map.putDouble("y", entry.getY());
        }
    } else if (entry instanceof BubbleEntry) {
        BubbleEntry bubbleEntry = (BubbleEntry) entry;
        map.putDouble("x", entry.getX());
        map.putDouble("y", entry.getY());
        map.putDouble("size", bubbleEntry.getSize());
    } else if (entry instanceof CandleEntry) {
        CandleEntry candleEntry = (CandleEntry) entry;
        map.putDouble("x", entry.getX());
        map.putDouble("open", candleEntry.getOpen());
        map.putDouble("close", candleEntry.getClose());
        map.putDouble("low", candleEntry.getLow());
        map.putDouble("high", candleEntry.getHigh());
    } else if (entry instanceof PieEntry) {
        PieEntry pieEntry = (PieEntry) entry;
        map.putDouble("value", pieEntry.getValue());
        map.putString("label", pieEntry.getLabel());
    } else if (entry instanceof RadarEntry) {
        RadarEntry radarEntry = (RadarEntry) entry;
        map.putDouble("value", radarEntry.getValue());
    } else {
        map.putDouble("x", entry.getX());
        map.putDouble("y", entry.getY());
    }
    return map;
}
Example 5
Project: react-native-background-geolocation-master  File: RNBackgroundGeolocationModule.java View source code
public static WritableMap jsonToMap(JSONObject jsonObject) throws JSONException {
    WritableMap map = new WritableNativeMap();
    Iterator<String> iterator = jsonObject.keys();
    while (iterator.hasNext()) {
        String key = iterator.next();
        Object value = jsonObject.get(key);
        if (value instanceof JSONObject) {
            map.putMap(key, jsonToMap((JSONObject) value));
        } else if (value instanceof JSONArray) {
            map.putArray(key, convertJsonToArray((JSONArray) value));
        } else if (value instanceof Boolean) {
            map.putBoolean(key, (Boolean) value);
        } else if (value instanceof Integer) {
            map.putInt(key, (Integer) value);
        } else if (value instanceof Double) {
            map.putDouble(key, (Double) value);
        } else if (value instanceof String) {
            map.putString(key, (String) value);
        } else {
            map.putString(key, value.toString());
        }
    }
    return map;
}
Example 6
Project: react-native-firestack-master  File: FirestackDatabase.java View source code
private <Any> Any castSnapshotValue(DataSnapshot snapshot) {
    if (snapshot.hasChildren()) {
        WritableMap data = Arguments.createMap();
        for (DataSnapshot child : snapshot.getChildren()) {
            Any castedChild = castSnapshotValue(child);
            switch(castedChild.getClass().getName()) {
                case "java.lang.Boolean":
                    data.putBoolean(child.getKey(), (Boolean) castedChild);
                    break;
                case "java.lang.Long":
                    data.putDouble(child.getKey(), (Long) castedChild);
                    break;
                case "java.lang.Double":
                    data.putDouble(child.getKey(), (Double) castedChild);
                    break;
                case "java.lang.String":
                    data.putString(child.getKey(), (String) castedChild);
                    break;
                case "com.facebook.react.bridge.WritableNativeMap":
                    data.putMap(child.getKey(), (WritableMap) castedChild);
                    break;
            }
        }
        return (Any) data;
    } else {
        if (snapshot.getValue() != null) {
            String type = snapshot.getValue().getClass().getName();
            switch(type) {
                case "java.lang.Boolean":
                    return (Any) ((Boolean) snapshot.getValue());
                case "java.lang.Long":
                    return (Any) ((Long) snapshot.getValue());
                case "java.lang.Double":
                    return (Any) ((Double) snapshot.getValue());
                case "java.lang.String":
                    return (Any) ((String) snapshot.getValue());
                default:
                    return (Any) null;
            }
        } else {
            return (Any) null;
        }
    }
}
Example 7
Project: react-native-beacons-manager-master  File: BeaconsAndroidModule.java View source code
@ReactMethod
public void getMonitoredRegions(Callback callback) {
    WritableArray array = new WritableNativeArray();
    for (Region region : mBeaconManager.getMonitoredRegions()) {
        WritableMap map = new WritableNativeMap();
        map.putString("identifier", region.getUniqueId());
        map.putString("uuid", region.getId1().toString());
        map.putInt("major", region.getId2() != null ? region.getId2().toInt() : 0);
        map.putInt("minor", region.getId3() != null ? region.getId3().toInt() : 0);
        array.pushMap(map);
    }
    callback.invoke(array);
}
Example 8
Project: RCTRealtimeMessagingAndroid-master  File: RealtimeMessagingAndroid.java View source code
@ReactMethod
public void connect(ReadableMap config, Integer id) {
    this.config = config;
    if (this.queue == null)
        queue = new HashMap<Integer, OrtcClient>();
    if (queue.containsKey(id)) {
        client = (OrtcClient) queue.get(id);
    } else {
        Ortc api = new Ortc();
        OrtcFactory factory = null;
        try {
            factory = api.loadOrtcFactory("IbtRealtimeSJ");
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        client = factory.createClient();
        queue.put(id, client);
    }
    client.setApplicationContext(this.getReactApplicationContext());
    if (this.config.hasKey("projectId"))
        client.setGoogleProjectId(this.config.getString("projectId"));
    if (this.config.hasKey("connectionMetadata"))
        client.setConnectionMetadata(this.config.getString("connectionMetadata"));
    if (this.config.hasKey("clusterUrl")) {
        client.setClusterUrl(this.config.getString("clusterUrl"));
    } else if (this.config.hasKey("url")) {
        client.setUrl(this.config.getString("url"));
    }
    client.onConnected = new OnConnected() {

        @Override
        public void run(OrtcClient ortcClient) {
            String thisId = "" + RealtimeMessagingAndroid.getKeyByValue(queue, ortcClient);
            sendEvent(getReactApplicationContext(), thisId + "-onConnected", null);
        }
    };
    client.onDisconnected = new OnDisconnected() {

        @Override
        public void run(OrtcClient ortcClient) {
            String thisId = "" + RealtimeMessagingAndroid.getKeyByValue(queue, ortcClient);
            sendEvent(getReactApplicationContext(), thisId + "-onDisconnected", null);
        }
    };
    client.onException = new OnException() {

        @Override
        public void run(OrtcClient ortcClient, Exception e) {
            WritableMap params = new WritableNativeMap();
            params.putString("error", e.toString());
            String thisId = "" + RealtimeMessagingAndroid.getKeyByValue(queue, ortcClient);
            sendEvent(getReactApplicationContext(), thisId + "-onException", params);
        }
    };
    client.onSubscribed = new OnSubscribed() {

        @Override
        public void run(OrtcClient ortcClient, String s) {
            checkForNotifications();
            WritableMap params = new WritableNativeMap();
            params.putString("channel", s);
            String thisId = "" + RealtimeMessagingAndroid.getKeyByValue(queue, ortcClient);
            sendEvent(getReactApplicationContext(), thisId + "-onSubscribed", params);
        }
    };
    client.onUnsubscribed = new OnUnsubscribed() {

        @Override
        public void run(OrtcClient ortcClient, String s) {
            WritableMap params = new WritableNativeMap();
            params.putString("channel", s);
            String thisId = "" + RealtimeMessagingAndroid.getKeyByValue(queue, ortcClient);
            sendEvent(getReactApplicationContext(), thisId + "-onUnsubscribed", params);
        }
    };
    client.onReconnected = new OnReconnected() {

        @Override
        public void run(OrtcClient ortcClient) {
            String thisId = "" + RealtimeMessagingAndroid.getKeyByValue(queue, ortcClient);
            sendEvent(getReactApplicationContext(), thisId + "-onReconnected", null);
        }
    };
    client.onReconnecting = new OnReconnecting() {

        @Override
        public void run(OrtcClient ortcClient) {
            String thisId = "" + RealtimeMessagingAndroid.getKeyByValue(queue, ortcClient);
            sendEvent(getReactApplicationContext(), thisId + "-onReconnecting", null);
        }
    };
    client.connect(this.config.getString("appKey"), this.config.getString("token"));
}
Example 9
Project: React-Native-Remote-Update-master  File: ReactInstanceManager.java View source code
private void attachMeasuredRootViewToInstance(ReactRootView rootView, CatalystInstance catalystInstance) {
    UiThreadUtil.assertOnUiThread();
    // Reset view content as it's going to be populated by the application content from JS
    rootView.removeAllViews();
    rootView.setId(View.NO_ID);
    UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class);
    int rootTag = uiManagerModule.addMeasuredRootView(rootView);
    @Nullable Bundle launchOptions = rootView.getLaunchOptions();
    WritableMap initialProps = launchOptions != null ? Arguments.fromBundle(launchOptions) : Arguments.createMap();
    String jsAppModuleName = rootView.getJSModuleName();
    WritableNativeMap appParams = new WritableNativeMap();
    appParams.putDouble("rootTag", rootTag);
    appParams.putMap("initialProps", initialProps);
    catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
}
Example 10
Project: react-native-camera-master  File: RCTCameraModule.java View source code
/**
     * Release media recorder following video capture (or failure to start recording session).
     *
     * See "Capturing Videos" at https://developer.android.com/guide/topics/media/camera.html for
     * a guideline of steps and more information in general.
     */
private void releaseMediaRecorder() {
    // Must record at least a second or MediaRecorder throws exceptions on some platforms
    long duration = System.currentTimeMillis() - MRStartTime;
    if (duration < 1500) {
        try {
            Thread.sleep(1500 - duration);
        } catch (InterruptedException ex) {
            Log.e(TAG, "releaseMediaRecorder thread sleep error.", ex);
        }
    }
    // Release actual MediaRecorder instance.
    if (mMediaRecorder != null) {
        // Stop recording video.
        try {
            // stop the recording
            mMediaRecorder.stop();
        } catch (RuntimeException ex) {
            Log.e(TAG, "Media recorder stop error.", ex);
        }
        // Optionally, remove the configuration settings from the recorder.
        mMediaRecorder.reset();
        // Release the MediaRecorder.
        mMediaRecorder.release();
        // Reset variable.
        mMediaRecorder = null;
    }
    // MediaRecorder.prepare() call fails.
    if (mCamera != null) {
        mCamera.lock();
    }
    if (mRecordingPromise == null) {
        return;
    }
    File f = new File(mVideoFile.getPath());
    if (!f.exists()) {
        mRecordingPromise.reject(new RuntimeException("There is nothing recorded."));
        mRecordingPromise = null;
        return;
    }
    // so mediaplayer can play it
    f.setReadable(true, false);
    // so can clean it up
    f.setWritable(true, false);
    WritableMap response = new WritableNativeMap();
    switch(mRecordingOptions.getInt("target")) {
        case RCT_CAMERA_CAPTURE_TARGET_MEMORY:
            byte[] encoded = convertFileToByteArray(mVideoFile);
            response.putString("data", new String(encoded, Base64.DEFAULT));
            mRecordingPromise.resolve(response);
            f.delete();
            break;
        case RCT_CAMERA_CAPTURE_TARGET_CAMERA_ROLL:
            ContentValues values = new ContentValues();
            values.put(MediaStore.Video.Media.DATA, mVideoFile.getPath());
            values.put(MediaStore.Video.Media.TITLE, mRecordingOptions.hasKey("title") ? mRecordingOptions.getString("title") : "video");
            if (mRecordingOptions.hasKey("description")) {
                values.put(MediaStore.Video.Media.DESCRIPTION, mRecordingOptions.hasKey("description"));
            }
            if (mRecordingOptions.hasKey("latitude")) {
                values.put(MediaStore.Video.Media.LATITUDE, mRecordingOptions.getString("latitude"));
            }
            if (mRecordingOptions.hasKey("longitude")) {
                values.put(MediaStore.Video.Media.LONGITUDE, mRecordingOptions.getString("longitude"));
            }
            values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
            _reactContext.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
            addToMediaStore(mVideoFile.getAbsolutePath());
            response.putString("path", Uri.fromFile(mVideoFile).toString());
            mRecordingPromise.resolve(response);
            break;
        case RCT_CAMERA_CAPTURE_TARGET_TEMP:
        case RCT_CAMERA_CAPTURE_TARGET_DISK:
            response.putString("path", Uri.fromFile(mVideoFile).toString());
            mRecordingPromise.resolve(response);
    }
    mRecordingPromise = null;
}
Example 11
Project: react-native-sidemenu-master  File: XReactInstanceManagerImpl.java View source code
private void attachMeasuredRootViewToInstance(ReactRootView rootView, CatalystInstance catalystInstance) {
    Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachMeasuredRootViewToInstance");
    UiThreadUtil.assertOnUiThread();
    // Reset view content as it's going to be populated by the application content from JS
    rootView.removeAllViews();
    rootView.setId(View.NO_ID);
    UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class);
    int rootTag = uiManagerModule.addMeasuredRootView(rootView);
    rootView.setRootViewTag(rootTag);
    @Nullable Bundle launchOptions = rootView.getLaunchOptions();
    WritableMap initialProps = Arguments.makeNativeMap(launchOptions);
    String jsAppModuleName = rootView.getJSModuleName();
    WritableNativeMap appParams = new WritableNativeMap();
    appParams.putDouble("rootTag", rootTag);
    appParams.putMap("initialProps", initialProps);
    catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
    rootView.onAttachedToReactInstance();
    Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}
Example 12
Project: ComicBook-master  File: ReactInstanceManager.java View source code
private void attachMeasuredRootViewToInstance(ReactRootView rootView, CatalystInstance catalystInstance) {
    Systrace.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "attachMeasuredRootViewToInstance");
    UiThreadUtil.assertOnUiThread();
    // Reset view content as it's going to be populated by the application content from JS
    rootView.removeAllViews();
    rootView.setId(View.NO_ID);
    UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class);
    int rootTag = uiManagerModule.addMeasuredRootView(rootView);
    rootView.setRootViewTag(rootTag);
    @Nullable Bundle launchOptions = rootView.getLaunchOptions();
    WritableMap initialProps = Arguments.makeNativeMap(launchOptions);
    String jsAppModuleName = rootView.getJSModuleName();
    WritableNativeMap appParams = new WritableNativeMap();
    appParams.putDouble("rootTag", rootTag);
    appParams.putMap("initialProps", initialProps);
    catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
    rootView.onAttachedToReactInstance();
    Systrace.endSection(TRACE_TAG_REACT_JAVA_BRIDGE);
}