mirror of
https://github.com/status-im/react-native-webview.git
synced 2026-08-31 19:51:10 +00:00
fix(Android): Don't show camera options for a file upload when they can not be used (#1210)
* Don't show camera options for a file upload that would result in nothing happening for the user. On Android, if the application declares the camera permission, then even intents that use the camera require permission to be granted. This is a problem for apps that combine an in-app camera with a WebView that has file uploading and the user has not given permission for the camera. Note, this will not request permission for camera. This will simply prevent showing the camera options that would be a no-op action for users. It does this by checking if the camera permission is declared, and if so, checks that the user has granted permission. More information: https://blog.egorand.me/taking-photos-not-so-simply-how-i-got-bitten-by-action_image_capture/ * Add example and documentation about camera option availability in file uploads for Android.
This commit is contained in:
@@ -32,6 +32,7 @@ import com.facebook.react.modules.core.PermissionListener;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static android.app.Activity.RESULT_OK;
|
||||
|
||||
@@ -180,11 +181,13 @@ public class RNCWebViewModule extends ReactContextBaseJavaModule implements Acti
|
||||
filePathCallback = callback;
|
||||
|
||||
ArrayList<Parcelable> extraIntents = new ArrayList<>();
|
||||
if (acceptsImages(acceptTypes)) {
|
||||
extraIntents.add(getPhotoIntent());
|
||||
}
|
||||
if (acceptsVideo(acceptTypes)) {
|
||||
extraIntents.add(getVideoIntent());
|
||||
if (! needsCameraPermission()) {
|
||||
if (acceptsImages(acceptTypes)) {
|
||||
extraIntents.add(getPhotoIntent());
|
||||
}
|
||||
if (acceptsVideo(acceptTypes)) {
|
||||
extraIntents.add(getVideoIntent());
|
||||
}
|
||||
}
|
||||
|
||||
Intent fileSelectionIntent = getFileChooserIntent(acceptTypes, allowMultiple);
|
||||
@@ -233,6 +236,23 @@ public class RNCWebViewModule extends ReactContextBaseJavaModule implements Acti
|
||||
return result;
|
||||
}
|
||||
|
||||
protected boolean needsCameraPermission() {
|
||||
boolean needed = false;
|
||||
|
||||
PackageManager packageManager = getCurrentActivity().getPackageManager();
|
||||
try {
|
||||
String[] requestedPermissions = packageManager.getPackageInfo(getReactApplicationContext().getPackageName(), PackageManager.GET_PERMISSIONS).requestedPermissions;
|
||||
if (Arrays.asList(requestedPermissions).contains(Manifest.permission.CAMERA)
|
||||
&& ContextCompat.checkSelfPermission(getCurrentActivity(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||
needed = true;
|
||||
}
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
needed = true;
|
||||
}
|
||||
|
||||
return needed;
|
||||
}
|
||||
|
||||
private Intent getPhotoIntent() {
|
||||
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
|
||||
outputFileUri = getOutputUri(MediaStore.ACTION_IMAGE_CAPTURE);
|
||||
|
||||
Reference in New Issue
Block a user