Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

[camera] Update Android Camera Access Permission Error Codes #5640

Merged
merged 35 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e397649
Make initial changes
camsim99 May 4, 2022
120ef5d
Remove ongoing, add tests
camsim99 May 4, 2022
ba1b893
Merge remote-tracking branch 'upstream/master' into issue_101384
camsim99 May 5, 2022
236d86d
Fix tests
camsim99 May 5, 2022
17d37f3
Update CHANGELOG
camsim99 May 5, 2022
300a674
Update pubspec
camsim99 May 5, 2022
0bdb42d
Add back ongoing error
camsim99 May 9, 2022
34bb410
Add ongoing test
camsim99 May 9, 2022
3759e72
Merge branch 'main' into issue_101384
camsim99 May 9, 2022
e995edb
Change error msg
camsim99 May 9, 2022
901d115
Remove error and related test
camsim99 May 10, 2022
a04b050
Add missing import
camsim99 May 10, 2022
985e653
Bump version
camsim99 May 10, 2022
255f295
Merge remote-tracking branch 'upstream/master' into issue_101384
camsim99 May 17, 2022
2d74e07
Make fixes
camsim99 May 17, 2022
32079c9
Merge remote-tracking branch 'upstream/master' into issue_101384
camsim99 May 31, 2022
7b40254
Make breaking changes
camsim99 May 31, 2022
33561f2
Make analyzer fixes
camsim99 May 31, 2022
a154e1a
Remove error
camsim99 May 31, 2022
bfb8737
Bump platform interface version
camsim99 May 31, 2022
7bf7a14
Bump platform interface version in other pubspec
camsim99 May 31, 2022
22ad172
Revert "Bump platform interface version in other pubspec"
camsim99 May 31, 2022
11c69bb
Revert "Bump platform interface version"
camsim99 May 31, 2022
f06cae4
Revert changes to platform interface
camsim99 May 31, 2022
5578901
Undo git error
camsim99 May 31, 2022
63a4e65
Update breaking change method in CHANGELOG
camsim99 Jun 3, 2022
f078260
Update README
camsim99 Jun 8, 2022
52e922f
Merge remote-tracking branch 'upstream/master' into issue_101384
camsim99 Jun 8, 2022
8d366ea
Merge remote-tracking branch 'upstream/main' into issue_101384
camsim99 Jun 8, 2022
f9c373e
Merge remote-tracking branch 'upstream/main' into issue_101384
camsim99 Jun 22, 2022
b432ee4
Bump versions
camsim99 Jun 22, 2022
5be9f81
Fix versioning
camsim99 Jun 22, 2022
961ffdb
Merge remote-tracking branch 'upstream/main' into issue_101384
camsim99 Jul 7, 2022
1e34820
Undo changes to camera
camsim99 Jul 7, 2022
d8ba867
Merge remote-tracking branch 'upstream/main' into issue_101384
camsim99 Jul 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/camera/camera/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.10.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The camera change will need to be moved to a separate follow-up PR that updates both the camera_android and camera_web versions at the same time, now that the Android implementation is federated.


* **Breaking Change** Bumps default camera_web package version to 0.3.0.
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
* **Breaking Change** Updates Android camera access permission error codes to be consistent with other platforms.
camsim99 marked this conversation as resolved.
Show resolved Hide resolved

## 0.9.7+1

* Moves streaming implementation to the platform interface package.
Expand Down
2 changes: 0 additions & 2 deletions packages/camera/camera/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ Here is a list of all permission error codes that can be thrown:

- `AudioAccessRestricted`: iOS only for now. Thrown when audio access is restricted and users cannot grant permission (parental control).

- `cameraPermission`: Android and Web only. A legacy error code for all kinds of camera permission errors.

### Example

Here is a small example flutter app displaying a full screen camera preview.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,32 @@ interface ResultCallback {
void onResult(String errorCode, String errorDescription);
}

/**
* Camera access permission errors handled when camera is created. See {@code MethodChannelCamera}
* in {@code camera/camera_platform_interface} for details.
*/
private static final String CAMERA_PERMISSIONS_REQUEST_ONGOING =
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
"CameraPermissionsRequestOngoing";

private static final String CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE =
"Another request is ongoing and multiple requests cannot be handled at once.";
private static final String CAMERA_ACCESS_DENIED = "CameraAccessDenied";
private static final String CAMERA_ACCESS_DENIED_MESSAGE = "Camera access permission was denied.";
private static final String AUDIO_ACCESS_DENIED = "AudioAccessDenied";
private static final String AUDIO_ACCESS_DENIED_MESSAGE = "Audio access permission was denied.";

private static final int CAMERA_REQUEST_ID = 9796;
private boolean ongoing = false;
@VisibleForTesting boolean ongoing = false;

void requestPermissions(
Activity activity,
PermissionsRegistry permissionsRegistry,
boolean enableAudio,
ResultCallback callback) {
if (ongoing) {
callback.onResult("cameraPermission", "Camera permission request ongoing");
callback.onResult(
CAMERA_PERMISSIONS_REQUEST_ONGOING, CAMERA_PERMISSIONS_REQUEST_ONGOING_MESSAGE);
return;
}
if (!hasCameraPermission(activity) || (enableAudio && !hasAudioPermission(activity))) {
permissionsRegistry.addListener(
Expand Down Expand Up @@ -90,9 +106,9 @@ public boolean onRequestPermissionsResult(int id, String[] permissions, int[] gr

alreadyCalled = true;
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
callback.onResult("cameraPermission", "MediaRecorderCamera permission not granted");
callback.onResult(CAMERA_ACCESS_DENIED, CAMERA_ACCESS_DENIED_MESSAGE);
} else if (grantResults.length > 1 && grantResults[1] != PackageManager.PERMISSION_GRANTED) {
callback.onResult("cameraPermission", "MediaRecorderAudio permission not granted");
callback.onResult(AUDIO_ACCESS_DENIED, AUDIO_ACCESS_DENIED_MESSAGE);
camsim99 marked this conversation as resolved.
Show resolved Hide resolved
} else {
callback.onResult(null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
package io.flutter.plugins.camera;

import static junit.framework.TestCase.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import android.content.pm.PackageManager;
import io.flutter.plugins.camera.CameraPermissions.CameraRequestPermissionsListener;
import io.flutter.plugins.camera.CameraPermissions.ResultCallback;
import org.junit.Test;

public class CameraPermissionsTest {
Expand All @@ -24,4 +28,48 @@ public void listener_respondsOnce() {

assertEquals(1, calledCounter[0]);
}

@Test
public void callback_respondsWithCameraAccessDenied() {
ResultCallback fakeResultCallback = mock(ResultCallback.class);
CameraRequestPermissionsListener permissionsListener =
new CameraRequestPermissionsListener(fakeResultCallback);

permissionsListener.onRequestPermissionsResult(
9796, null, new int[] {PackageManager.PERMISSION_DENIED});

verify(fakeResultCallback)
.onResult("CameraAccessDenied", "Camera access permission was denied.");
}

@Test
public void callback_respondsWithAudioAccessDenied() {
ResultCallback fakeResultCallback = mock(ResultCallback.class);
CameraRequestPermissionsListener permissionsListener =
new CameraRequestPermissionsListener(fakeResultCallback);

permissionsListener.onRequestPermissionsResult(
9796,
null,
new int[] {PackageManager.PERMISSION_GRANTED, PackageManager.PERMISSION_DENIED});

verify(fakeResultCallback).onResult("AudioAccessDenied", "Audio access permission was denied.");
}

@Test
public void callback_doesNotRespond() {
ResultCallback fakeResultCallback = mock(ResultCallback.class);
CameraRequestPermissionsListener permissionsListener =
new CameraRequestPermissionsListener(fakeResultCallback);

permissionsListener.onRequestPermissionsResult(
9796,
null,
new int[] {PackageManager.PERMISSION_GRANTED, PackageManager.PERMISSION_GRANTED});

verify(fakeResultCallback, never())
.onResult("CameraAccessDenied", "Camera access permission was denied.");
verify(fakeResultCallback, never())
.onResult("AudioAccessDenied", "Audio access permission was denied.");
}
}
4 changes: 0 additions & 4 deletions packages/camera/camera/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,6 @@ class _CameraExampleHomeState extends State<CameraExampleHome>
// iOS only
showInSnackBar('Audio access is restricted.');
break;
case 'cameraPermission':
// Android & web only
showInSnackBar('Unknown permission error.');
break;
default:
_showCameraException(e);
break;
Expand Down
4 changes: 2 additions & 2 deletions packages/camera/camera/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: A Flutter plugin for controlling the camera. Supports previewing
Dart.
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.9.7+1
version: 0.10.0

environment:
sdk: ">=2.14.0 <3.0.0"
Expand All @@ -23,7 +23,7 @@ flutter:

dependencies:
camera_platform_interface: ^2.2.0
camera_web: ^0.2.1
camera_web: ^0.3.0
flutter:
sdk: flutter
flutter_plugin_android_lifecycle: ^2.0.2
Expand Down