Skip to content

Commit

Permalink
address #313 (don't unbind service more than once!) and refactor Prev…
Browse files Browse the repository at this point in the history
…iew to CameraViewHolder
  • Loading branch information
n8fr8 committed Jul 5, 2018
1 parent 85de67b commit 2acb1be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -49,7 +43,7 @@

import io.github.silvaren.easyrs.tools.Nv21Image;

public class Preview {
public class CameraViewHolder {

/**
* Object to retrieve and set shared preferences
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -343,19 +337,23 @@ 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();
}
}

public int getCameraFacing() {
return cameraView.getFacing();
}

public void destroy ()
{
if (mConnection != null) {
this.context.unbindService(mConnection);
mConnection = null;
}
stopCamera();
}
}
9 changes: 4 additions & 5 deletions src/main/java/org/havenapp/main/ui/CameraFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -100,6 +98,7 @@ private void initCamera ()
@Override
public void onDestroy() {
super.onDestroy();
cameraViewHolder.destroy();

}

Expand Down

0 comments on commit 2acb1be

Please sign in to comment.