fix(android): Possible ANR due to events not fired on the main thread (#2591)

* This is a fix for events possibly being fired not on the main thread, which could cause the whole app to freeze.

* Fire events on the right thread. No need to use UI thread, and fix the still works
This commit is contained in:
cristianoccazinsp 2019-11-18 09:52:35 -03:00 committed by Sibelius Seraphini
parent 9c6fcea43e
commit ad9d0840fd

View File

@ -156,80 +156,144 @@ public class RNCameraViewHelper {
{"int", ExifInterface.TAG_RW2_SENSOR_TOP_BORDER},
{"int", ExifInterface.TAG_RW2_ISO},
};
// Run all events on native modules queue thread since they might be fired
// from other non RN threads.
// Mount error event
public static void emitMountErrorEvent(ViewGroup view, String error) {
CameraMountErrorEvent event = CameraMountErrorEvent.obtain(view.getId(), error);
ReactContext reactContext = (ReactContext) view.getContext();
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
public static void emitMountErrorEvent(final ViewGroup view, final String error) {
final ReactContext reactContext = (ReactContext) view.getContext();
reactContext.runOnNativeModulesQueueThread(new Runnable() {
@Override
public void run() {
CameraMountErrorEvent event = CameraMountErrorEvent.obtain(view.getId(), error);
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
}
});
}
// Camera ready event
public static void emitCameraReadyEvent(ViewGroup view) {
CameraReadyEvent event = CameraReadyEvent.obtain(view.getId());
ReactContext reactContext = (ReactContext) view.getContext();
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
public static void emitCameraReadyEvent(final ViewGroup view) {
final ReactContext reactContext = (ReactContext) view.getContext();
reactContext.runOnNativeModulesQueueThread(new Runnable() {
@Override
public void run() {
CameraReadyEvent event = CameraReadyEvent.obtain(view.getId());
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
}
});
}
// Picture saved event
public static void emitPictureSavedEvent(ViewGroup view, WritableMap response) {
PictureSavedEvent event = PictureSavedEvent.obtain(view.getId(), response);
ReactContext reactContext = (ReactContext) view.getContext();
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
public static void emitPictureSavedEvent(final ViewGroup view, final WritableMap response) {
final ReactContext reactContext = (ReactContext) view.getContext();
reactContext.runOnNativeModulesQueueThread(new Runnable() {
@Override
public void run() {
PictureSavedEvent event = PictureSavedEvent.obtain(view.getId(), response);
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
}
});
}
// Picture taken event
public static void emitPictureTakenEvent(ViewGroup view) {
PictureTakenEvent event = PictureTakenEvent.obtain(view.getId());
ReactContext reactContext = (ReactContext) view.getContext();
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
public static void emitPictureTakenEvent(final ViewGroup view) {
final ReactContext reactContext = (ReactContext) view.getContext();
reactContext.runOnNativeModulesQueueThread(new Runnable() {
@Override
public void run() {
PictureTakenEvent event = PictureTakenEvent.obtain(view.getId());
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
}
});
}
// Face detection events
public static void emitFacesDetectedEvent(ViewGroup view, WritableArray data) {
FacesDetectedEvent event = FacesDetectedEvent.obtain(view.getId(), data);
ReactContext reactContext = (ReactContext) view.getContext();
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
public static void emitFacesDetectedEvent(final ViewGroup view, final WritableArray data) {
final ReactContext reactContext = (ReactContext) view.getContext();
reactContext.runOnNativeModulesQueueThread(new Runnable() {
@Override
public void run() {
FacesDetectedEvent event = FacesDetectedEvent.obtain(view.getId(), data);
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
}
});
}
public static void emitFaceDetectionErrorEvent(ViewGroup view, RNFaceDetector faceDetector) {
FaceDetectionErrorEvent event = FaceDetectionErrorEvent.obtain(view.getId(), faceDetector);
ReactContext reactContext = (ReactContext) view.getContext();
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
public static void emitFaceDetectionErrorEvent(final ViewGroup view, final RNFaceDetector faceDetector) {
final ReactContext reactContext = (ReactContext) view.getContext();
reactContext.runOnNativeModulesQueueThread(new Runnable() {
@Override
public void run() {
FaceDetectionErrorEvent event = FaceDetectionErrorEvent.obtain(view.getId(), faceDetector);
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
}
});
}
// Barcode detection events
public static void emitBarcodesDetectedEvent(ViewGroup view, WritableArray barcodes) {
BarcodesDetectedEvent event = BarcodesDetectedEvent.obtain(view.getId(), barcodes);
ReactContext reactContext = (ReactContext) view.getContext();
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
public static void emitBarcodesDetectedEvent(final ViewGroup view, final WritableArray barcodes) {
final ReactContext reactContext = (ReactContext) view.getContext();
reactContext.runOnNativeModulesQueueThread(new Runnable() {
@Override
public void run() {
BarcodesDetectedEvent event = BarcodesDetectedEvent.obtain(view.getId(), barcodes);
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
}
});
}
public static void emitBarcodeDetectionErrorEvent(ViewGroup view, RNBarcodeDetector barcodeDetector) {
BarcodeDetectionErrorEvent event = BarcodeDetectionErrorEvent.obtain(view.getId(), barcodeDetector);
ReactContext reactContext = (ReactContext) view.getContext();
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
public static void emitBarcodeDetectionErrorEvent(final ViewGroup view, final RNBarcodeDetector barcodeDetector) {
final ReactContext reactContext = (ReactContext) view.getContext();
reactContext.runOnNativeModulesQueueThread(new Runnable() {
@Override
public void run() {
BarcodeDetectionErrorEvent event = BarcodeDetectionErrorEvent.obtain(view.getId(), barcodeDetector);
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
}
});
}
// Bar code read event
public static void emitBarCodeReadEvent(ViewGroup view, Result barCode, int width, int height) {
BarCodeReadEvent event = BarCodeReadEvent.obtain(view.getId(), barCode, width, height);
ReactContext reactContext = (ReactContext) view.getContext();
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
public static void emitBarCodeReadEvent(final ViewGroup view, final Result barCode, final int width, final int height) {
final ReactContext reactContext = (ReactContext) view.getContext();
reactContext.runOnNativeModulesQueueThread(new Runnable() {
@Override
public void run() {
BarCodeReadEvent event = BarCodeReadEvent.obtain(view.getId(), barCode, width, height);
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
}
});
}
// Text recognition event
public static void emitTextRecognizedEvent(ViewGroup view, WritableArray data) {
TextRecognizedEvent event = TextRecognizedEvent.obtain(view.getId(), data);
ReactContext reactContext = (ReactContext) view.getContext();
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
public static void emitTextRecognizedEvent(final ViewGroup view, final WritableArray data) {
final ReactContext reactContext = (ReactContext) view.getContext();
reactContext.runOnNativeModulesQueueThread(new Runnable() {
@Override
public void run() {
TextRecognizedEvent event = TextRecognizedEvent.obtain(view.getId(), data);
reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher().dispatchEvent(event);
}
});
}
// Utilities
@ -329,9 +393,9 @@ public class RNCameraViewHelper {
}
}
}
if (exifMap.hasKey(ExifInterface.TAG_GPS_LATITUDE) &&
exifMap.hasKey(ExifInterface.TAG_GPS_LONGITUDE) &&
if (exifMap.hasKey(ExifInterface.TAG_GPS_LATITUDE) &&
exifMap.hasKey(ExifInterface.TAG_GPS_LONGITUDE) &&
exifMap.hasKey(ExifInterface.TAG_GPS_ALTITUDE)) {
exifInterface.setLatLong(exifMap.getDouble(ExifInterface.TAG_GPS_LATITUDE),
exifMap.getDouble(ExifInterface.TAG_GPS_LONGITUDE));