Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let BarcodeScannerView manage lifecycle event. #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@

import android.view.View;

import javax.annotation.Nullable;

import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;

public class BarcodeScannerManager extends ViewGroupManager<BarcodeScannerView> implements LifecycleEventListener {
import javax.annotation.Nullable;

public class BarcodeScannerManager extends ViewGroupManager<BarcodeScannerView> {
private static final String REACT_CLASS = "RNBarcodeScannerView";

private static final String DEFAULT_TORCH_MODE = "off";
private static final String DEFAULT_CAMERA_TYPE = "back";

private BarcodeScannerView mScannerView;
private boolean mScannerViewVisible;

@Override
public String getName() {
return REACT_CLASS;
Expand All @@ -40,31 +35,21 @@ public void setTorchMode(BarcodeScannerView view, @Nullable String torchMode) {

@Override
public BarcodeScannerView createViewInstance(ThemedReactContext context) {
context.addLifecycleEventListener(this);
mScannerView = new BarcodeScannerView(context);
mScannerView.setCameraType(DEFAULT_CAMERA_TYPE);
mScannerView.setFlash(DEFAULT_TORCH_MODE.equals("on"));
mScannerViewVisible = true;
return mScannerView;
}

@Override
public void onHostResume() {
mScannerView.onResume();
BarcodeScannerView scannerView = new BarcodeScannerView(context);
scannerView.setCameraType(DEFAULT_CAMERA_TYPE);
scannerView.setFlash(DEFAULT_TORCH_MODE.equals("on"));
context.addLifecycleEventListener(scannerView);
return scannerView;
}

@Override
public void onHostPause() {
mScannerView.onPause();
public void addView(BarcodeScannerView parent, View child, int index) {
parent.addView(child, index + 1); // index 0 for camera preview reserved
}

@Override
public void onHostDestroy() {
mScannerView.stopCamera();
public void onDropViewInstance(BarcodeScannerView view) {
((ThemedReactContext) view.getContext()).removeLifecycleEventListener(view);
}

@Override
public void addView(BarcodeScannerView parent, View child, int index) {
parent.addView(child, index + 1); // index 0 for camera preview reserved
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.eguma.barcodescanner;

import android.content.Context;
import android.graphics.Color;
import android.hardware.Camera;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
Expand All @@ -19,7 +17,7 @@
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;

public class BarcodeScannerView extends FrameLayout implements Camera.PreviewCallback {
public class BarcodeScannerView extends FrameLayout implements Camera.PreviewCallback, LifecycleEventListener {
private CameraPreview mPreview;
private MultiFormatReader mMultiFormatReader;

Expand All @@ -33,16 +31,6 @@ public BarcodeScannerView(Context context) {
this.addView(mPreview);
}

public void onResume() {
mPreview.startCamera(); // workaround for reload js
// mPreview.onResume();
}

public void onPause() {
mPreview.stopCamera(); // workaround for reload js
// mPreview.onPause();
}

public void setCameraType(String cameraType) {
mPreview.setCameraType(cameraType);
}
Expand All @@ -51,10 +39,6 @@ public void setFlash(boolean flag) {
mPreview.setFlash(flag);
}

public void stopCamera() {
mPreview.stopCamera();
}

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
try {
Expand Down Expand Up @@ -99,4 +83,22 @@ public void onPreviewFrame(byte[] data, Camera camera) {
Log.e(TAG, e.toString(), e);
}
}

@Override
public void onHostResume() {
mPreview.startCamera(); // workaround for reload js
// mPreview.onResume();
}

@Override
public void onHostPause() {
mPreview.stopCamera(); // workaround for reload js
// mPreview.onPause();
}

@Override
public void onHostDestroy() {
mPreview.stopCamera();
}

}
17 changes: 3 additions & 14 deletions android/src/main/java/com/eguma/barcodescanner/CameraPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,13 @@

import android.content.Context;
import android.hardware.Camera;
import android.os.Handler;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.view.Display;
import android.os.Handler;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.PlanarYUVLuminanceSource;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.common.HybridBinarizer;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private CameraManager mCameraManager;
Expand Down Expand Up @@ -51,6 +39,7 @@ public void startCamera() {
public void stopCamera() {
stopCameraPreview();
mCameraManager.releaseCamera();
mCamera = null;
}

public void setCameraType(String cameraType) {
Expand Down