From c49348801e102f6e818172c2714f54928705317f Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Sat, 21 Dec 2019 22:04:02 +0100 Subject: [PATCH] Add cameraRequestPermissions XML attribute --- README.md | 1 + .../java/com/otaliastudios/cameraview/CameraView.java | 11 +++++++++-- cameraview/src/main/res/values/attrs.xml | 3 ++- docs/_posts/2018-12-20-runtime-permissions.md | 11 +++++++---- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 80eda18b..f00e99a2 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ Using CameraView is extremely simple: app:cameraPictureMetering="true|false" app:cameraPictureSnapshotMetering="false|true" app:cameraPictureFormat="jpeg|dng" + app:cameraRequestPermissions="true|false" app:cameraExperimental="false|true"> diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index bbf9d636..5e842f2d 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -116,12 +116,14 @@ public class CameraView extends FrameLayout implements LifecycleObserver { final static boolean DEFAULT_USE_DEVICE_ORIENTATION = true; final static boolean DEFAULT_PICTURE_METERING = true; final static boolean DEFAULT_PICTURE_SNAPSHOT_METERING = false; + final static boolean DEFAULT_REQUEST_PERMISSIONS = true; final static int DEFAULT_FRAME_PROCESSING_POOL_SIZE = 2; final static int DEFAULT_FRAME_PROCESSING_EXECUTORS = 1; // Self managed parameters private boolean mPlaySounds; private boolean mUseDeviceOrientation; + private boolean mRequestPermissions; private HashMap mGestureMap = new HashMap<>(4); private Preview mPreview; private Engine mEngine; @@ -186,6 +188,8 @@ public class CameraView extends FrameLayout implements LifecycleObserver { boolean useDeviceOrientation = a.getBoolean( R.styleable.CameraView_cameraUseDeviceOrientation, DEFAULT_USE_DEVICE_ORIENTATION); mExperimental = a.getBoolean(R.styleable.CameraView_cameraExperimental, false); + mRequestPermissions = a.getBoolean(R.styleable.CameraView_cameraRequestPermissions, + DEFAULT_REQUEST_PERMISSIONS); mPreview = controls.getPreview(); mEngine = controls.getEngine(); @@ -779,11 +783,14 @@ public class CameraView extends FrameLayout implements LifecycleObserver { needsAudio = needsAudio && c.checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED; - if (needsCamera || needsAudio) { + if (!needsCamera && !needsAudio) { + return true; + } else if (mRequestPermissions) { requestPermissions(needsCamera, needsAudio); return false; + } else { + return false; } - return true; } /** diff --git a/cameraview/src/main/res/values/attrs.xml b/cameraview/src/main/res/values/attrs.xml index 1b92128d..780566a0 100644 --- a/cameraview/src/main/res/values/attrs.xml +++ b/cameraview/src/main/res/values/attrs.xml @@ -156,7 +156,8 @@ - + + diff --git a/docs/_posts/2018-12-20-runtime-permissions.md b/docs/_posts/2018-12-20-runtime-permissions.md index 1a44eb2e..0b7ed22a 100644 --- a/docs/_posts/2018-12-20-runtime-permissions.md +++ b/docs/_posts/2018-12-20-runtime-permissions.md @@ -39,11 +39,14 @@ device has cameras, and then start the camera view. ### Handling -On Marshmallow+, the user must explicitly approve our permissions. You can +On Marshmallow+, the user must explicitly approve our permissions. You can either: - handle permissions yourself and then call `open()` or `setLifecycleOwner()` once they are acquired -- ignore this: `CameraView` will present a permission request to the user based on +- let `CameraView` request permissions: we will present a permission request to the user based on whether they are needed or not with the current configuration. -Note however, that this is done at the activity level, so the permission request callback -`onRequestPermissionResults()` will be invoked on the parent activity, not the fragment. \ No newline at end of file +The automatic request is currently done at the activity level, so the permission request callback +`onRequestPermissionResults()` will be invoked on the parent activity, not the fragment. + +The automatic request can be disabled by setting `app:cameraRequestPermissions="false"` in your +XML declaration. \ No newline at end of file