<commit_before>package eu.livotov.labs.android.camview;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * (c) Livotov Labs Ltd. 2012
 * Date: 06/12/2013
 */
public class CAMView extends FrameLayout implements SurfaceHolder.Callback, Camera.PreviewCallback, Camera.ErrorCallback
{

    private SurfaceHolder surfaceHolder;
    private SurfaceView surface;
    private Camera camera;
    private int previewFormat = ImageFormat.NV21;
    private int cameraId = -1;
    private Camera.PreviewCallback previewCallback;
    private AutoFocusManager autoFocusManager;

    private CAMViewListener camViewListener;
    private AtomicBoolean cameraIsLive = new AtomicBoolean(false);
    private AtomicBoolean cameraIsStopping = new AtomicBoolean(false);

    private int lastUsedCameraId = -1;
    private Camera.ErrorCallback errorCallback;

    public CAMView(final Context context)
    {
        super(context);
        initUI();
    }

    public CAMView(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);
        initUI();
    }

    public CAMView(final Context context, final AttributeSet attrs, final int defStyle)
    {
        super(context, attrs, defStyle);
    }

    private void initUI()
    {
    }

    public void setCamViewListener(final CAMViewListener camViewListener)
    {
        this.camViewListener = camViewListener;
    }

    public void switchFlash(boolean on)
    {
        if (cameraIsLive.get() && camera != null)
        {
            final Camera camera = getCamera();
            final Camera.Parameters parameters = camera.getParameters();

            if (parameters != null && parameters.getSupportedFlashModes() != null && parameters.getFlashMode() != null && parameters.getSupportedFlashModes().size() > 0)
            {
                if (on)
                {
                    if (!parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH))
                    {
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    }
                }
                else
                {
                    if (!parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_OFF))
                    {
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    }
                }

                camera.setParameters(parameters);
            }
        }
        else
        {
            throw new IllegalAccessError("switchFlash may only be used in a live mode. Please turn on camera with the start() method before using flash.");
        }
    }

    @TargetApi(9)
    public static int getNumberOfCameras()
    {
        return Camera.getNumberOfCameras();
    }

    @TargetApi(9)
    public static Collection<CameraEnumeration> enumarateCameras()
    {

        if (Build.VERSION.SDK_INT < 9)
        {
            throw new UnsupportedOperationException("Camera enumeration is only available for Android SDK version 9 and above.");
        }


        List<CameraEnumeration> cameras = new ArrayList<CameraEnumeration>();

        final int camerasCount = Camera.getNumberOfCameras();

        for (int id = 0; id < camerasCount; id++)
        {
            final Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(id, cameraInfo);
            cameras.add(new CameraEnumeration(id, cameraInfo));
        }

        return cameras;
    }

    public static CameraEnumeration findFrontCamera()
    {
        Collection<CameraEnumeration> cams = enumarateCameras();

        for (CameraEnumeration cam : cams)
        {
            if (cam.isFrontCamera())
            {
                return cam;
            }
        }
        return null;
    }

    public synchronized void stop()
    {
        if (cameraIsLive.get() && !cameraIsStopping.compareAndSet(false, true))
        {
            return;
        }

        switchFlash(false);

        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                try<commit_msg>on the go fixing<commit_after>package eu.livotov.labs.android.camview;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Build;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.FrameLayout;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * (c) Livotov Labs Ltd. 2012
 * Date: 06/12/2013
 */
public class CAMView extends FrameLayout implements SurfaceHolder.Callback, Camera.PreviewCallback, Camera.ErrorCallback
{

    private SurfaceHolder surfaceHolder;
    private SurfaceView surface;
    private Camera camera;
    private int previewFormat = ImageFormat.NV21;
    private int cameraId = -1;
    private Camera.PreviewCallback previewCallback;
    private AutoFocusManager autoFocusManager;

    private CAMViewListener camViewListener;
    private AtomicBoolean cameraIsLive = new AtomicBoolean(false);
    private AtomicBoolean cameraIsStopping = new AtomicBoolean(false);

    private int lastUsedCameraId = -1;
    private Camera.ErrorCallback errorCallback;

    public CAMView(final Context context)
    {
        super(context);
        initUI();
    }

    public CAMView(final Context context, final AttributeSet attrs)
    {
        super(context, attrs);
        initUI();
    }

    public CAMView(final Context context, final AttributeSet attrs, final int defStyle)
    {
        super(context, attrs, defStyle);
    }

    private void initUI()
    {
    }

    public void setCamViewListener(final CAMViewListener camViewListener)
    {
        this.camViewListener = camViewListener;
    }

    public void switchFlash(boolean on)
    {
        if (cameraIsLive.get() && camera != null)
        {
            final Camera camera = getCamera();
            final Camera.Parameters parameters = camera.getParameters();

            if (parameters != null && parameters.getSupportedFlashModes() != null && parameters.getFlashMode() != null && parameters.getSupportedFlashModes().size() > 0)
            {
                if (on)
                {
                    if (!parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_TORCH))
                    {
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    }
                }
                else
                {
                    if (!parameters.getFlashMode().equals(Camera.Parameters.FLASH_MODE_OFF))
                    {
                        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    }
                }

                camera.setParameters(parameters);
            }
        }
        else
        {
            throw new IllegalAccessError("switchFlash may only be used in a live mode. Please turn on camera with the start() method before using flash.");
        }
    }

    @TargetApi(9)
    public static int getNumberOfCameras()
    {
        return Camera.getNumberOfCameras();
    }

    @TargetApi(9)
    public static Collection<CameraEnumeration> enumarateCameras()
    {

        if (Build.VERSION.SDK_INT < 9)
        {
            throw new UnsupportedOperationException("Camera enumeration is only available for Android SDK version 9 and above.");
        }


        List<CameraEnumeration> cameras = new ArrayList<CameraEnumeration>();

        final int camerasCount = Camera.getNumberOfCameras();

        for (int id = 0; id < camerasCount; id++)
        {
            final Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            Camera.getCameraInfo(id, cameraInfo);
            cameras.add(new CameraEnumeration(id, cameraInfo));
        }

        return cameras;
    }

    public static CameraEnumeration findFrontCamera()
    {
        Collection<CameraEnumeration> cams = enumarateCameras();

        for (CameraEnumeration cam : cams)
        {
            if (cam.isFrontCamera())
            {
                return cam;
            }
        }
        return null;
    }

    public synchronized void stop()
    {
        if (!cameraIsStopping.compareAndSet(false, true))
        {
            return;
        }

        if (cameraIsLive.get())
        {
            switchFlash(false);
        }

        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                try
{'fraction_non_alphanumeric': 0.0850293020777837, 'fraction_numerical': 0.004688332445391582, 'mean_word_length': 2.2232142857142856, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, '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': '3109318', 'n_tokens_mistral': 2520, 'n_tokens_neox': 2343, 'n_words': 577}