Search in sources :

Example 6 with ReactApplicationContext

use of com.facebook.react.bridge.ReactApplicationContext in project react-native-push-notification by zo0r.

the class RNPushNotificationListenerService method onMessageReceived.

@Override
public void onMessageReceived(String from, final Bundle bundle) {
    JSONObject data = getPushData(bundle.getString("data"));
    if (data != null) {
        if (!bundle.containsKey("message")) {
            bundle.putString("message", data.optString("alert", "Notification received"));
        }
        if (!bundle.containsKey("title")) {
            bundle.putString("title", data.optString("title", null));
        }
        if (!bundle.containsKey("sound")) {
            bundle.putString("soundName", data.optString("sound", null));
        }
        if (!bundle.containsKey("color")) {
            bundle.putString("color", data.optString("color", null));
        }
        final int badge = data.optInt("badge", -1);
        if (badge >= 0) {
            ApplicationBadgeHelper.INSTANCE.setApplicationIconBadgeNumber(this, badge);
        }
    }
    Log.v(LOG_TAG, "onMessageReceived: " + bundle);
    // We need to run this on the main thread, as the React code assumes that is true.
    // Namely, DevServerHelper constructs a Handler() without a Looper, which triggers:
    // "Can't create handler inside thread that has not called Looper.prepare()"
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {

        public void run() {
            // Construct and load our normal React JS code bundle
            ReactInstanceManager mReactInstanceManager = ((ReactApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
            ReactContext context = mReactInstanceManager.getCurrentReactContext();
            // If it's constructed, send a notification
            if (context != null) {
                handleRemotePushNotification((ReactApplicationContext) context, bundle);
            } else {
                // Otherwise wait for construction, then send the notification
                mReactInstanceManager.addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() {

                    public void onReactContextInitialized(ReactContext context) {
                        handleRemotePushNotification((ReactApplicationContext) context, bundle);
                    }
                });
                if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
                    // Construct it in the background
                    mReactInstanceManager.createReactContextInBackground();
                }
            }
        }
    });
}
Also used : JSONObject(org.json.JSONObject) ReactInstanceManager(com.facebook.react.ReactInstanceManager) ReactContext(com.facebook.react.bridge.ReactContext) Handler(android.os.Handler) ReactApplication(com.facebook.react.ReactApplication) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext)

Example 7 with ReactApplicationContext

use of com.facebook.react.bridge.ReactApplicationContext in project react-native-system-setting by c19354837.

the class SystemSetting method listenVolume.

private void listenVolume(final ReactApplicationContext reactContext) {
    volumeBR = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.media.VOLUME_CHANGED_ACTION")) {
                WritableMap para = Arguments.createMap();
                para.putDouble("value", getNormalizationVolume(VOL_MUSIC));
                para.putDouble(VOL_VOICE_CALL, getNormalizationVolume(VOL_VOICE_CALL));
                para.putDouble(VOL_SYSTEM, getNormalizationVolume(VOL_SYSTEM));
                para.putDouble(VOL_RING, getNormalizationVolume(VOL_RING));
                para.putDouble(VOL_MUSIC, getNormalizationVolume(VOL_MUSIC));
                para.putDouble(VOL_ALARM, getNormalizationVolume(VOL_ALARM));
                para.putDouble(VOL_NOTIFICATION, getNormalizationVolume(VOL_NOTIFICATION));
                reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("EventVolume", para);
            }
        }
    };
    filter = new IntentFilter("android.media.VOLUME_CHANGED_ACTION");
    reactContext.registerReceiver(volumeBR, filter);
}
Also used : Context(android.content.Context) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext) DeviceEventManagerModule(com.facebook.react.modules.core.DeviceEventManagerModule) IntentFilter(android.content.IntentFilter) WritableMap(com.facebook.react.bridge.WritableMap) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver)

Example 8 with ReactApplicationContext

use of com.facebook.react.bridge.ReactApplicationContext in project react-native-camera by react-native-community.

the class CameraModule method stopRecording.

@ReactMethod
public void stopRecording(final int viewTag) {
    final ReactApplicationContext context = getReactApplicationContext();
    UIManagerModule uiManager = context.getNativeModule(UIManagerModule.class);
    uiManager.addUIBlock(new UIBlock() {

        @Override
        public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
            final RNCameraView cameraView;
            try {
                cameraView = (RNCameraView) nativeViewHierarchyManager.resolveView(viewTag);
                if (cameraView.isCameraOpened()) {
                    cameraView.stopRecording();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : UIBlock(com.facebook.react.uimanager.UIBlock) UIManagerModule(com.facebook.react.uimanager.UIManagerModule) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext) NativeViewHierarchyManager(com.facebook.react.uimanager.NativeViewHierarchyManager) ReactMethod(com.facebook.react.bridge.ReactMethod)

Example 9 with ReactApplicationContext

use of com.facebook.react.bridge.ReactApplicationContext in project react-native-camera by react-native-community.

the class CameraModule method getSupportedRatios.

@ReactMethod
public void getSupportedRatios(final int viewTag, final Promise promise) {
    final ReactApplicationContext context = getReactApplicationContext();
    UIManagerModule uiManager = context.getNativeModule(UIManagerModule.class);
    uiManager.addUIBlock(new UIBlock() {

        @Override
        public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
            final RNCameraView cameraView;
            try {
                cameraView = (RNCameraView) nativeViewHierarchyManager.resolveView(viewTag);
                WritableArray result = Arguments.createArray();
                if (cameraView.isCameraOpened()) {
                    Set<AspectRatio> ratios = cameraView.getSupportedAspectRatios();
                    for (AspectRatio ratio : ratios) {
                        result.pushString(ratio.toString());
                    }
                    promise.resolve(result);
                } else {
                    promise.reject("E_CAMERA_UNAVAILABLE", "Camera is not running");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : UIBlock(com.facebook.react.uimanager.UIBlock) AspectRatio(com.google.android.cameraview.AspectRatio) Set(java.util.Set) UIManagerModule(com.facebook.react.uimanager.UIManagerModule) WritableArray(com.facebook.react.bridge.WritableArray) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext) NativeViewHierarchyManager(com.facebook.react.uimanager.NativeViewHierarchyManager) ReactMethod(com.facebook.react.bridge.ReactMethod)

Example 10 with ReactApplicationContext

use of com.facebook.react.bridge.ReactApplicationContext in project react-native-camera by react-native-community.

the class CameraModule method takePicture.

@ReactMethod
public void takePicture(final ReadableMap options, final int viewTag, final Promise promise) {
    final ReactApplicationContext context = getReactApplicationContext();
    final File cacheDirectory = mScopedContext.getCacheDirectory();
    UIManagerModule uiManager = context.getNativeModule(UIManagerModule.class);
    uiManager.addUIBlock(new UIBlock() {

        @Override
        public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) {
            RNCameraView cameraView = (RNCameraView) nativeViewHierarchyManager.resolveView(viewTag);
            try {
                if (!Build.FINGERPRINT.contains("generic")) {
                    if (cameraView.isCameraOpened()) {
                        cameraView.takePicture(options, promise, cacheDirectory);
                    } else {
                        promise.reject("E_CAMERA_UNAVAILABLE", "Camera is not running");
                    }
                } else {
                    Bitmap image = RNCameraViewHelper.generateSimulatorPhoto(cameraView.getWidth(), cameraView.getHeight());
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    image.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    new ResolveTakenPictureAsyncTask(stream.toByteArray(), promise, options, cacheDirectory).execute();
                }
            } catch (Exception e) {
                promise.reject("E_CAMERA_BAD_VIEWTAG", "takePictureAsync: Expected a Camera component");
            }
        }
    });
}
Also used : UIBlock(com.facebook.react.uimanager.UIBlock) Bitmap(android.graphics.Bitmap) UIManagerModule(com.facebook.react.uimanager.UIManagerModule) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ReactApplicationContext(com.facebook.react.bridge.ReactApplicationContext) File(java.io.File) NativeViewHierarchyManager(com.facebook.react.uimanager.NativeViewHierarchyManager) ResolveTakenPictureAsyncTask(org.reactnative.camera.tasks.ResolveTakenPictureAsyncTask) ReactMethod(com.facebook.react.bridge.ReactMethod)

Aggregations

ReactApplicationContext (com.facebook.react.bridge.ReactApplicationContext)13 ReactMethod (com.facebook.react.bridge.ReactMethod)6 Intent (android.content.Intent)4 NativeViewHierarchyManager (com.facebook.react.uimanager.NativeViewHierarchyManager)4 UIBlock (com.facebook.react.uimanager.UIBlock)4 UIManagerModule (com.facebook.react.uimanager.UIManagerModule)4 WritableMap (com.facebook.react.bridge.WritableMap)3 BroadcastReceiver (android.content.BroadcastReceiver)2 Context (android.content.Context)2 IntentFilter (android.content.IntentFilter)2 Handler (android.os.Handler)2 ReactInstanceManager (com.facebook.react.ReactInstanceManager)2 ReactContext (com.facebook.react.bridge.ReactContext)2 File (java.io.File)2 Activity (android.app.Activity)1 Bitmap (android.graphics.Bitmap)1 InterstitialAd (com.facebook.ads.InterstitialAd)1 NativeAdsManager (com.facebook.ads.NativeAdsManager)1 ReactApplication (com.facebook.react.ReactApplication)1 WritableArray (com.facebook.react.bridge.WritableArray)1