Skip to content

Commit

Permalink
[camera] Handle empty grantResults on permission request (flutter#6758)
Browse files Browse the repository at this point in the history
* [camera] Handle empty grantResults on permission request

* update changelog and add test

* fix typo

* format

* Update packages/camera/camera_android/CHANGELOG.md

Co-authored-by: Camille Simon <43054281+camsim99@users.noreply.github.com>

Co-authored-by: Camille Simon <43054281+camsim99@users.noreply.github.com>
  • Loading branch information
davidmartos96 and camsim99 committed Dec 5, 2022
1 parent 2a330bc commit 63f1b8f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/camera/camera_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.10.0+5

* Fixes `ArrayIndexOutOfBoundsException` when the permission request is interrupted.

## 0.10.0+4

* Upgrades `androidx.annotation` version to 1.5.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ public boolean onRequestPermissionsResult(int id, String[] permissions, int[] gr
}

alreadyCalled = true;
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
// grantResults could be empty if the permissions request with the user is interrupted
// https://developer.android.com/reference/android/app/Activity#onRequestPermissionsResult(int,%20java.lang.String[],%20int[])
if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
callback.onResult(CAMERA_ACCESS_DENIED, CAMERA_ACCESS_DENIED_MESSAGE);
} else if (grantResults.length > 1 && grantResults[1] != PackageManager.PERMISSION_GRANTED) {
callback.onResult(AUDIO_ACCESS_DENIED, AUDIO_ACCESS_DENIED_MESSAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,18 @@ public void callback_doesNotRespond() {
verify(fakeResultCallback, never())
.onResult("AudioAccessDenied", "Audio access permission was denied.");
}

@Test
public void callback_respondsWithCameraAccessDeniedWhenEmptyResult() {
// Handles the case where the grantResults array is empty

ResultCallback fakeResultCallback = mock(ResultCallback.class);
CameraRequestPermissionsListener permissionsListener =
new CameraRequestPermissionsListener(fakeResultCallback);

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

verify(fakeResultCallback)
.onResult("CameraAccessDenied", "Camera access permission was denied.");
}
}
2 changes: 1 addition & 1 deletion packages/camera/camera_android/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: camera_android
description: Android implementation of the camera plugin.
repository: https://github.com/flutter/plugins/tree/main/packages/camera/camera_android
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+camera%22
version: 0.10.0+4
version: 0.10.0+5

environment:
sdk: ">=2.14.0 <3.0.0"
Expand Down

0 comments on commit 63f1b8f

Please sign in to comment.