From 0a8e2f3de7b217205d22c4466fa29ff41c5d1040 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Mon, 11 Sep 2017 00:50:15 +0200 Subject: [PATCH] Dont require AUDIO permission if audio is OFF (#39) --- README.md | 6 +- .../cameraview/CameraViewTest.java | 2 +- .../otaliastudios/cameraview/CameraView.java | 65 ++++++++++--------- 3 files changed, 39 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index beb3a9aa..8f9095bb 100644 --- a/README.md +++ b/README.md @@ -445,15 +445,15 @@ Take also a look at public methods in `CameraUtils`, `CameraOptions`, `ExtraProp `CameraView` needs two permissions: - `android.permission.CAMERA` : required for capturing pictures and videos -- `android.permission.RECORD_AUDIO` : required for capturing videos +- `android.permission.RECORD_AUDIO` : required for capturing videos with `Audio.ON` (the default) -You can handle permissions yourself and then call `CameraView.start()` once they are acquired. If they are not, `CameraView` will request permissions to the user based on the `sessionType` that was set. In that case, you can restart the camera if you have a successful response from `onRequestPermissionResults()`. +You can handle permissions yourself and then call `CameraView.start()` once they are acquired. If they are not, `CameraView` will request permissions to the user based on whether they are needed. In that case, you can restart the camera if you have a successful response from `onRequestPermissionResults()`. ## Manifest file The library manifest file is not strict and only asks for camera permissions. This means that: -- If you wish to record videos, you should also add `android.permission.RECORD_AUDIO` to required permissions +- If you wish to record videos with `Audio.ON` (the default), you should also add `android.permission.RECORD_AUDIO` to required permissions ```xml diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java index 9e2f645d..7941cbcc 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java @@ -55,7 +55,7 @@ public class CameraViewTest extends BaseTest { } @Override - protected boolean checkPermissions(SessionType sessionType) { + protected boolean checkPermissions(SessionType sessionType, Audio audio) { return hasPermissions; } }; diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java index 1b6506e7..2f51b12f 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java @@ -494,7 +494,7 @@ public class CameraView extends FrameLayout { return; } - if (checkPermissions(getSessionType())) { + if (checkPermissions(getSessionType(), getAudio())) { mIsStarted = true; // Update display orientation for current CameraController mOrientationHelper.enable(getContext()); @@ -509,31 +509,21 @@ public class CameraView extends FrameLayout { * @return true if we can go on, false otherwise. */ @SuppressLint("NewApi") - protected boolean checkPermissions(SessionType sessionType) { - checkPermissionsManifestOrThrow(sessionType); - boolean api23 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; - int cameraCheck, audioCheck; - if (!api23) { - cameraCheck = PackageManager.PERMISSION_GRANTED; - audioCheck = PackageManager.PERMISSION_GRANTED; - } else { - cameraCheck = getContext().checkSelfPermission(Manifest.permission.CAMERA); - audioCheck = getContext().checkSelfPermission(Manifest.permission.RECORD_AUDIO); - } - switch (sessionType) { - case VIDEO: - if (cameraCheck != PackageManager.PERMISSION_GRANTED || audioCheck != PackageManager.PERMISSION_GRANTED) { - requestPermissions(true, true); - return false; - } - break; + protected boolean checkPermissions(SessionType sessionType, Audio audio) { + checkPermissionsManifestOrThrow(sessionType, audio); + // Manifest is OK at this point. Let's check runtime permissions. + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return true; - case PICTURE: - if (cameraCheck != PackageManager.PERMISSION_GRANTED) { - requestPermissions(true, false); - return false; - } - break; + Context c = getContext(); + boolean needsCamera = true; + boolean needsAudio = sessionType == SessionType.VIDEO && audio == Audio.ON; + + needsCamera = needsCamera && c.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED; + needsAudio = needsAudio && c.checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED; + + if (needsCamera || needsAudio) { + requestPermissions(needsCamera, needsAudio); + return false; } return true; } @@ -544,8 +534,8 @@ public class CameraView extends FrameLayout { * If the developer did not add this to its manifest, throw and fire warnings. * (Hoping this is not caught elsewhere... we should test). */ - private void checkPermissionsManifestOrThrow(SessionType sessionType) { - if (sessionType == SessionType.VIDEO) { + private void checkPermissionsManifestOrThrow(SessionType sessionType, Audio audio) { + if (sessionType == SessionType.VIDEO && audio == Audio.ON) { try { PackageManager manager = getContext().getPackageManager(); PackageInfo info = manager.getPackageInfo(getContext().getPackageName(), PackageManager.GET_PERMISSIONS); @@ -868,7 +858,7 @@ public class CameraView extends FrameLayout { /** - * Controls the audio + * Controls the audio mode. * * @see Audio#OFF * @see Audio#ON @@ -876,7 +866,22 @@ public class CameraView extends FrameLayout { * @param audio desired audio value */ public void setAudio(Audio audio) { - mCameraController.setAudio(audio); + + if (audio == getAudio() || !mIsStarted) { + // Check did took place, or will happen on start(). + mCameraController.setAudio(audio); + + } else if (checkPermissions(getSessionType(), audio)) { + // Camera is running. Pass. + mCameraController.setAudio(audio); + + } else { + // This means that the audio permission is being asked. + // Stop the camera so it can be restarted by the developer onPermissionResult. + // Developer must also set the audio value again... + // Not ideal but good for now. + stop(); + } } @@ -920,7 +925,7 @@ public class CameraView extends FrameLayout { // Check did took place, or will happen on start(). mCameraController.setSessionType(sessionType); - } else if (checkPermissions(sessionType)) { + } else if (checkPermissions(sessionType, getAudio())) { // Camera is running. CameraImpl setSessionType will do the trick. mCameraController.setSessionType(sessionType);