Dont require AUDIO permission if audio is OFF (#39)

pull/43/head
Mattia Iavarone 7 years ago committed by GitHub
parent aeb0083352
commit 0a8e2f3de7
  1. 6
      README.md
  2. 2
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java
  3. 65
      cameraview/src/main/java/com/otaliastudios/cameraview/CameraView.java

@ -445,15 +445,15 @@ Take also a look at public methods in `CameraUtils`, `CameraOptions`, `ExtraProp
`CameraView` needs two permissions: `CameraView` needs two permissions:
- `android.permission.CAMERA` : required for capturing pictures and videos - `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 ## Manifest file
The library manifest file is not strict and only asks for camera permissions. This means that: 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 ```xml
<uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/>

@ -55,7 +55,7 @@ public class CameraViewTest extends BaseTest {
} }
@Override @Override
protected boolean checkPermissions(SessionType sessionType) { protected boolean checkPermissions(SessionType sessionType, Audio audio) {
return hasPermissions; return hasPermissions;
} }
}; };

@ -494,7 +494,7 @@ public class CameraView extends FrameLayout {
return; return;
} }
if (checkPermissions(getSessionType())) { if (checkPermissions(getSessionType(), getAudio())) {
mIsStarted = true; mIsStarted = true;
// Update display orientation for current CameraController // Update display orientation for current CameraController
mOrientationHelper.enable(getContext()); mOrientationHelper.enable(getContext());
@ -509,31 +509,21 @@ public class CameraView extends FrameLayout {
* @return true if we can go on, false otherwise. * @return true if we can go on, false otherwise.
*/ */
@SuppressLint("NewApi") @SuppressLint("NewApi")
protected boolean checkPermissions(SessionType sessionType) { protected boolean checkPermissions(SessionType sessionType, Audio audio) {
checkPermissionsManifestOrThrow(sessionType); checkPermissionsManifestOrThrow(sessionType, audio);
boolean api23 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; // Manifest is OK at this point. Let's check runtime permissions.
int cameraCheck, audioCheck; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return true;
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;
case PICTURE: Context c = getContext();
if (cameraCheck != PackageManager.PERMISSION_GRANTED) { boolean needsCamera = true;
requestPermissions(true, false); boolean needsAudio = sessionType == SessionType.VIDEO && audio == Audio.ON;
return false;
} needsCamera = needsCamera && c.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED;
break; needsAudio = needsAudio && c.checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED;
if (needsCamera || needsAudio) {
requestPermissions(needsCamera, needsAudio);
return false;
} }
return true; 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. * If the developer did not add this to its manifest, throw and fire warnings.
* (Hoping this is not caught elsewhere... we should test). * (Hoping this is not caught elsewhere... we should test).
*/ */
private void checkPermissionsManifestOrThrow(SessionType sessionType) { private void checkPermissionsManifestOrThrow(SessionType sessionType, Audio audio) {
if (sessionType == SessionType.VIDEO) { if (sessionType == SessionType.VIDEO && audio == Audio.ON) {
try { try {
PackageManager manager = getContext().getPackageManager(); PackageManager manager = getContext().getPackageManager();
PackageInfo info = manager.getPackageInfo(getContext().getPackageName(), PackageManager.GET_PERMISSIONS); 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#OFF
* @see Audio#ON * @see Audio#ON
@ -876,7 +866,22 @@ public class CameraView extends FrameLayout {
* @param audio desired audio value * @param audio desired audio value
*/ */
public void setAudio(Audio audio) { 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(). // Check did took place, or will happen on start().
mCameraController.setSessionType(sessionType); mCameraController.setSessionType(sessionType);
} else if (checkPermissions(sessionType)) { } else if (checkPermissions(sessionType, getAudio())) {
// Camera is running. CameraImpl setSessionType will do the trick. // Camera is running. CameraImpl setSessionType will do the trick.
mCameraController.setSessionType(sessionType); mCameraController.setSessionType(sessionType);

Loading…
Cancel
Save