From 2ab90438519fae18a6f145139007c2aaf11b8b82 Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Sun, 11 Jun 2017 08:07:04 -0400 Subject: [PATCH] Migrate constructor code to init and use a single worker thread. - Moving the initialization code to an init function allows the creation of the CameraView in code using the single context constructor. - Creating a single HandlerThread will avoid having to create new threads for every operation. --- .../com/flurgle/camerakit/CameraView.java | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java index e71b8055..44635311 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java @@ -8,6 +8,8 @@ import android.content.pm.PackageManager; import android.content.res.TypedArray; import android.graphics.Rect; import android.graphics.YuvImage; +import android.os.Handler; +import android.os.HandlerThread; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; @@ -37,6 +39,17 @@ import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_STRICT; public class CameraView extends FrameLayout { + private static Handler sWorkerHandler; + + static { + // Initialize a single worker thread. This can be static since only a single camera + // reference can exist at a time. + HandlerThread workerThread = new HandlerThread("CameraViewWorker"); + workerThread.setDaemon(true); + workerThread.start(); + sWorkerHandler = new Handler(workerThread.getLooper()); + } + @Facing private int mFacing; @@ -57,24 +70,30 @@ public class CameraView extends FrameLayout { @VideoQuality private int mVideoQuality; - private int mJpegQuality; private boolean mCropOutput; - private boolean mAdjustViewBounds; + private boolean mAdjustViewBounds; private CameraListenerMiddleWare mCameraListener; - private DisplayOrientationDetector mDisplayOrientationDetector; + private DisplayOrientationDetector mDisplayOrientationDetector; private CameraImpl mCameraImpl; + private PreviewImpl mPreviewImpl; public CameraView(@NonNull Context context) { super(context, null); + init(context, null); } @SuppressWarnings("all") public CameraView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); + init(context, attrs); + } + + @SuppressWarnings("WrongConstant") + private void init(@NonNull Context context, @Nullable AttributeSet attrs) { if (attrs != null) { TypedArray a = context.getTheme().obtainStyledAttributes( attrs, @@ -210,28 +229,27 @@ public class CameraView extends FrameLayout { break; } - new Thread(new Runnable() { + sWorkerHandler.post(new Runnable() { @Override public void run() { mCameraImpl.start(); } - }).start(); + }); } public void stop() { mCameraImpl.stop(); } - public void setFacing(@Facing - final int facing) { + public void setFacing(@Facing final int facing) { this.mFacing = facing; - new Thread(new Runnable() { + sWorkerHandler.post(new Runnable() { @Override public void run() { mCameraImpl.setFacing(facing); } - }).start(); + }); } public void setFlash(@Flash int flash) {