package com.zxing.cameraapplication;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.cjt2325.cameralibrary.CameraInterface;
import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.lisenter.JCameraLisenter;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private final int GET_PERMISSION_REQUEST = 100; //权限申请自定义码
    private JCameraView jCameraView;
    private boolean granted = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("CJT", "onCreate");

        jCameraView = (JCameraView) findViewById(R.id.jcameraview);

        //设置视频保存路径
        jCameraView.setSaveVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "JCamera");

        //JCameraView监听
        jCameraView.setJCameraLisenter(new JCameraLisenter() {
            @Override
            public void captureSuccess(Bitmap bitmap) {
                //获取图片bitmap
                Log.i("JCameraView", "bitmap = " + bitmap.getWidth());
            }

            @Override
            public void recordSuccess(String url) {
                //获取视频路径
                Log.i("CJT", "url -= " + url);
            }

            @Override
            public void quit() {
                //退出按钮
                MainActivity.this.finish();
            }
        });
        //6.0动态权限获取
        getPermissions();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.i("CJT", "onStart");
        //全屏显示
        if (Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        } else {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(option);
        }
    }



    @Override
    protected void onResume() {
        super.onResume();
//        if (granted) {
        jCameraView.onResume();

//        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        jCameraView.onPause();
        Log.i("CJT", "onPause");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CameraInterface.getInstance().doDestroyCamera();
        Log.i("CJT", "onDestroy");
    }

    /**
     * 获取权限
     */
    private void getPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager
                    .PERMISSION_GRANTED &&
                    ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager
                            .PERMISSION_GRANTED &&
                    ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager
                            .PERMISSION_GRANTED) {
                //具有权限
                granted = true;
            } else {
                //不具有获取权限，需要进行权限申请
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.CAMERA}, GET_PERMISSION_REQUEST);
                granted = false;
            }
        }
    }

    @TargetApi(23)
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == GET_PERMISSION_REQUEST) {
            int size = 0;
            if (grantResults.length >= 1) {
                int writeResult = grantResults[0];
                //读写内存权限
                boolean writeGranted = writeResult == PackageManager.PERMISSION_GRANTED;//读写内存权限
                if (!writeGranted) {
                    size++;
                }
                //录音权限
                int recordPermissionResult = grantResults[1];
                boolean recordPermissionGranted = recordPermissionResult == PackageManager.PERMISSION_GRANTED;
                if (!recordPermissionGranted) {
                    size++;
                }
                //相机权限
                int cameraPermissionResult = grantResults[2];
                boolean cameraPermissionGranted = cameraPermissionResult == PackageManager.PERMISSION_GRANTED;
                if (!cameraPermissionGranted) {
                    size++;
                }
                if (size == 0) {
                    granted = true;
                    jCameraView.onResume();
                } else {
                    Toast.makeText(this, "请到设置-权限管理中开启", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        }
    }
}

@@ -440,24 +440,59 @@
 .view.View;%0A
+import android.view.WindowManager;%0A
 import andro
@@ -508,16 +508,16 @@
 .Toast;%0A
-
 %0Aimport 
@@ -994,16 +994,134 @@
 State);%0A
+        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);%0A
         
@@ -1171,27 +1171,35 @@
      Log.i(%22
-CJT
+JCameraView
 %22, %22onCreate
@@ -1198,25 +1198,24 @@
 onCreate%22);%0A
-%0A
         jCam
@@ -1270,17 +1270,16 @@
 aview);%0A
-%0A
         
@@ -1816,19 +1816,27 @@
 .i(%22
-CJT
+JCameraView
 %22, %22url 
 -= %22
@@ -1831,17 +1831,16 @@
 %22, %22url 
--
 = %22 + ur
@@ -2140,27 +2140,35 @@
      Log.i(%22
-CJT
+JCameraView
 %22, %22onStart%22
@@ -2923,17 +2923,16 @@
     %7D%0A%0A%0A
-%0A
     @Ove
@@ -2995,18 +2995,134 @@
 sume();%0A
-//
+        if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.M) %7B%0A            jCameraView.onResume();%0A        %7D else %7B%0A    
         
@@ -3136,32 +3136,40 @@
 nted) %7B%0A        
+      
+  
 jCameraView.onRe
@@ -3180,11 +3180,22 @@
 ();%0A
-%0A//
+            %7D%0A
     
@@ -3319,27 +3319,35 @@
      Log.i(%22
-CJT
+JCameraView
 %22, %22onPause%22
@@ -3506,11 +3506,19 @@
 .i(%22
-CJT
+JCameraView
 %22, %22

fix bug