<commit_before>package com.siteshot.siteshot;

import android.app.Fragment;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.siteshot.siteshot.utils.PhotoUtils;

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.List;
/**
 * Take a picture directly from inside the app using this fragment.
 *
 * Reference: http://developer.android.com/training/camera/cameradirect.html
 * Reference: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
 * Reference: http://stackoverflow.com/questions/10913181/camera-preview-is-not-restarting
 *
 * Created by <NAME> (on behalf of AirPair.com) on 3/4/14.
 */
public class CameraFragment extends Fragment {

    private PhotoUtils mPhotoUtils;
    // Native camera.
    private Camera mCamera;
    // View to display the camera output.
    private CameraPreview mPreview;
    // Reference to the containing view.
    private View mCameraView;
    /**
     * Default empty constructor.
     */
    public CameraFragment(){
        super();
    }
    /**
     * Static factory method
     * @param sectionNumber
     * @return
     */
    public static CameraFragment newInstance(int sectionNumber) {
        CameraFragment fragment = new CameraFragment();
        Bundle args = new Bundle();
        //args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }
    /**
     * OnCreateView fragment override
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.camera_fragment, container, false);
// Create our Preview view and set it as the content of our activity.
        mPhotoUtils = new PhotoUtils();
        boolean opened = safeCameraOpenInView(view);
        if(opened == false){
            Log.d("CameraGuide","Error, Camera failed to open");
            return view;
        }
// Trap the capture button.
        Button captureButton = (Button) view.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);
                    }
                }
        );
        return view;
    }
    /**
     * Recommended "safe" way to open the camera.
     * @param view
     * @return
     */
    private boolean safeCameraOpenInView(View view) {
        boolean qOpened = false;
        releaseCameraAndPreview();
        mCamera = getCameraInstance();
        mCamera.setDisplayOrientation(90);
        mCameraView = view;
        qOpened = (mCamera != null);
        if(qOpened == true){
            mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera,view);
            FrameLayout preview = (FrameLayout) view.findViewById(R.id.camera_preview);
            preview.addView(mPreview);
            mPreview.startCameraPreview();
        }
        return qOpened;
    }
    /**
     * Safe method for getting a camera instance.
     * @return
     */
    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return c; // returns null if camera is unavailable
    }
    @Override
    public void onPause() {
        super.onPause();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        releaseCameraAndPreview();
    }
    /**
     * Clear any existing preview / camera.
     */
    private void releaseCameraAndPreview() {
        if (mCamera != null) {
            FrameLayout preview = (FrameLayout) getView().findViewById(R.id.camera_preview);
            preview.removeView(mPreview);
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
        if(mPreview != null){
            //mPreview.destroyDrawingCache();
            mPreview.mCamera = null;
        }
    }
    /**
     * Surface on which the camera projects it's capture results. This is derived both from Google's docs and the
     * excellent StackOverflow answer provided below.
     *
     * Reference / Credit: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
     */
    class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
        // SurfaceHolder
        private SurfaceHolder mHolder;
        // Our Camera.
        private Camera mCamera;
        // Parent Context.
        private Context mContext;
        // Camera Sizing (For rotation, orientation changes)
        private Camera.Size mPreviewSize;
        // List of supported preview sizes
        private List<Camera.Size> mSupportedPreviewSizes;
        // Flash modes supported by this camera
        private List<String> mSupportedFlashModes;
        // View holding this camera.
        private View mCameraView;
        public CameraPreview(Context context, Camera camera, View cameraView) {
            super(context);
// Capture the context
            mCameraView = cameraView;
            mContext = context;
            setCamera(camera);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
            mHolder = getHolder();
            mHolder.addCallback(this);
            mHolder.setKeepScreenOn(true);
// deprecated setting, but required on Android versions prior to 3.0
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }
        /**
         * Begin the preview of the camera input.
         */
        public void startCameraPreview()
        {
            try{
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
        /**
         * Extract supported preview and flash modes from the camera.
         * @param camera
         */
        private void setCamera(Camera camera)
        {
// Source: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
            mCamera = camera;
            mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();<commit_msg>add front and back camera funtionality<commit_after>package com.siteshot.siteshot;

import android.app.Fragment;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
import android.hardware.Camera.CameraInfo;

import com.siteshot.siteshot.utils.PhotoUtils;

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.List;
/**
 * Take a picture directly from inside the app using this fragment.
 *
 * Reference: http://developer.android.com/training/camera/cameradirect.html
 * Reference: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
 * Reference: http://stackoverflow.com/questions/10913181/camera-preview-is-not-restarting
 *
 * Created by <NAME> (on behalf of AirPair.com) on 3/4/14.
 */
public class CameraFragment extends Fragment {

    private PhotoUtils mPhotoUtils;
    // Native camera.
    private Camera mCamera;
    // View to display the camera output.
    private CameraPreview mPreview;
    int mNumberOfCameras;
    int mCurrentCamera;  // Camera ID currently chosen
    int mCameraCurrentlyLocked;  // Camera ID that's actually acquired
    // The first rear facing camera
    int mDefaultCameraId;


    // Reference to the containing view.
    private View mCameraView;
    /**
     * Default empty constructor.
     */
    public CameraFragment(){
        super();
    }
    /**
     * Static factory method
     * @param sectionNumber
     * @return
     */
    public static CameraFragment newInstance(int sectionNumber) {
        CameraFragment fragment = new CameraFragment();
        Bundle args = new Bundle();
        //args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }
    /**
     * OnCreateView fragment override
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.camera_fragment, container, false);
// Create our Preview view and set it as the content of our activity.
        mPhotoUtils = new PhotoUtils();
        // Find the total number of cameras available
        mNumberOfCameras = Camera.getNumberOfCameras();

        // Find the ID of the rear-facing ("default") camera
        CameraInfo cameraInfo = new CameraInfo();
        for (int i = 0; i < mNumberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
                mCurrentCamera = mDefaultCameraId = i;
            }
        }

            boolean opened = safeCameraOpenInView(view);
        if(opened == false){
            Log.d("CameraGuide","Error, Camera failed to open");
            return view;
        }
// Trap the capture button.
        Button captureButton = (Button) view.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);
                    }
                }
        );
        Button flipButton = (Button) view.findViewById(R.id.button_flip);
        if(Camera.getNumberOfCameras() == 1){
            flipButton.setVisibility(View.INVISIBLE);
        }
        else {
            flipButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mCamera != null) {
                            mCamera.stopPreview();
                            //mPreview.setCamera(null);
                            mCamera.release();
                            mCamera = null;
                        }

                        // Acquire the next camera and request Preview to reconfigure
                        // parameters.
                        mCurrentCamera = (mCameraCurrentlyLocked + 1) % mNumberOfCameras;
                        mCamera = Camera.open(mCurrentCamera);
                        mCameraCurrentlyLocked = mCurrentCamera;
                        mPreview.switchCamera(mCamera);

                        // Start the preview
                        mCamera.startPreview();

                    }
                }
                    );

        }

        return view;
    }
    /**
     * Recommended "safe" way to open the camera.
     * @param view
     * @return
     */
    private boolean safeCameraOpenInView(View view) {
        boolean qOpened = false;
        releaseCameraAndPreview();
        mCamera = getCameraInstance();
        mCamera.setDisplayOrientation(90);
        mCameraView = view;
        qOpened = (mCamera != null);
        if(qOpened == true){
            mPreview = new CameraPreview(getActivity().getBaseContext(), mCamera,view);
            FrameLayout preview = (FrameLayout) view.findViewById(R.id.camera_preview);
            preview.addView(mPreview);
            mPreview.startCameraPreview();
        }
        return qOpened;
    }
    /**
     * Safe method for getting a camera instance.
     * @return
     */
    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return c; // returns null if camera is unavailable
    }
    @Override
    public void onPause() {
        super.onPause();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        releaseCameraAndPreview();
    }
    /**
     * Clear any existing preview / camera.
     */
    private void releaseCameraAndPreview() {
        if (mCamera != null) {
            FrameLayout preview = (FrameLayout) getView().findViewById(R.id.camera_preview);
            preview.removeView(mPreview);
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
        if(mPreview != null){
            //mPreview.destroyDrawingCache();
            mPreview.mCamera = null;
        }
    }
    /**
     * Surface on which the camera projects it's capture results. This is derived both from Google's docs and the
     * excellent StackOverflow answer provided below.
     *
     * Reference / Credit: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
     */
    class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
        // SurfaceHolder
        private SurfaceHolder mHolder;
        // Our Camera.
        private Camera mCamera;
        // Parent Context.
        private Context mContext;
        // Camera Sizing (For rotation, orientation changes)
        private Camera.Size mPreviewSize;
        // List of supported preview sizes
        private List<Camera.Size> mSupportedPreviewSizes;
        // Flash modes supported by this camera
        private List<String> mSupportedFlashModes;
        // View holding this camera.
        private View mCameraView;
        public CameraPreview(Context context, Camera camera, View cameraView) {
            super(context);
// Capture the context
            mCameraView = cameraView;
            mContext = context;
            setCamera(camera);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
            mHolder = getHolder();
            mHolder.addCallback(this);
            mHolder.setKeepScreenOn(true);
// deprecated setting, but required on Android versions prior to 3.0
            mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }

        public void switchCamera(Camera camera) {
            setCamera(camera);
            try {
                camera.setDisplayOrientation(90);
                camera.setPreviewDisplay(mHolder);
            } catch (IOException exception) {
                exception.printStackTrace();
            }
        }


        /**
         * Begin the preview of the camera input.
         */
        public void startCameraPreview()
        {
            try{
                mCamera.setPreviewDisplay(mHolder);
                mCamera.startPreview();
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
        /**
         * Extract supported preview and flash modes from the camera.
         * @param camera
         */
        private void setCamera(Camera camera)
        {
// Source: http://stackoverflow.com/questions/7942378/android-camera-will-not-work-startpreview-fails
            mCamera = camera;
            mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
{'fraction_non_alphanumeric': 0.08932397048321829, 'fraction_numerical': 0.00470126160437991, 'mean_word_length': 2.086317722681359, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2889717', 'n_tokens_mistral': 4351, 'n_tokens_neox': 4146, 'n_words': 1111}