From 2acb1be84fc03a6a15f993f7c7912789eb20bfc5 Mon Sep 17 00:00:00 2001 From: n8fr8 Date: Wed, 4 Jul 2018 23:28:43 -0400 Subject: [PATCH] address #313 (don't unbind service more than once!) and refactor Preview to CameraViewHolder --- .../{Preview.java => CameraViewHolder.java} | 34 +++++++++---------- .../org/havenapp/main/ui/CameraFragment.java | 9 +++-- 2 files changed, 20 insertions(+), 23 deletions(-) rename src/main/java/org/havenapp/main/sensors/motion/{Preview.java => CameraViewHolder.java} (92%) diff --git a/src/main/java/org/havenapp/main/sensors/motion/Preview.java b/src/main/java/org/havenapp/main/sensors/motion/CameraViewHolder.java similarity index 92% rename from src/main/java/org/havenapp/main/sensors/motion/Preview.java rename to src/main/java/org/havenapp/main/sensors/motion/CameraViewHolder.java index 61e2b702..0b2ea183 100644 --- a/src/main/java/org/havenapp/main/sensors/motion/Preview.java +++ b/src/main/java/org/havenapp/main/sensors/motion/CameraViewHolder.java @@ -14,7 +14,6 @@ import android.content.Intent; import android.content.ServiceConnection; import android.graphics.Bitmap; -import android.hardware.Camera; import android.os.Environment; import android.os.Handler; import android.os.IBinder; @@ -29,16 +28,11 @@ import org.havenapp.main.PreferenceManager; import org.havenapp.main.model.EventTrigger; import org.havenapp.main.service.MonitorService; -import org.jcodec.api.SequenceEncoder; import org.jcodec.api.android.AndroidSequenceEncoder; -import org.jcodec.codecs.vpx.IVFMuxer; -import org.jcodec.codecs.vpx.VP8Encoder; -import org.jcodec.common.io.SeekableByteChannel; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -49,7 +43,7 @@ import io.github.silvaren.easyrs.tools.Nv21Image; -public class Preview { +public class CameraViewHolder { /** * Object to retrieve and set shared preferences @@ -118,7 +112,7 @@ public void onServiceDisconnected(ComponentName arg0) { } }; - public Preview (Context context, CameraView cameraView) { + public CameraViewHolder(Context context, CameraView cameraView) { //super(context); this.context = context; this.cameraView = cameraView; @@ -175,14 +169,14 @@ public void initCamera() { cameraView.setOnFrameListener((data, width, height, rotationDegrees) -> { long now = System.currentTimeMillis(); - if (now < Preview.this.lastTimestamp + PREVIEW_INTERVAL) + if (now < CameraViewHolder.this.lastTimestamp + PREVIEW_INTERVAL) return; - Preview.this.lastTimestamp = now; + CameraViewHolder.this.lastTimestamp = now; if (!doingVideoProcessing) { - Log.i("Preview", "Processing new image"); + Log.i("CameraViewHolder", "Processing new image"); mDecodeThreadPool.execute(() -> processNewFrame(data, width, height, rotationDegrees)); } @@ -304,7 +298,7 @@ private void processNewFrame (byte[] data, int width, int height, int rotationDe } catch (Exception e) { // Cannot happen - Log.e("Preview", "error creating image", e); + Log.e("CameraViewHolder", "error creating image", e); } } } @@ -343,14 +337,9 @@ private synchronized boolean recordVideo() { } - public void stopCamera () + public synchronized void stopCamera () { if (cameraView != null) { - // Surface will be destroyed when we return, so stop the preview. - // Because the CameraDevice object is not a shared resource, it's very - // important to release it when the activity is paused. - this.context.unbindService(mConnection); - cameraView.stop(); } } @@ -358,4 +347,13 @@ public void stopCamera () public int getCameraFacing() { return cameraView.getFacing(); } + + public void destroy () + { + if (mConnection != null) { + this.context.unbindService(mConnection); + mConnection = null; + } + stopCamera(); + } } diff --git a/src/main/java/org/havenapp/main/ui/CameraFragment.java b/src/main/java/org/havenapp/main/ui/CameraFragment.java index 43c4229d..c725cac5 100644 --- a/src/main/java/org/havenapp/main/ui/CameraFragment.java +++ b/src/main/java/org/havenapp/main/ui/CameraFragment.java @@ -10,7 +10,6 @@ import android.os.Bundle; import android.support.v4.app.Fragment; -import android.hardware.Camera; import android.hardware.SensorEvent; import android.view.LayoutInflater; import android.view.View; @@ -21,12 +20,11 @@ import org.havenapp.main.PreferenceManager; import org.havenapp.main.R; -import org.havenapp.main.sensors.media.ImageCodec; -import org.havenapp.main.sensors.motion.Preview; +import org.havenapp.main.sensors.motion.CameraViewHolder; public final class CameraFragment extends Fragment { - private Preview cameraViewHolder; + private CameraViewHolder cameraViewHolder; private ImageView newImage; @Override @@ -85,7 +83,7 @@ private void initCamera () CameraView cameraView = getActivity().findViewById(R.id.camera_view); - cameraViewHolder = new Preview(getContext().getApplicationContext(),cameraView); + cameraViewHolder = new CameraViewHolder(getContext().getApplicationContext(),cameraView); newImage = getActivity().findViewById(R.id.new_image); @@ -100,6 +98,7 @@ private void initCamera () @Override public void onDestroy() { super.onDestroy(); + cameraViewHolder.destroy(); }