package info.lofei.android.torch.Util;

import android.hardware.Camera;

import info.lofei.android.torch.Exception.TorchUnavailableException;

/**
 * @author lofei@duotin.com
 * @version 1.0.0
 *          lofei 2014-10-20 下午6:02
 * @package info.lofei.android.torch.Util
 * @copyright Copyright (C) 2012-2020 DuoTin Network Technology Co.,LTD
 */
public class TorchUtil {

    private static Camera sCamera;

    public static void turnOn() throws TorchUnavailableException {
        try {
            if (sCamera == null) {
                sCamera = Camera.open();
            }
            Camera.Parameters p = sCamera.getParameters();
            p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
            sCamera.setParameters(p);
            sCamera.startPreview();
        } catch (RuntimeException e) {
            throw new TorchUnavailableException(e);
        }
    }

    public static void turnOff() throws TorchUnavailableException {
        try {
            if (sCamera == null) {
                sCamera = Camera.open();
            }
            Camera.Parameters p = sCamera.getParameters();
            p.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            sCamera.setParameters(p);
            sCamera.stopPreview();
            sCamera.release();
            sCamera = null;
        } catch (RuntimeException e) {
            throw new TorchUnavailableException(e);
        }
    }

    public static boolean isTorchOn() throws TorchUnavailableException {
        try {
            if (sCamera == null) {
                sCamera = Camera.open();
            }
            Camera.Parameters p = sCamera.getParameters();
            return Camera.Parameters.FLASH_MODE_TORCH.equals(p.getFlashMode());
        } catch (RuntimeException e){
            throw new TorchUnavailableException(e);
        }
    }
}

@@ -52,23 +52,140 @@
 oid.
-hardware.Camera
+graphics.SurfaceTexture;%0Aimport android.hardware.Camera;%0Aimport android.os.Build;%0A%0Aimport java.io.IOException;%0Aimport java.util.List
 ;%0A%0Ai
@@ -741,32 +741,33 @@
 etParameters();%0A
+%0A
             p.se
@@ -814,16 +814,17 @@
 TORCH);%0A
+%0A
         
@@ -857,68 +857,309 @@
 p);%0A
-            sCamera.startPreview();%0A        %7D catch (Runtime
+%0A            if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.HONEYCOMB ) %7B%0A                sCamera.setPreviewTexture(new SurfaceTexture(0));%0A            %7D%0A            sCamera.startPreview();%0A        %7D catch (RuntimeException e) %7B%0A            throw new TorchUnavailableException(e);%0A        %7Dcatch (IO
 Exce

fix bug :can't turn on with Nexus 5