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

[image_picker] Removed redundant request for camera permission #4001

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions packages/image_picker/image_picker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.0+1

* Removed redundant request for camera permissions.

## 0.8.0

* BREAKING CHANGE: Changed storage location for captured images and videos to internal cache on Android,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

package io.flutter.plugins.imagepicker;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
Expand All @@ -15,7 +14,6 @@
import android.os.Build;
import android.provider.MediaStore;
import androidx.annotation.VisibleForTesting;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
Expand All @@ -42,19 +40,7 @@ enum CameraDevice {
* means that the chooseImageFromGallery() or takeImageWithCamera() method was called at least
* twice. In this case, stop executing and finish with an error.
*
* <p>2. Check that a required runtime permission has been granted. The takeImageWithCamera() method
* checks that {@link Manifest.permission#CAMERA} has been granted.
*
* <p>The permission check can end up in two different outcomes:
*
* <p>A) If the permission has already been granted, continue with picking the image from gallery or
* camera.
*
* <p>B) If the permission hasn't already been granted, ask for the permission from the user. If the
* user grants the permission, proceed with step #3. If the user denies the permission, stop doing
* anything else and finish with a null result.
*
* <p>3. Launch the gallery or camera for picking the image, depending on whether
* <p>2. Launch the gallery or camera for picking the image, depending on whether
* chooseImageFromGallery() or takeImageWithCamera() was called.
*
* <p>This can end up in three different outcomes:
Expand All @@ -69,36 +55,23 @@ enum CameraDevice {
*
* <p>C) User cancels picking an image. Finish with null result.
*/
public class ImagePickerDelegate
implements PluginRegistry.ActivityResultListener,
PluginRegistry.RequestPermissionsResultListener {
public class ImagePickerDelegate implements PluginRegistry.ActivityResultListener {
@VisibleForTesting static final int REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY = 2342;
@VisibleForTesting static final int REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA = 2343;
@VisibleForTesting static final int REQUEST_CAMERA_IMAGE_PERMISSION = 2345;
@VisibleForTesting static final int REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY = 2352;
@VisibleForTesting static final int REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA = 2353;
@VisibleForTesting static final int REQUEST_CAMERA_VIDEO_PERMISSION = 2355;

@VisibleForTesting final String fileProviderName;

private final Activity activity;
@VisibleForTesting final File externalFilesDirectory;
private final ImageResizer imageResizer;
private final ImagePickerCache cache;
private final PermissionManager permissionManager;
private final IntentResolver intentResolver;
private final FileUriResolver fileUriResolver;
private final FileUtils fileUtils;
private CameraDevice cameraDevice;

interface PermissionManager {
boolean isPermissionGranted(String permissionName);

void askForPermission(String permissionName, int requestCode);

boolean needRequestCameraPermission();
}

interface IntentResolver {
boolean resolveActivity(Intent intent);
}
Expand Down Expand Up @@ -129,23 +102,6 @@ public ImagePickerDelegate(
null,
null,
cache,
new PermissionManager() {
@Override
public boolean isPermissionGranted(String permissionName) {
return ActivityCompat.checkSelfPermission(activity, permissionName)
== PackageManager.PERMISSION_GRANTED;
}

@Override
public void askForPermission(String permissionName, int requestCode) {
ActivityCompat.requestPermissions(activity, new String[] {permissionName}, requestCode);
}

@Override
public boolean needRequestCameraPermission() {
return ImagePickerUtils.needRequestCameraPermission(activity);
}
},
new IntentResolver() {
@Override
public boolean resolveActivity(Intent intent) {
Expand Down Expand Up @@ -187,7 +143,6 @@ public void onScanCompleted(String path, Uri uri) {
final MethodChannel.Result result,
final MethodCall methodCall,
final ImagePickerCache cache,
final PermissionManager permissionManager,
final IntentResolver intentResolver,
final FileUriResolver fileUriResolver,
final FileUtils fileUtils) {
Expand All @@ -197,7 +152,6 @@ public void onScanCompleted(String path, Uri uri) {
this.fileProviderName = activity.getPackageName() + ".flutter.image_provider";
this.pendingResult = result;
this.methodCall = methodCall;
this.permissionManager = permissionManager;
this.intentResolver = intentResolver;
this.fileUriResolver = fileUriResolver;
this.fileUtils = fileUtils;
Expand Down Expand Up @@ -269,13 +223,6 @@ public void takeVideoWithCamera(MethodCall methodCall, MethodChannel.Result resu
return;
}

if (needRequestCameraPermission()
&& !permissionManager.isPermissionGranted(Manifest.permission.CAMERA)) {
permissionManager.askForPermission(
Manifest.permission.CAMERA, REQUEST_CAMERA_VIDEO_PERMISSION);
return;
}

launchTakeVideoWithCameraIntent();
}

Expand Down Expand Up @@ -328,22 +275,9 @@ public void takeImageWithCamera(MethodCall methodCall, MethodChannel.Result resu
return;
}

if (needRequestCameraPermission()
&& !permissionManager.isPermissionGranted(Manifest.permission.CAMERA)) {
permissionManager.askForPermission(
Manifest.permission.CAMERA, REQUEST_CAMERA_IMAGE_PERMISSION);
return;
}
launchTakeImageWithCameraIntent();
}

private boolean needRequestCameraPermission() {
if (permissionManager == null) {
return false;
}
return permissionManager.needRequestCameraPermission();
}

private void launchTakeImageWithCameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraDevice == CameraDevice.FRONT) {
Expand Down Expand Up @@ -401,39 +335,6 @@ private void grantUriPermissions(Intent intent, Uri imageUri) {
}
}

@Override
public boolean onRequestPermissionsResult(
int requestCode, String[] permissions, int[] grantResults) {
boolean permissionGranted =
grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;

switch (requestCode) {
case REQUEST_CAMERA_IMAGE_PERMISSION:
if (permissionGranted) {
launchTakeImageWithCameraIntent();
}
break;
case REQUEST_CAMERA_VIDEO_PERMISSION:
if (permissionGranted) {
launchTakeVideoWithCameraIntent();
}
break;
default:
return false;
}

if (!permissionGranted) {
switch (requestCode) {
case REQUEST_CAMERA_IMAGE_PERMISSION:
case REQUEST_CAMERA_VIDEO_PERMISSION:
finishWithError("camera_access_denied", "The user did not allow camera access.");
break;
}
}

return true;
}

@Override
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,19 +192,16 @@ private void setup(
// V1 embedding setup for activity listeners.
application.registerActivityLifecycleCallbacks(observer);
registrar.addActivityResultListener(delegate);
registrar.addRequestPermissionsResultListener(delegate);
} else {
// V2 embedding setup for activity listeners.
activityBinding.addActivityResultListener(delegate);
activityBinding.addRequestPermissionsResultListener(delegate);
lifecycle = FlutterLifecycleAdapter.getActivityLifecycle(activityBinding);
lifecycle.addObserver(observer);
}
}

private void tearDown() {
activityBinding.removeActivityResultListener(delegate);
activityBinding.removeRequestPermissionsResultListener(delegate);
activityBinding = null;
lifecycle.removeObserver(observer);
lifecycle = null;
Expand Down
Loading