Skip to content

Commit e686de5

Browse files
authored
[image_picker] Removed redundant request for camera permission (flutter#4001)
* Removed all permissions and updated unit tests * Updated pubspec version and changelog. Updated pubspec version and changelog. * Update version
1 parent aaea6b6 commit e686de5

File tree

5 files changed

+34
-201
lines changed

5 files changed

+34
-201
lines changed

packages/image_picker/image_picker/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.8.0+1
2+
3+
* Removed redundant request for camera permissions.
4+
15
## 0.8.0
26

37
* BREAKING CHANGE: Changed storage location for captured images and videos to internal cache on Android,

packages/image_picker/image_picker/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerDelegate.java

Lines changed: 2 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
package io.flutter.plugins.imagepicker;
66

7-
import android.Manifest;
87
import android.app.Activity;
98
import android.content.Intent;
109
import android.content.pm.PackageManager;
@@ -15,7 +14,6 @@
1514
import android.os.Build;
1615
import android.provider.MediaStore;
1716
import androidx.annotation.VisibleForTesting;
18-
import androidx.core.app.ActivityCompat;
1917
import androidx.core.content.FileProvider;
2018
import io.flutter.plugin.common.MethodCall;
2119
import io.flutter.plugin.common.MethodChannel;
@@ -42,19 +40,7 @@ enum CameraDevice {
4240
* means that the chooseImageFromGallery() or takeImageWithCamera() method was called at least
4341
* twice. In this case, stop executing and finish with an error.
4442
*
45-
* <p>2. Check that a required runtime permission has been granted. The takeImageWithCamera() method
46-
* checks that {@link Manifest.permission#CAMERA} has been granted.
47-
*
48-
* <p>The permission check can end up in two different outcomes:
49-
*
50-
* <p>A) If the permission has already been granted, continue with picking the image from gallery or
51-
* camera.
52-
*
53-
* <p>B) If the permission hasn't already been granted, ask for the permission from the user. If the
54-
* user grants the permission, proceed with step #3. If the user denies the permission, stop doing
55-
* anything else and finish with a null result.
56-
*
57-
* <p>3. Launch the gallery or camera for picking the image, depending on whether
43+
* <p>2. Launch the gallery or camera for picking the image, depending on whether
5844
* chooseImageFromGallery() or takeImageWithCamera() was called.
5945
*
6046
* <p>This can end up in three different outcomes:
@@ -69,36 +55,23 @@ enum CameraDevice {
6955
*
7056
* <p>C) User cancels picking an image. Finish with null result.
7157
*/
72-
public class ImagePickerDelegate
73-
implements PluginRegistry.ActivityResultListener,
74-
PluginRegistry.RequestPermissionsResultListener {
58+
public class ImagePickerDelegate implements PluginRegistry.ActivityResultListener {
7559
@VisibleForTesting static final int REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY = 2342;
7660
@VisibleForTesting static final int REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA = 2343;
77-
@VisibleForTesting static final int REQUEST_CAMERA_IMAGE_PERMISSION = 2345;
7861
@VisibleForTesting static final int REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY = 2352;
7962
@VisibleForTesting static final int REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA = 2353;
80-
@VisibleForTesting static final int REQUEST_CAMERA_VIDEO_PERMISSION = 2355;
8163

8264
@VisibleForTesting final String fileProviderName;
8365

8466
private final Activity activity;
8567
@VisibleForTesting final File externalFilesDirectory;
8668
private final ImageResizer imageResizer;
8769
private final ImagePickerCache cache;
88-
private final PermissionManager permissionManager;
8970
private final IntentResolver intentResolver;
9071
private final FileUriResolver fileUriResolver;
9172
private final FileUtils fileUtils;
9273
private CameraDevice cameraDevice;
9374

94-
interface PermissionManager {
95-
boolean isPermissionGranted(String permissionName);
96-
97-
void askForPermission(String permissionName, int requestCode);
98-
99-
boolean needRequestCameraPermission();
100-
}
101-
10275
interface IntentResolver {
10376
boolean resolveActivity(Intent intent);
10477
}
@@ -129,23 +102,6 @@ public ImagePickerDelegate(
129102
null,
130103
null,
131104
cache,
132-
new PermissionManager() {
133-
@Override
134-
public boolean isPermissionGranted(String permissionName) {
135-
return ActivityCompat.checkSelfPermission(activity, permissionName)
136-
== PackageManager.PERMISSION_GRANTED;
137-
}
138-
139-
@Override
140-
public void askForPermission(String permissionName, int requestCode) {
141-
ActivityCompat.requestPermissions(activity, new String[] {permissionName}, requestCode);
142-
}
143-
144-
@Override
145-
public boolean needRequestCameraPermission() {
146-
return ImagePickerUtils.needRequestCameraPermission(activity);
147-
}
148-
},
149105
new IntentResolver() {
150106
@Override
151107
public boolean resolveActivity(Intent intent) {
@@ -187,7 +143,6 @@ public void onScanCompleted(String path, Uri uri) {
187143
final MethodChannel.Result result,
188144
final MethodCall methodCall,
189145
final ImagePickerCache cache,
190-
final PermissionManager permissionManager,
191146
final IntentResolver intentResolver,
192147
final FileUriResolver fileUriResolver,
193148
final FileUtils fileUtils) {
@@ -197,7 +152,6 @@ public void onScanCompleted(String path, Uri uri) {
197152
this.fileProviderName = activity.getPackageName() + ".flutter.image_provider";
198153
this.pendingResult = result;
199154
this.methodCall = methodCall;
200-
this.permissionManager = permissionManager;
201155
this.intentResolver = intentResolver;
202156
this.fileUriResolver = fileUriResolver;
203157
this.fileUtils = fileUtils;
@@ -269,13 +223,6 @@ public void takeVideoWithCamera(MethodCall methodCall, MethodChannel.Result resu
269223
return;
270224
}
271225

272-
if (needRequestCameraPermission()
273-
&& !permissionManager.isPermissionGranted(Manifest.permission.CAMERA)) {
274-
permissionManager.askForPermission(
275-
Manifest.permission.CAMERA, REQUEST_CAMERA_VIDEO_PERMISSION);
276-
return;
277-
}
278-
279226
launchTakeVideoWithCameraIntent();
280227
}
281228

@@ -328,22 +275,9 @@ public void takeImageWithCamera(MethodCall methodCall, MethodChannel.Result resu
328275
return;
329276
}
330277

331-
if (needRequestCameraPermission()
332-
&& !permissionManager.isPermissionGranted(Manifest.permission.CAMERA)) {
333-
permissionManager.askForPermission(
334-
Manifest.permission.CAMERA, REQUEST_CAMERA_IMAGE_PERMISSION);
335-
return;
336-
}
337278
launchTakeImageWithCameraIntent();
338279
}
339280

340-
private boolean needRequestCameraPermission() {
341-
if (permissionManager == null) {
342-
return false;
343-
}
344-
return permissionManager.needRequestCameraPermission();
345-
}
346-
347281
private void launchTakeImageWithCameraIntent() {
348282
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
349283
if (cameraDevice == CameraDevice.FRONT) {
@@ -401,39 +335,6 @@ private void grantUriPermissions(Intent intent, Uri imageUri) {
401335
}
402336
}
403337

404-
@Override
405-
public boolean onRequestPermissionsResult(
406-
int requestCode, String[] permissions, int[] grantResults) {
407-
boolean permissionGranted =
408-
grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED;
409-
410-
switch (requestCode) {
411-
case REQUEST_CAMERA_IMAGE_PERMISSION:
412-
if (permissionGranted) {
413-
launchTakeImageWithCameraIntent();
414-
}
415-
break;
416-
case REQUEST_CAMERA_VIDEO_PERMISSION:
417-
if (permissionGranted) {
418-
launchTakeVideoWithCameraIntent();
419-
}
420-
break;
421-
default:
422-
return false;
423-
}
424-
425-
if (!permissionGranted) {
426-
switch (requestCode) {
427-
case REQUEST_CAMERA_IMAGE_PERMISSION:
428-
case REQUEST_CAMERA_VIDEO_PERMISSION:
429-
finishWithError("camera_access_denied", "The user did not allow camera access.");
430-
break;
431-
}
432-
}
433-
434-
return true;
435-
}
436-
437338
@Override
438339
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
439340
switch (requestCode) {

packages/image_picker/image_picker/android/src/main/java/io/flutter/plugins/imagepicker/ImagePickerPlugin.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,16 @@ private void setup(
192192
// V1 embedding setup for activity listeners.
193193
application.registerActivityLifecycleCallbacks(observer);
194194
registrar.addActivityResultListener(delegate);
195-
registrar.addRequestPermissionsResultListener(delegate);
196195
} else {
197196
// V2 embedding setup for activity listeners.
198197
activityBinding.addActivityResultListener(delegate);
199-
activityBinding.addRequestPermissionsResultListener(delegate);
200198
lifecycle = FlutterLifecycleAdapter.getActivityLifecycle(activityBinding);
201199
lifecycle.addObserver(observer);
202200
}
203201
}
204202

205203
private void tearDown() {
206204
activityBinding.removeActivityResultListener(delegate);
207-
activityBinding.removeRequestPermissionsResultListener(delegate);
208205
activityBinding = null;
209206
lifecycle.removeObserver(observer);
210207
lifecycle = null;

0 commit comments

Comments
 (0)