diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraKit.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraKit.java index 1268f701..4bdd1365 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraKit.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraKit.java @@ -22,10 +22,6 @@ public class CameraKit { public static final int FLASH_ON = 1; public static final int FLASH_AUTO = 2; - public static final int METHOD_STANDARD = 0; - public static final int METHOD_STILL = 1; - public static final int METHOD_SPEED = 2; - public static final int FOCUS_OFF = 0; public static final int FOCUS_CONTINUOUS = 1; public static final int FOCUS_TAP = 2; @@ -34,6 +30,14 @@ public class CameraKit { public static final int ZOOM_OFF = 0; public static final int ZOOM_PINCH = 1; + public static final int METHOD_STANDARD = 0; + public static final int METHOD_STILL = 1; + public static final int METHOD_SPEED = 2; + + public static final int PERMISSIONS_STRICT = 0; + public static final int PERMISSIONS_LAZY = 1; + public static final int PERMISSIONS_PICTURE = 2; + } static class Defaults { @@ -41,8 +45,9 @@ public class CameraKit { static final int DEFAULT_FACING = Constants.FACING_BACK; static final int DEFAULT_FLASH = Constants.FLASH_OFF; static final int DEFAULT_FOCUS = Constants.FOCUS_CONTINUOUS; - static final int DEFAULT_METHOD = Constants.METHOD_STANDARD; static final int DEFAULT_ZOOM = Constants.ZOOM_OFF; + static final int DEFAULT_METHOD = Constants.METHOD_STANDARD; + static final int DEFAULT_PERMISSIONS = Constants.PERMISSIONS_STRICT; static final int DEFAULT_JPEG_QUALITY = 100; static final boolean DEFAULT_CROP_OUTPUT = false; diff --git a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java index 6bc33083..45c0ad84 100644 --- a/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java +++ b/camerakit/src/main/java/com/flurgle/camerakit/CameraView.java @@ -29,6 +29,9 @@ import static com.flurgle.camerakit.CameraKit.Constants.FLASH_AUTO; import static com.flurgle.camerakit.CameraKit.Constants.FLASH_OFF; import static com.flurgle.camerakit.CameraKit.Constants.FLASH_ON; import static com.flurgle.camerakit.CameraKit.Constants.METHOD_STANDARD; +import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_LAZY; +import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_PICTURE; +import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_STRICT; public class CameraView extends FrameLayout { @@ -47,6 +50,9 @@ public class CameraView extends FrameLayout { @Zoom private int mZoom; + @Permissions + private int mPermissions; + private int mJpegQuality; private boolean mCropOutput; private boolean mAdjustViewBounds; @@ -76,6 +82,7 @@ public class CameraView extends FrameLayout { mFocus = a.getInteger(R.styleable.CameraView_ckFocus, CameraKit.Defaults.DEFAULT_FOCUS); mMethod = a.getInteger(R.styleable.CameraView_ckMethod, CameraKit.Defaults.DEFAULT_METHOD); mZoom = a.getInteger(R.styleable.CameraView_ckZoom, CameraKit.Defaults.DEFAULT_ZOOM); + mPermissions = a.getInteger(R.styleable.CameraView_ckPermissions, CameraKit.Defaults.DEFAULT_PERMISSIONS); mJpegQuality = a.getInteger(R.styleable.CameraView_ckJpegQuality, CameraKit.Defaults.DEFAULT_JPEG_QUALITY); mCropOutput = a.getBoolean(R.styleable.CameraView_ckCropOutput, CameraKit.Defaults.DEFAULT_CROP_OUTPUT); mAdjustViewBounds = a.getBoolean(R.styleable.CameraView_android_adjustViewBounds, CameraKit.Defaults.DEFAULT_ADJUST_VIEW_BOUNDS); @@ -139,7 +146,7 @@ public class CameraView extends FrameLayout { protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if (mAdjustViewBounds) { Size previewSize = getPreviewSize(); - if(previewSize != null) { + if (previewSize != null) { if (getLayoutParams().width == LayoutParams.WRAP_CONTENT) { int height = MeasureSpec.getSize(heightMeasureSpec); float ratio = (float) height / (float) previewSize.getWidth(); @@ -159,7 +166,7 @@ public class CameraView extends FrameLayout { ); return; } - }else{ + } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); return; } @@ -169,24 +176,46 @@ public class CameraView extends FrameLayout { } public void start() { - int permissionCheck = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA); - if (permissionCheck == PackageManager.PERMISSION_GRANTED) { - new Thread(new Runnable() { - @Override - public void run() { - mCameraImpl.start(); + int cameraCheck = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA); + int audioCheck = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.RECORD_AUDIO); + + switch (mPermissions) { + case PERMISSIONS_STRICT: + if (cameraCheck != PackageManager.PERMISSION_GRANTED || audioCheck != PackageManager.PERMISSION_GRANTED) { + requestPermissions(true, true); + return; + } + break; + + case PERMISSIONS_LAZY: + if (cameraCheck != PackageManager.PERMISSION_GRANTED) { + requestPermissions(true, true); + return; } - }).start(); - } else { - requestCameraPermission(); + break; + + case PERMISSIONS_PICTURE: + if (cameraCheck != PackageManager.PERMISSION_GRANTED) { + requestPermissions(true, false); + return; + } + break; } + + new Thread(new Runnable() { + @Override + public void run() { + mCameraImpl.start(); + } + }).start(); } public void stop() { mCameraImpl.stop(); } - public void setFacing(@Facing final int facing) { + public void setFacing(@Facing + final int facing) { this.mFacing = facing; new Thread(new Runnable() { @@ -288,7 +317,7 @@ public class CameraView extends FrameLayout { return mCameraImpl != null ? mCameraImpl.getCaptureResolution() : null; } - private void requestCameraPermission() { + private void requestPermissions(boolean requestCamera, boolean requestAudio) { Activity activity = null; Context context = getContext(); while (context instanceof ContextWrapper) { diff --git a/camerakit/src/main/res/values/attrs.xml b/camerakit/src/main/res/values/attrs.xml index 77eecd99..3e7d1b0f 100644 --- a/camerakit/src/main/res/values/attrs.xml +++ b/camerakit/src/main/res/values/attrs.xml @@ -37,6 +37,12 @@ + + + + + + diff --git a/camerakit/src/main/types/com/flurgle/camerakit/Permissions.java b/camerakit/src/main/types/com/flurgle/camerakit/Permissions.java new file mode 100644 index 00000000..f3588f1d --- /dev/null +++ b/camerakit/src/main/types/com/flurgle/camerakit/Permissions.java @@ -0,0 +1,15 @@ +package com.flurgle.camerakit; + +import android.support.annotation.IntDef; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_LAZY; +import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_PICTURE; +import static com.flurgle.camerakit.CameraKit.Constants.PERMISSIONS_STRICT; + +@Retention(RetentionPolicy.SOURCE) +@IntDef({PERMISSIONS_STRICT, PERMISSIONS_LAZY, PERMISSIONS_PICTURE}) +public @interface Permissions { +}