package com.prt2121.cam;

import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class CameraActivity extends AppCompatActivity {

    public static final String TAG = CameraActivity.class.getSimpleName();

    public static final int MEDIA_TYPE_IMAGE = 1;

    public static final int MEDIA_TYPE_VIDEO = 2;

    private Camera mCamera;

    private CameraPreview mPreview;

    private Camera.PictureCallback mPicture = new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            if (pictureFile == null) {
                Log.d(TAG, "Error creating media file, check storage permissions.");
                return;
            }

            try {
                // TODO: use io thread?
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            } finally {
                if (mCamera != null) {
                    mCamera.startPreview();
                }
            }
        }

    };

    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance() {
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        } catch (Exception e) {
            // Camera is not available (in use or does not exist)
            Log.e(TAG, "Camera is not available " + e.getMessage());
        }
        return c; // returns null if camera is unavailable
    }

    /** Create a file Uri for saving an image or video */
    private static Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }

    /** Create a File for saving an image or video */
    private static File getOutputMediaFile(int type) {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "MyCameraApp");
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_" + timeStamp + ".jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create an instance of Camera
        mCamera = getCameraInstance();

        // Create our Preview view and set it as the content of our activity.
        if (mCamera != null) {
            mPreview = new CameraPreview(this, mCamera);
            FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
            preview.addView(mPreview);
            Button captureButton = (Button) findViewById(R.id.button_capture);
            captureButton.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // get an image from the camera
                            mCamera.takePicture(null, null, mPicture);
                        }
                    }
            );
        }

    }

    /** Check if this device has a camera */
    private boolean checkCameraHardware(Context context) {
        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
    }

    private void releaseCameraAndPreview() {
        mPreview.setCamera(null);
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseCameraAndPreview();
    }

}

@@ -1845,16 +1845,55 @@
     %7D;%0A%0A
+    private FrameLayout mFrameLayout;%0A%0A
     /** 
@@ -4196,762 +4196,8 @@
 );%0A%0A
-        // Create our Preview view and set it as the content of our activity.%0A        if (mCamera != null) %7B%0A            mPreview = new CameraPreview(this, mCamera);%0A            FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);%0A            preview.addView(mPreview);%0A            Button captureButton = (Button) findViewById(R.id.button_capture);%0A            captureButton.setOnClickListener(%0A                    new View.OnClickListener() %7B%0A                        @Override%0A                        public void onClick(View v) %7B%0A                            // get an image from the camera%0A                            mCamera.takePicture(null, null, mPicture);%0A                        %7D%0A                    %7D%0A            );%0A        %7D%0A
 %0A   
@@ -4460,33 +4460,155 @@
     
-mPreview.setCamera(null);
+if (mPreview != null) %7B%0A            mFrameLayout.removeView(mPreview);%0A            mPreview.setCamera(null);%0A            mPreview = null;%0A        %7D
 %0A   
@@ -4777,24 +4777,55 @@
 .onPause();%0A
+        Log.d(TAG, %22onPause%22);%0A
         rele
@@ -4854,10 +4854,873 @@
 %0A    %7D%0A%0A
+    @Override%0A    protected void onResume() %7B%0A        super.onResume();%0A        Log.d(TAG, %22onResume%22);%0A        // Create our Preview view and set it as the content of our activity.%0A        if (mCamera != null) %7B%0A            mPreview = new CameraPreview(this, mCamera);%0A            mFrameLayout = (FrameLayout) findViewById(R.id.camera_preview);%0A            mFrameLayout.addView(mPreview);%0A            Button captureButton = (Button) findViewById(R.id.button_capture);%0A            captureButton.setOnClickListener(%0A                    new View.OnClickListener() %7B%0A                        @Override%0A                        public void onClick(View v) %7B%0A                            // get an image from the camera%0A                            mCamera.takePicture(null, null, mPicture);%0A                        %7D%0A                    %7D%0A            );%0A        %7D%0A    %7D%0A%0A
 %7D%0A

Fix break after screen lock